entry_pagination_builder.go 4.6 KB

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