2023-06-19 23:42:47 +02:00
|
|
|
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2018-06-09 22:40:20 +02:00
|
|
|
|
2023-08-11 04:46:45 +02:00
|
|
|
package storage // import "miniflux.app/v2/internal/storage"
|
2018-06-09 22:40:20 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2023-08-11 04:46:45 +02:00
|
|
|
"miniflux.app/v2/internal/model"
|
|
|
|
"miniflux.app/v2/internal/timer"
|
2018-06-09 22:40:20 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// EntryPaginationBuilder is a builder for entry prev/next queries.
|
|
|
|
type EntryPaginationBuilder struct {
|
|
|
|
store *Storage
|
|
|
|
conditions []string
|
|
|
|
args []interface{}
|
|
|
|
entryID int64
|
2021-09-22 08:21:01 +02:00
|
|
|
order string
|
2018-06-09 22:40:20 +02:00
|
|
|
direction string
|
|
|
|
}
|
|
|
|
|
2018-07-05 07:05:19 +02:00
|
|
|
// WithSearchQuery adds full-text search query to the condition.
|
|
|
|
func (e *EntryPaginationBuilder) WithSearchQuery(query string) {
|
|
|
|
if query != "" {
|
|
|
|
e.conditions = append(e.conditions, fmt.Sprintf("e.document_vectors @@ plainto_tsquery($%d)", len(e.args)+1))
|
|
|
|
e.args = append(e.args, query)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-09 22:40:20 +02:00
|
|
|
// WithStarred adds starred to the condition.
|
|
|
|
func (e *EntryPaginationBuilder) WithStarred() {
|
|
|
|
e.conditions = append(e.conditions, "e.starred is true")
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithFeedID adds feed_id to the condition.
|
|
|
|
func (e *EntryPaginationBuilder) WithFeedID(feedID int64) {
|
|
|
|
if feedID != 0 {
|
|
|
|
e.conditions = append(e.conditions, fmt.Sprintf("e.feed_id = $%d", len(e.args)+1))
|
|
|
|
e.args = append(e.args, feedID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithCategoryID adds category_id to the condition.
|
|
|
|
func (e *EntryPaginationBuilder) WithCategoryID(categoryID int64) {
|
|
|
|
if categoryID != 0 {
|
|
|
|
e.conditions = append(e.conditions, fmt.Sprintf("f.category_id = $%d", len(e.args)+1))
|
|
|
|
e.args = append(e.args, categoryID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithStatus adds status to the condition.
|
|
|
|
func (e *EntryPaginationBuilder) WithStatus(status string) {
|
|
|
|
if status != "" {
|
|
|
|
e.conditions = append(e.conditions, fmt.Sprintf("e.status = $%d", len(e.args)+1))
|
|
|
|
e.args = append(e.args, status)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-15 21:18:24 +02:00
|
|
|
// WithGloballyVisible adds global visibility to the condition.
|
|
|
|
func (e *EntryPaginationBuilder) WithGloballyVisible() {
|
|
|
|
e.conditions = append(e.conditions, "not c.hide_globally")
|
|
|
|
e.conditions = append(e.conditions, "not f.hide_globally")
|
|
|
|
}
|
|
|
|
|
2018-06-09 22:40:20 +02:00
|
|
|
// Entries returns previous and next entries.
|
|
|
|
func (e *EntryPaginationBuilder) Entries() (*model.Entry, *model.Entry, error) {
|
|
|
|
tx, err := e.store.db.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("begin transaction for entry pagination: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
prevID, nextID, err := e.getPrevNextID(tx)
|
|
|
|
if err != nil {
|
|
|
|
tx.Rollback()
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
prevEntry, err := e.getEntry(tx, prevID)
|
|
|
|
if err != nil {
|
|
|
|
tx.Rollback()
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
nextEntry, err := e.getEntry(tx, nextID)
|
|
|
|
if err != nil {
|
|
|
|
tx.Rollback()
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
tx.Commit()
|
|
|
|
|
|
|
|
if e.direction == "desc" {
|
|
|
|
return nextEntry, prevEntry, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return prevEntry, nextEntry, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *EntryPaginationBuilder) getPrevNextID(tx *sql.Tx) (prevID int64, nextID int64, err error) {
|
|
|
|
defer timer.ExecutionTime(time.Now(), fmt.Sprintf("[EntryPaginationBuilder] %v, %v", e.conditions, e.args))
|
|
|
|
|
|
|
|
cte := `
|
|
|
|
WITH entry_pagination AS (
|
|
|
|
SELECT
|
|
|
|
e.id,
|
2021-09-22 08:21:01 +02:00
|
|
|
lag(e.id) over (order by e.%[1]s asc, e.id desc) as prev_id,
|
|
|
|
lead(e.id) over (order by e.%[1]s asc, e.id desc) as next_id
|
2018-06-09 22:40:20 +02:00
|
|
|
FROM entries AS e
|
2021-08-15 21:18:24 +02:00
|
|
|
JOIN feeds AS f ON f.id=e.feed_id
|
|
|
|
JOIN categories c ON c.id = f.category_id
|
2021-09-22 08:21:01 +02:00
|
|
|
WHERE %[2]s
|
|
|
|
ORDER BY e.%[1]s asc, e.id desc
|
2018-06-09 22:40:20 +02:00
|
|
|
)
|
2021-09-22 08:21:01 +02:00
|
|
|
SELECT prev_id, next_id FROM entry_pagination AS ep WHERE %[3]s;
|
2018-06-09 22:40:20 +02:00
|
|
|
`
|
|
|
|
|
|
|
|
subCondition := strings.Join(e.conditions, " AND ")
|
|
|
|
finalCondition := fmt.Sprintf("ep.id = $%d", len(e.args)+1)
|
2021-09-22 08:21:01 +02:00
|
|
|
query := fmt.Sprintf(cte, e.order, subCondition, finalCondition)
|
2018-06-09 22:40:20 +02:00
|
|
|
e.args = append(e.args, e.entryID)
|
|
|
|
|
|
|
|
var pID, nID sql.NullInt64
|
|
|
|
err = tx.QueryRow(query, e.args...).Scan(&pID, &nID)
|
|
|
|
switch {
|
|
|
|
case err == sql.ErrNoRows:
|
|
|
|
return 0, 0, nil
|
|
|
|
case err != nil:
|
|
|
|
return 0, 0, fmt.Errorf("entry pagination: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if pID.Valid {
|
|
|
|
prevID = pID.Int64
|
|
|
|
}
|
|
|
|
|
|
|
|
if nID.Valid {
|
|
|
|
nextID = nID.Int64
|
|
|
|
}
|
|
|
|
|
|
|
|
return prevID, nextID, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *EntryPaginationBuilder) getEntry(tx *sql.Tx, entryID int64) (*model.Entry, error) {
|
|
|
|
var entry model.Entry
|
|
|
|
|
|
|
|
err := tx.QueryRow(`SELECT id, title FROM entries WHERE id = $1`, entryID).Scan(
|
|
|
|
&entry.ID,
|
|
|
|
&entry.Title,
|
|
|
|
)
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case err == sql.ErrNoRows:
|
|
|
|
return nil, nil
|
|
|
|
case err != nil:
|
|
|
|
return nil, fmt.Errorf("fetching sibling entry: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &entry, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewEntryPaginationBuilder returns a new EntryPaginationBuilder.
|
2021-09-22 08:21:01 +02:00
|
|
|
func NewEntryPaginationBuilder(store *Storage, userID, entryID int64, order, direction string) *EntryPaginationBuilder {
|
2018-06-09 22:40:20 +02:00
|
|
|
return &EntryPaginationBuilder{
|
|
|
|
store: store,
|
|
|
|
args: []interface{}{userID, "removed"},
|
|
|
|
conditions: []string{"e.user_id = $1", "e.status <> $2"},
|
|
|
|
entryID: entryID,
|
2021-09-22 08:21:01 +02:00
|
|
|
order: order,
|
2018-06-09 22:40:20 +02:00
|
|
|
direction: direction,
|
|
|
|
}
|
|
|
|
}
|