entry_pagination_builder.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. "miniflux.app/v2/internal/model"
  10. )
  11. // EntryPaginationBuilder is a builder for entry prev/next queries.
  12. type EntryPaginationBuilder struct {
  13. store *Storage
  14. conditions []string
  15. args []any
  16. entryID int64
  17. order string
  18. direction string
  19. }
  20. // WithSearchQuery adds full-text search query to the condition.
  21. func (e *EntryPaginationBuilder) WithSearchQuery(query string) {
  22. if query != "" {
  23. e.conditions = append(e.conditions, fmt.Sprintf("e.document_vectors @@ plainto_tsquery($%d)", len(e.args)+1))
  24. e.args = append(e.args, query)
  25. }
  26. }
  27. // WithStarred adds starred to the condition.
  28. func (e *EntryPaginationBuilder) WithStarred() {
  29. e.conditions = append(e.conditions, "e.starred is true")
  30. }
  31. // WithFeedID adds feed_id to the condition.
  32. func (e *EntryPaginationBuilder) WithFeedID(feedID int64) {
  33. if feedID != 0 {
  34. e.conditions = append(e.conditions, "e.feed_id = $"+strconv.Itoa(len(e.args)+1))
  35. e.args = append(e.args, feedID)
  36. }
  37. }
  38. // WithCategoryID adds category_id to the condition.
  39. func (e *EntryPaginationBuilder) WithCategoryID(categoryID int64) {
  40. if categoryID != 0 {
  41. e.conditions = append(e.conditions, "f.category_id = $"+strconv.Itoa(len(e.args)+1))
  42. e.args = append(e.args, categoryID)
  43. }
  44. }
  45. // WithStatus adds status to the condition.
  46. func (e *EntryPaginationBuilder) WithStatus(status string) {
  47. if status != "" {
  48. e.conditions = append(e.conditions, "e.status = $"+strconv.Itoa(len(e.args)+1))
  49. e.args = append(e.args, status)
  50. }
  51. }
  52. func (e *EntryPaginationBuilder) WithTags(tags []string) {
  53. if len(tags) > 0 {
  54. for _, tag := range tags {
  55. e.conditions = append(e.conditions, fmt.Sprintf("LOWER($%d) = ANY(LOWER(e.tags::text)::text[])", len(e.args)+1))
  56. e.args = append(e.args, tag)
  57. }
  58. }
  59. }
  60. // WithGloballyVisible adds global visibility to the condition.
  61. func (e *EntryPaginationBuilder) WithGloballyVisible() {
  62. e.conditions = append(e.conditions, "not c.hide_globally")
  63. e.conditions = append(e.conditions, "not f.hide_globally")
  64. }
  65. // Entries returns previous and next entries.
  66. func (e *EntryPaginationBuilder) Entries() (*model.Entry, *model.Entry, error) {
  67. tx, err := e.store.db.Begin()
  68. if err != nil {
  69. return nil, nil, fmt.Errorf("begin transaction for entry pagination: %v", err)
  70. }
  71. prevID, nextID, err := e.getPrevNextID(tx)
  72. if err != nil {
  73. tx.Rollback()
  74. return nil, nil, err
  75. }
  76. prevEntry, err := e.getEntry(tx, prevID)
  77. if err != nil {
  78. tx.Rollback()
  79. return nil, nil, err
  80. }
  81. nextEntry, err := e.getEntry(tx, nextID)
  82. if err != nil {
  83. tx.Rollback()
  84. return nil, nil, err
  85. }
  86. tx.Commit()
  87. if e.direction == "desc" {
  88. return nextEntry, prevEntry, nil
  89. }
  90. return prevEntry, nextEntry, nil
  91. }
  92. func (e *EntryPaginationBuilder) getPrevNextID(tx *sql.Tx) (prevID int64, nextID int64, err error) {
  93. cte := `
  94. WITH entry_pagination AS (
  95. SELECT
  96. e.id,
  97. lag(e.id) over (order by e.%[1]s asc, e.created_at asc, e.id desc) as prev_id,
  98. lead(e.id) over (order by e.%[1]s asc, e.created_at asc, e.id desc) as next_id
  99. FROM entries AS e
  100. JOIN feeds AS f ON f.id=e.feed_id
  101. JOIN categories c ON c.id = f.category_id
  102. WHERE %[2]s
  103. ORDER BY e.%[1]s asc, e.created_at asc, e.id desc
  104. )
  105. SELECT prev_id, next_id FROM entry_pagination AS ep WHERE %[3]s;
  106. `
  107. subCondition := strings.Join(e.conditions, " AND ")
  108. finalCondition := "ep.id = $" + strconv.Itoa(len(e.args)+1)
  109. query := fmt.Sprintf(cte, e.order, subCondition, finalCondition)
  110. e.args = append(e.args, e.entryID)
  111. var pID, nID sql.NullInt64
  112. err = tx.QueryRow(query, e.args...).Scan(&pID, &nID)
  113. switch {
  114. case err == sql.ErrNoRows:
  115. return 0, 0, nil
  116. case err != nil:
  117. return 0, 0, fmt.Errorf("entry pagination: %v", err)
  118. }
  119. if pID.Valid {
  120. prevID = pID.Int64
  121. }
  122. if nID.Valid {
  123. nextID = nID.Int64
  124. }
  125. return prevID, nextID, nil
  126. }
  127. func (e *EntryPaginationBuilder) getEntry(tx *sql.Tx, entryID int64) (*model.Entry, error) {
  128. var entry model.Entry
  129. err := tx.QueryRow(`SELECT id, title FROM entries WHERE id = $1`, entryID).Scan(
  130. &entry.ID,
  131. &entry.Title,
  132. )
  133. switch {
  134. case err == sql.ErrNoRows:
  135. return nil, nil
  136. case err != nil:
  137. return nil, fmt.Errorf("fetching sibling entry: %v", err)
  138. }
  139. return &entry, nil
  140. }
  141. // NewEntryPaginationBuilder returns a new EntryPaginationBuilder.
  142. func NewEntryPaginationBuilder(store *Storage, userID, entryID int64, order, direction string) *EntryPaginationBuilder {
  143. return &EntryPaginationBuilder{
  144. store: store,
  145. args: []any{userID, "removed"},
  146. conditions: []string{"e.user_id = $1", "e.status <> $2"},
  147. entryID: entryID,
  148. order: order,
  149. direction: direction,
  150. }
  151. }