handler.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. "log/slog"
  6. "miniflux.app/v2/internal/config"
  7. "miniflux.app/v2/internal/errors"
  8. "miniflux.app/v2/internal/http/client"
  9. "miniflux.app/v2/internal/integration"
  10. "miniflux.app/v2/internal/locale"
  11. "miniflux.app/v2/internal/model"
  12. "miniflux.app/v2/internal/reader/browser"
  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. errDuplicate = "This feed already exists (%s)"
  20. errNotFound = "Feed %d not found"
  21. errCategoryNotFound = "Category not found for this user"
  22. )
  23. // CreateFeed fetch, parse and store a new feed.
  24. func CreateFeed(store *storage.Storage, userID int64, feedCreationRequest *model.FeedCreationRequest) (*model.Feed, error) {
  25. slog.Debug("Begin feed creation process",
  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, storeErr
  32. }
  33. if !store.CategoryIDExists(userID, feedCreationRequest.CategoryID) {
  34. return nil, errors.NewLocalizedError(errCategoryNotFound)
  35. }
  36. request := client.NewClientWithConfig(feedCreationRequest.FeedURL, config.Opts)
  37. request.WithCredentials(feedCreationRequest.Username, feedCreationRequest.Password)
  38. request.WithUserAgent(feedCreationRequest.UserAgent)
  39. request.WithCookie(feedCreationRequest.Cookie)
  40. request.AllowSelfSignedCertificates = feedCreationRequest.AllowSelfSignedCertificates
  41. if feedCreationRequest.FetchViaProxy {
  42. request.WithProxy()
  43. }
  44. response, requestErr := browser.Exec(request)
  45. if requestErr != nil {
  46. return nil, requestErr
  47. }
  48. if store.FeedURLExists(userID, response.EffectiveURL) {
  49. return nil, errors.NewLocalizedError(errDuplicate, response.EffectiveURL)
  50. }
  51. subscription, parseErr := parser.ParseFeed(response.EffectiveURL, response.BodyAsString())
  52. if parseErr != nil {
  53. return nil, parseErr
  54. }
  55. subscription.UserID = userID
  56. subscription.UserAgent = feedCreationRequest.UserAgent
  57. subscription.Cookie = feedCreationRequest.Cookie
  58. subscription.Username = feedCreationRequest.Username
  59. subscription.Password = feedCreationRequest.Password
  60. subscription.Crawler = feedCreationRequest.Crawler
  61. subscription.Disabled = feedCreationRequest.Disabled
  62. subscription.IgnoreHTTPCache = feedCreationRequest.IgnoreHTTPCache
  63. subscription.AllowSelfSignedCertificates = feedCreationRequest.AllowSelfSignedCertificates
  64. subscription.FetchViaProxy = feedCreationRequest.FetchViaProxy
  65. subscription.ScraperRules = feedCreationRequest.ScraperRules
  66. subscription.RewriteRules = feedCreationRequest.RewriteRules
  67. subscription.BlocklistRules = feedCreationRequest.BlocklistRules
  68. subscription.KeeplistRules = feedCreationRequest.KeeplistRules
  69. subscription.UrlRewriteRules = feedCreationRequest.UrlRewriteRules
  70. subscription.WithCategoryID(feedCreationRequest.CategoryID)
  71. subscription.WithClientResponse(response)
  72. subscription.CheckedNow()
  73. processor.ProcessFeedEntries(store, subscription, user, true)
  74. if storeErr := store.CreateFeed(subscription); storeErr != nil {
  75. return nil, storeErr
  76. }
  77. slog.Debug("Created feed",
  78. slog.Int64("user_id", userID),
  79. slog.Int64("feed_id", subscription.ID),
  80. slog.String("feed_url", subscription.FeedURL),
  81. )
  82. checkFeedIcon(
  83. store,
  84. subscription.ID,
  85. subscription.SiteURL,
  86. subscription.IconURL,
  87. feedCreationRequest.UserAgent,
  88. feedCreationRequest.FetchViaProxy,
  89. feedCreationRequest.AllowSelfSignedCertificates,
  90. )
  91. return subscription, nil
  92. }
  93. // RefreshFeed refreshes a feed.
  94. func RefreshFeed(store *storage.Storage, userID, feedID int64, forceRefresh bool) error {
  95. slog.Debug("Begin feed refresh process",
  96. slog.Int64("user_id", userID),
  97. slog.Int64("feed_id", feedID),
  98. slog.Bool("force_refresh", forceRefresh),
  99. )
  100. user, storeErr := store.UserByID(userID)
  101. if storeErr != nil {
  102. return storeErr
  103. }
  104. printer := locale.NewPrinter(user.Language)
  105. originalFeed, storeErr := store.FeedByID(userID, feedID)
  106. if storeErr != nil {
  107. return storeErr
  108. }
  109. if originalFeed == nil {
  110. return errors.NewLocalizedError(errNotFound, feedID)
  111. }
  112. weeklyEntryCount := 0
  113. if config.Opts.PollingScheduler() == model.SchedulerEntryFrequency {
  114. var weeklyCountErr error
  115. weeklyEntryCount, weeklyCountErr = store.WeeklyFeedEntryCount(userID, feedID)
  116. if weeklyCountErr != nil {
  117. return weeklyCountErr
  118. }
  119. }
  120. originalFeed.CheckedNow()
  121. originalFeed.ScheduleNextCheck(weeklyEntryCount)
  122. request := client.NewClientWithConfig(originalFeed.FeedURL, config.Opts)
  123. request.WithCredentials(originalFeed.Username, originalFeed.Password)
  124. request.WithUserAgent(originalFeed.UserAgent)
  125. request.WithCookie(originalFeed.Cookie)
  126. request.AllowSelfSignedCertificates = originalFeed.AllowSelfSignedCertificates
  127. if !originalFeed.IgnoreHTTPCache {
  128. request.WithCacheHeaders(originalFeed.EtagHeader, originalFeed.LastModifiedHeader)
  129. }
  130. if originalFeed.FetchViaProxy {
  131. request.WithProxy()
  132. }
  133. response, requestErr := browser.Exec(request)
  134. if requestErr != nil {
  135. originalFeed.WithError(requestErr.Localize(printer))
  136. store.UpdateFeedError(originalFeed)
  137. return requestErr
  138. }
  139. if store.AnotherFeedURLExists(userID, originalFeed.ID, response.EffectiveURL) {
  140. storeErr := errors.NewLocalizedError(errDuplicate, response.EffectiveURL)
  141. originalFeed.WithError(storeErr.Error())
  142. store.UpdateFeedError(originalFeed)
  143. return storeErr
  144. }
  145. if originalFeed.IgnoreHTTPCache || response.IsModified(originalFeed.EtagHeader, originalFeed.LastModifiedHeader) {
  146. slog.Debug("Feed modified",
  147. slog.Int64("user_id", userID),
  148. slog.Int64("feed_id", feedID),
  149. )
  150. updatedFeed, parseErr := parser.ParseFeed(response.EffectiveURL, response.BodyAsString())
  151. if parseErr != nil {
  152. originalFeed.WithError(parseErr.Localize(printer))
  153. store.UpdateFeedError(originalFeed)
  154. return parseErr
  155. }
  156. originalFeed.Entries = updatedFeed.Entries
  157. processor.ProcessFeedEntries(store, originalFeed, user, forceRefresh)
  158. // We don't update existing entries when the crawler is enabled (we crawl only inexisting entries). Unless it is forced to refresh
  159. updateExistingEntries := forceRefresh || !originalFeed.Crawler
  160. newEntries, storeErr := store.RefreshFeedEntries(originalFeed.UserID, originalFeed.ID, originalFeed.Entries, updateExistingEntries)
  161. if storeErr != nil {
  162. originalFeed.WithError(storeErr.Error())
  163. store.UpdateFeedError(originalFeed)
  164. return storeErr
  165. }
  166. userIntegrations, intErr := store.Integration(userID)
  167. if intErr != nil {
  168. slog.Error("Fetching integrations failed; the refresh process will go on, but no integrations will run this time",
  169. slog.Int64("user_id", userID),
  170. slog.Int64("feed_id", feedID),
  171. slog.Any("error", intErr),
  172. )
  173. } else if userIntegrations != nil && len(newEntries) > 0 {
  174. go integration.PushEntries(originalFeed, newEntries, userIntegrations)
  175. }
  176. // We update caching headers only if the feed has been modified,
  177. // because some websites don't return the same headers when replying with a 304.
  178. originalFeed.WithClientResponse(response)
  179. checkFeedIcon(
  180. store,
  181. originalFeed.ID,
  182. originalFeed.SiteURL,
  183. updatedFeed.IconURL,
  184. originalFeed.UserAgent,
  185. originalFeed.FetchViaProxy,
  186. originalFeed.AllowSelfSignedCertificates,
  187. )
  188. } else {
  189. slog.Debug("Feed not modified",
  190. slog.Int64("user_id", userID),
  191. slog.Int64("feed_id", feedID),
  192. )
  193. }
  194. originalFeed.ResetErrorCounter()
  195. if storeErr := store.UpdateFeed(originalFeed); storeErr != nil {
  196. originalFeed.WithError(storeErr.Error())
  197. store.UpdateFeedError(originalFeed)
  198. return storeErr
  199. }
  200. return nil
  201. }
  202. func checkFeedIcon(store *storage.Storage, feedID int64, websiteURL, feedIconURL, userAgent string, fetchViaProxy, allowSelfSignedCertificates bool) {
  203. if !store.HasIcon(feedID) {
  204. iconFinder := icon.NewIconFinder(websiteURL, feedIconURL, userAgent, fetchViaProxy, allowSelfSignedCertificates)
  205. if icon, err := iconFinder.FindIcon(); err != nil {
  206. slog.Warn("Unable to find feed icon",
  207. slog.Int64("feed_id", feedID),
  208. slog.String("website_url", websiteURL),
  209. slog.String("feed_icon_url", feedIconURL),
  210. slog.Any("error", err),
  211. )
  212. } else if icon == nil {
  213. slog.Debug("No icon found",
  214. slog.Int64("feed_id", feedID),
  215. slog.String("website_url", websiteURL),
  216. slog.String("feed_icon_url", feedIconURL),
  217. )
  218. } else {
  219. if err := store.CreateFeedIcon(feedID, icon); err != nil {
  220. slog.Error("Unable to store feed icon",
  221. slog.Int64("feed_id", feedID),
  222. slog.String("website_url", websiteURL),
  223. slog.String("feed_icon_url", feedIconURL),
  224. slog.Any("error", err),
  225. )
  226. }
  227. }
  228. }
  229. }