feed.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. Description string `json:"description"`
  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. AppriseServiceURLs string `json:"apprise_service_urls"`
  55. WebhookURL string `json:"webhook_url"`
  56. NtfyPriority int `json:"ntfy_priority"`
  57. NtfyTopic string `json:"ntfy_topic"`
  58. PushoverPriority int `json:"pushover_priority"`
  59. ProxyURL string `json:"proxy_url"`
  60. // Non-persisted attributes
  61. Category *Category `json:"category,omitempty"`
  62. Icon *FeedIcon `json:"icon"`
  63. Entries Entries `json:"entries,omitempty"`
  64. // Internal attributes (not exposed in the API and not persisted in the database)
  65. TTL int `json:"-"`
  66. IconURL string `json:"-"`
  67. UnreadCount int `json:"-"`
  68. ReadCount int `json:"-"`
  69. NumberOfVisibleEntries int `json:"-"`
  70. }
  71. type FeedCounters struct {
  72. ReadCounters map[int64]int `json:"reads"`
  73. UnreadCounters map[int64]int `json:"unreads"`
  74. }
  75. func (f *Feed) String() string {
  76. return fmt.Sprintf("ID=%d, UserID=%d, FeedURL=%s, SiteURL=%s, Title=%s, Category={%s}",
  77. f.ID,
  78. f.UserID,
  79. f.FeedURL,
  80. f.SiteURL,
  81. f.Title,
  82. f.Category,
  83. )
  84. }
  85. // WithCategoryID initializes the category attribute of the feed.
  86. func (f *Feed) WithCategoryID(categoryID int64) {
  87. f.Category = &Category{ID: categoryID}
  88. }
  89. // WithTranslatedErrorMessage adds a new error message and increment the error counter.
  90. func (f *Feed) WithTranslatedErrorMessage(message string) {
  91. f.ParsingErrorCount++
  92. f.ParsingErrorMsg = message
  93. }
  94. // ResetErrorCounter removes all previous errors.
  95. func (f *Feed) ResetErrorCounter() {
  96. f.ParsingErrorCount = 0
  97. f.ParsingErrorMsg = ""
  98. }
  99. // CheckedNow set attribute values when the feed is refreshed.
  100. func (f *Feed) CheckedNow() {
  101. f.CheckedAt = time.Now()
  102. if f.SiteURL == "" {
  103. f.SiteURL = f.FeedURL
  104. }
  105. }
  106. // ScheduleNextCheck set "next_check_at" of a feed based on the scheduler selected from the configuration.
  107. func (f *Feed) ScheduleNextCheck(weeklyCount int, refreshDelayInMinutes int) int {
  108. // Default to the global config Polling Frequency.
  109. intervalMinutes := config.Opts.SchedulerRoundRobinMinInterval()
  110. if config.Opts.PollingScheduler() == SchedulerEntryFrequency {
  111. if weeklyCount <= 0 {
  112. intervalMinutes = config.Opts.SchedulerEntryFrequencyMaxInterval()
  113. } else {
  114. intervalMinutes = int(math.Round(float64(7*24*60) / float64(weeklyCount*config.Opts.SchedulerEntryFrequencyFactor())))
  115. intervalMinutes = min(intervalMinutes, config.Opts.SchedulerEntryFrequencyMaxInterval())
  116. intervalMinutes = max(intervalMinutes, config.Opts.SchedulerEntryFrequencyMinInterval())
  117. }
  118. }
  119. // Use the RSS TTL field, Retry-After, Cache-Control or Expires HTTP headers if defined.
  120. if refreshDelayInMinutes > 0 && refreshDelayInMinutes > intervalMinutes {
  121. intervalMinutes = refreshDelayInMinutes
  122. }
  123. // Limit the max interval value for misconfigured feeds.
  124. switch config.Opts.PollingScheduler() {
  125. case SchedulerRoundRobin:
  126. intervalMinutes = min(intervalMinutes, config.Opts.SchedulerRoundRobinMaxInterval())
  127. case SchedulerEntryFrequency:
  128. intervalMinutes = min(intervalMinutes, config.Opts.SchedulerEntryFrequencyMaxInterval())
  129. }
  130. f.NextCheckAt = time.Now().Add(time.Minute * time.Duration(intervalMinutes))
  131. return intervalMinutes
  132. }
  133. // FeedCreationRequest represents the request to create a feed.
  134. type FeedCreationRequest struct {
  135. FeedURL string `json:"feed_url"`
  136. CategoryID int64 `json:"category_id"`
  137. UserAgent string `json:"user_agent"`
  138. Cookie string `json:"cookie"`
  139. Username string `json:"username"`
  140. Password string `json:"password"`
  141. Crawler bool `json:"crawler"`
  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. UserAgent *string `json:"user_agent"`
  179. Cookie *string `json:"cookie"`
  180. Username *string `json:"username"`
  181. Password *string `json:"password"`
  182. CategoryID *int64 `json:"category_id"`
  183. Disabled *bool `json:"disabled"`
  184. NoMediaPlayer *bool `json:"no_media_player"`
  185. IgnoreHTTPCache *bool `json:"ignore_http_cache"`
  186. AllowSelfSignedCertificates *bool `json:"allow_self_signed_certificates"`
  187. FetchViaProxy *bool `json:"fetch_via_proxy"`
  188. HideGlobally *bool `json:"hide_globally"`
  189. DisableHTTP2 *bool `json:"disable_http2"`
  190. ProxyURL *string `json:"proxy_url"`
  191. }
  192. // Patch updates a feed with modified values.
  193. func (f *FeedModificationRequest) Patch(feed *Feed) {
  194. if f.FeedURL != nil && *f.FeedURL != "" {
  195. feed.FeedURL = *f.FeedURL
  196. }
  197. if f.SiteURL != nil && *f.SiteURL != "" {
  198. feed.SiteURL = *f.SiteURL
  199. }
  200. if f.Title != nil && *f.Title != "" {
  201. feed.Title = *f.Title
  202. }
  203. if f.Description != nil && *f.Description != "" {
  204. feed.Description = *f.Description
  205. }
  206. if f.ScraperRules != nil {
  207. feed.ScraperRules = *f.ScraperRules
  208. }
  209. if f.RewriteRules != nil {
  210. feed.RewriteRules = *f.RewriteRules
  211. }
  212. if f.UrlRewriteRules != nil {
  213. feed.UrlRewriteRules = *f.UrlRewriteRules
  214. }
  215. if f.KeeplistRules != nil {
  216. feed.KeeplistRules = *f.KeeplistRules
  217. }
  218. if f.BlocklistRules != nil {
  219. feed.BlocklistRules = *f.BlocklistRules
  220. }
  221. if f.BlockFilterEntryRules != nil {
  222. feed.BlockFilterEntryRules = *f.BlockFilterEntryRules
  223. }
  224. if f.KeepFilterEntryRules != nil {
  225. feed.KeepFilterEntryRules = *f.KeepFilterEntryRules
  226. }
  227. if f.Crawler != nil {
  228. feed.Crawler = *f.Crawler
  229. }
  230. if f.UserAgent != nil {
  231. feed.UserAgent = *f.UserAgent
  232. }
  233. if f.Cookie != nil {
  234. feed.Cookie = *f.Cookie
  235. }
  236. if f.Username != nil {
  237. feed.Username = *f.Username
  238. }
  239. if f.Password != nil {
  240. feed.Password = *f.Password
  241. }
  242. if f.CategoryID != nil && *f.CategoryID > 0 {
  243. feed.Category.ID = *f.CategoryID
  244. }
  245. if f.Disabled != nil {
  246. feed.Disabled = *f.Disabled
  247. }
  248. if f.NoMediaPlayer != nil {
  249. feed.NoMediaPlayer = *f.NoMediaPlayer
  250. }
  251. if f.IgnoreHTTPCache != nil {
  252. feed.IgnoreHTTPCache = *f.IgnoreHTTPCache
  253. }
  254. if f.AllowSelfSignedCertificates != nil {
  255. feed.AllowSelfSignedCertificates = *f.AllowSelfSignedCertificates
  256. }
  257. if f.FetchViaProxy != nil {
  258. feed.FetchViaProxy = *f.FetchViaProxy
  259. }
  260. if f.HideGlobally != nil {
  261. feed.HideGlobally = *f.HideGlobally
  262. }
  263. if f.DisableHTTP2 != nil {
  264. feed.DisableHTTP2 = *f.DisableHTTP2
  265. }
  266. if f.ProxyURL != nil {
  267. feed.ProxyURL = *f.ProxyURL
  268. }
  269. }
  270. // Feeds is a list of feed
  271. type Feeds []*Feed