entry_pagination_builder.go 5.2 KB

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