entry_pagination_builder.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. store *Storage
  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 @@ plainto_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. for _, tag := range tags {
  77. e.conditions = append(e.conditions, fmt.Sprintf("LOWER($%d) = ANY(LOWER(e.tags::text)::text[])", len(e.args)+1))
  78. e.args = append(e.args, tag)
  79. }
  80. }
  81. return e
  82. }
  83. // WithGloballyVisible adds global visibility to the condition.
  84. func (e *entryPaginationBuilder) WithGloballyVisible() *entryPaginationBuilder {
  85. e.conditions = append(e.conditions, "not c.hide_globally")
  86. e.conditions = append(e.conditions, "not f.hide_globally")
  87. return e
  88. }
  89. // Entries returns previous and next entries.
  90. func (e *entryPaginationBuilder) Entries() (*model.Entry, *model.Entry, error) {
  91. tx, err := e.store.db.Begin()
  92. if err != nil {
  93. return nil, nil, fmt.Errorf("begin transaction for entry pagination: %v", err)
  94. }
  95. prevID, nextID, err := e.getPrevNextID(tx)
  96. if err != nil {
  97. tx.Rollback()
  98. return nil, nil, err
  99. }
  100. prevEntry, err := e.getEntry(tx, prevID)
  101. if err != nil {
  102. tx.Rollback()
  103. return nil, nil, err
  104. }
  105. nextEntry, err := e.getEntry(tx, nextID)
  106. if err != nil {
  107. tx.Rollback()
  108. return nil, nil, err
  109. }
  110. tx.Commit()
  111. if e.direction == "desc" {
  112. return nextEntry, prevEntry, nil
  113. }
  114. return prevEntry, nextEntry, nil
  115. }
  116. func (e *entryPaginationBuilder) getPrevNextID(tx *sql.Tx) (prevID int64, nextID int64, err error) {
  117. cte := `
  118. WITH entry_pagination AS (
  119. SELECT
  120. e.id,
  121. lag(e.id) over (order by e.%[1]s asc, e.created_at asc, e.id desc) as prev_id,
  122. lead(e.id) over (order by e.%[1]s asc, e.created_at asc, e.id desc) as next_id
  123. FROM entries AS e
  124. JOIN feeds AS f ON f.id=e.feed_id
  125. JOIN categories c ON c.id = f.category_id
  126. WHERE %[2]s
  127. ORDER BY e.%[1]s asc, e.created_at asc, e.id desc
  128. )
  129. SELECT prev_id, next_id FROM entry_pagination AS ep WHERE %[3]s;
  130. `
  131. subCondition := strings.Join(e.conditions, " AND ")
  132. finalCondition := "ep.id = $" + strconv.Itoa(len(e.args)+1)
  133. query := fmt.Sprintf(cte, e.order, subCondition, finalCondition)
  134. e.args = append(e.args, e.entryID)
  135. var pID, nID sql.NullInt64
  136. err = tx.QueryRow(query, e.args...).Scan(&pID, &nID)
  137. switch {
  138. case errors.Is(err, sql.ErrNoRows):
  139. return 0, 0, nil
  140. case err != nil:
  141. return 0, 0, fmt.Errorf("entry pagination: %v", err)
  142. }
  143. if pID.Valid {
  144. prevID = pID.Int64
  145. }
  146. if nID.Valid {
  147. nextID = nID.Int64
  148. }
  149. return prevID, nextID, nil
  150. }
  151. func (e *entryPaginationBuilder) getEntry(tx *sql.Tx, entryID int64) (*model.Entry, error) {
  152. var entry model.Entry
  153. err := tx.QueryRow(`SELECT id, title FROM entries WHERE id = $1`, entryID).Scan(
  154. &entry.ID,
  155. &entry.Title,
  156. )
  157. switch {
  158. case errors.Is(err, sql.ErrNoRows):
  159. return nil, nil
  160. case err != nil:
  161. return nil, fmt.Errorf("fetching sibling entry: %v", err)
  162. }
  163. return &entry, nil
  164. }
  165. // NewEntryPaginationBuilder returns a new EntryPaginationBuilder.
  166. func NewEntryPaginationBuilder(store *Storage, userID, entryID int64, order, direction string) *entryPaginationBuilder {
  167. return &entryPaginationBuilder{
  168. store: store,
  169. args: []any{userID},
  170. conditions: []string{"e.user_id = $1"},
  171. entryID: entryID,
  172. order: pq.QuoteIdentifier(order),
  173. direction: direction,
  174. }
  175. }