entry_pagination_builder.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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
  5. import (
  6. "database/sql"
  7. "fmt"
  8. "strings"
  9. "time"
  10. "github.com/miniflux/miniflux/model"
  11. "github.com/miniflux/miniflux/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. direction string
  20. }
  21. // WithStarred adds starred to the condition.
  22. func (e *EntryPaginationBuilder) WithStarred() {
  23. e.conditions = append(e.conditions, "e.starred is true")
  24. }
  25. // WithFeedID adds feed_id to the condition.
  26. func (e *EntryPaginationBuilder) WithFeedID(feedID int64) {
  27. if feedID != 0 {
  28. e.conditions = append(e.conditions, fmt.Sprintf("e.feed_id = $%d", len(e.args)+1))
  29. e.args = append(e.args, feedID)
  30. }
  31. }
  32. // WithCategoryID adds category_id to the condition.
  33. func (e *EntryPaginationBuilder) WithCategoryID(categoryID int64) {
  34. if categoryID != 0 {
  35. e.conditions = append(e.conditions, fmt.Sprintf("f.category_id = $%d", len(e.args)+1))
  36. e.args = append(e.args, categoryID)
  37. }
  38. }
  39. // WithStatus adds status to the condition.
  40. func (e *EntryPaginationBuilder) WithStatus(status string) {
  41. if status != "" {
  42. e.conditions = append(e.conditions, fmt.Sprintf("e.status = $%d", len(e.args)+1))
  43. e.args = append(e.args, status)
  44. }
  45. }
  46. // Entries returns previous and next entries.
  47. func (e *EntryPaginationBuilder) Entries() (*model.Entry, *model.Entry, error) {
  48. tx, err := e.store.db.Begin()
  49. if err != nil {
  50. return nil, nil, fmt.Errorf("begin transaction for entry pagination: %v", err)
  51. }
  52. prevID, nextID, err := e.getPrevNextID(tx)
  53. if err != nil {
  54. tx.Rollback()
  55. return nil, nil, err
  56. }
  57. prevEntry, err := e.getEntry(tx, prevID)
  58. if err != nil {
  59. tx.Rollback()
  60. return nil, nil, err
  61. }
  62. nextEntry, err := e.getEntry(tx, nextID)
  63. if err != nil {
  64. tx.Rollback()
  65. return nil, nil, err
  66. }
  67. tx.Commit()
  68. if e.direction == "desc" {
  69. return nextEntry, prevEntry, nil
  70. }
  71. return prevEntry, nextEntry, nil
  72. }
  73. func (e *EntryPaginationBuilder) getPrevNextID(tx *sql.Tx) (prevID int64, nextID int64, err error) {
  74. defer timer.ExecutionTime(time.Now(), fmt.Sprintf("[EntryPaginationBuilder] %v, %v", e.conditions, e.args))
  75. cte := `
  76. WITH entry_pagination AS (
  77. SELECT
  78. e.id,
  79. lag(e.id) over (order by e.published_at asc, e.id desc) as prev_id,
  80. lead(e.id) over (order by e.published_at asc, e.id desc) as next_id
  81. FROM entries AS e
  82. LEFT JOIN feeds AS f ON f.id=e.feed_id
  83. WHERE %s
  84. ORDER BY e.published_at asc, e.id desc
  85. )
  86. SELECT prev_id, next_id FROM entry_pagination AS ep WHERE %s;
  87. `
  88. subCondition := strings.Join(e.conditions, " AND ")
  89. finalCondition := fmt.Sprintf("ep.id = $%d", len(e.args)+1)
  90. query := fmt.Sprintf(cte, subCondition, finalCondition)
  91. e.args = append(e.args, e.entryID)
  92. var pID, nID sql.NullInt64
  93. err = tx.QueryRow(query, e.args...).Scan(&pID, &nID)
  94. switch {
  95. case err == sql.ErrNoRows:
  96. return 0, 0, nil
  97. case err != nil:
  98. return 0, 0, fmt.Errorf("entry pagination: %v", err)
  99. }
  100. if pID.Valid {
  101. prevID = pID.Int64
  102. }
  103. if nID.Valid {
  104. nextID = nID.Int64
  105. }
  106. return prevID, nextID, nil
  107. }
  108. func (e *EntryPaginationBuilder) getEntry(tx *sql.Tx, entryID int64) (*model.Entry, error) {
  109. var entry model.Entry
  110. err := tx.QueryRow(`SELECT id, title FROM entries WHERE id = $1`, entryID).Scan(
  111. &entry.ID,
  112. &entry.Title,
  113. )
  114. switch {
  115. case err == sql.ErrNoRows:
  116. return nil, nil
  117. case err != nil:
  118. return nil, fmt.Errorf("fetching sibling entry: %v", err)
  119. }
  120. return &entry, nil
  121. }
  122. // NewEntryPaginationBuilder returns a new EntryPaginationBuilder.
  123. func NewEntryPaginationBuilder(store *Storage, userID, entryID int64, direction string) *EntryPaginationBuilder {
  124. return &EntryPaginationBuilder{
  125. store: store,
  126. args: []interface{}{userID, "removed"},
  127. conditions: []string{"e.user_id = $1", "e.status <> $2"},
  128. entryID: entryID,
  129. direction: direction,
  130. }
  131. }