110 lines
3.9 KiB
Go
110 lines
3.9 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
// FeedActionRecord is a receipt plus currently available local metadata. A
|
|
// deleted source or evicted article must not hide an unresolved remote write.
|
|
type FeedActionRecord struct {
|
|
FeedAction
|
|
SourceTitle string `json:"source_title"`
|
|
ItemTitle string `json:"item_title"`
|
|
SourcePresent bool `json:"source_present"`
|
|
ItemPresent bool `json:"item_present"`
|
|
CanCheck bool `json:"can_check"`
|
|
CheckAfterMS int64 `json:"check_after_ms"`
|
|
}
|
|
|
|
type FeedActionQuery struct {
|
|
Source string
|
|
Status string
|
|
Before int64
|
|
Key string
|
|
}
|
|
|
|
const feedActionMetadata = `,COALESCE(s.title,''),COALESCE(i.title,''),s.uuid IS NOT NULL,i.id IS NOT NULL,COALESCE(s.enabled=1 AND s.provider='freshrss',0)`
|
|
const feedActionJoins = ` FROM feed_actions a LEFT JOIN feed_sources s ON s.uuid=a.source_uuid AND s.domain_uuid=a.domain_uuid AND s.owner_uuid=a.owner_uuid LEFT JOIN feed_items i ON i.source_uuid=s.uuid AND i.id=a.item_id WHERE a.domain_uuid=? AND a.owner_uuid=?`
|
|
|
|
func feedActionRecordColumns() string {
|
|
return "a." + strings.ReplaceAll(feedActionColumns, ",", ",a.") + feedActionMetadata
|
|
}
|
|
|
|
func scanFeedActionRecord(row interface{ Scan(...any) error }, writable bool) (a FeedActionRecord, e error) {
|
|
var enabled bool
|
|
e = row.Scan(&a.Domain, &a.Owner, &a.RequestID, &a.Source, &a.ID, &a.Field, &a.Desired, &a.SourceVersion, &a.Status, &a.Claim, &a.UntilMS, &a.CreatedMS, &a.Error, &a.SourceTitle, &a.ItemTitle, &a.SourcePresent, &a.ItemPresent, &enabled)
|
|
if a.Status == "unknown" {
|
|
a.CheckAfterMS = a.UntilMS
|
|
a.CanCheck = writable && enabled && a.UntilMS < time.Now().UnixMilli()
|
|
}
|
|
return
|
|
}
|
|
|
|
// FeedActionHistory returns at most 51 local receipts (50 plus a next-page
|
|
// sentinel). The owner cap of 10,000 receipts bounds the sorting work even
|
|
// without a new schema/index. No fetch, retry, expiry or deletion runs here.
|
|
func (s *Store) FeedActionHistory(ctx context.Context, domain, owner string, q FeedActionQuery) ([]FeedActionRecord, error) {
|
|
if e := feedActor(ctx, s.DB, domain, owner, false); e != nil {
|
|
return nil, e
|
|
}
|
|
where := ""
|
|
switch q.Status {
|
|
case "", "unresolved":
|
|
where = ` AND a.status NOT IN ('confirmed','cancelled')`
|
|
case "all":
|
|
case "pending":
|
|
where = ` AND a.status IN ('queued','preparing','sending','check_queued','checking')`
|
|
case "unknown", "confirmed", "failed", "conflict", "cancelled":
|
|
where = ` AND a.status='` + q.Status + `'`
|
|
default:
|
|
return nil, ErrTaskInvalid
|
|
}
|
|
args := []any{domain, owner}
|
|
if q.Source != "" {
|
|
if _, e := uuid.Parse(q.Source); e != nil {
|
|
return nil, ErrTaskInvalid
|
|
}
|
|
where += ` AND a.source_uuid=?`
|
|
args = append(args, q.Source)
|
|
}
|
|
if q.Before >= 0 {
|
|
if _, e := uuid.Parse(q.Key); e != nil {
|
|
return nil, ErrTaskInvalid
|
|
}
|
|
where += ` AND (a.created_ms<? OR (a.created_ms=? AND a.request_id<?))`
|
|
args = append(args, q.Before, q.Before, q.Key)
|
|
} else if q.Before != -1 || q.Key != "" {
|
|
return nil, ErrTaskInvalid
|
|
}
|
|
writable := !s.ReadOnly && feedActor(ctx, s.DB, domain, owner, true) == nil
|
|
rows, e := s.DB.QueryContext(ctx, `SELECT `+feedActionRecordColumns()+feedActionJoins+where+` ORDER BY a.created_ms DESC,a.request_id DESC LIMIT 51`, args...)
|
|
if e != nil {
|
|
return nil, e
|
|
}
|
|
defer rows.Close()
|
|
out := []FeedActionRecord{}
|
|
for rows.Next() {
|
|
a, e := scanFeedActionRecord(rows, writable)
|
|
if e != nil {
|
|
return nil, e
|
|
}
|
|
out = append(out, a)
|
|
}
|
|
return out, rows.Err()
|
|
}
|
|
|
|
func (s *Store) FeedActionRecord(ctx context.Context, domain, owner, request string) (FeedActionRecord, error) {
|
|
if e := feedActor(ctx, s.DB, domain, owner, false); e != nil {
|
|
return FeedActionRecord{}, e
|
|
}
|
|
if _, e := uuid.Parse(request); e != nil {
|
|
return FeedActionRecord{}, ErrTaskInvalid
|
|
}
|
|
writable := !s.ReadOnly && feedActor(ctx, s.DB, domain, owner, true) == nil
|
|
return scanFeedActionRecord(s.DB.QueryRowContext(ctx, `SELECT `+feedActionRecordColumns()+feedActionJoins+` AND a.request_id=?`, domain, owner, request), writable)
|
|
}
|