383 lines
14 KiB
Go
383 lines
14 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"errors"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
const TaskDigestRetentionDays = 90
|
|
|
|
type TaskDigestAttempt struct {
|
|
UUID string `json:"uuid"`
|
|
Day string `json:"day"`
|
|
Kind string `json:"kind"`
|
|
State string `json:"state"`
|
|
Reason string `json:"reason"`
|
|
Timezone string `json:"timezone"`
|
|
CreatedMS int64 `json:"created_ms"`
|
|
UpdatedMS int64 `json:"updated_ms"`
|
|
}
|
|
type TaskDigestRetry struct {
|
|
RequestID string `json:"request_id"`
|
|
Day string `json:"day"`
|
|
ExpectedAttempt string `json:"expected_attempt"`
|
|
DuplicateAcknowledged bool `json:"duplicate_acknowledged"`
|
|
}
|
|
type TaskDigestHistory struct {
|
|
Items []TaskDigestAttempt `json:"items"`
|
|
Next string `json:"next"`
|
|
Today string `json:"today"`
|
|
Current *TaskDigestAttempt `json:"current"`
|
|
Retries int `json:"retries"`
|
|
Enabled bool `json:"enabled"`
|
|
RetentionDays int `json:"retention_days"`
|
|
}
|
|
|
|
const digestAttemptColumns = `uuid,day,kind,state,reason,timezone,created_ms,updated_ms`
|
|
|
|
func scanDigestAttempt(row interface{ Scan(...any) error }) (TaskDigestAttempt, error) {
|
|
var v TaskDigestAttempt
|
|
err := row.Scan(&v.UUID, &v.Day, &v.Kind, &v.State, &v.Reason, &v.Timezone, &v.CreatedMS, &v.UpdatedMS)
|
|
return v, err
|
|
}
|
|
|
|
func insertDigestAttempt(ctx context.Context, tx *sql.Tx, c TaskDigestClaim, kind, state string, now time.Time) error {
|
|
_, err := tx.ExecContext(ctx, `INSERT INTO organizer_digest_attempts(uuid,domain_uuid,owner_uuid,day,kind,state,preference_version,timezone,created_ms,updated_ms) VALUES(?,?,?,?,?,?,?,?,?,?)`, c.AttemptUUID, c.Domain, c.Owner, c.Day, kind, state, c.Version, c.Timezone, now.UnixMilli(), now.UnixMilli())
|
|
return err
|
|
}
|
|
|
|
// Receipt lookup is read-only. It lets clients confirm an accepted request even
|
|
// when SMTP is subsequently disabled; it never reactivates a delivery.
|
|
func (s *Store) TaskDigestRetryReceipt(ctx context.Context, domain, actor string, input TaskDigestRetry) (string, error) {
|
|
if err := taskActor(ctx, s.DB, domain, actor, false); err != nil {
|
|
return "", err
|
|
}
|
|
id, err := uuid.Parse(input.RequestID)
|
|
if err != nil || id.String() != input.RequestID {
|
|
return "", ErrTaskInvalid
|
|
}
|
|
var day, expected string
|
|
var ack bool
|
|
err = s.DB.QueryRowContext(ctx, `SELECT day,expected_attempt,duplicate_ack FROM organizer_digest_requests WHERE uuid=? AND domain_uuid=? AND owner_uuid=?`, input.RequestID, domain, actor).Scan(&day, &expected, &ack)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if day != input.Day || expected != input.ExpectedAttempt || ack != input.DuplicateAcknowledged {
|
|
return "", ErrTaskConflict
|
|
}
|
|
return input.RequestID, nil
|
|
}
|
|
|
|
// A retry is durable queue admission, never an SMTP send in a browser request.
|
|
// Keep request receipts and daily reservations even after detail retention.
|
|
func (s *Store) QueueTaskDigestRetry(ctx context.Context, domain, actor string, input TaskDigestRetry, now time.Time) (string, error) {
|
|
if s.ReadOnly {
|
|
return "", ErrReadOnly
|
|
}
|
|
id, err := uuid.Parse(input.RequestID)
|
|
if err != nil || id.String() != input.RequestID {
|
|
return "", ErrTaskInvalid
|
|
}
|
|
tx, err := s.DB.BeginTx(ctx, nil)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
defer tx.Rollback()
|
|
if err = taskProductivityWriter(ctx, tx, domain, actor); err != nil {
|
|
return "", err
|
|
}
|
|
var oldDomain, oldOwner, oldDay, oldExpected string
|
|
var oldAck bool
|
|
err = tx.QueryRowContext(ctx, `SELECT domain_uuid,owner_uuid,day,expected_attempt,duplicate_ack FROM organizer_digest_requests WHERE uuid=?`, input.RequestID).Scan(&oldDomain, &oldOwner, &oldDay, &oldExpected, &oldAck)
|
|
if err == nil {
|
|
if oldDomain != domain || oldOwner != actor || oldDay != input.Day || oldExpected != input.ExpectedAttempt || oldAck != input.DuplicateAcknowledged {
|
|
return "", ErrTaskConflict
|
|
}
|
|
return input.RequestID, tx.Commit()
|
|
}
|
|
if !errors.Is(err, sql.ErrNoRows) {
|
|
return "", err
|
|
}
|
|
var raw string
|
|
if err = tx.QueryRowContext(ctx, `SELECT data FROM organizer_task_preferences WHERE domain_uuid=? AND owner_uuid=?`, domain, actor).Scan(&raw); err != nil {
|
|
return "", err
|
|
}
|
|
var p TaskPreferences
|
|
if err = json.Unmarshal([]byte(raw), &p); err != nil {
|
|
return "", err
|
|
}
|
|
loc, err := time.LoadLocation(p.Timezone)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if !p.DigestEnabled {
|
|
return "", ErrAccessDenied
|
|
}
|
|
if input.Day != now.In(loc).Format("2006-01-02") {
|
|
return "", ErrTaskInvalid
|
|
}
|
|
var state, current string
|
|
if err = tx.QueryRowContext(ctx, `SELECT state,attempt_uuid FROM organizer_digest_deliveries WHERE domain_uuid=? AND owner_uuid=? AND day=?`, domain, actor, input.Day).Scan(&state, ¤t); err != nil {
|
|
return "", err
|
|
}
|
|
if current != input.ExpectedAttempt || state == "queued" || state == "sending" {
|
|
return "", ErrTaskConflict
|
|
}
|
|
if state != "sent" && state != "uncertain" && state != "cancelled" {
|
|
return "", ErrTaskConflict
|
|
}
|
|
if (state == "sent" || state == "uncertain") && !input.DuplicateAcknowledged {
|
|
return "", ErrTaskInvalid
|
|
}
|
|
var count int
|
|
if err = tx.QueryRowContext(ctx, `SELECT count(*) FROM organizer_digest_requests WHERE domain_uuid=? AND owner_uuid=? AND day=?`, domain, actor, input.Day).Scan(&count); err != nil {
|
|
return "", err
|
|
}
|
|
if count >= 5 {
|
|
return "", ErrTaskConflict
|
|
}
|
|
_, err = tx.ExecContext(ctx, `INSERT INTO organizer_digest_requests VALUES(?,?,?,?,?,?,?)`, input.RequestID, domain, actor, input.Day, input.ExpectedAttempt, input.DuplicateAcknowledged, now.UnixMilli())
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
c := TaskDigestClaim{TaskDigestDue: TaskDigestDue{domain, actor}, Day: input.Day, Version: p.Version, Timezone: p.Timezone, AttemptUUID: input.RequestID}
|
|
if err = insertDigestAttempt(ctx, tx, c, "manual", "queued", now); err != nil {
|
|
return "", err
|
|
}
|
|
if _, err = tx.ExecContext(ctx, `UPDATE organizer_digest_deliveries SET state='queued',attempt_uuid=?,updated_ms=? WHERE domain_uuid=? AND owner_uuid=? AND day=?`, c.AttemptUUID, now.UnixMilli(), domain, actor, input.Day); err != nil {
|
|
return "", err
|
|
}
|
|
return c.AttemptUUID, tx.Commit()
|
|
}
|
|
|
|
func (s *Store) QueuedTaskDigests(ctx context.Context) ([]TaskDigestClaim, error) {
|
|
rows, err := s.DB.QueryContext(ctx, `SELECT uuid,domain_uuid,owner_uuid,day,preference_version,timezone FROM organizer_digest_attempts WHERE state='queued' ORDER BY updated_ms,uuid LIMIT 10`)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
out := []TaskDigestClaim{}
|
|
for rows.Next() {
|
|
var c TaskDigestClaim
|
|
if err = rows.Scan(&c.AttemptUUID, &c.Domain, &c.Owner, &c.Day, &c.Version, &c.Timezone); err != nil {
|
|
return nil, err
|
|
}
|
|
out = append(out, c)
|
|
}
|
|
return out, rows.Err()
|
|
}
|
|
|
|
func (s *Store) ClaimQueuedTaskDigest(ctx context.Context, c TaskDigestClaim, now time.Time) (TaskDigestClaim, error) {
|
|
if s.ReadOnly {
|
|
return c, ErrReadOnly
|
|
}
|
|
tx, err := s.DB.BeginTx(ctx, nil)
|
|
if err != nil {
|
|
return c, err
|
|
}
|
|
defer tx.Rollback()
|
|
res, err := tx.ExecContext(ctx, `UPDATE organizer_digest_attempts SET state='sending',updated_ms=? WHERE uuid=? AND domain_uuid=? AND owner_uuid=? AND state='queued' AND EXISTS(SELECT 1 FROM organizer_digest_deliveries d WHERE d.domain_uuid=? AND d.owner_uuid=? AND d.day=? AND d.attempt_uuid=? AND d.state='queued')`, now.UnixMilli(), c.AttemptUUID, c.Domain, c.Owner, c.Domain, c.Owner, c.Day, c.AttemptUUID)
|
|
if err != nil {
|
|
return c, err
|
|
}
|
|
n, err := res.RowsAffected()
|
|
if err != nil {
|
|
return c, err
|
|
}
|
|
if n != 1 {
|
|
return c, sql.ErrNoRows
|
|
}
|
|
// Reload all claim fields from the queue, not from an earlier enumeration.
|
|
if err = tx.QueryRowContext(ctx, `SELECT day,preference_version,timezone FROM organizer_digest_attempts WHERE uuid=?`, c.AttemptUUID).Scan(&c.Day, &c.Version, &c.Timezone); err != nil {
|
|
return c, err
|
|
}
|
|
state, reason := "sending", ""
|
|
allowed := taskActor(ctx, tx, c.Domain, c.Owner, true)
|
|
if allowed != nil && !errors.Is(allowed, ErrAccessDenied) {
|
|
return c, allowed
|
|
}
|
|
on, err := taskReminderExtensionEnabled(ctx, tx, c.Domain)
|
|
if err != nil {
|
|
return c, err
|
|
}
|
|
var raw string
|
|
err = tx.QueryRowContext(ctx, `SELECT data FROM organizer_task_preferences WHERE domain_uuid=? AND owner_uuid=?`, c.Domain, c.Owner).Scan(&raw)
|
|
if err != nil && !errors.Is(err, sql.ErrNoRows) {
|
|
return c, err
|
|
}
|
|
var p TaskPreferences
|
|
if err == nil {
|
|
if err = json.Unmarshal([]byte(raw), &p); err != nil {
|
|
return c, err
|
|
}
|
|
}
|
|
if allowed != nil || !on || !p.DigestEnabled || p.Version != c.Version {
|
|
state, reason = "cancelled", "consent_changed"
|
|
}
|
|
loc, e := time.LoadLocation(c.Timezone)
|
|
if e != nil || now.In(loc).Format("2006-01-02") != c.Day {
|
|
state, reason = "cancelled", "day_expired"
|
|
}
|
|
if _, err = tx.ExecContext(ctx, `UPDATE organizer_digest_attempts SET state=?,reason=? WHERE uuid=?`, state, reason, c.AttemptUUID); err != nil {
|
|
return c, err
|
|
}
|
|
if _, err = tx.ExecContext(ctx, `UPDATE organizer_digest_deliveries SET state=?,updated_ms=? WHERE domain_uuid=? AND owner_uuid=? AND day=? AND attempt_uuid=?`, state, now.UnixMilli(), c.Domain, c.Owner, c.Day, c.AttemptUUID); err != nil {
|
|
return c, err
|
|
}
|
|
if err = tx.Commit(); err != nil {
|
|
return c, err
|
|
}
|
|
if state != "sending" {
|
|
return c, sql.ErrNoRows
|
|
}
|
|
return c, nil
|
|
}
|
|
|
|
func (s *Store) FinishTaskDigestReason(ctx context.Context, c TaskDigestClaim, state, reason string, now time.Time) error {
|
|
if s.ReadOnly {
|
|
return ErrReadOnly
|
|
}
|
|
if state != "sent" && state != "uncertain" && state != "cancelled" {
|
|
return ErrTaskInvalid
|
|
}
|
|
switch reason {
|
|
case "", "smtp_unavailable", "tasks_unavailable", "consent_changed", "transport_uncertain", "extension_disabled":
|
|
default:
|
|
return ErrTaskInvalid
|
|
}
|
|
tx, err := s.DB.BeginTx(ctx, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer tx.Rollback()
|
|
res, err := tx.ExecContext(ctx, `UPDATE organizer_digest_deliveries SET state=?,updated_ms=? WHERE domain_uuid=? AND owner_uuid=? AND day=? AND attempt_uuid=? AND state='sending'`, state, now.UnixMilli(), c.Domain, c.Owner, c.Day, c.AttemptUUID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
n, err := res.RowsAffected()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if n != 1 {
|
|
return ErrTaskConflict
|
|
}
|
|
res, err = tx.ExecContext(ctx, `UPDATE organizer_digest_attempts SET state=?,reason=?,updated_ms=? WHERE uuid=? AND state='sending'`, state, reason, now.UnixMilli(), c.AttemptUUID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
n, err = res.RowsAffected()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if n != 1 {
|
|
return ErrTaskConflict
|
|
}
|
|
return tx.Commit()
|
|
}
|
|
|
|
func (s *Store) TaskDigestHistory(ctx context.Context, domain, actor, after string, limit int, now time.Time) (TaskDigestHistory, error) {
|
|
out := TaskDigestHistory{Items: []TaskDigestAttempt{}, RetentionDays: TaskDigestRetentionDays}
|
|
p, err := s.TaskPreferences(ctx, domain, actor)
|
|
if err != nil {
|
|
return out, err
|
|
}
|
|
loc, err := time.LoadLocation(p.Timezone)
|
|
if err != nil {
|
|
return out, err
|
|
}
|
|
out.Today = now.In(loc).Format("2006-01-02")
|
|
out.Enabled = p.DigestEnabled
|
|
if limit < 1 || limit > 100 {
|
|
return out, ErrTaskInvalid
|
|
}
|
|
query := `SELECT ` + digestAttemptColumns + ` FROM organizer_digest_attempts WHERE domain_uuid=? AND owner_uuid=?`
|
|
args := []any{domain, actor}
|
|
if after != "" {
|
|
if len(after) > 256 {
|
|
return out, ErrTaskInvalid
|
|
}
|
|
var cursor struct {
|
|
MS int64
|
|
ID string
|
|
}
|
|
raw, e := base64.RawURLEncoding.DecodeString(after)
|
|
if e != nil || json.Unmarshal(raw, &cursor) != nil || cursor.MS < 0 || len(cursor.ID) > 80 || cursor.ID == "" {
|
|
return out, ErrTaskInvalid
|
|
}
|
|
query += ` AND (created_ms,uuid)<(?,?)`
|
|
args = append(args, cursor.MS, cursor.ID)
|
|
}
|
|
query += ` ORDER BY created_ms DESC,uuid DESC LIMIT ?`
|
|
args = append(args, limit+1)
|
|
rows, err := s.DB.QueryContext(ctx, query, args...)
|
|
if err != nil {
|
|
return out, err
|
|
}
|
|
for rows.Next() {
|
|
v, e := scanDigestAttempt(rows)
|
|
if e != nil {
|
|
rows.Close()
|
|
return out, e
|
|
}
|
|
out.Items = append(out.Items, v)
|
|
}
|
|
err = rows.Err()
|
|
rows.Close()
|
|
if err != nil {
|
|
return out, err
|
|
}
|
|
if len(out.Items) > limit {
|
|
out.Items = out.Items[:limit]
|
|
last := out.Items[limit-1]
|
|
raw, _ := json.Marshal(struct {
|
|
MS int64
|
|
ID string
|
|
}{last.CreatedMS, last.UUID})
|
|
out.Next = base64.RawURLEncoding.EncodeToString(raw)
|
|
}
|
|
current, e := scanDigestAttempt(s.DB.QueryRowContext(ctx, `SELECT `+digestAttemptColumns+` FROM organizer_digest_attempts WHERE uuid=(SELECT attempt_uuid FROM organizer_digest_deliveries WHERE domain_uuid=? AND owner_uuid=? AND day=?)`, domain, actor, out.Today))
|
|
if e == nil {
|
|
out.Current = ¤t
|
|
} else if !errors.Is(e, sql.ErrNoRows) {
|
|
return out, e
|
|
}
|
|
err = s.DB.QueryRowContext(ctx, `SELECT count(*) FROM organizer_digest_requests WHERE domain_uuid=? AND owner_uuid=? AND day=?`, domain, actor, out.Today).Scan(&out.Retries)
|
|
return out, err
|
|
}
|
|
|
|
// Only detailed terminal attempts expire. Reservations and request receipts are
|
|
// permanent deduplication state; retention must never delete them.
|
|
func (s *Store) MaintainTaskDigests(ctx context.Context, now time.Time) (int64, error) {
|
|
if s.ReadOnly {
|
|
return 0, ErrReadOnly
|
|
}
|
|
tx, err := s.DB.BeginTx(ctx, nil)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
defer tx.Rollback()
|
|
_, err = tx.ExecContext(ctx, `UPDATE organizer_digest_attempts SET state='uncertain',reason='worker_interrupted',updated_ms=? WHERE uuid IN (SELECT uuid FROM organizer_digest_attempts WHERE state='sending' AND updated_ms<? ORDER BY updated_ms,uuid LIMIT 100)`, now.UnixMilli(), now.Add(-10*time.Minute).UnixMilli())
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
_, err = tx.ExecContext(ctx, `UPDATE organizer_digest_deliveries SET state='uncertain',updated_ms=? WHERE state='sending' AND attempt_uuid IN (SELECT uuid FROM organizer_digest_attempts WHERE state='uncertain' AND reason='worker_interrupted' AND updated_ms=?)`, now.UnixMilli(), now.UnixMilli())
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
res, err := tx.ExecContext(ctx, `DELETE FROM organizer_digest_attempts WHERE uuid IN (SELECT uuid FROM organizer_digest_attempts WHERE state IN ('sent','uncertain','cancelled') AND updated_ms<? ORDER BY updated_ms,uuid LIMIT 500)`, now.Add(-TaskDigestRetentionDays*24*time.Hour).UnixMilli())
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
n, err := res.RowsAffected()
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return n, tx.Commit()
|
|
}
|