2023-06-19 17:42:47 -04:00
|
|
|
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2017-11-20 00:10:04 -05:00
|
|
|
|
2023-08-10 22:46:45 -04:00
|
|
|
package handler // import "miniflux.app/v2/internal/reader/handler"
|
2017-11-20 00:10:04 -05:00
|
|
|
|
|
|
|
import (
|
2023-10-22 19:07:06 -04:00
|
|
|
"bytes"
|
2023-10-21 22:50:29 -04:00
|
|
|
"errors"
|
2023-09-24 19:32:09 -04:00
|
|
|
"log/slog"
|
2023-10-20 22:39:32 -04:00
|
|
|
"time"
|
2017-11-20 20:12:37 -05:00
|
|
|
|
2023-08-10 22:46:45 -04:00
|
|
|
"miniflux.app/v2/internal/config"
|
2023-09-09 01:45:17 -04:00
|
|
|
"miniflux.app/v2/internal/integration"
|
2023-08-10 22:46:45 -04:00
|
|
|
"miniflux.app/v2/internal/locale"
|
|
|
|
"miniflux.app/v2/internal/model"
|
2023-10-21 22:50:29 -04:00
|
|
|
"miniflux.app/v2/internal/reader/fetcher"
|
2023-08-10 22:46:45 -04:00
|
|
|
"miniflux.app/v2/internal/reader/icon"
|
|
|
|
"miniflux.app/v2/internal/reader/parser"
|
|
|
|
"miniflux.app/v2/internal/reader/processor"
|
|
|
|
"miniflux.app/v2/internal/storage"
|
2017-11-20 00:10:04 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2023-10-21 22:50:29 -04:00
|
|
|
ErrCategoryNotFound = errors.New("fetcher: category not found")
|
|
|
|
ErrFeedNotFound = errors.New("fetcher: feed not found")
|
|
|
|
ErrDuplicatedFeed = errors.New("fetcher: duplicated feed")
|
2017-11-20 00:10:04 -05:00
|
|
|
)
|
|
|
|
|
2023-10-22 19:07:06 -04:00
|
|
|
func CreateFeedFromSubscriptionDiscovery(store *storage.Storage, userID int64, feedCreationRequest *model.FeedCreationRequestFromSubscriptionDiscovery) (*model.Feed, *locale.LocalizedErrorWrapper) {
|
|
|
|
slog.Debug("Begin feed creation process from subscription discovery",
|
|
|
|
slog.Int64("user_id", userID),
|
|
|
|
slog.String("feed_url", feedCreationRequest.FeedURL),
|
|
|
|
)
|
|
|
|
|
|
|
|
user, storeErr := store.UserByID(userID)
|
|
|
|
if storeErr != nil {
|
|
|
|
return nil, locale.NewLocalizedErrorWrapper(storeErr, "error.database_error", storeErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !store.CategoryIDExists(userID, feedCreationRequest.CategoryID) {
|
|
|
|
return nil, locale.NewLocalizedErrorWrapper(ErrCategoryNotFound, "error.category_not_found")
|
|
|
|
}
|
|
|
|
|
|
|
|
if store.FeedURLExists(userID, feedCreationRequest.FeedURL) {
|
|
|
|
return nil, locale.NewLocalizedErrorWrapper(ErrDuplicatedFeed, "error.duplicated_feed")
|
|
|
|
}
|
|
|
|
|
|
|
|
subscription, parseErr := parser.ParseFeed(feedCreationRequest.FeedURL, feedCreationRequest.Content)
|
|
|
|
if parseErr != nil {
|
|
|
|
return nil, locale.NewLocalizedErrorWrapper(parseErr, "error.unable_to_parse_feed", parseErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
subscription.UserID = userID
|
|
|
|
subscription.UserAgent = feedCreationRequest.UserAgent
|
|
|
|
subscription.Cookie = feedCreationRequest.Cookie
|
|
|
|
subscription.Username = feedCreationRequest.Username
|
|
|
|
subscription.Password = feedCreationRequest.Password
|
|
|
|
subscription.Crawler = feedCreationRequest.Crawler
|
|
|
|
subscription.Disabled = feedCreationRequest.Disabled
|
|
|
|
subscription.IgnoreHTTPCache = feedCreationRequest.IgnoreHTTPCache
|
|
|
|
subscription.AllowSelfSignedCertificates = feedCreationRequest.AllowSelfSignedCertificates
|
|
|
|
subscription.FetchViaProxy = feedCreationRequest.FetchViaProxy
|
|
|
|
subscription.ScraperRules = feedCreationRequest.ScraperRules
|
|
|
|
subscription.RewriteRules = feedCreationRequest.RewriteRules
|
|
|
|
subscription.BlocklistRules = feedCreationRequest.BlocklistRules
|
|
|
|
subscription.KeeplistRules = feedCreationRequest.KeeplistRules
|
|
|
|
subscription.UrlRewriteRules = feedCreationRequest.UrlRewriteRules
|
|
|
|
subscription.EtagHeader = feedCreationRequest.ETag
|
|
|
|
subscription.LastModifiedHeader = feedCreationRequest.LastModified
|
|
|
|
subscription.FeedURL = feedCreationRequest.FeedURL
|
|
|
|
subscription.WithCategoryID(feedCreationRequest.CategoryID)
|
|
|
|
subscription.CheckedNow()
|
|
|
|
|
|
|
|
processor.ProcessFeedEntries(store, subscription, user, true)
|
|
|
|
|
|
|
|
if storeErr := store.CreateFeed(subscription); storeErr != nil {
|
|
|
|
return nil, locale.NewLocalizedErrorWrapper(storeErr, "error.database_error", storeErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
slog.Debug("Created feed",
|
|
|
|
slog.Int64("user_id", userID),
|
|
|
|
slog.Int64("feed_id", subscription.ID),
|
|
|
|
slog.String("feed_url", subscription.FeedURL),
|
|
|
|
)
|
|
|
|
|
|
|
|
requestBuilder := fetcher.NewRequestBuilder()
|
|
|
|
requestBuilder.WithUsernameAndPassword(feedCreationRequest.Username, feedCreationRequest.Password)
|
2023-11-15 22:12:00 -05:00
|
|
|
requestBuilder.WithUserAgent(feedCreationRequest.UserAgent, config.Opts.HTTPClientUserAgent())
|
2023-10-22 19:07:06 -04:00
|
|
|
requestBuilder.WithCookie(feedCreationRequest.Cookie)
|
|
|
|
requestBuilder.WithTimeout(config.Opts.HTTPClientTimeout())
|
|
|
|
requestBuilder.WithProxy(config.Opts.HTTPClientProxy())
|
|
|
|
requestBuilder.UseProxy(feedCreationRequest.FetchViaProxy)
|
|
|
|
requestBuilder.IgnoreTLSErrors(feedCreationRequest.AllowSelfSignedCertificates)
|
|
|
|
|
|
|
|
checkFeedIcon(
|
|
|
|
store,
|
|
|
|
requestBuilder,
|
|
|
|
subscription.ID,
|
|
|
|
subscription.SiteURL,
|
|
|
|
subscription.IconURL,
|
|
|
|
)
|
|
|
|
|
|
|
|
return subscription, nil
|
|
|
|
}
|
|
|
|
|
2017-11-20 00:10:04 -05:00
|
|
|
// CreateFeed fetch, parse and store a new feed.
|
2023-10-21 22:50:29 -04:00
|
|
|
func CreateFeed(store *storage.Storage, userID int64, feedCreationRequest *model.FeedCreationRequest) (*model.Feed, *locale.LocalizedErrorWrapper) {
|
2023-09-24 19:32:09 -04:00
|
|
|
slog.Debug("Begin feed creation process",
|
|
|
|
slog.Int64("user_id", userID),
|
|
|
|
slog.String("feed_url", feedCreationRequest.FeedURL),
|
|
|
|
)
|
2017-11-20 00:10:04 -05:00
|
|
|
|
2021-08-30 10:53:05 -04:00
|
|
|
user, storeErr := store.UserByID(userID)
|
|
|
|
if storeErr != nil {
|
2023-10-21 22:50:29 -04:00
|
|
|
return nil, locale.NewLocalizedErrorWrapper(storeErr, "error.database_error", storeErr)
|
2021-08-30 10:53:05 -04:00
|
|
|
}
|
|
|
|
|
2021-01-04 16:49:28 -05:00
|
|
|
if !store.CategoryIDExists(userID, feedCreationRequest.CategoryID) {
|
2023-10-21 22:50:29 -04:00
|
|
|
return nil, locale.NewLocalizedErrorWrapper(ErrCategoryNotFound, "error.category_not_found")
|
2017-11-25 01:29:20 -05:00
|
|
|
}
|
|
|
|
|
2023-10-21 22:50:29 -04:00
|
|
|
requestBuilder := fetcher.NewRequestBuilder()
|
|
|
|
requestBuilder.WithUsernameAndPassword(feedCreationRequest.Username, feedCreationRequest.Password)
|
2023-11-15 22:12:00 -05:00
|
|
|
requestBuilder.WithUserAgent(feedCreationRequest.UserAgent, config.Opts.HTTPClientUserAgent())
|
2023-10-21 22:50:29 -04:00
|
|
|
requestBuilder.WithCookie(feedCreationRequest.Cookie)
|
|
|
|
requestBuilder.WithTimeout(config.Opts.HTTPClientTimeout())
|
|
|
|
requestBuilder.WithProxy(config.Opts.HTTPClientProxy())
|
|
|
|
requestBuilder.UseProxy(feedCreationRequest.FetchViaProxy)
|
|
|
|
requestBuilder.IgnoreTLSErrors(feedCreationRequest.AllowSelfSignedCertificates)
|
|
|
|
|
|
|
|
responseHandler := fetcher.NewResponseHandler(requestBuilder.ExecuteRequest(feedCreationRequest.FeedURL))
|
|
|
|
defer responseHandler.Close()
|
|
|
|
|
|
|
|
if localizedError := responseHandler.LocalizedError(); localizedError != nil {
|
|
|
|
slog.Warn("Unable to fetch feed", slog.String("feed_url", feedCreationRequest.FeedURL), slog.Any("error", localizedError.Error()))
|
|
|
|
return nil, localizedError
|
2020-09-10 02:28:54 -04:00
|
|
|
}
|
|
|
|
|
2023-10-21 22:50:29 -04:00
|
|
|
responseBody, localizedError := responseHandler.ReadBody(config.Opts.HTTPClientMaxBodySize())
|
|
|
|
if localizedError != nil {
|
|
|
|
slog.Warn("Unable to fetch feed", slog.String("feed_url", feedCreationRequest.FeedURL), slog.Any("error", localizedError.Error()))
|
|
|
|
return nil, localizedError
|
2018-01-04 21:32:36 -05:00
|
|
|
}
|
|
|
|
|
2023-10-21 22:50:29 -04:00
|
|
|
if store.FeedURLExists(userID, responseHandler.EffectiveURL()) {
|
|
|
|
return nil, locale.NewLocalizedErrorWrapper(ErrDuplicatedFeed, "error.duplicated_feed")
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2023-10-22 19:07:06 -04:00
|
|
|
subscription, parseErr := parser.ParseFeed(responseHandler.EffectiveURL(), bytes.NewReader(responseBody))
|
2018-10-15 00:43:48 -04:00
|
|
|
if parseErr != nil {
|
2023-10-21 22:50:29 -04:00
|
|
|
return nil, locale.NewLocalizedErrorWrapper(parseErr, "error.unable_to_parse_feed", parseErr)
|
2017-11-20 20:12:37 -05:00
|
|
|
}
|
|
|
|
|
2021-01-04 16:49:28 -05:00
|
|
|
subscription.UserID = userID
|
|
|
|
subscription.UserAgent = feedCreationRequest.UserAgent
|
2021-03-22 23:27:58 -04:00
|
|
|
subscription.Cookie = feedCreationRequest.Cookie
|
2021-01-04 16:49:28 -05:00
|
|
|
subscription.Username = feedCreationRequest.Username
|
|
|
|
subscription.Password = feedCreationRequest.Password
|
|
|
|
subscription.Crawler = feedCreationRequest.Crawler
|
|
|
|
subscription.Disabled = feedCreationRequest.Disabled
|
|
|
|
subscription.IgnoreHTTPCache = feedCreationRequest.IgnoreHTTPCache
|
2021-02-21 16:42:49 -05:00
|
|
|
subscription.AllowSelfSignedCertificates = feedCreationRequest.AllowSelfSignedCertificates
|
2021-01-04 16:49:28 -05:00
|
|
|
subscription.FetchViaProxy = feedCreationRequest.FetchViaProxy
|
|
|
|
subscription.ScraperRules = feedCreationRequest.ScraperRules
|
|
|
|
subscription.RewriteRules = feedCreationRequest.RewriteRules
|
|
|
|
subscription.BlocklistRules = feedCreationRequest.BlocklistRules
|
|
|
|
subscription.KeeplistRules = feedCreationRequest.KeeplistRules
|
2022-07-12 00:12:26 -04:00
|
|
|
subscription.UrlRewriteRules = feedCreationRequest.UrlRewriteRules
|
2023-10-21 22:50:29 -04:00
|
|
|
subscription.EtagHeader = responseHandler.ETag()
|
|
|
|
subscription.LastModifiedHeader = responseHandler.LastModified()
|
|
|
|
subscription.FeedURL = responseHandler.EffectiveURL()
|
2021-01-04 16:49:28 -05:00
|
|
|
subscription.WithCategoryID(feedCreationRequest.CategoryID)
|
2018-10-15 00:43:48 -04:00
|
|
|
subscription.CheckedNow()
|
2017-11-20 00:10:04 -05:00
|
|
|
|
2023-08-08 10:12:41 -04:00
|
|
|
processor.ProcessFeedEntries(store, subscription, user, true)
|
2017-12-12 01:16:32 -05:00
|
|
|
|
2021-01-02 19:33:41 -05:00
|
|
|
if storeErr := store.CreateFeed(subscription); storeErr != nil {
|
2023-10-21 22:50:29 -04:00
|
|
|
return nil, locale.NewLocalizedErrorWrapper(storeErr, "error.database_error", storeErr)
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2023-09-24 19:32:09 -04:00
|
|
|
slog.Debug("Created feed",
|
|
|
|
slog.Int64("user_id", userID),
|
|
|
|
slog.Int64("feed_id", subscription.ID),
|
|
|
|
slog.String("feed_url", subscription.FeedURL),
|
|
|
|
)
|
2017-11-20 00:10:04 -05:00
|
|
|
|
2021-02-21 16:42:49 -05:00
|
|
|
checkFeedIcon(
|
|
|
|
store,
|
2023-10-21 22:50:29 -04:00
|
|
|
requestBuilder,
|
2021-02-21 16:42:49 -05:00
|
|
|
subscription.ID,
|
|
|
|
subscription.SiteURL,
|
2023-06-04 18:01:59 -04:00
|
|
|
subscription.IconURL,
|
2021-02-21 16:42:49 -05:00
|
|
|
)
|
2017-11-20 00:10:04 -05:00
|
|
|
return subscription, nil
|
|
|
|
}
|
|
|
|
|
2020-09-27 19:01:06 -04:00
|
|
|
// RefreshFeed refreshes a feed.
|
2023-10-21 22:50:29 -04:00
|
|
|
func RefreshFeed(store *storage.Storage, userID, feedID int64, forceRefresh bool) *locale.LocalizedErrorWrapper {
|
2023-09-24 19:32:09 -04:00
|
|
|
slog.Debug("Begin feed refresh process",
|
|
|
|
slog.Int64("user_id", userID),
|
|
|
|
slog.Int64("feed_id", feedID),
|
|
|
|
slog.Bool("force_refresh", forceRefresh),
|
|
|
|
)
|
|
|
|
|
2021-08-30 10:53:05 -04:00
|
|
|
user, storeErr := store.UserByID(userID)
|
|
|
|
if storeErr != nil {
|
2023-10-21 22:50:29 -04:00
|
|
|
return locale.NewLocalizedErrorWrapper(storeErr, "error.database_error", storeErr)
|
2021-08-30 10:53:05 -04:00
|
|
|
}
|
|
|
|
|
2021-01-02 19:33:41 -05:00
|
|
|
originalFeed, storeErr := store.FeedByID(userID, feedID)
|
2018-10-15 00:43:48 -04:00
|
|
|
if storeErr != nil {
|
2023-10-21 22:50:29 -04:00
|
|
|
return locale.NewLocalizedErrorWrapper(storeErr, "error.database_error", storeErr)
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if originalFeed == nil {
|
2023-10-21 22:50:29 -04:00
|
|
|
return locale.NewLocalizedErrorWrapper(ErrFeedNotFound, "error.feed_not_found")
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2020-05-25 17:59:15 -04:00
|
|
|
weeklyEntryCount := 0
|
|
|
|
if config.Opts.PollingScheduler() == model.SchedulerEntryFrequency {
|
|
|
|
var weeklyCountErr error
|
2021-01-02 19:33:41 -05:00
|
|
|
weeklyEntryCount, weeklyCountErr = store.WeeklyFeedEntryCount(userID, feedID)
|
2020-05-25 17:59:15 -04:00
|
|
|
if weeklyCountErr != nil {
|
2023-10-21 22:50:29 -04:00
|
|
|
return locale.NewLocalizedErrorWrapper(weeklyCountErr, "error.database_error", weeklyCountErr)
|
2020-05-25 17:59:15 -04:00
|
|
|
}
|
2020-05-25 17:06:56 -04:00
|
|
|
}
|
|
|
|
|
2018-10-15 00:43:48 -04:00
|
|
|
originalFeed.CheckedNow()
|
2020-05-25 17:59:15 -04:00
|
|
|
originalFeed.ScheduleNextCheck(weeklyEntryCount)
|
2018-02-08 21:16:54 -05:00
|
|
|
|
2023-10-21 22:50:29 -04:00
|
|
|
requestBuilder := fetcher.NewRequestBuilder()
|
|
|
|
requestBuilder.WithUsernameAndPassword(originalFeed.Username, originalFeed.Password)
|
2023-11-15 22:12:00 -05:00
|
|
|
requestBuilder.WithUserAgent(originalFeed.UserAgent, config.Opts.HTTPClientUserAgent())
|
2023-10-21 22:50:29 -04:00
|
|
|
requestBuilder.WithCookie(originalFeed.Cookie)
|
|
|
|
requestBuilder.WithTimeout(config.Opts.HTTPClientTimeout())
|
|
|
|
requestBuilder.WithProxy(config.Opts.HTTPClientProxy())
|
|
|
|
requestBuilder.UseProxy(originalFeed.FetchViaProxy)
|
|
|
|
requestBuilder.IgnoreTLSErrors(originalFeed.AllowSelfSignedCertificates)
|
|
|
|
|
|
|
|
responseHandler := fetcher.NewResponseHandler(requestBuilder.ExecuteRequest(originalFeed.FeedURL))
|
|
|
|
defer responseHandler.Close()
|
|
|
|
|
|
|
|
if localizedError := responseHandler.LocalizedError(); localizedError != nil {
|
|
|
|
slog.Warn("Unable to fetch feed", slog.String("feed_url", originalFeed.FeedURL), slog.Any("error", localizedError.Error()))
|
|
|
|
originalFeed.WithTranslatedErrorMessage(localizedError.Translate(user.Language))
|
2021-01-02 19:33:41 -05:00
|
|
|
store.UpdateFeedError(originalFeed)
|
2023-10-21 22:50:29 -04:00
|
|
|
return localizedError
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2023-10-21 22:50:29 -04:00
|
|
|
if store.AnotherFeedURLExists(userID, originalFeed.ID, responseHandler.EffectiveURL()) {
|
|
|
|
localizedError := locale.NewLocalizedErrorWrapper(ErrDuplicatedFeed, "error.duplicated_feed")
|
|
|
|
originalFeed.WithTranslatedErrorMessage(localizedError.Translate(user.Language))
|
2021-01-02 19:33:41 -05:00
|
|
|
store.UpdateFeedError(originalFeed)
|
2023-10-21 22:50:29 -04:00
|
|
|
return localizedError
|
2020-09-21 02:29:51 -04:00
|
|
|
}
|
|
|
|
|
2023-10-21 22:50:29 -04:00
|
|
|
if originalFeed.IgnoreHTTPCache || responseHandler.IsModified(originalFeed.EtagHeader, originalFeed.LastModifiedHeader) {
|
2023-09-24 19:32:09 -04:00
|
|
|
slog.Debug("Feed modified",
|
|
|
|
slog.Int64("user_id", userID),
|
|
|
|
slog.Int64("feed_id", feedID),
|
|
|
|
)
|
2018-01-04 21:32:36 -05:00
|
|
|
|
2023-10-21 22:50:29 -04:00
|
|
|
responseBody, localizedError := responseHandler.ReadBody(config.Opts.HTTPClientMaxBodySize())
|
|
|
|
if localizedError != nil {
|
|
|
|
slog.Warn("Unable to fetch feed", slog.String("feed_url", originalFeed.FeedURL), slog.Any("error", localizedError.Error()))
|
|
|
|
return localizedError
|
|
|
|
}
|
|
|
|
|
2023-10-22 19:07:06 -04:00
|
|
|
updatedFeed, parseErr := parser.ParseFeed(responseHandler.EffectiveURL(), bytes.NewReader(responseBody))
|
2018-02-28 00:08:32 -05:00
|
|
|
if parseErr != nil {
|
2023-10-21 22:50:29 -04:00
|
|
|
localizedError := locale.NewLocalizedErrorWrapper(parseErr, "error.unable_to_parse_feed")
|
|
|
|
|
|
|
|
if errors.Is(parseErr, parser.ErrFeedFormatNotDetected) {
|
|
|
|
localizedError = locale.NewLocalizedErrorWrapper(parseErr, "error.feed_format_not_detected", parseErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
originalFeed.WithTranslatedErrorMessage(localizedError.Translate(user.Language))
|
2021-01-02 19:33:41 -05:00
|
|
|
store.UpdateFeedError(originalFeed)
|
2023-10-21 22:50:29 -04:00
|
|
|
return localizedError
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2023-10-20 22:39:32 -04:00
|
|
|
// If the feed has a TTL defined, we use it to make sure we don't check it too often.
|
|
|
|
if updatedFeed.TTL > 0 {
|
|
|
|
minNextCheckAt := time.Now().Add(time.Minute * time.Duration(updatedFeed.TTL))
|
|
|
|
slog.Debug("Feed TTL",
|
|
|
|
slog.Int64("user_id", userID),
|
|
|
|
slog.Int64("feed_id", feedID),
|
|
|
|
slog.Int("ttl", updatedFeed.TTL),
|
|
|
|
slog.Time("next_check_at", originalFeed.NextCheckAt),
|
|
|
|
)
|
|
|
|
|
|
|
|
if originalFeed.NextCheckAt.IsZero() || originalFeed.NextCheckAt.Before(minNextCheckAt) {
|
|
|
|
slog.Debug("Updating next check date based on TTL",
|
|
|
|
slog.Int64("user_id", userID),
|
|
|
|
slog.Int64("feed_id", feedID),
|
|
|
|
slog.Int("ttl", updatedFeed.TTL),
|
|
|
|
slog.Time("new_next_check_at", minNextCheckAt),
|
|
|
|
slog.Time("old_next_check_at", originalFeed.NextCheckAt),
|
|
|
|
)
|
|
|
|
originalFeed.NextCheckAt = minNextCheckAt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-15 01:33:19 -04:00
|
|
|
originalFeed.Entries = updatedFeed.Entries
|
2023-08-08 10:12:41 -04:00
|
|
|
processor.ProcessFeedEntries(store, originalFeed, user, forceRefresh)
|
2017-12-12 01:16:32 -05:00
|
|
|
|
2023-08-08 10:12:41 -04:00
|
|
|
// We don't update existing entries when the crawler is enabled (we crawl only inexisting entries). Unless it is forced to refresh
|
|
|
|
updateExistingEntries := forceRefresh || !originalFeed.Crawler
|
2023-09-09 01:45:17 -04:00
|
|
|
newEntries, storeErr := store.RefreshFeedEntries(originalFeed.UserID, originalFeed.ID, originalFeed.Entries, updateExistingEntries)
|
|
|
|
if storeErr != nil {
|
2023-10-21 22:50:29 -04:00
|
|
|
localizedError := locale.NewLocalizedErrorWrapper(storeErr, "error.database_error", storeErr)
|
|
|
|
originalFeed.WithTranslatedErrorMessage(localizedError.Translate(user.Language))
|
2021-01-02 19:33:41 -05:00
|
|
|
store.UpdateFeedError(originalFeed)
|
2023-10-21 22:50:29 -04:00
|
|
|
return localizedError
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2023-09-09 01:45:17 -04:00
|
|
|
userIntegrations, intErr := store.Integration(userID)
|
|
|
|
if intErr != nil {
|
2023-09-24 19:32:09 -04:00
|
|
|
slog.Error("Fetching integrations failed; the refresh process will go on, but no integrations will run this time",
|
|
|
|
slog.Int64("user_id", userID),
|
|
|
|
slog.Int64("feed_id", feedID),
|
|
|
|
slog.Any("error", intErr),
|
|
|
|
)
|
2023-09-09 01:45:17 -04:00
|
|
|
} else if userIntegrations != nil && len(newEntries) > 0 {
|
|
|
|
go integration.PushEntries(originalFeed, newEntries, userIntegrations)
|
|
|
|
}
|
|
|
|
|
2018-10-15 00:43:48 -04:00
|
|
|
// We update caching headers only if the feed has been modified,
|
|
|
|
// because some websites don't return the same headers when replying with a 304.
|
2023-10-21 22:50:29 -04:00
|
|
|
originalFeed.EtagHeader = responseHandler.ETag()
|
|
|
|
originalFeed.LastModifiedHeader = responseHandler.LastModified()
|
2023-09-09 01:45:17 -04:00
|
|
|
|
2021-02-21 16:42:49 -05:00
|
|
|
checkFeedIcon(
|
|
|
|
store,
|
2023-10-21 22:50:29 -04:00
|
|
|
requestBuilder,
|
2021-02-21 16:42:49 -05:00
|
|
|
originalFeed.ID,
|
|
|
|
originalFeed.SiteURL,
|
2023-06-04 18:01:59 -04:00
|
|
|
updatedFeed.IconURL,
|
2021-02-21 16:42:49 -05:00
|
|
|
)
|
2017-11-20 00:10:04 -05:00
|
|
|
} else {
|
2023-09-24 19:32:09 -04:00
|
|
|
slog.Debug("Feed not modified",
|
|
|
|
slog.Int64("user_id", userID),
|
|
|
|
slog.Int64("feed_id", feedID),
|
|
|
|
)
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2018-10-15 00:43:48 -04:00
|
|
|
originalFeed.ResetErrorCounter()
|
2018-12-15 16:04:38 -05:00
|
|
|
|
2021-01-02 19:33:41 -05:00
|
|
|
if storeErr := store.UpdateFeed(originalFeed); storeErr != nil {
|
2023-10-21 22:50:29 -04:00
|
|
|
localizedError := locale.NewLocalizedErrorWrapper(storeErr, "error.database_error", storeErr)
|
|
|
|
originalFeed.WithTranslatedErrorMessage(localizedError.Translate(user.Language))
|
2021-01-02 19:33:41 -05:00
|
|
|
store.UpdateFeedError(originalFeed)
|
2023-10-21 22:50:29 -04:00
|
|
|
return localizedError
|
2018-12-15 16:04:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2023-10-21 22:50:29 -04:00
|
|
|
func checkFeedIcon(store *storage.Storage, requestBuilder *fetcher.RequestBuilder, feedID int64, websiteURL, feedIconURL string) {
|
2018-10-15 00:43:48 -04:00
|
|
|
if !store.HasIcon(feedID) {
|
2023-10-21 22:50:29 -04:00
|
|
|
iconFinder := icon.NewIconFinder(requestBuilder, websiteURL, feedIconURL)
|
2023-10-19 00:42:34 -04:00
|
|
|
if icon, err := iconFinder.FindIcon(); err != nil {
|
2023-10-19 23:04:26 -04:00
|
|
|
slog.Debug("Unable to find feed icon",
|
2023-09-24 19:32:09 -04:00
|
|
|
slog.Int64("feed_id", feedID),
|
|
|
|
slog.String("website_url", websiteURL),
|
|
|
|
slog.String("feed_icon_url", feedIconURL),
|
|
|
|
slog.Any("error", err),
|
|
|
|
)
|
2018-10-15 00:43:48 -04:00
|
|
|
} else if icon == nil {
|
2023-09-24 19:32:09 -04:00
|
|
|
slog.Debug("No icon found",
|
|
|
|
slog.Int64("feed_id", feedID),
|
|
|
|
slog.String("website_url", websiteURL),
|
|
|
|
slog.String("feed_icon_url", feedIconURL),
|
|
|
|
)
|
2018-10-15 00:43:48 -04:00
|
|
|
} else {
|
|
|
|
if err := store.CreateFeedIcon(feedID, icon); err != nil {
|
2023-09-24 19:32:09 -04:00
|
|
|
slog.Error("Unable to store feed icon",
|
|
|
|
slog.Int64("feed_id", feedID),
|
|
|
|
slog.String("website_url", websiteURL),
|
|
|
|
slog.String("feed_icon_url", feedIconURL),
|
|
|
|
slog.Any("error", err),
|
|
|
|
)
|
2018-10-15 00:43:48 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-29 14:17:14 -05:00
|
|
|
}
|