entry_pagination_builder.go 4.7 KB

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