handler.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package handler // import "miniflux.app/reader/handler"
  4. import (
  5. "fmt"
  6. "time"
  7. "miniflux.app/config"
  8. "miniflux.app/errors"
  9. "miniflux.app/http/client"
  10. "miniflux.app/locale"
  11. "miniflux.app/logger"
  12. "miniflux.app/model"
  13. "miniflux.app/reader/browser"
  14. "miniflux.app/reader/icon"
  15. "miniflux.app/reader/parser"
  16. "miniflux.app/reader/processor"
  17. "miniflux.app/storage"
  18. "miniflux.app/timer"
  19. )
  20. var (
  21. errDuplicate = "This feed already exists (%s)"
  22. errNotFound = "Feed %d not found"
  23. errCategoryNotFound = "Category not found for this user"
  24. )
  25. // CreateFeed fetch, parse and store a new feed.
  26. func CreateFeed(store *storage.Storage, userID int64, feedCreationRequest *model.FeedCreationRequest) (*model.Feed, error) {
  27. defer timer.ExecutionTime(time.Now(), fmt.Sprintf("[CreateFeed] FeedURL=%s", feedCreationRequest.FeedURL))
  28. user, storeErr := store.UserByID(userID)
  29. if storeErr != nil {
  30. return nil, storeErr
  31. }
  32. if !store.CategoryIDExists(userID, feedCreationRequest.CategoryID) {
  33. return nil, errors.NewLocalizedError(errCategoryNotFound)
  34. }
  35. request := client.NewClientWithConfig(feedCreationRequest.FeedURL, config.Opts)
  36. request.WithCredentials(feedCreationRequest.Username, feedCreationRequest.Password)
  37. request.WithUserAgent(feedCreationRequest.UserAgent)
  38. request.WithCookie(feedCreationRequest.Cookie)
  39. request.AllowSelfSignedCertificates = feedCreationRequest.AllowSelfSignedCertificates
  40. if feedCreationRequest.FetchViaProxy {
  41. request.WithProxy()
  42. }
  43. response, requestErr := browser.Exec(request)
  44. if requestErr != nil {
  45. return nil, requestErr
  46. }
  47. if store.FeedURLExists(userID, response.EffectiveURL) {
  48. return nil, errors.NewLocalizedError(errDuplicate, response.EffectiveURL)
  49. }
  50. subscription, parseErr := parser.ParseFeed(response.EffectiveURL, response.BodyAsString())
  51. if parseErr != nil {
  52. return nil, parseErr
  53. }
  54. subscription.UserID = userID
  55. subscription.UserAgent = feedCreationRequest.UserAgent
  56. subscription.Cookie = feedCreationRequest.Cookie
  57. subscription.Username = feedCreationRequest.Username
  58. subscription.Password = feedCreationRequest.Password
  59. subscription.Crawler = feedCreationRequest.Crawler
  60. subscription.Disabled = feedCreationRequest.Disabled
  61. subscription.IgnoreHTTPCache = feedCreationRequest.IgnoreHTTPCache
  62. subscription.AllowSelfSignedCertificates = feedCreationRequest.AllowSelfSignedCertificates
  63. subscription.FetchViaProxy = feedCreationRequest.FetchViaProxy
  64. subscription.ScraperRules = feedCreationRequest.ScraperRules
  65. subscription.RewriteRules = feedCreationRequest.RewriteRules
  66. subscription.BlocklistRules = feedCreationRequest.BlocklistRules
  67. subscription.KeeplistRules = feedCreationRequest.KeeplistRules
  68. subscription.UrlRewriteRules = feedCreationRequest.UrlRewriteRules
  69. subscription.WithCategoryID(feedCreationRequest.CategoryID)
  70. subscription.WithClientResponse(response)
  71. subscription.CheckedNow()
  72. processor.ProcessFeedEntries(store, subscription, user, true)
  73. if storeErr := store.CreateFeed(subscription); storeErr != nil {
  74. return nil, storeErr
  75. }
  76. logger.Debug("[CreateFeed] Feed saved with ID: %d", subscription.ID)
  77. checkFeedIcon(
  78. store,
  79. subscription.ID,
  80. subscription.SiteURL,
  81. subscription.IconURL,
  82. feedCreationRequest.UserAgent,
  83. feedCreationRequest.FetchViaProxy,
  84. feedCreationRequest.AllowSelfSignedCertificates,
  85. )
  86. return subscription, nil
  87. }
  88. // RefreshFeed refreshes a feed.
  89. func RefreshFeed(store *storage.Storage, userID, feedID int64, forceRefresh bool) error {
  90. defer timer.ExecutionTime(time.Now(), fmt.Sprintf("[RefreshFeed] feedID=%d", feedID))
  91. user, storeErr := store.UserByID(userID)
  92. if storeErr != nil {
  93. return storeErr
  94. }
  95. printer := locale.NewPrinter(user.Language)
  96. originalFeed, storeErr := store.FeedByID(userID, feedID)
  97. if storeErr != nil {
  98. return storeErr
  99. }
  100. if originalFeed == nil {
  101. return errors.NewLocalizedError(errNotFound, feedID)
  102. }
  103. weeklyEntryCount := 0
  104. if config.Opts.PollingScheduler() == model.SchedulerEntryFrequency {
  105. var weeklyCountErr error
  106. weeklyEntryCount, weeklyCountErr = store.WeeklyFeedEntryCount(userID, feedID)
  107. if weeklyCountErr != nil {
  108. return weeklyCountErr
  109. }
  110. }
  111. originalFeed.CheckedNow()
  112. originalFeed.ScheduleNextCheck(weeklyEntryCount)
  113. request := client.NewClientWithConfig(originalFeed.FeedURL, config.Opts)
  114. request.WithCredentials(originalFeed.Username, originalFeed.Password)
  115. request.WithUserAgent(originalFeed.UserAgent)
  116. request.WithCookie(originalFeed.Cookie)
  117. request.AllowSelfSignedCertificates = originalFeed.AllowSelfSignedCertificates
  118. if !originalFeed.IgnoreHTTPCache {
  119. request.WithCacheHeaders(originalFeed.EtagHeader, originalFeed.LastModifiedHeader)
  120. }
  121. if originalFeed.FetchViaProxy {
  122. request.WithProxy()
  123. }
  124. response, requestErr := browser.Exec(request)
  125. if requestErr != nil {
  126. originalFeed.WithError(requestErr.Localize(printer))
  127. store.UpdateFeedError(originalFeed)
  128. return requestErr
  129. }
  130. if store.AnotherFeedURLExists(userID, originalFeed.ID, response.EffectiveURL) {
  131. storeErr := errors.NewLocalizedError(errDuplicate, response.EffectiveURL)
  132. originalFeed.WithError(storeErr.Error())
  133. store.UpdateFeedError(originalFeed)
  134. return storeErr
  135. }
  136. if originalFeed.IgnoreHTTPCache || response.IsModified(originalFeed.EtagHeader, originalFeed.LastModifiedHeader) {
  137. logger.Debug("[RefreshFeed] Feed #%d has been modified", feedID)
  138. updatedFeed, parseErr := parser.ParseFeed(response.EffectiveURL, response.BodyAsString())
  139. if parseErr != nil {
  140. originalFeed.WithError(parseErr.Localize(printer))
  141. store.UpdateFeedError(originalFeed)
  142. return parseErr
  143. }
  144. originalFeed.Entries = updatedFeed.Entries
  145. processor.ProcessFeedEntries(store, originalFeed, user, forceRefresh)
  146. // We don't update existing entries when the crawler is enabled (we crawl only inexisting entries). Unless it is forced to refresh
  147. updateExistingEntries := forceRefresh || !originalFeed.Crawler
  148. if storeErr := store.RefreshFeedEntries(originalFeed.UserID, originalFeed.ID, originalFeed.Entries, updateExistingEntries); storeErr != nil {
  149. originalFeed.WithError(storeErr.Error())
  150. store.UpdateFeedError(originalFeed)
  151. return storeErr
  152. }
  153. // We update caching headers only if the feed has been modified,
  154. // because some websites don't return the same headers when replying with a 304.
  155. originalFeed.WithClientResponse(response)
  156. checkFeedIcon(
  157. store,
  158. originalFeed.ID,
  159. originalFeed.SiteURL,
  160. updatedFeed.IconURL,
  161. originalFeed.UserAgent,
  162. originalFeed.FetchViaProxy,
  163. originalFeed.AllowSelfSignedCertificates,
  164. )
  165. } else {
  166. logger.Debug("[RefreshFeed] Feed #%d not modified", feedID)
  167. }
  168. originalFeed.ResetErrorCounter()
  169. if storeErr := store.UpdateFeed(originalFeed); storeErr != nil {
  170. originalFeed.WithError(storeErr.Error())
  171. store.UpdateFeedError(originalFeed)
  172. return storeErr
  173. }
  174. return nil
  175. }
  176. func checkFeedIcon(store *storage.Storage, feedID int64, websiteURL, iconURL, userAgent string, fetchViaProxy, allowSelfSignedCertificates bool) {
  177. if !store.HasIcon(feedID) {
  178. icon, err := icon.FindIcon(websiteURL, iconURL, userAgent, fetchViaProxy, allowSelfSignedCertificates)
  179. if err != nil {
  180. logger.Debug(`[CheckFeedIcon] %v (feedID=%d websiteURL=%s)`, err, feedID, websiteURL)
  181. } else if icon == nil {
  182. logger.Debug(`[CheckFeedIcon] No icon found (feedID=%d websiteURL=%s)`, feedID, websiteURL)
  183. } else {
  184. if err := store.CreateFeedIcon(feedID, icon); err != nil {
  185. logger.Debug(`[CheckFeedIcon] %v (feedID=%d websiteURL=%s)`, err, feedID, websiteURL)
  186. }
  187. }
  188. }
  189. }