handler.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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. if config.Opts.PollingScheduler() == model.SchedulerEntryFrequency {
  182. var weeklyCountErr error
  183. weeklyEntryCount, weeklyCountErr = store.WeeklyFeedEntryCount(userID, feedID)
  184. if weeklyCountErr != nil {
  185. return locale.NewLocalizedErrorWrapper(weeklyCountErr, "error.database_error", weeklyCountErr)
  186. }
  187. }
  188. originalFeed.CheckedNow()
  189. originalFeed.ScheduleNextCheck(weeklyEntryCount, time.Duration(0))
  190. requestBuilder := fetcher.NewRequestBuilder()
  191. requestBuilder.WithUsernameAndPassword(originalFeed.Username, originalFeed.Password)
  192. requestBuilder.WithUserAgent(originalFeed.UserAgent, config.Opts.HTTPClientUserAgent())
  193. requestBuilder.WithCookie(originalFeed.Cookie)
  194. requestBuilder.WithTimeout(config.Opts.HTTPClientTimeout())
  195. requestBuilder.WithProxyRotator(proxyrotator.ProxyRotatorInstance)
  196. requestBuilder.WithCustomFeedProxyURL(originalFeed.ProxyURL)
  197. requestBuilder.WithCustomApplicationProxyURL(config.Opts.HTTPClientProxyURL())
  198. requestBuilder.UseCustomApplicationProxyURL(originalFeed.FetchViaProxy)
  199. requestBuilder.IgnoreTLSErrors(originalFeed.AllowSelfSignedCertificates)
  200. requestBuilder.DisableHTTP2(originalFeed.DisableHTTP2)
  201. ignoreHTTPCache := originalFeed.IgnoreHTTPCache || forceRefresh
  202. if !ignoreHTTPCache {
  203. requestBuilder.WithETag(originalFeed.EtagHeader)
  204. requestBuilder.WithLastModified(originalFeed.LastModifiedHeader)
  205. }
  206. responseHandler := fetcher.NewResponseHandler(requestBuilder.ExecuteRequest(originalFeed.FeedURL))
  207. defer responseHandler.Close()
  208. if responseHandler.IsRateLimited() {
  209. retryDelay := responseHandler.ParseRetryDelay()
  210. calculatedNextCheckInterval := originalFeed.ScheduleNextCheck(weeklyEntryCount, retryDelay)
  211. slog.Warn("Feed is rate limited",
  212. slog.String("feed_url", originalFeed.FeedURL),
  213. slog.Int("retry_delay_in_seconds", int(retryDelay.Seconds())),
  214. slog.Int("calculated_next_check_interval_in_minutes", int(calculatedNextCheckInterval.Minutes())),
  215. slog.Time("new_next_check_at", originalFeed.NextCheckAt),
  216. )
  217. }
  218. if localizedError := responseHandler.LocalizedError(); localizedError != nil {
  219. slog.Warn("Unable to fetch feed",
  220. slog.Int64("user_id", userID),
  221. slog.Int64("feed_id", feedID),
  222. slog.String("feed_url", originalFeed.FeedURL),
  223. slog.Any("error", localizedError.Error()),
  224. )
  225. user, storeErr := store.UserByID(userID)
  226. if storeErr != nil {
  227. return locale.NewLocalizedErrorWrapper(storeErr, "error.database_error", storeErr)
  228. }
  229. originalFeed.WithTranslatedErrorMessage(localizedError.Translate(user.Language))
  230. store.UpdateFeedError(originalFeed)
  231. return localizedError
  232. }
  233. if store.AnotherFeedURLExists(userID, originalFeed.ID, responseHandler.EffectiveURL()) {
  234. localizedError := locale.NewLocalizedErrorWrapper(ErrDuplicatedFeed, "error.duplicated_feed")
  235. user, storeErr := store.UserByID(userID)
  236. if storeErr != nil {
  237. return locale.NewLocalizedErrorWrapper(storeErr, "error.database_error", storeErr)
  238. }
  239. originalFeed.WithTranslatedErrorMessage(localizedError.Translate(user.Language))
  240. store.UpdateFeedError(originalFeed)
  241. return localizedError
  242. }
  243. if ignoreHTTPCache || responseHandler.IsModified(originalFeed.EtagHeader, originalFeed.LastModifiedHeader) {
  244. slog.Debug("Feed modified",
  245. slog.Int64("user_id", userID),
  246. slog.Int64("feed_id", feedID),
  247. slog.String("etag_header", originalFeed.EtagHeader),
  248. slog.String("last_modified_header", originalFeed.LastModifiedHeader),
  249. )
  250. responseBody, localizedError := responseHandler.ReadBody(config.Opts.HTTPClientMaxBodySize())
  251. if localizedError != nil {
  252. slog.Warn("Unable to fetch feed", slog.String("feed_url", originalFeed.FeedURL), slog.Any("error", localizedError.Error()))
  253. return localizedError
  254. }
  255. updatedFeed, parseErr := parser.ParseFeed(responseHandler.EffectiveURL(), bytes.NewReader(responseBody))
  256. if parseErr != nil {
  257. localizedError := locale.NewLocalizedErrorWrapper(parseErr, "error.unable_to_parse_feed", parseErr)
  258. if errors.Is(parseErr, parser.ErrFeedFormatNotDetected) {
  259. localizedError = locale.NewLocalizedErrorWrapper(parseErr, "error.feed_format_not_detected", parseErr)
  260. }
  261. user, storeErr := store.UserByID(userID)
  262. if storeErr != nil {
  263. return locale.NewLocalizedErrorWrapper(storeErr, "error.database_error", storeErr)
  264. }
  265. originalFeed.WithTranslatedErrorMessage(localizedError.Translate(user.Language))
  266. store.UpdateFeedError(originalFeed)
  267. return localizedError
  268. }
  269. // Use the RSS TTL value, or the Cache-Control or Expires HTTP headers if available.
  270. // Otherwise, we use the default value from the configuration (min interval parameter).
  271. feedTTLValue := updatedFeed.TTL
  272. cacheControlMaxAgeValue := responseHandler.CacheControlMaxAge()
  273. expiresValue := responseHandler.Expires()
  274. refreshDelay := max(feedTTLValue, cacheControlMaxAgeValue, expiresValue)
  275. // Set the next check at with updated arguments.
  276. calculatedNextCheckInterval := originalFeed.ScheduleNextCheck(weeklyEntryCount, refreshDelay)
  277. slog.Debug("Updated next check date",
  278. slog.Int64("user_id", userID),
  279. slog.Int64("feed_id", feedID),
  280. slog.String("feed_url", originalFeed.FeedURL),
  281. slog.Int("feed_ttl_minutes", int(feedTTLValue.Minutes())),
  282. slog.Int("cache_control_max_age_in_minutes", int(cacheControlMaxAgeValue.Minutes())),
  283. slog.Int("expires_in_minutes", int(expiresValue.Minutes())),
  284. slog.Int("refresh_delay_in_minutes", int(refreshDelay.Minutes())),
  285. slog.Int("calculated_next_check_interval_in_minutes", int(calculatedNextCheckInterval.Minutes())),
  286. slog.Time("new_next_check_at", originalFeed.NextCheckAt),
  287. )
  288. originalFeed.Entries = updatedFeed.Entries
  289. processor.ProcessFeedEntries(store, originalFeed, userID, forceRefresh)
  290. // We don't update existing entries when the crawler is enabled (we crawl only inexisting entries).
  291. // We also skip updating existing entries if the feed has ignore_entry_updates enabled.
  292. // Unless it is forced to refresh.
  293. updateExistingEntries := forceRefresh || (!originalFeed.Crawler && !originalFeed.IgnoreEntryUpdates)
  294. newEntries, storeErr := store.RefreshFeedEntries(originalFeed.UserID, originalFeed.ID, originalFeed.Entries, updateExistingEntries)
  295. if storeErr != nil {
  296. localizedError := locale.NewLocalizedErrorWrapper(storeErr, "error.database_error", storeErr)
  297. user, storeErr := store.UserByID(userID)
  298. if storeErr != nil {
  299. return locale.NewLocalizedErrorWrapper(storeErr, "error.database_error", storeErr)
  300. }
  301. originalFeed.WithTranslatedErrorMessage(localizedError.Translate(user.Language))
  302. store.UpdateFeedError(originalFeed)
  303. return localizedError
  304. }
  305. userIntegrations, intErr := store.Integration(userID)
  306. if intErr != nil {
  307. slog.Error("Fetching integrations failed; the refresh process will go on, but no integrations will run this time",
  308. slog.Int64("user_id", userID),
  309. slog.Int64("feed_id", feedID),
  310. slog.Any("error", intErr),
  311. )
  312. } else if userIntegrations != nil && len(newEntries) > 0 {
  313. go integration.PushEntries(originalFeed, newEntries, userIntegrations)
  314. }
  315. originalFeed.EtagHeader = responseHandler.ETag()
  316. originalFeed.LastModifiedHeader = responseHandler.LastModified()
  317. originalFeed.IconURL = updatedFeed.IconURL
  318. iconChecker := icon.NewIconChecker(store, originalFeed)
  319. if forceRefresh {
  320. iconChecker.UpdateOrCreateFeedIcon()
  321. } else {
  322. iconChecker.CreateFeedIconIfMissing()
  323. }
  324. } else {
  325. slog.Debug("Feed not modified",
  326. slog.Int64("user_id", userID),
  327. slog.Int64("feed_id", feedID),
  328. )
  329. // Last-Modified may be updated even if ETag is not. In this case, per
  330. // RFC9111 sections 3.2 and 4.3.4, the stored response must be updated.
  331. if responseHandler.LastModified() != "" {
  332. originalFeed.LastModifiedHeader = responseHandler.LastModified()
  333. }
  334. }
  335. originalFeed.ResetErrorCounter()
  336. if storeErr := store.UpdateFeed(originalFeed); storeErr != nil {
  337. localizedError := locale.NewLocalizedErrorWrapper(storeErr, "error.database_error", storeErr)
  338. user, storeErr := store.UserByID(userID)
  339. if storeErr != nil {
  340. return locale.NewLocalizedErrorWrapper(storeErr, "error.database_error", storeErr)
  341. }
  342. originalFeed.WithTranslatedErrorMessage(localizedError.Translate(user.Language))
  343. store.UpdateFeedError(originalFeed)
  344. return localizedError
  345. }
  346. return nil
  347. }