204 lines
9.5 KiB
Go
204 lines
9.5 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
type FeedAction struct {
|
|
Domain string `json:"-"`
|
|
Owner string `json:"-"`
|
|
RequestID string `json:"request_id"`
|
|
Source string `json:"source"`
|
|
ID string `json:"id"`
|
|
Field string `json:"field"`
|
|
Desired bool `json:"desired"`
|
|
SourceVersion int64 `json:"source_version"`
|
|
Status string `json:"status"`
|
|
Claim string `json:"-"`
|
|
UntilMS int64 `json:"-"`
|
|
CreatedMS int64 `json:"created_ms"`
|
|
Error string `json:"error"`
|
|
}
|
|
|
|
const feedActionColumns = `domain_uuid,owner_uuid,request_id,source_uuid,item_id,field,desired,source_version,status,claim,until_ms,created_ms,error`
|
|
|
|
func scanFeedAction(row interface{ Scan(...any) error }) (a FeedAction, e error) {
|
|
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)
|
|
return
|
|
}
|
|
func (s *Store) FeedAction(ctx context.Context, domain, owner, source, id string) (*FeedAction, error) {
|
|
if e := feedActor(ctx, s.DB, domain, owner, false); e != nil {
|
|
return nil, e
|
|
}
|
|
a, e := scanFeedAction(s.DB.QueryRowContext(ctx, `SELECT `+feedActionColumns+` FROM feed_actions WHERE domain_uuid=? AND owner_uuid=? AND source_uuid=? AND item_id=? ORDER BY created_ms DESC,rowid DESC LIMIT 1`, domain, owner, source, id))
|
|
if errors.Is(e, sql.ErrNoRows) {
|
|
return nil, nil
|
|
}
|
|
return &a, e
|
|
}
|
|
func (s *Store) QueueFeedAction(ctx context.Context, domain, owner string, a FeedAction) (FeedAction, error) {
|
|
if _, e := uuid.Parse(a.RequestID); e != nil {
|
|
return a, ErrTaskInvalid
|
|
}
|
|
if (a.Field != "read" && a.Field != "starred") || a.ID == "" || len(a.ID) > 1024 {
|
|
return a, ErrTaskInvalid
|
|
}
|
|
a.Domain, a.Owner = domain, owner
|
|
a.Error, a.Claim, a.UntilMS = "", "", 0
|
|
e := s.feedWrite(ctx, domain, owner, func(tx *sql.Tx) error {
|
|
old, e := scanFeedAction(tx.QueryRowContext(ctx, `SELECT `+feedActionColumns+` FROM feed_actions WHERE domain_uuid=? AND owner_uuid=? AND request_id=?`, domain, owner, a.RequestID))
|
|
if e == nil {
|
|
if old.Source != a.Source || old.ID != a.ID || old.Field != a.Field || old.Desired != a.Desired || old.SourceVersion != a.SourceVersion {
|
|
return ErrTaskConflict
|
|
}
|
|
a = old
|
|
return nil
|
|
}
|
|
if !errors.Is(e, sql.ErrNoRows) {
|
|
return e
|
|
}
|
|
var allowed bool
|
|
if e = tx.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM feed_sources s JOIN feed_items i ON i.source_uuid=s.uuid WHERE s.domain_uuid=? AND s.owner_uuid=? AND s.uuid=? AND i.id=? AND s.provider='freshrss' AND s.enabled=1 AND s.write_enabled=1 AND s.version=?)`, domain, owner, a.Source, a.ID, a.SourceVersion).Scan(&allowed); e != nil {
|
|
return e
|
|
}
|
|
if !allowed {
|
|
return ErrTaskConflict
|
|
}
|
|
var total, pending int
|
|
if e = tx.QueryRowContext(ctx, `SELECT count(*),COALESCE(sum(status IN ('queued','preparing','sending','unknown','check_queued','checking')),0) FROM feed_actions WHERE domain_uuid=? AND owner_uuid=?`, domain, owner).Scan(&total, &pending); e != nil {
|
|
return e
|
|
}
|
|
if total >= 10000 || pending >= 20 {
|
|
return ErrTaskDAVLimit
|
|
}
|
|
if e = tx.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM feed_actions WHERE source_uuid=? AND item_id=? AND status IN ('queued','preparing','sending','unknown','check_queued','checking'))`, a.Source, a.ID).Scan(&allowed); e != nil {
|
|
return e
|
|
}
|
|
if allowed {
|
|
return ErrTaskConflict
|
|
}
|
|
a.Status, a.CreatedMS = "queued", time.Now().UnixMilli()
|
|
_, e = tx.ExecContext(ctx, `INSERT INTO feed_actions(domain_uuid,owner_uuid,request_id,source_uuid,item_id,field,desired,source_version,status,created_ms) VALUES(?,?,?,?,?,?,?,?,?,?)`, domain, owner, a.RequestID, a.Source, a.ID, a.Field, a.Desired, a.SourceVersion, a.Status, a.CreatedMS)
|
|
return e
|
|
})
|
|
return a, e
|
|
}
|
|
|
|
// Recheck is deliberately read-only at the remote side, even with writeback OFF.
|
|
func (s *Store) RecheckFeedAction(ctx context.Context, domain, owner, request string) error {
|
|
return s.feedWrite(ctx, domain, owner, func(tx *sql.Tx) error {
|
|
r, e := tx.ExecContext(ctx, `UPDATE feed_actions SET status='check_queued',error='' WHERE domain_uuid=? AND owner_uuid=? AND request_id=? AND status='unknown' AND until_ms<? AND EXISTS(SELECT 1 FROM feed_sources s WHERE s.uuid=feed_actions.source_uuid AND s.enabled=1)`, domain, owner, request, time.Now().UnixMilli())
|
|
return feedActionAffected(r, e)
|
|
})
|
|
}
|
|
func feedActionAffected(r sql.Result, e error) error {
|
|
if e != nil {
|
|
return e
|
|
}
|
|
n, e := r.RowsAffected()
|
|
if e != nil {
|
|
return e
|
|
}
|
|
if n != 1 {
|
|
return ErrTaskConflict
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *Store) DueFeedActions(ctx context.Context) ([]FeedAction, error) {
|
|
if s.ReadOnly {
|
|
return nil, ErrReadOnly
|
|
}
|
|
now := time.Now().UnixMilli()
|
|
// Recovery changes only our receipts, never retries an uncertain remote write.
|
|
if _, e := s.DB.ExecContext(ctx, `UPDATE feed_actions SET status=CASE WHEN status IN ('sending','checking') THEN 'unknown' ELSE 'failed' END,error='expired' WHERE (status IN ('preparing','sending','checking') AND until_ms<?) OR (status='queued' AND created_ms<?)`, now, now-300000); e != nil {
|
|
return nil, e
|
|
}
|
|
rows, e := s.DB.QueryContext(ctx, `SELECT `+feedActionColumns+` FROM feed_actions WHERE status IN ('queued','check_queued') AND EXISTS(SELECT 1 FROM feed_sources s JOIN principals p ON p.uuid=s.owner_uuid JOIN domain_memberships m ON m.principal_uuid=p.uuid AND m.domain_uuid=s.domain_uuid JOIN domains d ON d.uuid=s.domain_uuid WHERE s.uuid=feed_actions.source_uuid AND s.enabled=1 AND s.lease_until<? AND p.state='active' AND m.state='active' AND d.enabled=1 AND COALESCE(d.placement,'local')='local' AND NOT EXISTS(SELECT 1 FROM config c WHERE c.domain_uuid=s.domain_uuid AND c.key='system_extension_states' AND COALESCE(json_extract(CASE WHEN json_valid(c.value) THEN c.value ELSE '{"organizer.feeds":false}' END,'$."organizer.feeds"'),1)=0)) ORDER BY created_ms LIMIT 2`, now)
|
|
if e != nil {
|
|
return nil, e
|
|
}
|
|
defer rows.Close()
|
|
out := []FeedAction{}
|
|
for rows.Next() {
|
|
a, e := scanFeedAction(rows)
|
|
if e != nil {
|
|
return nil, e
|
|
}
|
|
out = append(out, a)
|
|
}
|
|
return out, rows.Err()
|
|
}
|
|
func (s *Store) ClaimFeedAction(ctx context.Context, a FeedAction) (FeedAction, FeedSource, error) {
|
|
var source FeedSource
|
|
e := s.feedWrite(ctx, a.Domain, a.Owner, func(tx *sql.Tx) error {
|
|
var e error
|
|
source, e = scanFeedSource(tx.QueryRowContext(ctx, `SELECT `+feedSourceColumns+` FROM feed_sources WHERE uuid=? AND domain_uuid=? AND owner_uuid=?`, a.Source, a.Domain, a.Owner))
|
|
if e != nil {
|
|
return e
|
|
}
|
|
check := a.Status == "check_queued"
|
|
if !source.Enabled || source.Provider != "freshrss" || (!check && (!source.WriteEnabled || source.Version != a.SourceVersion)) {
|
|
return ErrTaskConflict
|
|
}
|
|
now := time.Now().UnixMilli()
|
|
a.Claim = uuid.NewString()
|
|
a.UntilMS = now + 60000
|
|
state := "preparing"
|
|
if check {
|
|
state = "checking"
|
|
}
|
|
r, e := tx.ExecContext(ctx, `UPDATE feed_sources SET lease=?,lease_until=? WHERE uuid=? AND lease_until<?`, a.Claim, a.UntilMS, a.Source, now)
|
|
if e = feedActionAffected(r, e); e != nil {
|
|
return e
|
|
}
|
|
r, e = tx.ExecContext(ctx, `UPDATE feed_actions SET status=?,claim=?,until_ms=? WHERE domain_uuid=? AND owner_uuid=? AND request_id=? AND status=? AND (status='check_queued' OR created_ms>?)`, state, a.Claim, a.UntilMS, a.Domain, a.Owner, a.RequestID, a.Status, now-300000)
|
|
if e = feedActionAffected(r, e); e != nil {
|
|
return e
|
|
}
|
|
a.Status = state
|
|
source.Lease = a.Claim
|
|
source.LeaseUntil = a.UntilMS
|
|
return nil
|
|
})
|
|
return a, source, e
|
|
}
|
|
func (s *Store) MarkFeedActionSending(ctx context.Context, a FeedAction, source FeedSource) error {
|
|
return s.feedWrite(ctx, a.Domain, a.Owner, func(tx *sql.Tx) error {
|
|
r, e := tx.ExecContext(ctx, `UPDATE feed_actions SET status='sending' WHERE domain_uuid=? AND owner_uuid=? AND request_id=? AND claim=? AND status='preparing' AND until_ms>? AND EXISTS(SELECT 1 FROM feed_sources WHERE uuid=? AND enabled=1 AND write_enabled=1 AND version=? AND lease=?)`, a.Domain, a.Owner, a.RequestID, a.Claim, time.Now().UnixMilli(), a.Source, source.Version, a.Claim)
|
|
return feedActionAffected(r, e)
|
|
})
|
|
}
|
|
func (s *Store) CompleteFeedAction(ctx context.Context, a FeedAction, source FeedSource, status, code string, observed *FeedItem) error {
|
|
if (observed != nil && observed.ID != a.ID) || ((status == "confirmed" || status == "conflict") && observed == nil) {
|
|
return ErrTaskInvalid
|
|
}
|
|
if status != "confirmed" && status != "unknown" && status != "failed" && status != "conflict" {
|
|
return ErrTaskInvalid
|
|
}
|
|
switch code {
|
|
case "", "network", "credentials", "invalid_feed", "remote_error", "conflict", "limit":
|
|
default:
|
|
return ErrTaskInvalid
|
|
}
|
|
return s.feedWrite(ctx, a.Domain, a.Owner, func(tx *sql.Tx) error {
|
|
r, e := tx.ExecContext(ctx, `UPDATE feed_sources SET lease='',lease_until=0 WHERE uuid=? AND version=? AND lease=? AND lease_until>? AND enabled=1`, a.Source, source.Version, a.Claim, time.Now().UnixMilli())
|
|
if e = feedActionAffected(r, e); e != nil {
|
|
return e
|
|
}
|
|
r, e = tx.ExecContext(ctx, `UPDATE feed_actions SET status=?,error=?,until_ms=CASE WHEN ?='unknown' THEN until_ms ELSE 0 END WHERE domain_uuid=? AND owner_uuid=? AND request_id=? AND claim=? AND status IN ('preparing','sending','checking')`, status, code, status, a.Domain, a.Owner, a.RequestID, a.Claim)
|
|
if e = feedActionAffected(r, e); e != nil {
|
|
return e
|
|
}
|
|
if observed != nil {
|
|
_, e = tx.ExecContext(ctx, `UPDATE feed_items SET is_read=?,starred=? WHERE source_uuid=? AND id=?`, observed.Read, observed.Starred, a.Source, a.ID)
|
|
}
|
|
return e
|
|
})
|
|
}
|