4
0

feed.go 9.6 KB

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