Skip to content

Commit

Permalink
only update icon_url if successful
Browse files Browse the repository at this point in the history
  • Loading branch information
rystaf committed Oct 26, 2023
1 parent 320acaa commit 261d48b
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 28 deletions.
8 changes: 4 additions & 4 deletions internal/model/feed.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,16 @@ type Feed struct {
FetchViaProxy bool `json:"fetch_via_proxy"`
HideGlobally bool `json:"hide_globally"`
AppriseServiceURLs string `json:"apprise_service_urls"`
IconURL string `json:"icon_url"`

// Non persisted attributes
Category *Category `json:"category,omitempty"`
Icon *FeedIcon `json:"icon"`
Entries Entries `json:"entries,omitempty"`

TTL int `json:"-"`
IconURL string `json:"-"`
UnreadCount int `json:"-"`
ReadCount int `json:"-"`
TTL int `json:"-"`
UnreadCount int `json:"-"`
ReadCount int `json:"-"`
}

type FeedCounters struct {
Expand Down
1 change: 1 addition & 0 deletions internal/model/icon.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type Icon struct {
Hash string `json:"hash"`
MimeType string `json:"mime_type"`
Content []byte `json:"-"`
URL string `json:"-"`
}

// DataURL returns the data URL of the icon.
Expand Down
33 changes: 17 additions & 16 deletions internal/reader/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ func CreateFeedFromSubscriptionDiscovery(store *storage.Storage, userID int64, f
subscription.ID,
subscription.SiteURL,
subscription.IconURL,
false,
)

return subscription, nil
Expand Down Expand Up @@ -190,6 +191,7 @@ func CreateFeed(store *storage.Storage, userID int64, feedCreationRequest *model
subscription.ID,
subscription.SiteURL,
subscription.IconURL,
false,
)
return subscription, nil
}
Expand Down Expand Up @@ -254,8 +256,6 @@ func RefreshFeed(store *storage.Storage, userID, feedID int64, forceRefresh bool
return localizedError
}

refreshIcon := forceRefresh

if originalFeed.IgnoreHTTPCache || responseHandler.IsModified(originalFeed.EtagHeader, originalFeed.LastModifiedHeader) {
slog.Debug("Feed modified",
slog.Int64("user_id", userID),
Expand Down Expand Up @@ -334,7 +334,7 @@ func RefreshFeed(store *storage.Storage, userID, feedID int64, forceRefresh bool

if updatedFeed.IconURL != originalFeed.IconURL {
originalFeed.IconURL = updatedFeed.IconURL
refreshIcon = true
forceRefresh = true
}
} else {
slog.Debug("Feed not modified",
Expand All @@ -343,22 +343,13 @@ func RefreshFeed(store *storage.Storage, userID, feedID int64, forceRefresh bool
)
}

if refreshIcon {
if err := store.RemoveFeedIcon(feedID); err != nil {
slog.Debug("Unable to remove Feed Icon",
slog.Int64("feed_id", feedID),
)
}
}

checkFeedIcon(
store,
requestBuilder,
originalFeed.ID,
originalFeed.SiteURL,
originalFeed.IconURL,
originalFeed.UserAgent,
originalFeed.FetchViaProxy,
originalFeed.AllowSelfSignedCertificates,
forceRefresh,
)

originalFeed.ResetErrorCounter()
Expand All @@ -373,8 +364,8 @@ func RefreshFeed(store *storage.Storage, userID, feedID int64, forceRefresh bool
return nil
}

func checkFeedIcon(store *storage.Storage, requestBuilder *fetcher.RequestBuilder, feedID int64, websiteURL, feedIconURL string) {
if !store.HasIcon(feedID) {
func checkFeedIcon(store *storage.Storage, requestBuilder *fetcher.RequestBuilder, feedID int64, websiteURL, feedIconURL string, forceRefresh bool) {
if !store.HasIcon(feedID) || forceRefresh {
iconFinder := icon.NewIconFinder(requestBuilder, websiteURL, feedIconURL)
if icon, err := iconFinder.FindIcon(); err != nil {
slog.Debug("Unable to find feed icon",
Expand All @@ -390,6 +381,16 @@ func checkFeedIcon(store *storage.Storage, requestBuilder *fetcher.RequestBuilde
slog.String("feed_icon_url", feedIconURL),
)
} else {
if icon.URL == "" {
icon.URL = feedIconURL
}
if forceRefresh {
if err := store.RemoveFeedIcon(feedID); err != nil {
slog.Debug("Unable to remove Feed Icon",
slog.Int64("feed_id", feedID),
)
}
}
if err := store.CreateFeedIcon(feedID, icon); err != nil {
slog.Error("Unable to store feed icon",
slog.Int64("feed_id", feedID),
Expand Down
1 change: 1 addition & 0 deletions internal/reader/icon/finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ func (f *IconFinder) DownloadIcon(iconURL string) (*model.Icon, error) {
Hash: crypto.HashFromBytes(responseBody),
MimeType: responseHandler.ContentType(),
Content: responseBody,
URL: iconURL,
}

return icon, nil
Expand Down
12 changes: 4 additions & 8 deletions internal/storage/feed.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,11 +229,10 @@ func (s *Storage) CreateFeed(feed *model.Feed) error {
hide_globally,
url_rewrite_rules,
no_media_player,
apprise_service_urls,
icon_url
apprise_service_urls
)
VALUES
($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22, $23, $24, $25)
($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22, $23, $24)
RETURNING
id
`
Expand Down Expand Up @@ -263,7 +262,6 @@ func (s *Storage) CreateFeed(feed *model.Feed) error {
feed.UrlRewriteRules,
feed.NoMediaPlayer,
feed.AppriseServiceURLs,
feed.IconURL,
).Scan(&feed.ID)
if err != nil {
return fmt.Errorf(`store: unable to create feed %q: %v`, feed.FeedURL, err)
Expand Down Expand Up @@ -335,10 +333,9 @@ func (s *Storage) UpdateFeed(feed *model.Feed) (err error) {
hide_globally=$24,
url_rewrite_rules=$25,
no_media_player=$26,
apprise_service_urls=$27,
icon_url=$28
apprise_service_urls=$27
WHERE
id=$29 AND user_id=$30
id=$28 AND user_id=$29
`
_, err = s.db.Exec(query,
feed.FeedURL,
Expand Down Expand Up @@ -368,7 +365,6 @@ func (s *Storage) UpdateFeed(feed *model.Feed) (err error) {
feed.UrlRewriteRules,
feed.NoMediaPlayer,
feed.AppriseServiceURLs,
feed.IconURL,
feed.ID,
feed.UserID,
)
Expand Down
4 changes: 4 additions & 0 deletions internal/storage/icon.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ func (s *Storage) CreateFeedIcon(feedID int64, icon *model.Icon) error {
if err != nil {
return fmt.Errorf(`store: unable to create feed icon: %v`, err)
}
_, err = s.db.Exec(`UPDATE feed SET icon_url=$1 WHERE ID=$2`, icon.URL, feedID)
if err != nil {
return fmt.Errorf(`store: unable to save icon_url: %v`, err)
}

return nil
}
Expand Down

0 comments on commit 261d48b

Please sign in to comment.