Files
pages/internal/store/feeds_history.go
T
2026-11-15 05:47:14 +09:00

119 lines
4.5 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,CASE WHEN s.enabled=1 AND s.provider='freshrss' THEN 1 ELSE 0 END`
func (s *Store) feedActionJoins() string {
return ` 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=` + s.sqlParam(1) + ` AND a.owner_uuid=` + s.sqlParam(2)
}
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) (result []FeedActionRecord, resultErr error) {
defer func() { resultErr = s.sqlError("feeds repository", resultErr) }()
ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
defer cancel()
if e := s.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=` + s.sqlParam(len(args)+1)
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<` + s.sqlParam(len(args)+1) + ` OR (a.created_ms=` + s.sqlParam(len(args)+2) + ` AND a.request_id<` + s.sqlParam(len(args)+3) + `))`
args = append(args, q.Before, q.Before, q.Key)
} else if q.Before != -1 || q.Key != "" {
return nil, ErrTaskInvalid
}
writable := !s.ReadOnly && s.feedActor(ctx, s.DB, domain, owner, true) == nil
rows, e := s.DB.QueryContext(ctx, `SELECT `+feedActionRecordColumns()+s.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) (result FeedActionRecord, resultErr error) {
defer func() { resultErr = s.sqlError("feeds repository", resultErr) }()
ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
defer cancel()
if e := s.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 && s.feedActor(ctx, s.DB, domain, owner, true) == nil
return scanFeedActionRecord(s.DB.QueryRowContext(ctx, `SELECT `+feedActionRecordColumns()+s.feedActionJoins()+` AND a.request_id=`+s.sqlParam(3), domain, owner, request), writable)
}