feed.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package model // import "miniflux.app/v2/internal/model"
  4. import (
  5. "fmt"
  6. "io"
  7. "math"
  8. "time"
  9. "miniflux.app/v2/internal/config"
  10. )
  11. // List of supported schedulers.
  12. const (
  13. SchedulerRoundRobin = "round_robin"
  14. SchedulerEntryFrequency = "entry_frequency"
  15. // Default settings for the feed query builder
  16. DefaultFeedSorting = "parsing_error_count"
  17. DefaultFeedSortingDirection = "desc"
  18. )
  19. // Feed represents a feed in the application.
  20. type Feed struct {
  21. ID int64 `json:"id"`
  22. UserID int64 `json:"user_id"`
  23. FeedURL string `json:"feed_url"`
  24. SiteURL string `json:"site_url"`
  25. Title string `json:"title"`
  26. CheckedAt time.Time `json:"checked_at"`
  27. NextCheckAt time.Time `json:"next_check_at"`
  28. EtagHeader string `json:"etag_header"`
  29. LastModifiedHeader string `json:"last_modified_header"`
  30. ParsingErrorMsg string `json:"parsing_error_message"`
  31. ParsingErrorCount int `json:"parsing_error_count"`
  32. ScraperRules string `json:"scraper_rules"`
  33. RewriteRules string `json:"rewrite_rules"`
  34. Crawler bool `json:"crawler"`
  35. BlocklistRules string `json:"blocklist_rules"`
  36. KeeplistRules string `json:"keeplist_rules"`
  37. UrlRewriteRules string `json:"urlrewrite_rules"`
  38. UserAgent string `json:"user_agent"`
  39. Cookie string `json:"cookie"`
  40. Username string `json:"username"`
  41. Password string `json:"password"`
  42. Disabled bool `json:"disabled"`
  43. NoMediaPlayer bool `json:"no_media_player"`
  44. IgnoreHTTPCache bool `json:"ignore_http_cache"`
  45. AllowSelfSignedCertificates bool `json:"allow_self_signed_certificates"`
  46. FetchViaProxy bool `json:"fetch_via_proxy"`
  47. HideGlobally bool `json:"hide_globally"`
  48. AppriseServiceURLs string `json:"apprise_service_urls"`
  49. // Non persisted attributes
  50. Category *Category `json:"category,omitempty"`
  51. Icon *FeedIcon `json:"icon"`
  52. Entries Entries `json:"entries,omitempty"`
  53. TTL int `json:"-"`
  54. IconURL string `json:"-"`
  55. UnreadCount int `json:"-"`
  56. ReadCount int `json:"-"`
  57. NumberOfVisibleEntries int `json:"-"`
  58. }
  59. type FeedCounters struct {
  60. ReadCounters map[int64]int `json:"reads"`
  61. UnreadCounters map[int64]int `json:"unreads"`
  62. }
  63. func (f *Feed) String() string {
  64. return fmt.Sprintf("ID=%d, UserID=%d, FeedURL=%s, SiteURL=%s, Title=%s, Category={%s}",
  65. f.ID,
  66. f.UserID,
  67. f.FeedURL,
  68. f.SiteURL,
  69. f.Title,
  70. f.Category,
  71. )
  72. }
  73. // WithCategoryID initializes the category attribute of the feed.
  74. func (f *Feed) WithCategoryID(categoryID int64) {
  75. f.Category = &Category{ID: categoryID}
  76. }
  77. // WithTranslatedErrorMessage adds a new error message and increment the error counter.
  78. func (f *Feed) WithTranslatedErrorMessage(message string) {
  79. f.ParsingErrorCount++
  80. f.ParsingErrorMsg = message
  81. }
  82. // ResetErrorCounter removes all previous errors.
  83. func (f *Feed) ResetErrorCounter() {
  84. f.ParsingErrorCount = 0
  85. f.ParsingErrorMsg = ""
  86. }
  87. // CheckedNow set attribute values when the feed is refreshed.
  88. func (f *Feed) CheckedNow() {
  89. f.CheckedAt = time.Now()
  90. if f.SiteURL == "" {
  91. f.SiteURL = f.FeedURL
  92. }
  93. }
  94. // ScheduleNextCheck set "next_check_at" of a feed based on the scheduler selected from the configuration.
  95. func (f *Feed) ScheduleNextCheck(weeklyCount int, newTTL int) {
  96. f.TTL = newTTL
  97. // Default to the global config Polling Frequency.
  98. var intervalMinutes int
  99. switch config.Opts.PollingScheduler() {
  100. case SchedulerEntryFrequency:
  101. if weeklyCount <= 0 {
  102. intervalMinutes = config.Opts.SchedulerEntryFrequencyMaxInterval()
  103. } else {
  104. intervalMinutes = int(math.Round(float64(7*24*60) / float64(weeklyCount*config.Opts.SchedulerEntryFrequencyFactor())))
  105. intervalMinutes = int(math.Min(float64(intervalMinutes), float64(config.Opts.SchedulerEntryFrequencyMaxInterval())))
  106. intervalMinutes = int(math.Max(float64(intervalMinutes), float64(config.Opts.SchedulerEntryFrequencyMinInterval())))
  107. }
  108. default:
  109. intervalMinutes = config.Opts.SchedulerRoundRobinMinInterval()
  110. }
  111. // If the feed has a TTL defined, we use it to make sure we don't check it too often.
  112. if newTTL > intervalMinutes && newTTL > 0 {
  113. intervalMinutes = newTTL
  114. }
  115. f.NextCheckAt = time.Now().Add(time.Minute * time.Duration(intervalMinutes))
  116. }
  117. // FeedCreationRequest represents the request to create a feed.
  118. type FeedCreationRequest struct {
  119. FeedURL string `json:"feed_url"`
  120. CategoryID int64 `json:"category_id"`
  121. UserAgent string `json:"user_agent"`
  122. Cookie string `json:"cookie"`
  123. Username string `json:"username"`
  124. Password string `json:"password"`
  125. Crawler bool `json:"crawler"`
  126. Disabled bool `json:"disabled"`
  127. NoMediaPlayer bool `json:"no_media_player"`
  128. IgnoreHTTPCache bool `json:"ignore_http_cache"`
  129. AllowSelfSignedCertificates bool `json:"allow_self_signed_certificates"`
  130. FetchViaProxy bool `json:"fetch_via_proxy"`
  131. ScraperRules string `json:"scraper_rules"`
  132. RewriteRules string `json:"rewrite_rules"`
  133. BlocklistRules string `json:"blocklist_rules"`
  134. KeeplistRules string `json:"keeplist_rules"`
  135. HideGlobally bool `json:"hide_globally"`
  136. UrlRewriteRules string `json:"urlrewrite_rules"`
  137. }
  138. type FeedCreationRequestFromSubscriptionDiscovery struct {
  139. Content io.ReadSeeker
  140. ETag string
  141. LastModified string
  142. FeedURL string `json:"feed_url"`
  143. CategoryID int64 `json:"category_id"`
  144. UserAgent string `json:"user_agent"`
  145. Cookie string `json:"cookie"`
  146. Username string `json:"username"`
  147. Password string `json:"password"`
  148. Crawler bool `json:"crawler"`
  149. Disabled bool `json:"disabled"`
  150. NoMediaPlayer bool `json:"no_media_player"`
  151. IgnoreHTTPCache bool `json:"ignore_http_cache"`
  152. AllowSelfSignedCertificates bool `json:"allow_self_signed_certificates"`
  153. FetchViaProxy bool `json:"fetch_via_proxy"`
  154. ScraperRules string `json:"scraper_rules"`
  155. RewriteRules string `json:"rewrite_rules"`
  156. BlocklistRules string `json:"blocklist_rules"`
  157. KeeplistRules string `json:"keeplist_rules"`
  158. HideGlobally bool `json:"hide_globally"`
  159. UrlRewriteRules string `json:"urlrewrite_rules"`
  160. }
  161. // FeedModificationRequest represents the request to update a feed.
  162. type FeedModificationRequest struct {
  163. FeedURL *string `json:"feed_url"`
  164. SiteURL *string `json:"site_url"`
  165. Title *string `json:"title"`
  166. ScraperRules *string `json:"scraper_rules"`
  167. RewriteRules *string `json:"rewrite_rules"`
  168. BlocklistRules *string `json:"blocklist_rules"`
  169. KeeplistRules *string `json:"keeplist_rules"`
  170. UrlRewriteRules *string `json:"urlrewrite_rules"`
  171. Crawler *bool `json:"crawler"`
  172. UserAgent *string `json:"user_agent"`
  173. Cookie *string `json:"cookie"`
  174. Username *string `json:"username"`
  175. Password *string `json:"password"`
  176. CategoryID *int64 `json:"category_id"`
  177. Disabled *bool `json:"disabled"`
  178. NoMediaPlayer *bool `json:"no_media_player"`
  179. IgnoreHTTPCache *bool `json:"ignore_http_cache"`
  180. AllowSelfSignedCertificates *bool `json:"allow_self_signed_certificates"`
  181. FetchViaProxy *bool `json:"fetch_via_proxy"`
  182. HideGlobally *bool `json:"hide_globally"`
  183. }
  184. // Patch updates a feed with modified values.
  185. func (f *FeedModificationRequest) Patch(feed *Feed) {
  186. if f.FeedURL != nil && *f.FeedURL != "" {
  187. feed.FeedURL = *f.FeedURL
  188. }
  189. if f.SiteURL != nil && *f.SiteURL != "" {
  190. feed.SiteURL = *f.SiteURL
  191. }
  192. if f.Title != nil && *f.Title != "" {
  193. feed.Title = *f.Title
  194. }
  195. if f.ScraperRules != nil {
  196. feed.ScraperRules = *f.ScraperRules
  197. }
  198. if f.RewriteRules != nil {
  199. feed.RewriteRules = *f.RewriteRules
  200. }
  201. if f.KeeplistRules != nil {
  202. feed.KeeplistRules = *f.KeeplistRules
  203. }
  204. if f.UrlRewriteRules != nil {
  205. feed.UrlRewriteRules = *f.UrlRewriteRules
  206. }
  207. if f.BlocklistRules != nil {
  208. feed.BlocklistRules = *f.BlocklistRules
  209. }
  210. if f.Crawler != nil {
  211. feed.Crawler = *f.Crawler
  212. }
  213. if f.UserAgent != nil {
  214. feed.UserAgent = *f.UserAgent
  215. }
  216. if f.Cookie != nil {
  217. feed.Cookie = *f.Cookie
  218. }
  219. if f.Username != nil {
  220. feed.Username = *f.Username
  221. }
  222. if f.Password != nil {
  223. feed.Password = *f.Password
  224. }
  225. if f.CategoryID != nil && *f.CategoryID > 0 {
  226. feed.Category.ID = *f.CategoryID
  227. }
  228. if f.Disabled != nil {
  229. feed.Disabled = *f.Disabled
  230. }
  231. if f.NoMediaPlayer != nil {
  232. feed.NoMediaPlayer = *f.NoMediaPlayer
  233. }
  234. if f.IgnoreHTTPCache != nil {
  235. feed.IgnoreHTTPCache = *f.IgnoreHTTPCache
  236. }
  237. if f.AllowSelfSignedCertificates != nil {
  238. feed.AllowSelfSignedCertificates = *f.AllowSelfSignedCertificates
  239. }
  240. if f.FetchViaProxy != nil {
  241. feed.FetchViaProxy = *f.FetchViaProxy
  242. }
  243. if f.HideGlobally != nil {
  244. feed.HideGlobally = *f.HideGlobally
  245. }
  246. }
  247. // Feeds is a list of feed
  248. type Feeds []*Feed