feed.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. "time"
  8. "miniflux.app/v2/internal/config"
  9. )
  10. // List of supported schedulers.
  11. const (
  12. SchedulerRoundRobin = "round_robin"
  13. SchedulerEntryFrequency = "entry_frequency"
  14. // Default settings for the feed query builder
  15. DefaultFeedSorting = "parsing_error_count"
  16. DefaultFeedSortingDirection = "desc"
  17. )
  18. // Feed represents a feed in the application.
  19. type Feed struct {
  20. ID int64 `json:"id"`
  21. UserID int64 `json:"user_id"`
  22. FeedURL string `json:"feed_url"`
  23. SiteURL string `json:"site_url"`
  24. Title string `json:"title"`
  25. Description string `json:"description"`
  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. BlocklistRules string `json:"blocklist_rules"`
  35. KeeplistRules string `json:"keeplist_rules"`
  36. BlockFilterEntryRules string `json:"block_filter_entry_rules"`
  37. KeepFilterEntryRules string `json:"keep_filter_entry_rules"`
  38. UrlRewriteRules string `json:"urlrewrite_rules"`
  39. UserAgent string `json:"user_agent"`
  40. Cookie string `json:"cookie"`
  41. Username string `json:"username"`
  42. Password string `json:"password"`
  43. Disabled bool `json:"disabled"`
  44. NoMediaPlayer bool `json:"no_media_player"`
  45. IgnoreHTTPCache bool `json:"ignore_http_cache"`
  46. AllowSelfSignedCertificates bool `json:"allow_self_signed_certificates"`
  47. FetchViaProxy bool `json:"fetch_via_proxy"`
  48. HideGlobally bool `json:"hide_globally"`
  49. DisableHTTP2 bool `json:"disable_http2"`
  50. PushoverEnabled bool `json:"pushover_enabled"`
  51. NtfyEnabled bool `json:"ntfy_enabled"`
  52. Crawler bool `json:"crawler"`
  53. AppriseServiceURLs string `json:"apprise_service_urls"`
  54. WebhookURL string `json:"webhook_url"`
  55. NtfyPriority int `json:"ntfy_priority"`
  56. NtfyTopic string `json:"ntfy_topic"`
  57. PushoverPriority int `json:"pushover_priority"`
  58. ProxyURL string `json:"proxy_url"`
  59. // Non-persisted attributes
  60. Category *Category `json:"category,omitempty"`
  61. Icon *FeedIcon `json:"icon"`
  62. Entries Entries `json:"entries,omitempty"`
  63. // Internal attributes (not exposed in the API and not persisted in the database)
  64. TTL time.Duration `json:"-"`
  65. IconURL string `json:"-"`
  66. UnreadCount int `json:"-"`
  67. ReadCount int `json:"-"`
  68. NumberOfVisibleEntries int `json:"-"`
  69. }
  70. type FeedCounters struct {
  71. ReadCounters map[int64]int `json:"reads"`
  72. UnreadCounters map[int64]int `json:"unreads"`
  73. }
  74. func (f *Feed) String() string {
  75. return fmt.Sprintf("ID=%d, UserID=%d, FeedURL=%s, SiteURL=%s, Title=%s, Category={%s}",
  76. f.ID,
  77. f.UserID,
  78. f.FeedURL,
  79. f.SiteURL,
  80. f.Title,
  81. f.Category,
  82. )
  83. }
  84. // WithCategoryID initializes the category attribute of the feed.
  85. func (f *Feed) WithCategoryID(categoryID int64) {
  86. f.Category = &Category{ID: categoryID}
  87. }
  88. // WithTranslatedErrorMessage adds a new error message and increment the error counter.
  89. func (f *Feed) WithTranslatedErrorMessage(message string) {
  90. f.ParsingErrorCount++
  91. f.ParsingErrorMsg = message
  92. }
  93. // ResetErrorCounter removes all previous errors.
  94. func (f *Feed) ResetErrorCounter() {
  95. f.ParsingErrorCount = 0
  96. f.ParsingErrorMsg = ""
  97. }
  98. // CheckedNow set attribute values when the feed is refreshed.
  99. func (f *Feed) CheckedNow() {
  100. f.CheckedAt = time.Now()
  101. if f.SiteURL == "" {
  102. f.SiteURL = f.FeedURL
  103. }
  104. }
  105. // ScheduleNextCheck set "next_check_at" of a feed based on the scheduler selected from the configuration.
  106. func (f *Feed) ScheduleNextCheck(weeklyCount int, refreshDelay time.Duration) time.Duration {
  107. // Default to the global config Polling Frequency.
  108. interval := config.Opts.SchedulerRoundRobinMinInterval()
  109. if config.Opts.PollingScheduler() == SchedulerEntryFrequency {
  110. if weeklyCount <= 0 {
  111. interval = config.Opts.SchedulerEntryFrequencyMaxInterval()
  112. } else {
  113. interval = (7 * 24 * time.Hour) / time.Duration(weeklyCount*config.Opts.SchedulerEntryFrequencyFactor())
  114. interval = min(interval, config.Opts.SchedulerEntryFrequencyMaxInterval())
  115. interval = max(interval, config.Opts.SchedulerEntryFrequencyMinInterval())
  116. }
  117. }
  118. // Use the RSS TTL field, Retry-After, Cache-Control or Expires HTTP headers if defined.
  119. interval = max(interval, refreshDelay)
  120. // Limit the max interval value for misconfigured feeds.
  121. switch config.Opts.PollingScheduler() {
  122. case SchedulerRoundRobin:
  123. interval = min(interval, config.Opts.SchedulerRoundRobinMaxInterval())
  124. case SchedulerEntryFrequency:
  125. interval = min(interval, config.Opts.SchedulerEntryFrequencyMaxInterval())
  126. }
  127. f.NextCheckAt = time.Now().Add(interval)
  128. return interval
  129. }
  130. // FeedCreationRequest represents the request to create a feed.
  131. type FeedCreationRequest struct {
  132. FeedURL string `json:"feed_url"`
  133. CategoryID int64 `json:"category_id"`
  134. UserAgent string `json:"user_agent"`
  135. Cookie string `json:"cookie"`
  136. Username string `json:"username"`
  137. Password string `json:"password"`
  138. Crawler bool `json:"crawler"`
  139. Disabled bool `json:"disabled"`
  140. NoMediaPlayer bool `json:"no_media_player"`
  141. IgnoreHTTPCache bool `json:"ignore_http_cache"`
  142. AllowSelfSignedCertificates bool `json:"allow_self_signed_certificates"`
  143. FetchViaProxy bool `json:"fetch_via_proxy"`
  144. HideGlobally bool `json:"hide_globally"`
  145. DisableHTTP2 bool `json:"disable_http2"`
  146. ScraperRules string `json:"scraper_rules"`
  147. RewriteRules string `json:"rewrite_rules"`
  148. BlocklistRules string `json:"blocklist_rules"`
  149. KeeplistRules string `json:"keeplist_rules"`
  150. BlockFilterEntryRules string `json:"block_filter_entry_rules"`
  151. KeepFilterEntryRules string `json:"keep_filter_entry_rules"`
  152. UrlRewriteRules string `json:"urlrewrite_rules"`
  153. ProxyURL string `json:"proxy_url"`
  154. }
  155. type FeedCreationRequestFromSubscriptionDiscovery struct {
  156. Content io.ReadSeeker
  157. ETag string
  158. LastModified string
  159. FeedCreationRequest
  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. Description *string `json:"description"`
  167. ScraperRules *string `json:"scraper_rules"`
  168. RewriteRules *string `json:"rewrite_rules"`
  169. BlocklistRules *string `json:"blocklist_rules"`
  170. UrlRewriteRules *string `json:"urlrewrite_rules"`
  171. KeeplistRules *string `json:"keeplist_rules"`
  172. BlockFilterEntryRules *string `json:"block_filter_entry_rules"`
  173. KeepFilterEntryRules *string `json:"keep_filter_entry_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. ProxyURL *string `json:"proxy_url"`
  188. }
  189. // Patch updates a feed with modified values.
  190. func (f *FeedModificationRequest) Patch(feed *Feed) {
  191. if f.FeedURL != nil && *f.FeedURL != "" {
  192. feed.FeedURL = *f.FeedURL
  193. }
  194. if f.SiteURL != nil && *f.SiteURL != "" {
  195. feed.SiteURL = *f.SiteURL
  196. }
  197. if f.Title != nil && *f.Title != "" {
  198. feed.Title = *f.Title
  199. }
  200. if f.Description != nil && *f.Description != "" {
  201. feed.Description = *f.Description
  202. }
  203. if f.ScraperRules != nil {
  204. feed.ScraperRules = *f.ScraperRules
  205. }
  206. if f.RewriteRules != nil {
  207. feed.RewriteRules = *f.RewriteRules
  208. }
  209. if f.UrlRewriteRules != nil {
  210. feed.UrlRewriteRules = *f.UrlRewriteRules
  211. }
  212. if f.KeeplistRules != nil {
  213. feed.KeeplistRules = *f.KeeplistRules
  214. }
  215. if f.BlocklistRules != nil {
  216. feed.BlocklistRules = *f.BlocklistRules
  217. }
  218. if f.BlockFilterEntryRules != nil {
  219. feed.BlockFilterEntryRules = *f.BlockFilterEntryRules
  220. }
  221. if f.KeepFilterEntryRules != nil {
  222. feed.KeepFilterEntryRules = *f.KeepFilterEntryRules
  223. }
  224. if f.Crawler != nil {
  225. feed.Crawler = *f.Crawler
  226. }
  227. if f.UserAgent != nil {
  228. feed.UserAgent = *f.UserAgent
  229. }
  230. if f.Cookie != nil {
  231. feed.Cookie = *f.Cookie
  232. }
  233. if f.Username != nil {
  234. feed.Username = *f.Username
  235. }
  236. if f.Password != nil {
  237. feed.Password = *f.Password
  238. }
  239. if f.CategoryID != nil && *f.CategoryID > 0 {
  240. feed.Category.ID = *f.CategoryID
  241. }
  242. if f.Disabled != nil {
  243. feed.Disabled = *f.Disabled
  244. }
  245. if f.NoMediaPlayer != nil {
  246. feed.NoMediaPlayer = *f.NoMediaPlayer
  247. }
  248. if f.IgnoreHTTPCache != nil {
  249. feed.IgnoreHTTPCache = *f.IgnoreHTTPCache
  250. }
  251. if f.AllowSelfSignedCertificates != nil {
  252. feed.AllowSelfSignedCertificates = *f.AllowSelfSignedCertificates
  253. }
  254. if f.FetchViaProxy != nil {
  255. feed.FetchViaProxy = *f.FetchViaProxy
  256. }
  257. if f.HideGlobally != nil {
  258. feed.HideGlobally = *f.HideGlobally
  259. }
  260. if f.DisableHTTP2 != nil {
  261. feed.DisableHTTP2 = *f.DisableHTTP2
  262. }
  263. if f.ProxyURL != nil {
  264. feed.ProxyURL = *f.ProxyURL
  265. }
  266. }
  267. // Feeds is a list of feed
  268. type Feeds []*Feed