handler.go 9.5 KB

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