handler.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package handler // import "miniflux.app/v2/internal/reader/handler"
  4. import (
  5. "bytes"
  6. "errors"
  7. "log/slog"
  8. "time"
  9. "miniflux.app/v2/internal/config"
  10. "miniflux.app/v2/internal/integration"
  11. "miniflux.app/v2/internal/locale"
  12. "miniflux.app/v2/internal/model"
  13. "miniflux.app/v2/internal/proxyrotator"
  14. "miniflux.app/v2/internal/reader/fetcher"
  15. "miniflux.app/v2/internal/reader/icon"
  16. "miniflux.app/v2/internal/reader/parser"
  17. "miniflux.app/v2/internal/reader/processor"
  18. "miniflux.app/v2/internal/storage"
  19. )
  20. var (
  21. ErrCategoryNotFound = errors.New("fetcher: category not found")
  22. ErrFeedNotFound = errors.New("fetcher: feed not found")
  23. ErrDuplicatedFeed = errors.New("fetcher: duplicated feed")
  24. )
  25. func getTranslatedLocalizedError(store *storage.Storage, userID int64, originalFeed *model.Feed, localizedError *locale.LocalizedErrorWrapper) *locale.LocalizedErrorWrapper {
  26. user, storeErr := store.UserByID(userID)
  27. if storeErr != nil {
  28. return locale.NewLocalizedErrorWrapper(storeErr, "error.database_error", storeErr)
  29. }
  30. originalFeed.WithTranslatedErrorMessage(localizedError.Translate(user.Language))
  31. store.UpdateFeedError(originalFeed)
  32. return localizedError
  33. }
  34. func CreateFeedFromSubscriptionDiscovery(store *storage.Storage, userID int64, feedCreationRequest *model.FeedCreationRequestFromSubscriptionDiscovery) (*model.Feed, *locale.LocalizedErrorWrapper) {
  35. slog.Debug("Begin feed creation process from subscription discovery",
  36. slog.Int64("user_id", userID),
  37. slog.String("feed_url", feedCreationRequest.FeedURL),
  38. slog.String("proxy_url", feedCreationRequest.ProxyURL),
  39. )
  40. if !store.CategoryIDExists(userID, feedCreationRequest.CategoryID) {
  41. return nil, locale.NewLocalizedErrorWrapper(ErrCategoryNotFound, "error.category_not_found")
  42. }
  43. if store.FeedURLExists(userID, feedCreationRequest.FeedURL) {
  44. return nil, locale.NewLocalizedErrorWrapper(ErrDuplicatedFeed, "error.duplicated_feed")
  45. }
  46. subscription, parseErr := parser.ParseFeed(feedCreationRequest.FeedURL, feedCreationRequest.Content)
  47. if parseErr != nil {
  48. return nil, locale.NewLocalizedErrorWrapper(parseErr, "error.unable_to_parse_feed", parseErr)
  49. }
  50. subscription.UserID = userID
  51. subscription.UserAgent = feedCreationRequest.UserAgent
  52. subscription.Cookie = feedCreationRequest.Cookie
  53. subscription.Username = feedCreationRequest.Username
  54. subscription.Password = feedCreationRequest.Password
  55. subscription.Crawler = feedCreationRequest.Crawler
  56. subscription.IgnoreEntryUpdates = feedCreationRequest.IgnoreEntryUpdates
  57. subscription.Disabled = feedCreationRequest.Disabled
  58. subscription.IgnoreHTTPCache = feedCreationRequest.IgnoreHTTPCache
  59. subscription.AllowSelfSignedCertificates = feedCreationRequest.AllowSelfSignedCertificates
  60. subscription.FetchViaProxy = feedCreationRequest.FetchViaProxy
  61. subscription.ScraperRules = feedCreationRequest.ScraperRules
  62. subscription.RewriteRules = feedCreationRequest.RewriteRules
  63. subscription.BlocklistRules = feedCreationRequest.BlocklistRules
  64. subscription.KeeplistRules = feedCreationRequest.KeeplistRules
  65. subscription.UrlRewriteRules = feedCreationRequest.UrlRewriteRules
  66. subscription.BlockFilterEntryRules = feedCreationRequest.BlockFilterEntryRules
  67. subscription.KeepFilterEntryRules = feedCreationRequest.KeepFilterEntryRules
  68. subscription.EtagHeader = feedCreationRequest.ETag
  69. subscription.LastModifiedHeader = feedCreationRequest.LastModified
  70. subscription.FeedURL = feedCreationRequest.FeedURL
  71. subscription.DisableHTTP2 = feedCreationRequest.DisableHTTP2
  72. subscription.WithCategoryID(feedCreationRequest.CategoryID)
  73. subscription.ProxyURL = feedCreationRequest.ProxyURL
  74. subscription.CheckedNow()
  75. processor.ProcessFeedEntries(store, subscription, userID, true)
  76. if storeErr := store.CreateFeed(subscription); storeErr != nil {
  77. return nil, locale.NewLocalizedErrorWrapper(storeErr, "error.database_error", storeErr)
  78. }
  79. slog.Debug("Created feed",
  80. slog.Int64("user_id", userID),
  81. slog.Int64("feed_id", subscription.ID),
  82. slog.String("feed_url", subscription.FeedURL),
  83. )
  84. icon.NewIconChecker(store, subscription).UpdateOrCreateFeedIcon()
  85. return subscription, nil
  86. }
  87. // CreateFeed fetch, parse and store a new feed.
  88. func CreateFeed(store *storage.Storage, userID int64, feedCreationRequest *model.FeedCreationRequest) (*model.Feed, *locale.LocalizedErrorWrapper) {
  89. slog.Debug("Begin feed creation process",
  90. slog.Int64("user_id", userID),
  91. slog.String("feed_url", feedCreationRequest.FeedURL),
  92. slog.String("proxy_url", feedCreationRequest.ProxyURL),
  93. )
  94. if !store.CategoryIDExists(userID, feedCreationRequest.CategoryID) {
  95. return nil, locale.NewLocalizedErrorWrapper(ErrCategoryNotFound, "error.category_not_found")
  96. }
  97. requestBuilder := fetcher.NewRequestBuilder().
  98. WithUsernameAndPassword(feedCreationRequest.Username, feedCreationRequest.Password).
  99. WithUserAgent(feedCreationRequest.UserAgent, config.Opts.HTTPClientUserAgent()).
  100. WithCookie(feedCreationRequest.Cookie).
  101. WithTimeout(config.Opts.HTTPClientTimeout()).
  102. WithProxyRotator(proxyrotator.ProxyRotatorInstance).
  103. WithCustomFeedProxyURL(feedCreationRequest.ProxyURL).
  104. WithCustomApplicationProxyURL(config.Opts.HTTPClientProxyURL()).
  105. UseCustomApplicationProxyURL(feedCreationRequest.FetchViaProxy).
  106. IgnoreTLSErrors(feedCreationRequest.AllowSelfSignedCertificates).
  107. DisableHTTP2(feedCreationRequest.DisableHTTP2)
  108. responseHandler := fetcher.NewResponseHandler(requestBuilder.ExecuteRequest(feedCreationRequest.FeedURL))
  109. defer responseHandler.Close()
  110. if localizedError := responseHandler.LocalizedError(); localizedError != nil {
  111. slog.Warn("Unable to fetch feed", slog.String("feed_url", feedCreationRequest.FeedURL), slog.Any("error", localizedError.Error()))
  112. return nil, localizedError
  113. }
  114. responseBody, localizedError := responseHandler.ReadBody(config.Opts.HTTPClientMaxBodySize())
  115. if localizedError != nil {
  116. slog.Warn("Unable to fetch feed", slog.String("feed_url", feedCreationRequest.FeedURL), slog.Any("error", localizedError.Error()))
  117. return nil, localizedError
  118. }
  119. if store.FeedURLExists(userID, responseHandler.EffectiveURL()) {
  120. return nil, locale.NewLocalizedErrorWrapper(ErrDuplicatedFeed, "error.duplicated_feed")
  121. }
  122. subscription, parseErr := parser.ParseFeed(responseHandler.EffectiveURL(), bytes.NewReader(responseBody))
  123. if parseErr != nil {
  124. return nil, locale.NewLocalizedErrorWrapper(parseErr, "error.unable_to_parse_feed", parseErr)
  125. }
  126. subscription.UserID = userID
  127. subscription.UserAgent = feedCreationRequest.UserAgent
  128. subscription.Cookie = feedCreationRequest.Cookie
  129. subscription.Username = feedCreationRequest.Username
  130. subscription.Password = feedCreationRequest.Password
  131. subscription.Crawler = feedCreationRequest.Crawler
  132. subscription.IgnoreEntryUpdates = feedCreationRequest.IgnoreEntryUpdates
  133. subscription.Disabled = feedCreationRequest.Disabled
  134. subscription.IgnoreHTTPCache = feedCreationRequest.IgnoreHTTPCache
  135. subscription.AllowSelfSignedCertificates = feedCreationRequest.AllowSelfSignedCertificates
  136. subscription.DisableHTTP2 = feedCreationRequest.DisableHTTP2
  137. subscription.FetchViaProxy = feedCreationRequest.FetchViaProxy
  138. subscription.ScraperRules = feedCreationRequest.ScraperRules
  139. subscription.RewriteRules = feedCreationRequest.RewriteRules
  140. subscription.UrlRewriteRules = feedCreationRequest.UrlRewriteRules
  141. subscription.BlocklistRules = feedCreationRequest.BlocklistRules
  142. subscription.KeeplistRules = feedCreationRequest.KeeplistRules
  143. subscription.BlockFilterEntryRules = feedCreationRequest.BlockFilterEntryRules
  144. subscription.KeepFilterEntryRules = feedCreationRequest.KeepFilterEntryRules
  145. subscription.HideGlobally = feedCreationRequest.HideGlobally
  146. subscription.EtagHeader = responseHandler.ETag()
  147. subscription.LastModifiedHeader = responseHandler.LastModified()
  148. subscription.FeedURL = responseHandler.EffectiveURL()
  149. subscription.ProxyURL = feedCreationRequest.ProxyURL
  150. subscription.WithCategoryID(feedCreationRequest.CategoryID)
  151. subscription.CheckedNow()
  152. processor.ProcessFeedEntries(store, subscription, userID, true)
  153. if storeErr := store.CreateFeed(subscription); storeErr != nil {
  154. return nil, locale.NewLocalizedErrorWrapper(storeErr, "error.database_error", storeErr)
  155. }
  156. slog.Debug("Created feed",
  157. slog.Int64("user_id", userID),
  158. slog.Int64("feed_id", subscription.ID),
  159. slog.String("feed_url", subscription.FeedURL),
  160. )
  161. icon.NewIconChecker(store, subscription).UpdateOrCreateFeedIcon()
  162. return subscription, nil
  163. }
  164. // RefreshFeed refreshes a feed.
  165. func RefreshFeed(store *storage.Storage, userID, feedID int64, forceRefresh bool) *locale.LocalizedErrorWrapper {
  166. slog.Debug("Begin feed refresh process",
  167. slog.Int64("user_id", userID),
  168. slog.Int64("feed_id", feedID),
  169. slog.Bool("force_refresh", forceRefresh),
  170. )
  171. originalFeed, storeErr := store.FeedByID(userID, feedID)
  172. if storeErr != nil {
  173. return locale.NewLocalizedErrorWrapper(storeErr, "error.database_error", storeErr)
  174. }
  175. if originalFeed == nil {
  176. return locale.NewLocalizedErrorWrapper(ErrFeedNotFound, "error.feed_not_found")
  177. }
  178. weeklyEntryCount := 0
  179. if config.Opts.PollingScheduler() == model.SchedulerEntryFrequency {
  180. var weeklyCountErr error
  181. weeklyEntryCount, weeklyCountErr = store.WeeklyFeedEntryCount(userID, feedID)
  182. if weeklyCountErr != nil {
  183. return locale.NewLocalizedErrorWrapper(weeklyCountErr, "error.database_error", weeklyCountErr)
  184. }
  185. }
  186. originalFeed.CheckedNow()
  187. originalFeed.ScheduleNextCheck(weeklyEntryCount, time.Duration(0))
  188. requestBuilder := fetcher.NewRequestBuilder().
  189. WithUsernameAndPassword(originalFeed.Username, originalFeed.Password).
  190. WithUserAgent(originalFeed.UserAgent, config.Opts.HTTPClientUserAgent()).
  191. WithCookie(originalFeed.Cookie).
  192. WithTimeout(config.Opts.HTTPClientTimeout()).
  193. WithProxyRotator(proxyrotator.ProxyRotatorInstance).
  194. WithCustomFeedProxyURL(originalFeed.ProxyURL).
  195. WithCustomApplicationProxyURL(config.Opts.HTTPClientProxyURL()).
  196. UseCustomApplicationProxyURL(originalFeed.FetchViaProxy).
  197. IgnoreTLSErrors(originalFeed.AllowSelfSignedCertificates).
  198. DisableHTTP2(originalFeed.DisableHTTP2)
  199. ignoreHTTPCache := originalFeed.IgnoreHTTPCache || forceRefresh
  200. if !ignoreHTTPCache {
  201. requestBuilder = requestBuilder.
  202. WithETag(originalFeed.EtagHeader).
  203. WithLastModified(originalFeed.LastModifiedHeader)
  204. }
  205. responseHandler := fetcher.NewResponseHandler(requestBuilder.ExecuteRequest(originalFeed.FeedURL))
  206. defer responseHandler.Close()
  207. if responseHandler.IsRateLimited() {
  208. retryDelay := responseHandler.ParseRetryDelay()
  209. calculatedNextCheckInterval := originalFeed.ScheduleNextCheck(weeklyEntryCount, retryDelay)
  210. slog.Warn("Feed is rate limited",
  211. slog.String("feed_url", originalFeed.FeedURL),
  212. slog.Int("retry_delay_in_seconds", int(retryDelay.Seconds())),
  213. slog.Int("calculated_next_check_interval_in_minutes", int(calculatedNextCheckInterval.Minutes())),
  214. slog.Time("new_next_check_at", originalFeed.NextCheckAt),
  215. )
  216. }
  217. if localizedError := responseHandler.LocalizedError(); localizedError != nil {
  218. slog.Warn("Unable to fetch feed",
  219. slog.Int64("user_id", userID),
  220. slog.Int64("feed_id", feedID),
  221. slog.String("feed_url", originalFeed.FeedURL),
  222. slog.Any("error", localizedError.Error()),
  223. )
  224. return getTranslatedLocalizedError(store, userID, originalFeed, localizedError)
  225. }
  226. if store.AnotherFeedURLExists(userID, originalFeed.ID, responseHandler.EffectiveURL()) {
  227. localizedError := locale.NewLocalizedErrorWrapper(ErrDuplicatedFeed, "error.duplicated_feed")
  228. return getTranslatedLocalizedError(store, userID, originalFeed, localizedError)
  229. }
  230. if ignoreHTTPCache || responseHandler.IsModified(originalFeed.EtagHeader, originalFeed.LastModifiedHeader) {
  231. slog.Debug("Feed modified",
  232. slog.Int64("user_id", userID),
  233. slog.Int64("feed_id", feedID),
  234. slog.String("etag_header", originalFeed.EtagHeader),
  235. slog.String("last_modified_header", originalFeed.LastModifiedHeader),
  236. )
  237. responseBody, localizedError := responseHandler.ReadBody(config.Opts.HTTPClientMaxBodySize())
  238. if localizedError != nil {
  239. slog.Warn("Unable to fetch feed", slog.String("feed_url", originalFeed.FeedURL), slog.Any("error", localizedError.Error()))
  240. return localizedError
  241. }
  242. updatedFeed, parseErr := parser.ParseFeed(responseHandler.EffectiveURL(), bytes.NewReader(responseBody))
  243. if parseErr != nil {
  244. localizedError := locale.NewLocalizedErrorWrapper(parseErr, "error.unable_to_parse_feed", parseErr)
  245. if errors.Is(parseErr, parser.ErrFeedFormatNotDetected) {
  246. localizedError = locale.NewLocalizedErrorWrapper(parseErr, "error.feed_format_not_detected", parseErr)
  247. }
  248. return getTranslatedLocalizedError(store, userID, originalFeed, localizedError)
  249. }
  250. // Use the RSS TTL value, or the Cache-Control or Expires HTTP headers if available.
  251. // Otherwise, we use the default value from the configuration (min interval parameter).
  252. feedTTLValue := updatedFeed.TTL
  253. cacheControlMaxAgeValue := responseHandler.CacheControlMaxAge()
  254. expiresValue := responseHandler.Expires()
  255. refreshDelay := max(feedTTLValue, cacheControlMaxAgeValue, expiresValue)
  256. // Set the next check at with updated arguments.
  257. calculatedNextCheckInterval := originalFeed.ScheduleNextCheck(weeklyEntryCount, refreshDelay)
  258. slog.Debug("Updated next check date",
  259. slog.Int64("user_id", userID),
  260. slog.Int64("feed_id", feedID),
  261. slog.String("feed_url", originalFeed.FeedURL),
  262. slog.Int("feed_ttl_minutes", int(feedTTLValue.Minutes())),
  263. slog.Int("cache_control_max_age_in_minutes", int(cacheControlMaxAgeValue.Minutes())),
  264. slog.Int("expires_in_minutes", int(expiresValue.Minutes())),
  265. slog.Int("refresh_delay_in_minutes", int(refreshDelay.Minutes())),
  266. slog.Int("calculated_next_check_interval_in_minutes", int(calculatedNextCheckInterval.Minutes())),
  267. slog.Time("new_next_check_at", originalFeed.NextCheckAt),
  268. )
  269. originalFeed.Entries = updatedFeed.Entries
  270. processor.ProcessFeedEntries(store, originalFeed, userID, forceRefresh)
  271. // We don't update existing entries when the crawler is enabled (we crawl only inexisting entries).
  272. // We also skip updating existing entries if the feed has ignore_entry_updates enabled.
  273. // Unless it is forced to refresh.
  274. updateExistingEntries := forceRefresh || (!originalFeed.Crawler && !originalFeed.IgnoreEntryUpdates)
  275. newEntries, storeErr := store.RefreshFeedEntries(originalFeed.UserID, originalFeed.ID, originalFeed.Entries, updateExistingEntries)
  276. if storeErr != nil {
  277. localizedError := locale.NewLocalizedErrorWrapper(storeErr, "error.database_error", storeErr)
  278. return getTranslatedLocalizedError(store, userID, originalFeed, localizedError)
  279. }
  280. userIntegrations, intErr := store.Integration(userID)
  281. if intErr != nil {
  282. slog.Error("Fetching integrations failed; the refresh process will go on, but no integrations will run this time",
  283. slog.Int64("user_id", userID),
  284. slog.Int64("feed_id", feedID),
  285. slog.Any("error", intErr),
  286. )
  287. } else if userIntegrations != nil && len(newEntries) > 0 {
  288. go integration.PushEntries(originalFeed, newEntries, userIntegrations)
  289. }
  290. originalFeed.EtagHeader = responseHandler.ETag()
  291. originalFeed.LastModifiedHeader = responseHandler.LastModified()
  292. originalFeed.IconURL = updatedFeed.IconURL
  293. iconChecker := icon.NewIconChecker(store, originalFeed)
  294. if forceRefresh {
  295. iconChecker.UpdateOrCreateFeedIcon()
  296. } else {
  297. iconChecker.CreateFeedIconIfMissing()
  298. }
  299. } else {
  300. slog.Debug("Feed not modified",
  301. slog.Int64("user_id", userID),
  302. slog.Int64("feed_id", feedID),
  303. )
  304. // Last-Modified may be updated even if ETag is not. In this case, per
  305. // RFC9111 sections 3.2 and 4.3.4, the stored response must be updated.
  306. if responseHandler.LastModified() != "" {
  307. originalFeed.LastModifiedHeader = responseHandler.LastModified()
  308. }
  309. }
  310. originalFeed.ResetErrorCounter()
  311. if storeErr := store.UpdateFeed(originalFeed); storeErr != nil {
  312. localizedError := locale.NewLocalizedErrorWrapper(storeErr, "error.database_error", storeErr)
  313. return getTranslatedLocalizedError(store, userID, originalFeed, localizedError)
  314. }
  315. return nil
  316. }