feed_query_builder.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package storage // import "miniflux.app/v2/internal/storage"
  4. import (
  5. "database/sql"
  6. "fmt"
  7. "strings"
  8. "miniflux.app/v2/internal/model"
  9. "miniflux.app/v2/internal/timezone"
  10. )
  11. // FeedQueryBuilder builds a SQL query to fetch feeds.
  12. type FeedQueryBuilder struct {
  13. store *Storage
  14. args []interface{}
  15. conditions []string
  16. sortExpressions []string
  17. limit int
  18. offset int
  19. withCounters bool
  20. counterJoinFeeds bool
  21. counterArgs []interface{}
  22. counterConditions []string
  23. }
  24. // NewFeedQueryBuilder returns a new FeedQueryBuilder.
  25. func NewFeedQueryBuilder(store *Storage, userID int64) *FeedQueryBuilder {
  26. return &FeedQueryBuilder{
  27. store: store,
  28. args: []interface{}{userID},
  29. conditions: []string{"f.user_id = $1"},
  30. counterArgs: []interface{}{userID, model.EntryStatusRead, model.EntryStatusUnread},
  31. counterConditions: []string{"e.user_id = $1", "e.status IN ($2, $3)"},
  32. }
  33. }
  34. // WithCategoryID filter by category ID.
  35. func (f *FeedQueryBuilder) WithCategoryID(categoryID int64) *FeedQueryBuilder {
  36. if categoryID > 0 {
  37. f.conditions = append(f.conditions, fmt.Sprintf("f.category_id = $%d", len(f.args)+1))
  38. f.args = append(f.args, categoryID)
  39. f.counterConditions = append(f.counterConditions, fmt.Sprintf("f.category_id = $%d", len(f.counterArgs)+1))
  40. f.counterArgs = append(f.counterArgs, categoryID)
  41. f.counterJoinFeeds = true
  42. }
  43. return f
  44. }
  45. // WithFeedID filter by feed ID.
  46. func (f *FeedQueryBuilder) WithFeedID(feedID int64) *FeedQueryBuilder {
  47. if feedID > 0 {
  48. f.conditions = append(f.conditions, fmt.Sprintf("f.id = $%d", len(f.args)+1))
  49. f.args = append(f.args, feedID)
  50. }
  51. return f
  52. }
  53. // WithCounters let the builder return feeds with counters of statuses of entries.
  54. func (f *FeedQueryBuilder) WithCounters() *FeedQueryBuilder {
  55. f.withCounters = true
  56. return f
  57. }
  58. // WithSorting add a sort expression.
  59. func (f *FeedQueryBuilder) WithSorting(column, direction string) *FeedQueryBuilder {
  60. f.sortExpressions = append(f.sortExpressions, fmt.Sprintf("%s %s", column, direction))
  61. return f
  62. }
  63. // WithLimit set the limit.
  64. func (f *FeedQueryBuilder) WithLimit(limit int) *FeedQueryBuilder {
  65. f.limit = limit
  66. return f
  67. }
  68. // WithOffset set the offset.
  69. func (f *FeedQueryBuilder) WithOffset(offset int) *FeedQueryBuilder {
  70. f.offset = offset
  71. return f
  72. }
  73. func (f *FeedQueryBuilder) buildCondition() string {
  74. return strings.Join(f.conditions, " AND ")
  75. }
  76. func (f *FeedQueryBuilder) buildCounterCondition() string {
  77. return strings.Join(f.counterConditions, " AND ")
  78. }
  79. func (f *FeedQueryBuilder) buildSorting() string {
  80. var parts string
  81. if len(f.sortExpressions) > 0 {
  82. parts += fmt.Sprintf(" ORDER BY %s", strings.Join(f.sortExpressions, ", "))
  83. }
  84. if len(parts) > 0 {
  85. parts += ", lower(f.title) ASC"
  86. }
  87. if f.limit > 0 {
  88. parts += fmt.Sprintf(" LIMIT %d", f.limit)
  89. }
  90. if f.offset > 0 {
  91. parts += fmt.Sprintf(" OFFSET %d", f.offset)
  92. }
  93. return parts
  94. }
  95. // GetFeed returns a single feed that match the condition.
  96. func (f *FeedQueryBuilder) GetFeed() (*model.Feed, error) {
  97. f.limit = 1
  98. feeds, err := f.GetFeeds()
  99. if err != nil {
  100. return nil, err
  101. }
  102. if len(feeds) != 1 {
  103. return nil, nil
  104. }
  105. return feeds[0], nil
  106. }
  107. // GetFeeds returns a list of feeds that match the condition.
  108. func (f *FeedQueryBuilder) GetFeeds() (model.Feeds, error) {
  109. var query = `
  110. SELECT
  111. f.id,
  112. f.feed_url,
  113. f.site_url,
  114. f.title,
  115. f.description,
  116. f.etag_header,
  117. f.last_modified_header,
  118. f.user_id,
  119. f.checked_at at time zone u.timezone,
  120. f.next_check_at at time zone u.timezone,
  121. f.parsing_error_count,
  122. f.parsing_error_msg,
  123. f.scraper_rules,
  124. f.rewrite_rules,
  125. f.blocklist_rules,
  126. f.keeplist_rules,
  127. f.url_rewrite_rules,
  128. f.crawler,
  129. f.user_agent,
  130. f.cookie,
  131. f.username,
  132. f.password,
  133. f.ignore_http_cache,
  134. f.allow_self_signed_certificates,
  135. f.fetch_via_proxy,
  136. f.disabled,
  137. f.no_media_player,
  138. f.hide_globally,
  139. f.category_id,
  140. c.title as category_title,
  141. c.hide_globally as category_hidden,
  142. fi.icon_id,
  143. u.timezone,
  144. f.apprise_service_urls,
  145. f.disable_http2,
  146. f.ntfy_enabled,
  147. f.ntfy_priority
  148. FROM
  149. feeds f
  150. LEFT JOIN
  151. categories c ON c.id=f.category_id
  152. LEFT JOIN
  153. feed_icons fi ON fi.feed_id=f.id
  154. LEFT JOIN
  155. users u ON u.id=f.user_id
  156. WHERE %s
  157. %s
  158. `
  159. query = fmt.Sprintf(query, f.buildCondition(), f.buildSorting())
  160. rows, err := f.store.db.Query(query, f.args...)
  161. if err != nil {
  162. return nil, fmt.Errorf(`store: unable to fetch feeds: %w`, err)
  163. }
  164. defer rows.Close()
  165. readCounters, unreadCounters, err := f.fetchFeedCounter()
  166. if err != nil {
  167. return nil, err
  168. }
  169. feeds := make(model.Feeds, 0)
  170. for rows.Next() {
  171. var feed model.Feed
  172. var iconID sql.NullInt64
  173. var tz string
  174. feed.Category = &model.Category{}
  175. err := rows.Scan(
  176. &feed.ID,
  177. &feed.FeedURL,
  178. &feed.SiteURL,
  179. &feed.Title,
  180. &feed.Description,
  181. &feed.EtagHeader,
  182. &feed.LastModifiedHeader,
  183. &feed.UserID,
  184. &feed.CheckedAt,
  185. &feed.NextCheckAt,
  186. &feed.ParsingErrorCount,
  187. &feed.ParsingErrorMsg,
  188. &feed.ScraperRules,
  189. &feed.RewriteRules,
  190. &feed.BlocklistRules,
  191. &feed.KeeplistRules,
  192. &feed.UrlRewriteRules,
  193. &feed.Crawler,
  194. &feed.UserAgent,
  195. &feed.Cookie,
  196. &feed.Username,
  197. &feed.Password,
  198. &feed.IgnoreHTTPCache,
  199. &feed.AllowSelfSignedCertificates,
  200. &feed.FetchViaProxy,
  201. &feed.Disabled,
  202. &feed.NoMediaPlayer,
  203. &feed.HideGlobally,
  204. &feed.Category.ID,
  205. &feed.Category.Title,
  206. &feed.Category.HideGlobally,
  207. &iconID,
  208. &tz,
  209. &feed.AppriseServiceURLs,
  210. &feed.DisableHTTP2,
  211. &feed.NtfyEnabled,
  212. &feed.NtfyPriority,
  213. )
  214. if err != nil {
  215. return nil, fmt.Errorf(`store: unable to fetch feeds row: %w`, err)
  216. }
  217. if iconID.Valid {
  218. feed.Icon = &model.FeedIcon{FeedID: feed.ID, IconID: iconID.Int64}
  219. } else {
  220. feed.Icon = &model.FeedIcon{FeedID: feed.ID, IconID: 0}
  221. }
  222. if readCounters != nil {
  223. if count, found := readCounters[feed.ID]; found {
  224. feed.ReadCount = count
  225. }
  226. }
  227. if unreadCounters != nil {
  228. if count, found := unreadCounters[feed.ID]; found {
  229. feed.UnreadCount = count
  230. }
  231. }
  232. feed.NumberOfVisibleEntries = feed.ReadCount + feed.UnreadCount
  233. feed.CheckedAt = timezone.Convert(tz, feed.CheckedAt)
  234. feed.NextCheckAt = timezone.Convert(tz, feed.NextCheckAt)
  235. feed.Category.UserID = feed.UserID
  236. feeds = append(feeds, &feed)
  237. }
  238. return feeds, nil
  239. }
  240. func (f *FeedQueryBuilder) fetchFeedCounter() (unreadCounters map[int64]int, readCounters map[int64]int, err error) {
  241. if !f.withCounters {
  242. return nil, nil, nil
  243. }
  244. query := `
  245. SELECT
  246. e.feed_id,
  247. e.status,
  248. count(*)
  249. FROM
  250. entries e
  251. %s
  252. WHERE
  253. %s
  254. GROUP BY
  255. e.feed_id, e.status
  256. `
  257. join := ""
  258. if f.counterJoinFeeds {
  259. join = "LEFT JOIN feeds f ON f.id=e.feed_id"
  260. }
  261. query = fmt.Sprintf(query, join, f.buildCounterCondition())
  262. rows, err := f.store.db.Query(query, f.counterArgs...)
  263. if err != nil {
  264. return nil, nil, fmt.Errorf(`store: unable to fetch feed counts: %w`, err)
  265. }
  266. defer rows.Close()
  267. readCounters = make(map[int64]int)
  268. unreadCounters = make(map[int64]int)
  269. for rows.Next() {
  270. var feedID int64
  271. var status string
  272. var count int
  273. if err := rows.Scan(&feedID, &status, &count); err != nil {
  274. return nil, nil, fmt.Errorf(`store: unable to fetch feed counter row: %w`, err)
  275. }
  276. if status == model.EntryStatusRead {
  277. readCounters[feedID] = count
  278. } else if status == model.EntryStatusUnread {
  279. unreadCounters[feedID] = count
  280. }
  281. }
  282. return readCounters, unreadCounters, nil
  283. }