feed.go 11 KB

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