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

56 lines
2.3 KiB
Go

package store
import (
"context"
"database/sql"
"errors"
"github.com/google/uuid"
)
// A receipt without a live task or a deletion marker needs repair, not an
// implicit create that could silently revive an inconsistent record.
var ErrTaskCaptureMissing = errors.New("captured task is missing without a deletion record")
func TaskRecreationID(request string) string {
return uuid.NewSHA1(uuid.NameSpaceURL, []byte("pages:task-recreate:"+request)).String()
}
func (s *Store) deletedCapture(ctx context.Context, q interface {
QueryRowContext(context.Context, string, ...any) *sql.Row
}, domain, actor, request string) error {
var id string
if err := q.QueryRowContext(ctx, `SELECT item_uuid FROM organizer_capture_receipts WHERE domain_uuid=`+s.sqlParam(1)+` AND owner_uuid=`+s.sqlParam(2)+` AND request_uuid=`+s.sqlParam(3), domain, actor, request).Scan(&id); err != nil {
return err
}
var live, deleted bool
if err := q.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM organizer_items WHERE domain_uuid=`+s.sqlParam(1)+` AND uuid=`+s.sqlParam(2)+`), EXISTS(SELECT 1 FROM organizer_task_deletions WHERE domain_uuid=`+s.sqlParam(3)+` AND owner_uuid=`+s.sqlParam(4)+` AND item_uuid=`+s.sqlParam(5)+`)`, domain, id, domain, actor, id).Scan(&live, &deleted); err != nil {
return err
}
if live {
return ErrTaskConflict
}
if !deleted {
return ErrTaskCaptureMissing
}
return nil
}
// RecreateCapturedTask never removes old receipts, tombstones or DAV identity
// reservations. A deterministic successor ID makes a lost response retry safe.
// The caller authorizes read/create/write for target, including retained pages.
func (s *Store) RecreateCapturedTask(ctx context.Context, domain, actor, request, hash string, item TaskRecord, target, body, replaces string) (result TaskRecord, resultErr error) {
defer func() { resultErr = s.sqlError("task recreation", resultErr) }()
id, err := uuid.Parse(replaces)
if err != nil || id.String() != replaces || request != TaskRecreationID(replaces) {
return TaskRecord{}, ErrTaskInvalid
}
if err = s.taskActor(ctx, s.DB, domain, actor, true); err != nil {
return TaskRecord{}, err
}
if err = s.deletedCapture(ctx, s.DB, domain, actor, replaces); err != nil {
return TaskRecord{}, err
}
return s.captureTaskWithWiki(ctx, domain, actor, request, hash, item, target, body, replaces)
}