entry_pagination_builder.go 5.2 KB

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