feed.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. DisableHTTP2 bool `json:"disable_http2"`
  50. // Non persisted attributes
  51. Category *Category `json:"category,omitempty"`
  52. Icon *FeedIcon `json:"icon"`
  53. Entries Entries `json:"entries,omitempty"`
  54. TTL int `json:"-"`
  55. IconURL string `json:"-"`
  56. UnreadCount int `json:"-"`
  57. ReadCount int `json:"-"`
  58. NumberOfVisibleEntries int `json:"-"`
  59. }
  60. type FeedCounters struct {
  61. ReadCounters map[int64]int `json:"reads"`
  62. UnreadCounters map[int64]int `json:"unreads"`
  63. }
  64. func (f *Feed) String() string {
  65. return fmt.Sprintf("ID=%d, UserID=%d, FeedURL=%s, SiteURL=%s, Title=%s, Category={%s}",
  66. f.ID,
  67. f.UserID,
  68. f.FeedURL,
  69. f.SiteURL,
  70. f.Title,
  71. f.Category,
  72. )
  73. }
  74. // WithCategoryID initializes the category attribute of the feed.
  75. func (f *Feed) WithCategoryID(categoryID int64) {
  76. f.Category = &Category{ID: categoryID}
  77. }
  78. // WithTranslatedErrorMessage adds a new error message and increment the error counter.
  79. func (f *Feed) WithTranslatedErrorMessage(message string) {
  80. f.ParsingErrorCount++
  81. f.ParsingErrorMsg = message
  82. }
  83. // ResetErrorCounter removes all previous errors.
  84. func (f *Feed) ResetErrorCounter() {
  85. f.ParsingErrorCount = 0
  86. f.ParsingErrorMsg = ""
  87. }
  88. // CheckedNow set attribute values when the feed is refreshed.
  89. func (f *Feed) CheckedNow() {
  90. f.CheckedAt = time.Now()
  91. if f.SiteURL == "" {
  92. f.SiteURL = f.FeedURL
  93. }
  94. }
  95. // ScheduleNextCheck set "next_check_at" of a feed based on the scheduler selected from the configuration.
  96. func (f *Feed) ScheduleNextCheck(weeklyCount int, newTTL int) {
  97. f.TTL = newTTL
  98. // Default to the global config Polling Frequency.
  99. var intervalMinutes int
  100. switch config.Opts.PollingScheduler() {
  101. case SchedulerEntryFrequency:
  102. if weeklyCount <= 0 {
  103. intervalMinutes = config.Opts.SchedulerEntryFrequencyMaxInterval()
  104. } else {
  105. intervalMinutes = int(math.Round(float64(7*24*60) / float64(weeklyCount*config.Opts.SchedulerEntryFrequencyFactor())))
  106. intervalMinutes = int(math.Min(float64(intervalMinutes), float64(config.Opts.SchedulerEntryFrequencyMaxInterval())))
  107. intervalMinutes = int(math.Max(float64(intervalMinutes), float64(config.Opts.SchedulerEntryFrequencyMinInterval())))
  108. }
  109. default:
  110. intervalMinutes = config.Opts.SchedulerRoundRobinMinInterval()
  111. }
  112. // If the feed has a TTL defined, we use it to make sure we don't check it too often.
  113. if newTTL > intervalMinutes && newTTL > 0 {
  114. intervalMinutes = newTTL
  115. }
  116. f.NextCheckAt = time.Now().Add(time.Minute * time.Duration(intervalMinutes))
  117. }
  118. // FeedCreationRequest represents the request to create a feed.
  119. type FeedCreationRequest struct {
  120. FeedURL string `json:"feed_url"`
  121. CategoryID int64 `json:"category_id"`
  122. UserAgent string `json:"user_agent"`
  123. Cookie string `json:"cookie"`
  124. Username string `json:"username"`
  125. Password string `json:"password"`
  126. Crawler bool `json:"crawler"`
  127. Disabled bool `json:"disabled"`
  128. NoMediaPlayer bool `json:"no_media_player"`
  129. IgnoreHTTPCache bool `json:"ignore_http_cache"`
  130. AllowSelfSignedCertificates bool `json:"allow_self_signed_certificates"`
  131. FetchViaProxy bool `json:"fetch_via_proxy"`
  132. ScraperRules string `json:"scraper_rules"`
  133. RewriteRules string `json:"rewrite_rules"`
  134. BlocklistRules string `json:"blocklist_rules"`
  135. KeeplistRules string `json:"keeplist_rules"`
  136. HideGlobally bool `json:"hide_globally"`
  137. UrlRewriteRules string `json:"urlrewrite_rules"`
  138. DisableHTTP2 bool `json:"disable_http2"`
  139. }
  140. type FeedCreationRequestFromSubscriptionDiscovery struct {
  141. Content io.ReadSeeker
  142. ETag string
  143. LastModified string
  144. FeedURL string `json:"feed_url"`
  145. CategoryID int64 `json:"category_id"`
  146. UserAgent string `json:"user_agent"`
  147. Cookie string `json:"cookie"`
  148. Username string `json:"username"`
  149. Password string `json:"password"`
  150. Crawler bool `json:"crawler"`
  151. Disabled bool `json:"disabled"`
  152. NoMediaPlayer bool `json:"no_media_player"`
  153. IgnoreHTTPCache bool `json:"ignore_http_cache"`
  154. AllowSelfSignedCertificates bool `json:"allow_self_signed_certificates"`
  155. FetchViaProxy bool `json:"fetch_via_proxy"`
  156. ScraperRules string `json:"scraper_rules"`
  157. RewriteRules string `json:"rewrite_rules"`
  158. BlocklistRules string `json:"blocklist_rules"`
  159. KeeplistRules string `json:"keeplist_rules"`
  160. HideGlobally bool `json:"hide_globally"`
  161. UrlRewriteRules string `json:"urlrewrite_rules"`
  162. DisableHTTP2 bool `json:"disable_http2"`
  163. }
  164. // FeedModificationRequest represents the request to update a feed.
  165. type FeedModificationRequest struct {
  166. FeedURL *string `json:"feed_url"`
  167. SiteURL *string `json:"site_url"`
  168. Title *string `json:"title"`
  169. ScraperRules *string `json:"scraper_rules"`
  170. RewriteRules *string `json:"rewrite_rules"`
  171. BlocklistRules *string `json:"blocklist_rules"`
  172. KeeplistRules *string `json:"keeplist_rules"`
  173. UrlRewriteRules *string `json:"urlrewrite_rules"`
  174. Crawler *bool `json:"crawler"`
  175. UserAgent *string `json:"user_agent"`
  176. Cookie *string `json:"cookie"`
  177. Username *string `json:"username"`
  178. Password *string `json:"password"`
  179. CategoryID *int64 `json:"category_id"`
  180. Disabled *bool `json:"disabled"`
  181. NoMediaPlayer *bool `json:"no_media_player"`
  182. IgnoreHTTPCache *bool `json:"ignore_http_cache"`
  183. AllowSelfSignedCertificates *bool `json:"allow_self_signed_certificates"`
  184. FetchViaProxy *bool `json:"fetch_via_proxy"`
  185. HideGlobally *bool `json:"hide_globally"`
  186. DisableHTTP2 *bool `json:"disable_http2"`
  187. }
  188. // Patch updates a feed with modified values.
  189. func (f *FeedModificationRequest) Patch(feed *Feed) {
  190. if f.FeedURL != nil && *f.FeedURL != "" {
  191. feed.FeedURL = *f.FeedURL
  192. }
  193. if f.SiteURL != nil && *f.SiteURL != "" {
  194. feed.SiteURL = *f.SiteURL
  195. }
  196. if f.Title != nil && *f.Title != "" {
  197. feed.Title = *f.Title
  198. }
  199. if f.ScraperRules != nil {
  200. feed.ScraperRules = *f.ScraperRules
  201. }
  202. if f.RewriteRules != nil {
  203. feed.RewriteRules = *f.RewriteRules
  204. }
  205. if f.KeeplistRules != nil {
  206. feed.KeeplistRules = *f.KeeplistRules
  207. }
  208. if f.UrlRewriteRules != nil {
  209. feed.UrlRewriteRules = *f.UrlRewriteRules
  210. }
  211. if f.BlocklistRules != nil {
  212. feed.BlocklistRules = *f.BlocklistRules
  213. }
  214. if f.Crawler != nil {
  215. feed.Crawler = *f.Crawler
  216. }
  217. if f.UserAgent != nil {
  218. feed.UserAgent = *f.UserAgent
  219. }
  220. if f.Cookie != nil {
  221. feed.Cookie = *f.Cookie
  222. }
  223. if f.Username != nil {
  224. feed.Username = *f.Username
  225. }
  226. if f.Password != nil {
  227. feed.Password = *f.Password
  228. }
  229. if f.CategoryID != nil && *f.CategoryID > 0 {
  230. feed.Category.ID = *f.CategoryID
  231. }
  232. if f.Disabled != nil {
  233. feed.Disabled = *f.Disabled
  234. }
  235. if f.NoMediaPlayer != nil {
  236. feed.NoMediaPlayer = *f.NoMediaPlayer
  237. }
  238. if f.IgnoreHTTPCache != nil {
  239. feed.IgnoreHTTPCache = *f.IgnoreHTTPCache
  240. }
  241. if f.AllowSelfSignedCertificates != nil {
  242. feed.AllowSelfSignedCertificates = *f.AllowSelfSignedCertificates
  243. }
  244. if f.FetchViaProxy != nil {
  245. feed.FetchViaProxy = *f.FetchViaProxy
  246. }
  247. if f.HideGlobally != nil {
  248. feed.HideGlobally = *f.HideGlobally
  249. }
  250. if f.DisableHTTP2 != nil {
  251. feed.DisableHTTP2 = *f.DisableHTTP2
  252. }
  253. }
  254. // Feeds is a list of feed
  255. type Feeds []*Feed