handler.go 15 KB

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