feat(tasks): add explicit deletion with DAV tombstones
Build and publish container images / images (push) Successful in 11m42s
Release container images / release (push) Successful in 27s

This commit is contained in:
Anya Tokugawa committed 2026-11-08 18:04:54 +09:00
1 parent dc978231e2
commit 38a4fcf530
15 files changed
+530 -10

No files matched your search

+117
View File
@@ -0,0 +1,117 @@
package store
import (
"context"
"database/sql"
"encoding/json"
"errors"
"time"
"github.com/google/uuid"
)
var ErrTaskDeleted = errors.New("task has been deleted")
var ErrTaskHasChildren = errors.New("move or delete subtasks first")
var ErrTaskWorking = errors.New("stop task work and focus sessions first")
// DeleteTask is deliberately separate from reversible archiving. The head and
// revision references are removed; minimal identity tombstones prevent offline
// clients from resurrecting the same task. Wiki documents, calendar events and
// completed work records are independent resources and are not deleted here.
// CAS blobs/backups are not securely erased by this operation.
func (s *Store) DeleteTask(ctx context.Context, domain, actor, id string, expected int64) error {
if s.ReadOnly {
return ErrReadOnly
}
u, err := uuid.Parse(id)
if err != nil || u.String() != id || expected < 1 {
return ErrTaskInvalid
}
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
tx, err := s.DB.BeginTx(ctx, nil)
if err != nil {
return err
}
defer tx.Rollback()
if _, err = tx.ExecContext(ctx, `UPDATE domain_memberships SET state=state WHERE domain_uuid=? AND principal_uuid=?`, domain, actor); err != nil {
return err
}
if err = taskActor(ctx, tx, domain, actor, true); err != nil {
return err
}
enabled, err := taskReminderExtensionEnabled(ctx, tx, domain)
if err != nil {
return err
}
if !enabled {
return ErrAccessDenied
}
var raw string
err = tx.QueryRowContext(ctx, `SELECT data FROM organizer_items WHERE domain_uuid=? AND owner_uuid=? AND uuid=?`, domain, actor, id).Scan(&raw)
if errors.Is(err, sql.ErrNoRows) {
// A lost successful DELETE response can be retried with its original
// precondition, without disclosing another owner's tombstone.
var version int64
if e := tx.QueryRowContext(ctx, `SELECT version FROM organizer_task_deletions WHERE domain_uuid=? AND owner_uuid=? AND item_uuid=?`, domain, actor, id).Scan(&version); e != nil {
return e
}
if version-1 != expected {
return ErrTaskConflict
}
return nil
}
if err != nil {
return err
}
var item TaskRecord
if err = json.Unmarshal([]byte(raw), &item); err != nil {
return err
}
if item.Kind != "task" {
return ErrTaskInvalid
}
if item.Version != expected {
return ErrTaskConflict
}
var children, working bool
if err = tx.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM organizer_items WHERE domain_uuid=? AND owner_uuid=? AND parent_uuid=?)`, domain, actor, id).Scan(&children); err != nil {
return err
}
if children {
return ErrTaskHasChildren
}
if err = tx.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM organizer_focus WHERE domain_uuid=? AND owner_uuid=? AND task_uuid=? AND state IN ('running','paused')) OR EXISTS(SELECT 1 FROM organizer_work_sessions WHERE domain_uuid=? AND owner_uuid=? AND task_uuid=? AND end_ms=0 AND cancelled_ms=0)`, domain, actor, id, domain, actor, id).Scan(&working); err != nil {
return err
}
if working {
return ErrTaskWorking
}
name, uid := id+".ics", id
if item.CalDAV != nil {
name, uid = item.CalDAV.Name, item.CalDAV.UID
}
now := time.Now().UnixMilli()
_, err = tx.ExecContext(ctx, `INSERT INTO organizer_task_deletions(domain_uuid,owner_uuid,item_uuid,dav_name,dav_uid,version,deleted_at_ms) VALUES(?,?,?,?,?,?,?)`, domain, actor, id, taskDAVIndexKey(item.ProjectUUID, name), taskDAVIndexKey(item.ProjectUUID, uid), item.Version+1, now)
if err != nil {
return err
}
for _, q := range []string{
`DELETE FROM organizer_reminders WHERE domain_uuid=? AND item_uuid=?`,
`DELETE FROM organizer_task_metrics WHERE domain_uuid=? AND task_uuid=?`,
`UPDATE organizer_time_blocks SET state='unlinked' WHERE domain_uuid=? AND task_uuid=?`,
`DELETE FROM organizer_revisions WHERE domain_uuid=? AND item_uuid=?`,
`DELETE FROM organizer_items WHERE domain_uuid=? AND uuid=?`,
} {
if _, err = tx.ExecContext(ctx, q, domain, id); err != nil {
return err
}
}
if _, err = tx.ExecContext(ctx, `INSERT INTO organizer_changes(domain_uuid,item_uuid,version,actor_uuid,created_at_ms) VALUES(?,?,?,?,?)`, domain, id, item.Version+1, actor, now); err != nil {
return err
}
if _, err = s.appendTaskActivityTx(tx, domain, actor, id, "task", item.Version+1, now); err != nil {
return err
}
return tx.Commit()
}