feed.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. // Copyright 2017 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package model // import "miniflux.app/model"
  5. import (
  6. "fmt"
  7. "math"
  8. "time"
  9. "miniflux.app/config"
  10. "miniflux.app/http/client"
  11. )
  12. // List of supported schedulers.
  13. const (
  14. SchedulerRoundRobin = "round_robin"
  15. SchedulerEntryFrequency = "entry_frequency"
  16. // Default settings for the feed query builder
  17. DefaultFeedSorting = "parsing_error_count"
  18. DefaultFeedSortingDirection = "desc"
  19. )
  20. // Feed represents a feed in the application.
  21. type Feed struct {
  22. ID int64 `json:"id"`
  23. UserID int64 `json:"user_id"`
  24. FeedURL string `json:"feed_url"`
  25. SiteURL string `json:"site_url"`
  26. Title string `json:"title"`
  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. 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. Icon *FeedIcon `json:"icon"`
  50. HideGlobally bool `json:"hide_globally"`
  51. UnreadCount int `json:"-"`
  52. ReadCount int `json:"-"`
  53. }
  54. type FeedCounters struct {
  55. ReadCounters map[int64]int `json:"reads"`
  56. UnreadCounters map[int64]int `json:"unreads"`
  57. }
  58. func (f *Feed) String() string {
  59. return fmt.Sprintf("ID=%d, UserID=%d, FeedURL=%s, SiteURL=%s, Title=%s, Category={%s}",
  60. f.ID,
  61. f.UserID,
  62. f.FeedURL,
  63. f.SiteURL,
  64. f.Title,
  65. f.Category,
  66. )
  67. }
  68. // WithClientResponse updates feed attributes from an HTTP request.
  69. func (f *Feed) WithClientResponse(response *client.Response) {
  70. f.EtagHeader = response.ETag
  71. f.LastModifiedHeader = response.LastModified
  72. f.FeedURL = response.EffectiveURL
  73. }
  74. // WithCategoryID initializes the category attribute of the feed.
  75. func (f *Feed) WithCategoryID(categoryID int64) {
  76. f.Category = &Category{ID: categoryID}
  77. }
  78. // WithError adds a new error message and increment the error counter.
  79. func (f *Feed) WithError(message string) {
  80. f.ParsingErrorCount++
  81. f.ParsingErrorMsg = message
  82. }
  83. // ResetErrorCounter removes all previous errors.
  84. func (f *Feed) ResetErrorCounter() {
  85. f.ParsingErrorCount = 0
  86. f.ParsingErrorMsg = ""
  87. }
  88. // CheckedNow set attribute values when the feed is refreshed.
  89. func (f *Feed) CheckedNow() {
  90. f.CheckedAt = time.Now()
  91. if f.SiteURL == "" {
  92. f.SiteURL = f.FeedURL
  93. }
  94. }
  95. // ScheduleNextCheck set "next_check_at" of a feed based on the scheduler selected from the configuration.
  96. func (f *Feed) ScheduleNextCheck(weeklyCount int) {
  97. switch config.Opts.PollingScheduler() {
  98. case SchedulerEntryFrequency:
  99. var intervalMinutes int
  100. if weeklyCount == 0 {
  101. intervalMinutes = config.Opts.SchedulerEntryFrequencyMaxInterval()
  102. } else {
  103. intervalMinutes = int(math.Round(float64(7*24*60) / float64(weeklyCount)))
  104. }
  105. intervalMinutes = int(math.Min(float64(intervalMinutes), float64(config.Opts.SchedulerEntryFrequencyMaxInterval())))
  106. intervalMinutes = int(math.Max(float64(intervalMinutes), float64(config.Opts.SchedulerEntryFrequencyMinInterval())))
  107. f.NextCheckAt = time.Now().Add(time.Minute * time.Duration(intervalMinutes))
  108. default:
  109. f.NextCheckAt = time.Now()
  110. }
  111. }
  112. // FeedCreationRequest represents the request to create a feed.
  113. type FeedCreationRequest struct {
  114. FeedURL string `json:"feed_url"`
  115. CategoryID int64 `json:"category_id"`
  116. UserAgent string `json:"user_agent"`
  117. Cookie string `json:"cookie"`
  118. Username string `json:"username"`
  119. Password string `json:"password"`
  120. Crawler bool `json:"crawler"`
  121. Disabled bool `json:"disabled"`
  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. // FeedModificationRequest represents the request to update a feed.
  133. type FeedModificationRequest struct {
  134. FeedURL *string `json:"feed_url"`
  135. SiteURL *string `json:"site_url"`
  136. Title *string `json:"title"`
  137. ScraperRules *string `json:"scraper_rules"`
  138. RewriteRules *string `json:"rewrite_rules"`
  139. BlocklistRules *string `json:"blocklist_rules"`
  140. KeeplistRules *string `json:"keeplist_rules"`
  141. UrlRewriteRules *string `json:"urlrewrite_rules"`
  142. Crawler *bool `json:"crawler"`
  143. UserAgent *string `json:"user_agent"`
  144. Cookie *string `json:"cookie"`
  145. Username *string `json:"username"`
  146. Password *string `json:"password"`
  147. CategoryID *int64 `json:"category_id"`
  148. Disabled *bool `json:"disabled"`
  149. IgnoreHTTPCache *bool `json:"ignore_http_cache"`
  150. AllowSelfSignedCertificates *bool `json:"allow_self_signed_certificates"`
  151. FetchViaProxy *bool `json:"fetch_via_proxy"`
  152. HideGlobally *bool `json:"hide_globally"`
  153. }
  154. // Patch updates a feed with modified values.
  155. func (f *FeedModificationRequest) Patch(feed *Feed) {
  156. if f.FeedURL != nil && *f.FeedURL != "" {
  157. feed.FeedURL = *f.FeedURL
  158. }
  159. if f.SiteURL != nil && *f.SiteURL != "" {
  160. feed.SiteURL = *f.SiteURL
  161. }
  162. if f.Title != nil && *f.Title != "" {
  163. feed.Title = *f.Title
  164. }
  165. if f.ScraperRules != nil {
  166. feed.ScraperRules = *f.ScraperRules
  167. }
  168. if f.RewriteRules != nil {
  169. feed.RewriteRules = *f.RewriteRules
  170. }
  171. if f.KeeplistRules != nil {
  172. feed.KeeplistRules = *f.KeeplistRules
  173. }
  174. if f.UrlRewriteRules != nil {
  175. feed.UrlRewriteRules = *f.UrlRewriteRules
  176. }
  177. if f.BlocklistRules != nil {
  178. feed.BlocklistRules = *f.BlocklistRules
  179. }
  180. if f.Crawler != nil {
  181. feed.Crawler = *f.Crawler
  182. }
  183. if f.UserAgent != nil {
  184. feed.UserAgent = *f.UserAgent
  185. }
  186. if f.Cookie != nil {
  187. feed.Cookie = *f.Cookie
  188. }
  189. if f.Username != nil {
  190. feed.Username = *f.Username
  191. }
  192. if f.Password != nil {
  193. feed.Password = *f.Password
  194. }
  195. if f.CategoryID != nil && *f.CategoryID > 0 {
  196. feed.Category.ID = *f.CategoryID
  197. }
  198. if f.Disabled != nil {
  199. feed.Disabled = *f.Disabled
  200. }
  201. if f.IgnoreHTTPCache != nil {
  202. feed.IgnoreHTTPCache = *f.IgnoreHTTPCache
  203. }
  204. if f.AllowSelfSignedCertificates != nil {
  205. feed.AllowSelfSignedCertificates = *f.AllowSelfSignedCertificates
  206. }
  207. if f.FetchViaProxy != nil {
  208. feed.FetchViaProxy = *f.FetchViaProxy
  209. }
  210. if f.HideGlobally != nil {
  211. feed.HideGlobally = *f.HideGlobally
  212. }
  213. }
  214. // Feeds is a list of feed
  215. type Feeds []*Feed