entry_query_builder.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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. "strconv"
  8. "strings"
  9. "time"
  10. "github.com/lib/pq"
  11. "miniflux.app/v2/internal/model"
  12. "miniflux.app/v2/internal/timezone"
  13. )
  14. // EntryQueryBuilder builds a SQL query to fetch entries.
  15. type EntryQueryBuilder struct {
  16. store *Storage
  17. args []any
  18. conditions []string
  19. sortExpressions []string
  20. limit int
  21. offset int
  22. fetchEnclosures bool
  23. excludeContent bool
  24. }
  25. // WithEnclosures fetches enclosures for each entry.
  26. func (e *EntryQueryBuilder) WithEnclosures() *EntryQueryBuilder {
  27. e.fetchEnclosures = true
  28. return e
  29. }
  30. // WithoutContent excludes the content column from the query results,
  31. // replacing it with an empty string. This significantly reduces data
  32. // transfer from PostgreSQL on list pages where content is not displayed.
  33. func (e *EntryQueryBuilder) WithoutContent() *EntryQueryBuilder {
  34. e.excludeContent = true
  35. return e
  36. }
  37. // WithSearchQuery adds full-text search query to the condition.
  38. func (e *EntryQueryBuilder) WithSearchQuery(query string) *EntryQueryBuilder {
  39. if query != "" {
  40. nArgs := len(e.args) + 1
  41. e.conditions = append(e.conditions, fmt.Sprintf("e.document_vectors @@ plainto_tsquery($%d)", nArgs))
  42. e.args = append(e.args, query)
  43. // 0.0000001 = 0.1 / (seconds_in_a_day)
  44. e.WithSorting(
  45. fmt.Sprintf("ts_rank(document_vectors, plainto_tsquery($%d)) - extract (epoch from now() - published_at)::float * 0.0000001", nArgs),
  46. "DESC",
  47. )
  48. }
  49. return e
  50. }
  51. // WithStarred adds starred filter.
  52. func (e *EntryQueryBuilder) WithStarred(starred bool) *EntryQueryBuilder {
  53. if starred {
  54. e.conditions = append(e.conditions, "e.starred is true")
  55. } else {
  56. e.conditions = append(e.conditions, "e.starred is false")
  57. }
  58. return e
  59. }
  60. // BeforeChangedDate adds a condition < changed_at
  61. func (e *EntryQueryBuilder) BeforeChangedDate(date time.Time) *EntryQueryBuilder {
  62. e.conditions = append(e.conditions, "e.changed_at < $"+strconv.Itoa(len(e.args)+1))
  63. e.args = append(e.args, date)
  64. return e
  65. }
  66. // AfterChangedDate adds a condition > changed_at
  67. func (e *EntryQueryBuilder) AfterChangedDate(date time.Time) *EntryQueryBuilder {
  68. e.conditions = append(e.conditions, "e.changed_at > $"+strconv.Itoa(len(e.args)+1))
  69. e.args = append(e.args, date)
  70. return e
  71. }
  72. // BeforePublishedDate adds a condition < published_at
  73. func (e *EntryQueryBuilder) BeforePublishedDate(date time.Time) *EntryQueryBuilder {
  74. e.conditions = append(e.conditions, "e.published_at < $"+strconv.Itoa(len(e.args)+1))
  75. e.args = append(e.args, date)
  76. return e
  77. }
  78. // AfterPublishedDate adds a condition > published_at
  79. func (e *EntryQueryBuilder) AfterPublishedDate(date time.Time) *EntryQueryBuilder {
  80. e.conditions = append(e.conditions, "e.published_at > $"+strconv.Itoa(len(e.args)+1))
  81. e.args = append(e.args, date)
  82. return e
  83. }
  84. // BeforeEntryID adds a condition < entryID.
  85. func (e *EntryQueryBuilder) BeforeEntryID(entryID int64) *EntryQueryBuilder {
  86. if entryID != 0 {
  87. e.conditions = append(e.conditions, "e.id < $"+strconv.Itoa(len(e.args)+1))
  88. e.args = append(e.args, entryID)
  89. }
  90. return e
  91. }
  92. // AfterEntryID adds a condition > entryID.
  93. func (e *EntryQueryBuilder) AfterEntryID(entryID int64) *EntryQueryBuilder {
  94. if entryID != 0 {
  95. e.conditions = append(e.conditions, "e.id > $"+strconv.Itoa(len(e.args)+1))
  96. e.args = append(e.args, entryID)
  97. }
  98. return e
  99. }
  100. // WithEntryIDs filter by entry IDs.
  101. func (e *EntryQueryBuilder) WithEntryIDs(entryIDs []int64) *EntryQueryBuilder {
  102. if len(entryIDs) == 1 {
  103. e.conditions = append(e.conditions, fmt.Sprintf("e.id = $%d", len(e.args)+1))
  104. e.args = append(e.args, entryIDs[0])
  105. } else if len(entryIDs) > 1 {
  106. e.conditions = append(e.conditions, fmt.Sprintf("e.id = ANY($%d)", len(e.args)+1))
  107. e.args = append(e.args, pq.Int64Array(entryIDs))
  108. }
  109. return e
  110. }
  111. // WithEntryID filter by entry ID.
  112. func (e *EntryQueryBuilder) WithEntryID(entryID int64) *EntryQueryBuilder {
  113. if entryID != 0 {
  114. e.conditions = append(e.conditions, "e.id = $"+strconv.Itoa(len(e.args)+1))
  115. e.args = append(e.args, entryID)
  116. }
  117. return e
  118. }
  119. // WithFeedID filter by feed ID.
  120. func (e *EntryQueryBuilder) WithFeedID(feedID int64) *EntryQueryBuilder {
  121. if feedID > 0 {
  122. e.conditions = append(e.conditions, "e.feed_id = $"+strconv.Itoa(len(e.args)+1))
  123. e.args = append(e.args, feedID)
  124. }
  125. return e
  126. }
  127. // WithCategoryID filter by category ID.
  128. func (e *EntryQueryBuilder) WithCategoryID(categoryID int64) *EntryQueryBuilder {
  129. if categoryID > 0 {
  130. e.conditions = append(e.conditions, "f.category_id = $"+strconv.Itoa(len(e.args)+1))
  131. e.args = append(e.args, categoryID)
  132. }
  133. return e
  134. }
  135. // WithStatus filter by entry status.
  136. func (e *EntryQueryBuilder) WithStatus(status string) *EntryQueryBuilder {
  137. if status != "" {
  138. e.conditions = append(e.conditions, "e.status = $"+strconv.Itoa(len(e.args)+1))
  139. e.args = append(e.args, status)
  140. }
  141. return e
  142. }
  143. // WithStatuses filter by a list of entry statuses.
  144. func (e *EntryQueryBuilder) WithStatuses(statuses []string) *EntryQueryBuilder {
  145. if len(statuses) == 1 {
  146. e.conditions = append(e.conditions, fmt.Sprintf("e.status = $%d", len(e.args)+1))
  147. e.args = append(e.args, statuses[0])
  148. } else if len(statuses) > 1 {
  149. e.conditions = append(e.conditions, fmt.Sprintf("e.status = ANY($%d)", len(e.args)+1))
  150. e.args = append(e.args, pq.StringArray(statuses))
  151. }
  152. return e
  153. }
  154. // WithTags filter by a list of entry tags.
  155. func (e *EntryQueryBuilder) WithTags(tags []string) *EntryQueryBuilder {
  156. if len(tags) > 0 {
  157. for _, cat := range tags {
  158. e.conditions = append(e.conditions, fmt.Sprintf("LOWER($%d) = ANY(LOWER(e.tags::text)::text[])", len(e.args)+1))
  159. e.args = append(e.args, cat)
  160. }
  161. }
  162. return e
  163. }
  164. // WithoutStatus set the entry status that should not be returned.
  165. func (e *EntryQueryBuilder) WithoutStatus(status string) *EntryQueryBuilder {
  166. if status != "" {
  167. e.conditions = append(e.conditions, "e.status <> $"+strconv.Itoa(len(e.args)+1))
  168. e.args = append(e.args, status)
  169. }
  170. return e
  171. }
  172. // WithShareCode set the entry share code.
  173. func (e *EntryQueryBuilder) WithShareCode(shareCode string) *EntryQueryBuilder {
  174. e.conditions = append(e.conditions, "e.share_code = $"+strconv.Itoa(len(e.args)+1))
  175. e.args = append(e.args, shareCode)
  176. return e
  177. }
  178. // WithShareCodeNotEmpty adds a filter for non-empty share code.
  179. func (e *EntryQueryBuilder) WithShareCodeNotEmpty() *EntryQueryBuilder {
  180. e.conditions = append(e.conditions, "e.share_code <> ''")
  181. return e
  182. }
  183. // WithSorting add a sort expression.
  184. func (e *EntryQueryBuilder) WithSorting(column, direction string) *EntryQueryBuilder {
  185. e.sortExpressions = append(e.sortExpressions, column+" "+direction)
  186. return e
  187. }
  188. // WithLimit set the limit.
  189. func (e *EntryQueryBuilder) WithLimit(limit int) *EntryQueryBuilder {
  190. if limit > 0 {
  191. e.limit = limit
  192. }
  193. return e
  194. }
  195. // WithOffset set the offset.
  196. func (e *EntryQueryBuilder) WithOffset(offset int) *EntryQueryBuilder {
  197. if offset > 0 {
  198. e.offset = offset
  199. }
  200. return e
  201. }
  202. func (e *EntryQueryBuilder) WithGloballyVisible() *EntryQueryBuilder {
  203. e.conditions = append(e.conditions, "c.hide_globally IS FALSE")
  204. e.conditions = append(e.conditions, "f.hide_globally IS FALSE")
  205. return e
  206. }
  207. // CountEntries count the number of entries that match the condition.
  208. func (e *EntryQueryBuilder) CountEntries() (count int, err error) {
  209. query := `
  210. SELECT count(*)
  211. FROM entries e
  212. JOIN feeds f ON f.id = e.feed_id
  213. JOIN categories c ON c.id = f.category_id
  214. WHERE ` + e.buildCondition()
  215. err = e.store.db.QueryRow(query, e.args...).Scan(&count)
  216. if err != nil {
  217. return 0, fmt.Errorf("store: unable to count entries: %v", err)
  218. }
  219. return count, nil
  220. }
  221. // GetEntry returns a single entry that match the condition.
  222. func (e *EntryQueryBuilder) GetEntry() (*model.Entry, error) {
  223. e.limit = 1
  224. entries, err := e.GetEntries()
  225. if err != nil {
  226. return nil, err
  227. }
  228. if len(entries) != 1 {
  229. return nil, nil
  230. }
  231. entries[0].Enclosures, err = e.store.GetEnclosures(entries[0].ID)
  232. if err != nil {
  233. return nil, err
  234. }
  235. return entries[0], nil
  236. }
  237. // GetEntries returns a list of entries that match the condition.
  238. func (e *EntryQueryBuilder) GetEntries() (model.Entries, error) {
  239. entries, _, err := e.fetchEntries(false)
  240. return entries, err
  241. }
  242. // GetEntriesWithCount returns a list of entries and the total count of matching
  243. // rows (ignoring limit/offset) in a single query using a window function.
  244. // This avoids a separate CountEntries() round-trip.
  245. func (e *EntryQueryBuilder) GetEntriesWithCount() (model.Entries, int, error) {
  246. return e.fetchEntries(true)
  247. }
  248. // fetchEntries is the shared implementation for GetEntries and GetEntriesWithCount.
  249. // When withCount is true, count(*) OVER() is included in the SELECT and the total
  250. // count of matching rows is returned; otherwise the returned count is 0.
  251. func (e *EntryQueryBuilder) fetchEntries(withCount bool) (model.Entries, int, error) {
  252. countColumn := ""
  253. if withCount {
  254. countColumn = "count(*) OVER(),"
  255. }
  256. query := `
  257. SELECT
  258. ` + countColumn + `
  259. e.id,
  260. e.user_id,
  261. e.feed_id,
  262. e.hash,
  263. e.published_at at time zone u.timezone,
  264. e.title,
  265. e.url,
  266. e.comments_url,
  267. e.author,
  268. e.share_code,
  269. ` + e.contentColumn() + `,
  270. e.status,
  271. e.starred,
  272. e.reading_time,
  273. e.created_at,
  274. e.changed_at,
  275. e.tags,
  276. f.title as feed_title,
  277. f.feed_url,
  278. f.site_url,
  279. f.description,
  280. f.checked_at,
  281. f.category_id,
  282. c.title as category_title,
  283. c.hide_globally as category_hidden,
  284. f.scraper_rules,
  285. f.rewrite_rules,
  286. f.crawler,
  287. f.user_agent,
  288. f.cookie,
  289. f.hide_globally,
  290. f.no_media_player,
  291. f.webhook_url,
  292. fi.icon_id,
  293. i.external_id AS icon_external_id,
  294. u.timezone
  295. FROM
  296. entries e
  297. LEFT JOIN
  298. feeds f ON f.id=e.feed_id
  299. LEFT JOIN
  300. categories c ON c.id=f.category_id
  301. LEFT JOIN
  302. feed_icons fi ON fi.feed_id=f.id
  303. LEFT JOIN
  304. icons i ON i.id=fi.icon_id
  305. LEFT JOIN
  306. users u ON u.id=e.user_id
  307. WHERE ` + e.buildCondition() + " " + e.buildSorting()
  308. rows, err := e.store.db.Query(query, e.args...)
  309. if err != nil {
  310. return nil, 0, fmt.Errorf("store: unable to get entries: %v", err)
  311. }
  312. defer rows.Close()
  313. entries := make(model.Entries, 0)
  314. entryMap := make(map[int64]*model.Entry)
  315. var entryIDs []int64
  316. var totalCount int
  317. for rows.Next() {
  318. var iconID sql.NullInt64
  319. var externalIconID sql.NullString
  320. var tz string
  321. entry := model.NewEntry()
  322. dest := []any{
  323. &entry.ID,
  324. &entry.UserID,
  325. &entry.FeedID,
  326. &entry.Hash,
  327. &entry.Date,
  328. &entry.Title,
  329. &entry.URL,
  330. &entry.CommentsURL,
  331. &entry.Author,
  332. &entry.ShareCode,
  333. &entry.Content,
  334. &entry.Status,
  335. &entry.Starred,
  336. &entry.ReadingTime,
  337. &entry.CreatedAt,
  338. &entry.ChangedAt,
  339. pq.Array(&entry.Tags),
  340. &entry.Feed.Title,
  341. &entry.Feed.FeedURL,
  342. &entry.Feed.SiteURL,
  343. &entry.Feed.Description,
  344. &entry.Feed.CheckedAt,
  345. &entry.Feed.Category.ID,
  346. &entry.Feed.Category.Title,
  347. &entry.Feed.Category.HideGlobally,
  348. &entry.Feed.ScraperRules,
  349. &entry.Feed.RewriteRules,
  350. &entry.Feed.Crawler,
  351. &entry.Feed.UserAgent,
  352. &entry.Feed.Cookie,
  353. &entry.Feed.HideGlobally,
  354. &entry.Feed.NoMediaPlayer,
  355. &entry.Feed.WebhookURL,
  356. &iconID,
  357. &externalIconID,
  358. &tz,
  359. }
  360. if withCount {
  361. dest = append([]any{&totalCount}, dest...)
  362. }
  363. err := rows.Scan(dest...)
  364. if err != nil {
  365. return nil, 0, fmt.Errorf("store: unable to fetch entry row: %v", err)
  366. }
  367. if iconID.Valid && externalIconID.Valid && externalIconID.String != "" {
  368. entry.Feed.Icon.FeedID = entry.FeedID
  369. entry.Feed.Icon.IconID = iconID.Int64
  370. entry.Feed.Icon.ExternalIconID = externalIconID.String
  371. } else {
  372. entry.Feed.Icon.IconID = 0
  373. }
  374. // Make sure that timestamp fields contain timezone information (API)
  375. entry.Date = timezone.Convert(tz, entry.Date)
  376. entry.CreatedAt = timezone.Convert(tz, entry.CreatedAt)
  377. entry.ChangedAt = timezone.Convert(tz, entry.ChangedAt)
  378. entry.Feed.CheckedAt = timezone.Convert(tz, entry.Feed.CheckedAt)
  379. entry.Feed.ID = entry.FeedID
  380. entry.Feed.UserID = entry.UserID
  381. entry.Feed.Icon.FeedID = entry.FeedID
  382. entry.Feed.Category.UserID = entry.UserID
  383. entries = append(entries, entry)
  384. entryMap[entry.ID] = entry
  385. entryIDs = append(entryIDs, entry.ID)
  386. }
  387. if e.fetchEnclosures && len(entryIDs) > 0 {
  388. enclosures, err := e.store.GetEnclosuresForEntries(entryIDs)
  389. if err != nil {
  390. return nil, 0, fmt.Errorf("store: unable to fetch enclosures: %w", err)
  391. }
  392. for entryID, entryEnclosures := range enclosures {
  393. if entry, exists := entryMap[entryID]; exists {
  394. entry.Enclosures = entryEnclosures
  395. }
  396. }
  397. }
  398. return entries, totalCount, nil
  399. }
  400. // GetEntryIDs returns a list of entry IDs that match the condition.
  401. func (e *EntryQueryBuilder) GetEntryIDs() ([]int64, error) {
  402. query := `
  403. SELECT
  404. e.id
  405. FROM
  406. entries e
  407. LEFT JOIN
  408. feeds f
  409. ON
  410. f.id=e.feed_id
  411. WHERE ` + e.buildCondition() + " " + e.buildSorting()
  412. rows, err := e.store.db.Query(query, e.args...)
  413. if err != nil {
  414. return nil, fmt.Errorf("store: unable to get entries: %v", err)
  415. }
  416. defer rows.Close()
  417. var entryIDs []int64
  418. for rows.Next() {
  419. var entryID int64
  420. err := rows.Scan(&entryID)
  421. if err != nil {
  422. return nil, fmt.Errorf("store: unable to fetch entry row: %v", err)
  423. }
  424. entryIDs = append(entryIDs, entryID)
  425. }
  426. return entryIDs, nil
  427. }
  428. func (e *EntryQueryBuilder) contentColumn() string {
  429. if e.excludeContent {
  430. return "'' AS content"
  431. }
  432. return "e.content"
  433. }
  434. func (e *EntryQueryBuilder) buildCondition() string {
  435. return strings.Join(e.conditions, " AND ")
  436. }
  437. func (e *EntryQueryBuilder) buildSorting() string {
  438. var parts string
  439. if len(e.sortExpressions) > 0 {
  440. parts += " ORDER BY " + strings.Join(e.sortExpressions, ", ")
  441. }
  442. if e.limit > 0 {
  443. parts += " LIMIT " + strconv.Itoa(e.limit)
  444. }
  445. if e.offset > 0 {
  446. parts += " OFFSET " + strconv.Itoa(e.offset)
  447. }
  448. return parts
  449. }
  450. // NewEntryQueryBuilder returns a new EntryQueryBuilder.
  451. func NewEntryQueryBuilder(store *Storage, userID int64) *EntryQueryBuilder {
  452. return &EntryQueryBuilder{
  453. store: store,
  454. args: []any{userID},
  455. conditions: []string{"e.user_id = $1"},
  456. }
  457. }
  458. // NewAnonymousQueryBuilder returns a new EntryQueryBuilder suitable for anonymous users.
  459. func NewAnonymousQueryBuilder(store *Storage) *EntryQueryBuilder {
  460. return &EntryQueryBuilder{
  461. store: store,
  462. }
  463. }