From 9457b3e5d6ed487c48144b1ae0c18ae0ddc20580 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Guillot?= Date: Tue, 21 Nov 2017 16:08:43 -0800 Subject: [PATCH] Add missing attachments while refreshing a feed --- storage/enclosure.go | 31 ++++++++++++++++++++++++++++--- storage/entry.go | 18 ++++++++++++++---- 2 files changed, 42 insertions(+), 7 deletions(-) diff --git a/storage/enclosure.go b/storage/enclosure.go index ac85cb74..7b0acfd0 100644 --- a/storage/enclosure.go +++ b/storage/enclosure.go @@ -6,9 +6,11 @@ package storage import ( "fmt" + "github.com/miniflux/miniflux2/model" ) +// GetEnclosures returns all attachments for the given entry. func (s *Storage) GetEnclosures(entryID int64) (model.EnclosureList, error) { query := `SELECT id, user_id, entry_id, url, size, mime_type @@ -17,7 +19,7 @@ func (s *Storage) GetEnclosures(entryID int64) (model.EnclosureList, error) { rows, err := s.db.Query(query, entryID) if err != nil { - return nil, fmt.Errorf("Unable to get enclosures: %v", err) + return nil, fmt.Errorf("unable to get enclosures: %v", err) } defer rows.Close() @@ -34,7 +36,7 @@ func (s *Storage) GetEnclosures(entryID int64) (model.EnclosureList, error) { ) if err != nil { - return nil, fmt.Errorf("Unable to fetch enclosure row: %v", err) + return nil, fmt.Errorf("unable to fetch enclosure row: %v", err) } enclosures = append(enclosures, &enclosure) @@ -43,6 +45,7 @@ func (s *Storage) GetEnclosures(entryID int64) (model.EnclosureList, error) { return enclosures, nil } +// CreateEnclosure creates a new attachment. func (s *Storage) CreateEnclosure(enclosure *model.Enclosure) error { query := ` INSERT INTO enclosures @@ -61,7 +64,29 @@ func (s *Storage) CreateEnclosure(enclosure *model.Enclosure) error { ).Scan(&enclosure.ID) if err != nil { - return fmt.Errorf("Unable to create enclosure: %v", err) + return fmt.Errorf("unable to create enclosure: %v", err) + } + + return nil +} + +// IsEnclosureExists checks if an attachment exists. +func (s *Storage) IsEnclosureExists(enclosure *model.Enclosure) bool { + var result int + query := `SELECT count(*) as c FROM enclosures WHERE user_id=$1 AND entry_id=$2 AND url=$3` + s.db.QueryRow(query, enclosure.UserID, enclosure.EntryID, enclosure.URL).Scan(&result) + return result >= 1 +} + +// UpdateEnclosures add missing attachments while updating a feed. +func (s *Storage) UpdateEnclosures(enclosures model.EnclosureList) error { + for _, enclosure := range enclosures { + if !s.IsEnclosureExists(enclosure) { + err := s.CreateEnclosure(enclosure) + if err != nil { + return err + } + } } return nil diff --git a/storage/entry.go b/storage/entry.go index fe8c6f22..7bc1f869 100644 --- a/storage/entry.go +++ b/storage/entry.go @@ -42,7 +42,7 @@ func (s *Storage) CreateEntry(entry *model.Entry) error { ).Scan(&entry.ID) if err != nil { - return fmt.Errorf("Unable to create entry: %v", err) + return fmt.Errorf("unable to create entry: %v", err) } entry.Status = "unread" @@ -64,8 +64,9 @@ func (s *Storage) UpdateEntry(entry *model.Entry) error { UPDATE entries SET title=$1, url=$2, published_at=$3, content=$4, author=$5 WHERE user_id=$6 AND feed_id=$7 AND hash=$8 + RETURNING id ` - _, err := s.db.Exec( + err := s.db.QueryRow( query, entry.Title, entry.URL, @@ -75,9 +76,18 @@ func (s *Storage) UpdateEntry(entry *model.Entry) error { entry.UserID, entry.FeedID, entry.Hash, - ) + ).Scan(&entry.ID) - return err + if err != nil { + return err + } + + for _, enclosure := range entry.Enclosures { + enclosure.UserID = entry.UserID + enclosure.EntryID = entry.ID + } + + return s.UpdateEnclosures(entry.Enclosures) } // EntryExists checks if an entry already exists based on its hash when refreshing a feed.