handler.go 18 KB

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