feed.go 10 KB

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