4
0

entry_pagination_builder.go 5.5 KB

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