entry_pagination_builder.go 4.4 KB

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