Add missing attachments while refreshing a feed
This commit is contained in:
parent
549a4277b0
commit
9457b3e5d6
2 changed files with 42 additions and 7 deletions
|
@ -6,9 +6,11 @@ package storage
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/miniflux/miniflux2/model"
|
"github.com/miniflux/miniflux2/model"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// GetEnclosures returns all attachments for the given entry.
|
||||||
func (s *Storage) GetEnclosures(entryID int64) (model.EnclosureList, error) {
|
func (s *Storage) GetEnclosures(entryID int64) (model.EnclosureList, error) {
|
||||||
query := `SELECT
|
query := `SELECT
|
||||||
id, user_id, entry_id, url, size, mime_type
|
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)
|
rows, err := s.db.Query(query, entryID)
|
||||||
if err != nil {
|
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()
|
defer rows.Close()
|
||||||
|
|
||||||
|
@ -34,7 +36,7 @@ func (s *Storage) GetEnclosures(entryID int64) (model.EnclosureList, error) {
|
||||||
)
|
)
|
||||||
|
|
||||||
if err != nil {
|
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)
|
enclosures = append(enclosures, &enclosure)
|
||||||
|
@ -43,6 +45,7 @@ func (s *Storage) GetEnclosures(entryID int64) (model.EnclosureList, error) {
|
||||||
return enclosures, nil
|
return enclosures, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CreateEnclosure creates a new attachment.
|
||||||
func (s *Storage) CreateEnclosure(enclosure *model.Enclosure) error {
|
func (s *Storage) CreateEnclosure(enclosure *model.Enclosure) error {
|
||||||
query := `
|
query := `
|
||||||
INSERT INTO enclosures
|
INSERT INTO enclosures
|
||||||
|
@ -61,7 +64,29 @@ func (s *Storage) CreateEnclosure(enclosure *model.Enclosure) error {
|
||||||
).Scan(&enclosure.ID)
|
).Scan(&enclosure.ID)
|
||||||
|
|
||||||
if err != nil {
|
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
|
return nil
|
||||||
|
|
|
@ -42,7 +42,7 @@ func (s *Storage) CreateEntry(entry *model.Entry) error {
|
||||||
).Scan(&entry.ID)
|
).Scan(&entry.ID)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Unable to create entry: %v", err)
|
return fmt.Errorf("unable to create entry: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
entry.Status = "unread"
|
entry.Status = "unread"
|
||||||
|
@ -64,8 +64,9 @@ func (s *Storage) UpdateEntry(entry *model.Entry) error {
|
||||||
UPDATE entries SET
|
UPDATE entries SET
|
||||||
title=$1, url=$2, published_at=$3, content=$4, author=$5
|
title=$1, url=$2, published_at=$3, content=$4, author=$5
|
||||||
WHERE user_id=$6 AND feed_id=$7 AND hash=$8
|
WHERE user_id=$6 AND feed_id=$7 AND hash=$8
|
||||||
|
RETURNING id
|
||||||
`
|
`
|
||||||
_, err := s.db.Exec(
|
err := s.db.QueryRow(
|
||||||
query,
|
query,
|
||||||
entry.Title,
|
entry.Title,
|
||||||
entry.URL,
|
entry.URL,
|
||||||
|
@ -75,9 +76,18 @@ func (s *Storage) UpdateEntry(entry *model.Entry) error {
|
||||||
entry.UserID,
|
entry.UserID,
|
||||||
entry.FeedID,
|
entry.FeedID,
|
||||||
entry.Hash,
|
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.
|
// EntryExists checks if an entry already exists based on its hash when refreshing a feed.
|
||||||
|
|
Loading…
Reference in a new issue