feed.go 9.5 KB

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