handler.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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/reader/fetcher"
  14. "miniflux.app/v2/internal/reader/icon"
  15. "miniflux.app/v2/internal/reader/parser"
  16. "miniflux.app/v2/internal/reader/processor"
  17. "miniflux.app/v2/internal/storage"
  18. )
  19. var (
  20. ErrCategoryNotFound = errors.New("fetcher: category not found")
  21. ErrFeedNotFound = errors.New("fetcher: feed not found")
  22. ErrDuplicatedFeed = errors.New("fetcher: duplicated feed")
  23. )
  24. func CreateFeedFromSubscriptionDiscovery(store *storage.Storage, userID int64, feedCreationRequest *model.FeedCreationRequestFromSubscriptionDiscovery) (*model.Feed, *locale.LocalizedErrorWrapper) {
  25. slog.Debug("Begin feed creation process from subscription discovery",
  26. slog.Int64("user_id", userID),
  27. slog.String("feed_url", feedCreationRequest.FeedURL),
  28. )
  29. user, storeErr := store.UserByID(userID)
  30. if storeErr != nil {
  31. return nil, locale.NewLocalizedErrorWrapper(storeErr, "error.database_error", storeErr)
  32. }
  33. if !store.CategoryIDExists(userID, feedCreationRequest.CategoryID) {
  34. return nil, locale.NewLocalizedErrorWrapper(ErrCategoryNotFound, "error.category_not_found")
  35. }
  36. if store.FeedURLExists(userID, feedCreationRequest.FeedURL) {
  37. return nil, locale.NewLocalizedErrorWrapper(ErrDuplicatedFeed, "error.duplicated_feed")
  38. }
  39. subscription, parseErr := parser.ParseFeed(feedCreationRequest.FeedURL, feedCreationRequest.Content)
  40. if parseErr != nil {
  41. return nil, locale.NewLocalizedErrorWrapper(parseErr, "error.unable_to_parse_feed", parseErr)
  42. }
  43. subscription.UserID = userID
  44. subscription.UserAgent = feedCreationRequest.UserAgent
  45. subscription.Cookie = feedCreationRequest.Cookie
  46. subscription.Username = feedCreationRequest.Username
  47. subscription.Password = feedCreationRequest.Password
  48. subscription.Crawler = feedCreationRequest.Crawler
  49. subscription.Disabled = feedCreationRequest.Disabled
  50. subscription.IgnoreHTTPCache = feedCreationRequest.IgnoreHTTPCache
  51. subscription.AllowSelfSignedCertificates = feedCreationRequest.AllowSelfSignedCertificates
  52. subscription.FetchViaProxy = feedCreationRequest.FetchViaProxy
  53. subscription.ScraperRules = feedCreationRequest.ScraperRules
  54. subscription.RewriteRules = feedCreationRequest.RewriteRules
  55. subscription.BlocklistRules = feedCreationRequest.BlocklistRules
  56. subscription.KeeplistRules = feedCreationRequest.KeeplistRules
  57. subscription.UrlRewriteRules = feedCreationRequest.UrlRewriteRules
  58. subscription.EtagHeader = feedCreationRequest.ETag
  59. subscription.LastModifiedHeader = feedCreationRequest.LastModified
  60. subscription.FeedURL = feedCreationRequest.FeedURL
  61. subscription.WithCategoryID(feedCreationRequest.CategoryID)
  62. subscription.CheckedNow()
  63. processor.ProcessFeedEntries(store, subscription, user, true)
  64. if storeErr := store.CreateFeed(subscription); storeErr != nil {
  65. return nil, locale.NewLocalizedErrorWrapper(storeErr, "error.database_error", storeErr)
  66. }
  67. slog.Debug("Created feed",
  68. slog.Int64("user_id", userID),
  69. slog.Int64("feed_id", subscription.ID),
  70. slog.String("feed_url", subscription.FeedURL),
  71. )
  72. requestBuilder := fetcher.NewRequestBuilder()
  73. requestBuilder.WithUsernameAndPassword(feedCreationRequest.Username, feedCreationRequest.Password)
  74. requestBuilder.WithUserAgent(feedCreationRequest.UserAgent, config.Opts.HTTPClientUserAgent())
  75. requestBuilder.WithCookie(feedCreationRequest.Cookie)
  76. requestBuilder.WithTimeout(config.Opts.HTTPClientTimeout())
  77. requestBuilder.WithProxy(config.Opts.HTTPClientProxy())
  78. requestBuilder.UseProxy(feedCreationRequest.FetchViaProxy)
  79. requestBuilder.IgnoreTLSErrors(feedCreationRequest.AllowSelfSignedCertificates)
  80. checkFeedIcon(
  81. store,
  82. requestBuilder,
  83. subscription.ID,
  84. subscription.SiteURL,
  85. subscription.IconURL,
  86. )
  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. )
  95. user, storeErr := store.UserByID(userID)
  96. if storeErr != nil {
  97. return nil, locale.NewLocalizedErrorWrapper(storeErr, "error.database_error", storeErr)
  98. }
  99. if !store.CategoryIDExists(userID, feedCreationRequest.CategoryID) {
  100. return nil, locale.NewLocalizedErrorWrapper(ErrCategoryNotFound, "error.category_not_found")
  101. }
  102. requestBuilder := fetcher.NewRequestBuilder()
  103. requestBuilder.WithUsernameAndPassword(feedCreationRequest.Username, feedCreationRequest.Password)
  104. requestBuilder.WithUserAgent(feedCreationRequest.UserAgent, config.Opts.HTTPClientUserAgent())
  105. requestBuilder.WithCookie(feedCreationRequest.Cookie)
  106. requestBuilder.WithTimeout(config.Opts.HTTPClientTimeout())
  107. requestBuilder.WithProxy(config.Opts.HTTPClientProxy())
  108. requestBuilder.UseProxy(feedCreationRequest.FetchViaProxy)
  109. requestBuilder.IgnoreTLSErrors(feedCreationRequest.AllowSelfSignedCertificates)
  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.Disabled = feedCreationRequest.Disabled
  135. subscription.IgnoreHTTPCache = feedCreationRequest.IgnoreHTTPCache
  136. subscription.AllowSelfSignedCertificates = feedCreationRequest.AllowSelfSignedCertificates
  137. subscription.FetchViaProxy = feedCreationRequest.FetchViaProxy
  138. subscription.ScraperRules = feedCreationRequest.ScraperRules
  139. subscription.RewriteRules = feedCreationRequest.RewriteRules
  140. subscription.BlocklistRules = feedCreationRequest.BlocklistRules
  141. subscription.KeeplistRules = feedCreationRequest.KeeplistRules
  142. subscription.UrlRewriteRules = feedCreationRequest.UrlRewriteRules
  143. subscription.EtagHeader = responseHandler.ETag()
  144. subscription.LastModifiedHeader = responseHandler.LastModified()
  145. subscription.FeedURL = responseHandler.EffectiveURL()
  146. subscription.WithCategoryID(feedCreationRequest.CategoryID)
  147. subscription.CheckedNow()
  148. processor.ProcessFeedEntries(store, subscription, user, true)
  149. if storeErr := store.CreateFeed(subscription); storeErr != nil {
  150. return nil, locale.NewLocalizedErrorWrapper(storeErr, "error.database_error", storeErr)
  151. }
  152. slog.Debug("Created feed",
  153. slog.Int64("user_id", userID),
  154. slog.Int64("feed_id", subscription.ID),
  155. slog.String("feed_url", subscription.FeedURL),
  156. )
  157. checkFeedIcon(
  158. store,
  159. requestBuilder,
  160. subscription.ID,
  161. subscription.SiteURL,
  162. subscription.IconURL,
  163. )
  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. user, storeErr := store.UserByID(userID)
  174. if storeErr != nil {
  175. return locale.NewLocalizedErrorWrapper(storeErr, "error.database_error", storeErr)
  176. }
  177. originalFeed, storeErr := store.FeedByID(userID, feedID)
  178. if storeErr != nil {
  179. return locale.NewLocalizedErrorWrapper(storeErr, "error.database_error", storeErr)
  180. }
  181. if originalFeed == nil {
  182. return locale.NewLocalizedErrorWrapper(ErrFeedNotFound, "error.feed_not_found")
  183. }
  184. weeklyEntryCount := 0
  185. if config.Opts.PollingScheduler() == model.SchedulerEntryFrequency {
  186. var weeklyCountErr error
  187. weeklyEntryCount, weeklyCountErr = store.WeeklyFeedEntryCount(userID, feedID)
  188. if weeklyCountErr != nil {
  189. return locale.NewLocalizedErrorWrapper(weeklyCountErr, "error.database_error", weeklyCountErr)
  190. }
  191. }
  192. originalFeed.CheckedNow()
  193. originalFeed.ScheduleNextCheck(weeklyEntryCount)
  194. requestBuilder := fetcher.NewRequestBuilder()
  195. requestBuilder.WithUsernameAndPassword(originalFeed.Username, originalFeed.Password)
  196. requestBuilder.WithUserAgent(originalFeed.UserAgent, config.Opts.HTTPClientUserAgent())
  197. requestBuilder.WithCookie(originalFeed.Cookie)
  198. requestBuilder.WithTimeout(config.Opts.HTTPClientTimeout())
  199. requestBuilder.WithProxy(config.Opts.HTTPClientProxy())
  200. requestBuilder.UseProxy(originalFeed.FetchViaProxy)
  201. requestBuilder.IgnoreTLSErrors(originalFeed.AllowSelfSignedCertificates)
  202. responseHandler := fetcher.NewResponseHandler(requestBuilder.ExecuteRequest(originalFeed.FeedURL))
  203. defer responseHandler.Close()
  204. if localizedError := responseHandler.LocalizedError(); localizedError != nil {
  205. slog.Warn("Unable to fetch feed", slog.String("feed_url", originalFeed.FeedURL), slog.Any("error", localizedError.Error()))
  206. originalFeed.WithTranslatedErrorMessage(localizedError.Translate(user.Language))
  207. store.UpdateFeedError(originalFeed)
  208. return localizedError
  209. }
  210. if store.AnotherFeedURLExists(userID, originalFeed.ID, responseHandler.EffectiveURL()) {
  211. localizedError := locale.NewLocalizedErrorWrapper(ErrDuplicatedFeed, "error.duplicated_feed")
  212. originalFeed.WithTranslatedErrorMessage(localizedError.Translate(user.Language))
  213. store.UpdateFeedError(originalFeed)
  214. return localizedError
  215. }
  216. if originalFeed.IgnoreHTTPCache || responseHandler.IsModified(originalFeed.EtagHeader, originalFeed.LastModifiedHeader) {
  217. slog.Debug("Feed modified",
  218. slog.Int64("user_id", userID),
  219. slog.Int64("feed_id", feedID),
  220. )
  221. responseBody, localizedError := responseHandler.ReadBody(config.Opts.HTTPClientMaxBodySize())
  222. if localizedError != nil {
  223. slog.Warn("Unable to fetch feed", slog.String("feed_url", originalFeed.FeedURL), slog.Any("error", localizedError.Error()))
  224. return localizedError
  225. }
  226. updatedFeed, parseErr := parser.ParseFeed(responseHandler.EffectiveURL(), bytes.NewReader(responseBody))
  227. if parseErr != nil {
  228. localizedError := locale.NewLocalizedErrorWrapper(parseErr, "error.unable_to_parse_feed")
  229. if errors.Is(parseErr, parser.ErrFeedFormatNotDetected) {
  230. localizedError = locale.NewLocalizedErrorWrapper(parseErr, "error.feed_format_not_detected", parseErr)
  231. }
  232. originalFeed.WithTranslatedErrorMessage(localizedError.Translate(user.Language))
  233. store.UpdateFeedError(originalFeed)
  234. return localizedError
  235. }
  236. // If the feed has a TTL defined, we use it to make sure we don't check it too often.
  237. if updatedFeed.TTL > 0 {
  238. minNextCheckAt := time.Now().Add(time.Minute * time.Duration(updatedFeed.TTL))
  239. slog.Debug("Feed TTL",
  240. slog.Int64("user_id", userID),
  241. slog.Int64("feed_id", feedID),
  242. slog.Int("ttl", updatedFeed.TTL),
  243. slog.Time("next_check_at", originalFeed.NextCheckAt),
  244. )
  245. if originalFeed.NextCheckAt.IsZero() || originalFeed.NextCheckAt.Before(minNextCheckAt) {
  246. slog.Debug("Updating next check date based on TTL",
  247. slog.Int64("user_id", userID),
  248. slog.Int64("feed_id", feedID),
  249. slog.Int("ttl", updatedFeed.TTL),
  250. slog.Time("new_next_check_at", minNextCheckAt),
  251. slog.Time("old_next_check_at", originalFeed.NextCheckAt),
  252. )
  253. originalFeed.NextCheckAt = minNextCheckAt
  254. }
  255. }
  256. originalFeed.Entries = updatedFeed.Entries
  257. processor.ProcessFeedEntries(store, originalFeed, user, forceRefresh)
  258. // We don't update existing entries when the crawler is enabled (we crawl only inexisting entries). Unless it is forced to refresh
  259. updateExistingEntries := forceRefresh || !originalFeed.Crawler
  260. newEntries, storeErr := store.RefreshFeedEntries(originalFeed.UserID, originalFeed.ID, originalFeed.Entries, updateExistingEntries)
  261. if storeErr != nil {
  262. localizedError := locale.NewLocalizedErrorWrapper(storeErr, "error.database_error", storeErr)
  263. originalFeed.WithTranslatedErrorMessage(localizedError.Translate(user.Language))
  264. store.UpdateFeedError(originalFeed)
  265. return localizedError
  266. }
  267. userIntegrations, intErr := store.Integration(userID)
  268. if intErr != nil {
  269. slog.Error("Fetching integrations failed; the refresh process will go on, but no integrations will run this time",
  270. slog.Int64("user_id", userID),
  271. slog.Int64("feed_id", feedID),
  272. slog.Any("error", intErr),
  273. )
  274. } else if userIntegrations != nil && len(newEntries) > 0 {
  275. go integration.PushEntries(originalFeed, newEntries, userIntegrations)
  276. }
  277. // We update caching headers only if the feed has been modified,
  278. // because some websites don't return the same headers when replying with a 304.
  279. originalFeed.EtagHeader = responseHandler.ETag()
  280. originalFeed.LastModifiedHeader = responseHandler.LastModified()
  281. checkFeedIcon(
  282. store,
  283. requestBuilder,
  284. originalFeed.ID,
  285. originalFeed.SiteURL,
  286. updatedFeed.IconURL,
  287. )
  288. } else {
  289. slog.Debug("Feed not modified",
  290. slog.Int64("user_id", userID),
  291. slog.Int64("feed_id", feedID),
  292. )
  293. }
  294. originalFeed.ResetErrorCounter()
  295. if storeErr := store.UpdateFeed(originalFeed); storeErr != nil {
  296. localizedError := locale.NewLocalizedErrorWrapper(storeErr, "error.database_error", storeErr)
  297. originalFeed.WithTranslatedErrorMessage(localizedError.Translate(user.Language))
  298. store.UpdateFeedError(originalFeed)
  299. return localizedError
  300. }
  301. return nil
  302. }
  303. func checkFeedIcon(store *storage.Storage, requestBuilder *fetcher.RequestBuilder, feedID int64, websiteURL, feedIconURL string) {
  304. if !store.HasIcon(feedID) {
  305. iconFinder := icon.NewIconFinder(requestBuilder, websiteURL, feedIconURL)
  306. if icon, err := iconFinder.FindIcon(); err != nil {
  307. slog.Debug("Unable to find feed icon",
  308. slog.Int64("feed_id", feedID),
  309. slog.String("website_url", websiteURL),
  310. slog.String("feed_icon_url", feedIconURL),
  311. slog.Any("error", err),
  312. )
  313. } else if icon == nil {
  314. slog.Debug("No icon found",
  315. slog.Int64("feed_id", feedID),
  316. slog.String("website_url", websiteURL),
  317. slog.String("feed_icon_url", feedIconURL),
  318. )
  319. } else {
  320. if err := store.CreateFeedIcon(feedID, icon); err != nil {
  321. slog.Error("Unable to store feed icon",
  322. slog.Int64("feed_id", feedID),
  323. slog.String("website_url", websiteURL),
  324. slog.String("feed_icon_url", feedIconURL),
  325. slog.Any("error", err),
  326. )
  327. }
  328. }
  329. }
  330. }