feed.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package model // import "miniflux.app/model"
  4. import (
  5. "fmt"
  6. "math"
  7. "time"
  8. "miniflux.app/config"
  9. "miniflux.app/http/client"
  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. 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. Crawler bool `json:"crawler"`
  35. BlocklistRules string `json:"blocklist_rules"`
  36. KeeplistRules string `json:"keeplist_rules"`
  37. UrlRewriteRules string `json:"urlrewrite_rules"`
  38. UserAgent string `json:"user_agent"`
  39. Cookie string `json:"cookie"`
  40. Username string `json:"username"`
  41. Password string `json:"password"`
  42. Disabled bool `json:"disabled"`
  43. NoMediaPlayer bool `json:"no_media_player"`
  44. IgnoreHTTPCache bool `json:"ignore_http_cache"`
  45. AllowSelfSignedCertificates bool `json:"allow_self_signed_certificates"`
  46. FetchViaProxy bool `json:"fetch_via_proxy"`
  47. Category *Category `json:"category,omitempty"`
  48. Entries Entries `json:"entries,omitempty"`
  49. IconURL string `json:"icon_url"`
  50. Icon *FeedIcon `json:"icon"`
  51. HideGlobally bool `json:"hide_globally"`
  52. UnreadCount int `json:"-"`
  53. ReadCount int `json:"-"`
  54. }
  55. type FeedCounters struct {
  56. ReadCounters map[int64]int `json:"reads"`
  57. UnreadCounters map[int64]int `json:"unreads"`
  58. }
  59. func (f *Feed) String() string {
  60. return fmt.Sprintf("ID=%d, UserID=%d, FeedURL=%s, SiteURL=%s, Title=%s, Category={%s}",
  61. f.ID,
  62. f.UserID,
  63. f.FeedURL,
  64. f.SiteURL,
  65. f.Title,
  66. f.Category,
  67. )
  68. }
  69. // WithClientResponse updates feed attributes from an HTTP request.
  70. func (f *Feed) WithClientResponse(response *client.Response) {
  71. f.EtagHeader = response.ETag
  72. f.LastModifiedHeader = response.LastModified
  73. f.FeedURL = response.EffectiveURL
  74. }
  75. // WithCategoryID initializes the category attribute of the feed.
  76. func (f *Feed) WithCategoryID(categoryID int64) {
  77. f.Category = &Category{ID: categoryID}
  78. }
  79. // WithError adds a new error message and increment the error counter.
  80. func (f *Feed) WithError(message string) {
  81. f.ParsingErrorCount++
  82. f.ParsingErrorMsg = message
  83. }
  84. // ResetErrorCounter removes all previous errors.
  85. func (f *Feed) ResetErrorCounter() {
  86. f.ParsingErrorCount = 0
  87. f.ParsingErrorMsg = ""
  88. }
  89. // CheckedNow set attribute values when the feed is refreshed.
  90. func (f *Feed) CheckedNow() {
  91. f.CheckedAt = time.Now()
  92. if f.SiteURL == "" {
  93. f.SiteURL = f.FeedURL
  94. }
  95. }
  96. // ScheduleNextCheck set "next_check_at" of a feed based on the scheduler selected from the configuration.
  97. func (f *Feed) ScheduleNextCheck(weeklyCount int) {
  98. switch config.Opts.PollingScheduler() {
  99. case SchedulerEntryFrequency:
  100. var intervalMinutes int
  101. if weeklyCount == 0 {
  102. intervalMinutes = config.Opts.SchedulerEntryFrequencyMaxInterval()
  103. } else {
  104. intervalMinutes = int(math.Round(float64(7*24*60) / float64(weeklyCount)))
  105. }
  106. intervalMinutes = int(math.Min(float64(intervalMinutes), float64(config.Opts.SchedulerEntryFrequencyMaxInterval())))
  107. intervalMinutes = int(math.Max(float64(intervalMinutes), float64(config.Opts.SchedulerEntryFrequencyMinInterval())))
  108. f.NextCheckAt = time.Now().Add(time.Minute * time.Duration(intervalMinutes))
  109. default:
  110. f.NextCheckAt = time.Now()
  111. }
  112. }
  113. // FeedCreationRequest represents the request to create a feed.
  114. type FeedCreationRequest struct {
  115. FeedURL string `json:"feed_url"`
  116. CategoryID int64 `json:"category_id"`
  117. UserAgent string `json:"user_agent"`
  118. Cookie string `json:"cookie"`
  119. Username string `json:"username"`
  120. Password string `json:"password"`
  121. Crawler bool `json:"crawler"`
  122. Disabled bool `json:"disabled"`
  123. NoMediaPlayer bool `json:"no_media_player"`
  124. IgnoreHTTPCache bool `json:"ignore_http_cache"`
  125. AllowSelfSignedCertificates bool `json:"allow_self_signed_certificates"`
  126. FetchViaProxy bool `json:"fetch_via_proxy"`
  127. ScraperRules string `json:"scraper_rules"`
  128. RewriteRules string `json:"rewrite_rules"`
  129. BlocklistRules string `json:"blocklist_rules"`
  130. KeeplistRules string `json:"keeplist_rules"`
  131. HideGlobally bool `json:"hide_globally"`
  132. UrlRewriteRules string `json:"urlrewrite_rules"`
  133. }
  134. // FeedModificationRequest represents the request to update a feed.
  135. type FeedModificationRequest struct {
  136. FeedURL *string `json:"feed_url"`
  137. SiteURL *string `json:"site_url"`
  138. Title *string `json:"title"`
  139. ScraperRules *string `json:"scraper_rules"`
  140. RewriteRules *string `json:"rewrite_rules"`
  141. BlocklistRules *string `json:"blocklist_rules"`
  142. KeeplistRules *string `json:"keeplist_rules"`
  143. UrlRewriteRules *string `json:"urlrewrite_rules"`
  144. Crawler *bool `json:"crawler"`
  145. UserAgent *string `json:"user_agent"`
  146. Cookie *string `json:"cookie"`
  147. Username *string `json:"username"`
  148. Password *string `json:"password"`
  149. CategoryID *int64 `json:"category_id"`
  150. Disabled *bool `json:"disabled"`
  151. NoMediaPlayer *bool `json:"no_media_player"`
  152. IgnoreHTTPCache *bool `json:"ignore_http_cache"`
  153. AllowSelfSignedCertificates *bool `json:"allow_self_signed_certificates"`
  154. FetchViaProxy *bool `json:"fetch_via_proxy"`
  155. HideGlobally *bool `json:"hide_globally"`
  156. }
  157. // Patch updates a feed with modified values.
  158. func (f *FeedModificationRequest) Patch(feed *Feed) {
  159. if f.FeedURL != nil && *f.FeedURL != "" {
  160. feed.FeedURL = *f.FeedURL
  161. }
  162. if f.SiteURL != nil && *f.SiteURL != "" {
  163. feed.SiteURL = *f.SiteURL
  164. }
  165. if f.Title != nil && *f.Title != "" {
  166. feed.Title = *f.Title
  167. }
  168. if f.ScraperRules != nil {
  169. feed.ScraperRules = *f.ScraperRules
  170. }
  171. if f.RewriteRules != nil {
  172. feed.RewriteRules = *f.RewriteRules
  173. }
  174. if f.KeeplistRules != nil {
  175. feed.KeeplistRules = *f.KeeplistRules
  176. }
  177. if f.UrlRewriteRules != nil {
  178. feed.UrlRewriteRules = *f.UrlRewriteRules
  179. }
  180. if f.BlocklistRules != nil {
  181. feed.BlocklistRules = *f.BlocklistRules
  182. }
  183. if f.Crawler != nil {
  184. feed.Crawler = *f.Crawler
  185. }
  186. if f.UserAgent != nil {
  187. feed.UserAgent = *f.UserAgent
  188. }
  189. if f.Cookie != nil {
  190. feed.Cookie = *f.Cookie
  191. }
  192. if f.Username != nil {
  193. feed.Username = *f.Username
  194. }
  195. if f.Password != nil {
  196. feed.Password = *f.Password
  197. }
  198. if f.CategoryID != nil && *f.CategoryID > 0 {
  199. feed.Category.ID = *f.CategoryID
  200. }
  201. if f.Disabled != nil {
  202. feed.Disabled = *f.Disabled
  203. }
  204. if f.NoMediaPlayer != nil {
  205. feed.NoMediaPlayer = *f.NoMediaPlayer
  206. }
  207. if f.IgnoreHTTPCache != nil {
  208. feed.IgnoreHTTPCache = *f.IgnoreHTTPCache
  209. }
  210. if f.AllowSelfSignedCertificates != nil {
  211. feed.AllowSelfSignedCertificates = *f.AllowSelfSignedCertificates
  212. }
  213. if f.FetchViaProxy != nil {
  214. feed.FetchViaProxy = *f.FetchViaProxy
  215. }
  216. if f.HideGlobally != nil {
  217. feed.HideGlobally = *f.HideGlobally
  218. }
  219. }
  220. // Feeds is a list of feed
  221. type Feeds []*Feed