entry_pagination_builder.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. "strings"
  8. "time"
  9. "miniflux.app/v2/internal/model"
  10. "miniflux.app/v2/internal/timer"
  11. )
  12. // EntryPaginationBuilder is a builder for entry prev/next queries.
  13. type EntryPaginationBuilder struct {
  14. store *Storage
  15. conditions []string
  16. args []interface{}
  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, fmt.Sprintf("e.feed_id = $%d", 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, fmt.Sprintf("f.category_id = $%d", 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, fmt.Sprintf("e.status = $%d", len(e.args)+1))
  50. e.args = append(e.args, status)
  51. }
  52. }
  53. // WithGloballyVisible adds global visibility to the condition.
  54. func (e *EntryPaginationBuilder) WithGloballyVisible() {
  55. e.conditions = append(e.conditions, "not c.hide_globally")
  56. e.conditions = append(e.conditions, "not f.hide_globally")
  57. }
  58. // Entries returns previous and next entries.
  59. func (e *EntryPaginationBuilder) Entries() (*model.Entry, *model.Entry, error) {
  60. tx, err := e.store.db.Begin()
  61. if err != nil {
  62. return nil, nil, fmt.Errorf("begin transaction for entry pagination: %v", err)
  63. }
  64. prevID, nextID, err := e.getPrevNextID(tx)
  65. if err != nil {
  66. tx.Rollback()
  67. return nil, nil, err
  68. }
  69. prevEntry, err := e.getEntry(tx, prevID)
  70. if err != nil {
  71. tx.Rollback()
  72. return nil, nil, err
  73. }
  74. nextEntry, err := e.getEntry(tx, nextID)
  75. if err != nil {
  76. tx.Rollback()
  77. return nil, nil, err
  78. }
  79. tx.Commit()
  80. if e.direction == "desc" {
  81. return nextEntry, prevEntry, nil
  82. }
  83. return prevEntry, nextEntry, nil
  84. }
  85. func (e *EntryPaginationBuilder) getPrevNextID(tx *sql.Tx) (prevID int64, nextID int64, err error) {
  86. defer timer.ExecutionTime(time.Now(), fmt.Sprintf("[EntryPaginationBuilder] %v, %v", e.conditions, e.args))
  87. cte := `
  88. WITH entry_pagination AS (
  89. SELECT
  90. e.id,
  91. lag(e.id) over (order by e.%[1]s asc, e.id desc) as prev_id,
  92. lead(e.id) over (order by e.%[1]s asc, e.id desc) as next_id
  93. FROM entries AS e
  94. JOIN feeds AS f ON f.id=e.feed_id
  95. JOIN categories c ON c.id = f.category_id
  96. WHERE %[2]s
  97. ORDER BY e.%[1]s asc, e.id desc
  98. )
  99. SELECT prev_id, next_id FROM entry_pagination AS ep WHERE %[3]s;
  100. `
  101. subCondition := strings.Join(e.conditions, " AND ")
  102. finalCondition := fmt.Sprintf("ep.id = $%d", len(e.args)+1)
  103. query := fmt.Sprintf(cte, e.order, subCondition, finalCondition)
  104. e.args = append(e.args, e.entryID)
  105. var pID, nID sql.NullInt64
  106. err = tx.QueryRow(query, e.args...).Scan(&pID, &nID)
  107. switch {
  108. case err == sql.ErrNoRows:
  109. return 0, 0, nil
  110. case err != nil:
  111. return 0, 0, fmt.Errorf("entry pagination: %v", err)
  112. }
  113. if pID.Valid {
  114. prevID = pID.Int64
  115. }
  116. if nID.Valid {
  117. nextID = nID.Int64
  118. }
  119. return prevID, nextID, nil
  120. }
  121. func (e *EntryPaginationBuilder) getEntry(tx *sql.Tx, entryID int64) (*model.Entry, error) {
  122. var entry model.Entry
  123. err := tx.QueryRow(`SELECT id, title FROM entries WHERE id = $1`, entryID).Scan(
  124. &entry.ID,
  125. &entry.Title,
  126. )
  127. switch {
  128. case err == sql.ErrNoRows:
  129. return nil, nil
  130. case err != nil:
  131. return nil, fmt.Errorf("fetching sibling entry: %v", err)
  132. }
  133. return &entry, nil
  134. }
  135. // NewEntryPaginationBuilder returns a new EntryPaginationBuilder.
  136. func NewEntryPaginationBuilder(store *Storage, userID, entryID int64, order, direction string) *EntryPaginationBuilder {
  137. return &EntryPaginationBuilder{
  138. store: store,
  139. args: []interface{}{userID, "removed"},
  140. conditions: []string{"e.user_id = $1", "e.status <> $2"},
  141. entryID: entryID,
  142. order: order,
  143. direction: direction,
  144. }
  145. }