handler.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. "fmt"
  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/logger"
  13. "miniflux.app/v2/internal/model"
  14. "miniflux.app/v2/internal/reader/browser"
  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. "miniflux.app/v2/internal/timer"
  20. )
  21. var (
  22. errDuplicate = "This feed already exists (%s)"
  23. errNotFound = "Feed %d not found"
  24. errCategoryNotFound = "Category not found for this user"
  25. )
  26. // CreateFeed fetch, parse and store a new feed.
  27. func CreateFeed(store *storage.Storage, userID int64, feedCreationRequest *model.FeedCreationRequest) (*model.Feed, error) {
  28. defer timer.ExecutionTime(time.Now(), fmt.Sprintf("[CreateFeed] FeedURL=%s", feedCreationRequest.FeedURL))
  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. logger.Debug("[CreateFeed] Feed saved with ID: %d", subscription.ID)
  78. checkFeedIcon(
  79. store,
  80. subscription.ID,
  81. subscription.SiteURL,
  82. subscription.IconURL,
  83. feedCreationRequest.UserAgent,
  84. feedCreationRequest.FetchViaProxy,
  85. feedCreationRequest.AllowSelfSignedCertificates,
  86. )
  87. return subscription, nil
  88. }
  89. // RefreshFeed refreshes a feed.
  90. func RefreshFeed(store *storage.Storage, userID, feedID int64, forceRefresh bool) error {
  91. defer timer.ExecutionTime(time.Now(), fmt.Sprintf("[RefreshFeed] feedID=%d", feedID))
  92. user, storeErr := store.UserByID(userID)
  93. if storeErr != nil {
  94. return storeErr
  95. }
  96. printer := locale.NewPrinter(user.Language)
  97. originalFeed, storeErr := store.FeedByID(userID, feedID)
  98. if storeErr != nil {
  99. return storeErr
  100. }
  101. if originalFeed == nil {
  102. return errors.NewLocalizedError(errNotFound, feedID)
  103. }
  104. weeklyEntryCount := 0
  105. if config.Opts.PollingScheduler() == model.SchedulerEntryFrequency {
  106. var weeklyCountErr error
  107. weeklyEntryCount, weeklyCountErr = store.WeeklyFeedEntryCount(userID, feedID)
  108. if weeklyCountErr != nil {
  109. return weeklyCountErr
  110. }
  111. }
  112. originalFeed.CheckedNow()
  113. originalFeed.ScheduleNextCheck(weeklyEntryCount)
  114. request := client.NewClientWithConfig(originalFeed.FeedURL, config.Opts)
  115. request.WithCredentials(originalFeed.Username, originalFeed.Password)
  116. request.WithUserAgent(originalFeed.UserAgent)
  117. request.WithCookie(originalFeed.Cookie)
  118. request.AllowSelfSignedCertificates = originalFeed.AllowSelfSignedCertificates
  119. if !originalFeed.IgnoreHTTPCache {
  120. request.WithCacheHeaders(originalFeed.EtagHeader, originalFeed.LastModifiedHeader)
  121. }
  122. if originalFeed.FetchViaProxy {
  123. request.WithProxy()
  124. }
  125. response, requestErr := browser.Exec(request)
  126. if requestErr != nil {
  127. originalFeed.WithError(requestErr.Localize(printer))
  128. store.UpdateFeedError(originalFeed)
  129. return requestErr
  130. }
  131. if store.AnotherFeedURLExists(userID, originalFeed.ID, response.EffectiveURL) {
  132. storeErr := errors.NewLocalizedError(errDuplicate, response.EffectiveURL)
  133. originalFeed.WithError(storeErr.Error())
  134. store.UpdateFeedError(originalFeed)
  135. return storeErr
  136. }
  137. if originalFeed.IgnoreHTTPCache || response.IsModified(originalFeed.EtagHeader, originalFeed.LastModifiedHeader) {
  138. logger.Debug("[RefreshFeed] Feed #%d has been modified", feedID)
  139. updatedFeed, parseErr := parser.ParseFeed(response.EffectiveURL, response.BodyAsString())
  140. if parseErr != nil {
  141. originalFeed.WithError(parseErr.Localize(printer))
  142. store.UpdateFeedError(originalFeed)
  143. return parseErr
  144. }
  145. originalFeed.Entries = updatedFeed.Entries
  146. processor.ProcessFeedEntries(store, originalFeed, user, forceRefresh)
  147. // We don't update existing entries when the crawler is enabled (we crawl only inexisting entries). Unless it is forced to refresh
  148. updateExistingEntries := forceRefresh || !originalFeed.Crawler
  149. newEntries, storeErr := store.RefreshFeedEntries(originalFeed.UserID, originalFeed.ID, originalFeed.Entries, updateExistingEntries)
  150. if storeErr != nil {
  151. originalFeed.WithError(storeErr.Error())
  152. store.UpdateFeedError(originalFeed)
  153. return storeErr
  154. }
  155. userIntegrations, intErr := store.Integration(userID)
  156. if intErr != nil {
  157. logger.Error("[RefreshFeed] Fetching integrations for user %d failed: %v; the refresh process will go on, but no integrations will run this time.", userID, intErr)
  158. } else if userIntegrations != nil && len(newEntries) > 0 {
  159. go integration.PushEntries(originalFeed, newEntries, userIntegrations)
  160. }
  161. // We update caching headers only if the feed has been modified,
  162. // because some websites don't return the same headers when replying with a 304.
  163. originalFeed.WithClientResponse(response)
  164. checkFeedIcon(
  165. store,
  166. originalFeed.ID,
  167. originalFeed.SiteURL,
  168. updatedFeed.IconURL,
  169. originalFeed.UserAgent,
  170. originalFeed.FetchViaProxy,
  171. originalFeed.AllowSelfSignedCertificates,
  172. )
  173. } else {
  174. logger.Debug("[RefreshFeed] Feed #%d not modified", feedID)
  175. }
  176. originalFeed.ResetErrorCounter()
  177. if storeErr := store.UpdateFeed(originalFeed); storeErr != nil {
  178. originalFeed.WithError(storeErr.Error())
  179. store.UpdateFeedError(originalFeed)
  180. return storeErr
  181. }
  182. return nil
  183. }
  184. func checkFeedIcon(store *storage.Storage, feedID int64, websiteURL, iconURL, userAgent string, fetchViaProxy, allowSelfSignedCertificates bool) {
  185. if !store.HasIcon(feedID) {
  186. icon, err := icon.FindIcon(websiteURL, iconURL, userAgent, fetchViaProxy, allowSelfSignedCertificates)
  187. if err != nil {
  188. logger.Debug(`[CheckFeedIcon] %v (feedID=%d websiteURL=%s)`, err, feedID, websiteURL)
  189. } else if icon == nil {
  190. logger.Debug(`[CheckFeedIcon] No icon found (feedID=%d websiteURL=%s)`, feedID, websiteURL)
  191. } else {
  192. if err := store.CreateFeedIcon(feedID, icon); err != nil {
  193. logger.Debug(`[CheckFeedIcon] %v (feedID=%d websiteURL=%s)`, err, feedID, websiteURL)
  194. }
  195. }
  196. }
  197. }