feat: add annotations, undo system, and schema updates
Add annotations as JSON column on tasks table with Annotate/Denotate methods and CLI commands. Add undo system backed by change_log with lightweight undo_stack table (capped at 10 entries). All mutating CLI commands (add, done, delete, modify, start, stop) now record undo entries. Undo restores prior task state from change_log data. Schema changes (in v1 migration): - annotations TEXT column on tasks - undo_stack table - annotations field in change_log triggers Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
package engine
|
||||
|
||||
import "fmt"
|
||||
|
||||
// Annotate appends a timestamped annotation to the task and saves.
|
||||
func (t *Task) Annotate(text string) error {
|
||||
annotation := Annotation{
|
||||
Timestamp: timeNow().Unix(),
|
||||
Text: text,
|
||||
}
|
||||
t.Annotations = append(t.Annotations, annotation)
|
||||
return t.Save()
|
||||
}
|
||||
|
||||
// Denotate removes the most recent annotation from the task and saves.
|
||||
// Returns the removed annotation, or an error if there are none.
|
||||
func (t *Task) Denotate() (*Annotation, error) {
|
||||
if len(t.Annotations) == 0 {
|
||||
return nil, fmt.Errorf("task has no annotations")
|
||||
}
|
||||
removed := t.Annotations[len(t.Annotations)-1]
|
||||
t.Annotations = t.Annotations[:len(t.Annotations)-1]
|
||||
if err := t.Save(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &removed, nil
|
||||
}
|
||||
@@ -112,7 +112,8 @@ func runMigrations() error {
|
||||
|
||||
recurrence_duration INTEGER,
|
||||
parent_uuid TEXT,
|
||||
|
||||
annotations TEXT DEFAULT NULL,
|
||||
|
||||
FOREIGN KEY (parent_uuid) REFERENCES tasks(uuid) ON DELETE CASCADE
|
||||
);
|
||||
|
||||
@@ -209,6 +210,15 @@ func runMigrations() error {
|
||||
CREATE INDEX idx_refresh_tokens_hash ON refresh_tokens(token_hash);
|
||||
CREATE INDEX idx_refresh_tokens_user ON refresh_tokens(user_id);
|
||||
|
||||
-- Undo stack (local-only, references change_log entries)
|
||||
CREATE TABLE undo_stack (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
created_at INTEGER NOT NULL,
|
||||
op_type TEXT NOT NULL,
|
||||
task_uuid TEXT NOT NULL,
|
||||
change_log_id INTEGER NOT NULL
|
||||
);
|
||||
|
||||
-- Triggers to populate change_log
|
||||
CREATE TRIGGER track_task_create AFTER INSERT ON tasks
|
||||
BEGIN
|
||||
@@ -241,10 +251,11 @@ func runMigrations() error {
|
||||
CASE WHEN NEW.until_date IS NOT NULL THEN 'until: ' || NEW.until_date || CHAR(10) ELSE '' END ||
|
||||
CASE WHEN NEW.recurrence_duration IS NOT NULL THEN 'recurrence: ' || NEW.recurrence_duration || CHAR(10) ELSE '' END ||
|
||||
CASE WHEN NEW.parent_uuid IS NOT NULL THEN 'parent_uuid: ' || NEW.parent_uuid || CHAR(10) ELSE '' END ||
|
||||
(SELECT CASE WHEN COUNT(*) > 0
|
||||
CASE WHEN NEW.annotations IS NOT NULL THEN 'annotations: ' || NEW.annotations || CHAR(10) ELSE '' END ||
|
||||
(SELECT CASE WHEN COUNT(*) > 0
|
||||
THEN 'tags: ' || GROUP_CONCAT(tag, ',') || CHAR(10)
|
||||
ELSE ''
|
||||
END
|
||||
ELSE ''
|
||||
END
|
||||
FROM (SELECT tag FROM tags WHERE task_id = NEW.id ORDER BY tag))
|
||||
);
|
||||
END;
|
||||
@@ -280,10 +291,11 @@ func runMigrations() error {
|
||||
CASE WHEN NEW.until_date IS NOT NULL THEN 'until: ' || NEW.until_date || CHAR(10) ELSE '' END ||
|
||||
CASE WHEN NEW.recurrence_duration IS NOT NULL THEN 'recurrence: ' || NEW.recurrence_duration || CHAR(10) ELSE '' END ||
|
||||
CASE WHEN NEW.parent_uuid IS NOT NULL THEN 'parent_uuid: ' || NEW.parent_uuid || CHAR(10) ELSE '' END ||
|
||||
(SELECT CASE WHEN COUNT(*) > 0
|
||||
CASE WHEN NEW.annotations IS NOT NULL THEN 'annotations: ' || NEW.annotations || CHAR(10) ELSE '' END ||
|
||||
(SELECT CASE WHEN COUNT(*) > 0
|
||||
THEN 'tags: ' || GROUP_CONCAT(tag, ',') || CHAR(10)
|
||||
ELSE ''
|
||||
END
|
||||
ELSE ''
|
||||
END
|
||||
FROM (SELECT tag FROM tags WHERE task_id = NEW.id ORDER BY tag))
|
||||
);
|
||||
END;
|
||||
|
||||
@@ -175,6 +175,18 @@ func FormatTaskDetail(task *Task) string {
|
||||
t.AppendRow(table.Row{"Tags", formatTags(task.Tags)})
|
||||
}
|
||||
|
||||
if len(task.Annotations) > 0 {
|
||||
t.AppendSeparator()
|
||||
for i, ann := range task.Annotations {
|
||||
label := ""
|
||||
if i == 0 {
|
||||
label = "Annotations"
|
||||
}
|
||||
ts := time.Unix(ann.Timestamp, 0).Format("2006-01-02 15:04")
|
||||
t.AppendRow(table.Row{label, fmt.Sprintf("%s %s", ts, ann.Text)})
|
||||
}
|
||||
}
|
||||
|
||||
return t.Render()
|
||||
}
|
||||
|
||||
|
||||
@@ -44,6 +44,12 @@ const (
|
||||
PriorityHigh Priority = 3
|
||||
)
|
||||
|
||||
// Annotation represents a timestamped note on a task
|
||||
type Annotation struct {
|
||||
Timestamp int64 `json:"timestamp"`
|
||||
Text string `json:"text"`
|
||||
}
|
||||
|
||||
type Task struct {
|
||||
// Identity
|
||||
UUID uuid.UUID `json:"uuid"`
|
||||
@@ -69,6 +75,9 @@ type Task struct {
|
||||
RecurrenceDuration *time.Duration `json:"recurrence_duration,omitempty"`
|
||||
ParentUUID *uuid.UUID `json:"parent_uuid,omitempty"`
|
||||
|
||||
// Annotations (stored as JSON in DB)
|
||||
Annotations []Annotation `json:"annotations,omitempty"`
|
||||
|
||||
// Derived fields (not stored in DB)
|
||||
Tags []string `json:"tags"`
|
||||
Urgency float64 `json:"urgency"`
|
||||
@@ -77,24 +86,25 @@ type Task struct {
|
||||
// MarshalJSON emits Task with unix timestamps (int64) instead of RFC3339 strings.
|
||||
func (t Task) MarshalJSON() ([]byte, error) {
|
||||
type taskJSON struct {
|
||||
UUID uuid.UUID `json:"uuid"`
|
||||
ID int `json:"id"`
|
||||
Status Status `json:"status"`
|
||||
Description string `json:"description"`
|
||||
Project *string `json:"project"`
|
||||
Priority Priority `json:"priority"`
|
||||
Created int64 `json:"created"`
|
||||
Modified int64 `json:"modified"`
|
||||
Start *int64 `json:"start,omitempty"`
|
||||
End *int64 `json:"end,omitempty"`
|
||||
Due *int64 `json:"due,omitempty"`
|
||||
Scheduled *int64 `json:"scheduled,omitempty"`
|
||||
Wait *int64 `json:"wait,omitempty"`
|
||||
Until *int64 `json:"until,omitempty"`
|
||||
RecurrenceDuration *int64 `json:"recurrence_duration,omitempty"`
|
||||
ParentUUID *uuid.UUID `json:"parent_uuid,omitempty"`
|
||||
Tags []string `json:"tags"`
|
||||
Urgency float64 `json:"urgency"`
|
||||
UUID uuid.UUID `json:"uuid"`
|
||||
ID int `json:"id"`
|
||||
Status Status `json:"status"`
|
||||
Description string `json:"description"`
|
||||
Project *string `json:"project"`
|
||||
Priority Priority `json:"priority"`
|
||||
Created int64 `json:"created"`
|
||||
Modified int64 `json:"modified"`
|
||||
Start *int64 `json:"start,omitempty"`
|
||||
End *int64 `json:"end,omitempty"`
|
||||
Due *int64 `json:"due,omitempty"`
|
||||
Scheduled *int64 `json:"scheduled,omitempty"`
|
||||
Wait *int64 `json:"wait,omitempty"`
|
||||
Until *int64 `json:"until,omitempty"`
|
||||
RecurrenceDuration *int64 `json:"recurrence_duration,omitempty"`
|
||||
ParentUUID *uuid.UUID `json:"parent_uuid,omitempty"`
|
||||
Annotations []Annotation `json:"annotations,omitempty"`
|
||||
Tags []string `json:"tags"`
|
||||
Urgency float64 `json:"urgency"`
|
||||
}
|
||||
|
||||
toUnix := func(tp *time.Time) *int64 {
|
||||
@@ -128,6 +138,7 @@ func (t Task) MarshalJSON() ([]byte, error) {
|
||||
Until: toUnix(t.Until),
|
||||
RecurrenceDuration: recurDur,
|
||||
ParentUUID: t.ParentUUID,
|
||||
Annotations: t.Annotations,
|
||||
Tags: t.Tags,
|
||||
Urgency: t.Urgency,
|
||||
})
|
||||
@@ -136,24 +147,25 @@ func (t Task) MarshalJSON() ([]byte, error) {
|
||||
// UnmarshalJSON parses Task from JSON with unix timestamps (int64) and duration in seconds.
|
||||
func (t *Task) UnmarshalJSON(data []byte) error {
|
||||
type taskJSON struct {
|
||||
UUID uuid.UUID `json:"uuid"`
|
||||
ID int `json:"id"`
|
||||
Status Status `json:"status"`
|
||||
Description string `json:"description"`
|
||||
Project *string `json:"project"`
|
||||
Priority Priority `json:"priority"`
|
||||
Created int64 `json:"created"`
|
||||
Modified int64 `json:"modified"`
|
||||
Start *int64 `json:"start,omitempty"`
|
||||
End *int64 `json:"end,omitempty"`
|
||||
Due *int64 `json:"due,omitempty"`
|
||||
Scheduled *int64 `json:"scheduled,omitempty"`
|
||||
Wait *int64 `json:"wait,omitempty"`
|
||||
Until *int64 `json:"until,omitempty"`
|
||||
RecurrenceDuration *int64 `json:"recurrence_duration,omitempty"`
|
||||
ParentUUID *uuid.UUID `json:"parent_uuid,omitempty"`
|
||||
Tags []string `json:"tags"`
|
||||
Urgency float64 `json:"urgency"`
|
||||
UUID uuid.UUID `json:"uuid"`
|
||||
ID int `json:"id"`
|
||||
Status Status `json:"status"`
|
||||
Description string `json:"description"`
|
||||
Project *string `json:"project"`
|
||||
Priority Priority `json:"priority"`
|
||||
Created int64 `json:"created"`
|
||||
Modified int64 `json:"modified"`
|
||||
Start *int64 `json:"start,omitempty"`
|
||||
End *int64 `json:"end,omitempty"`
|
||||
Due *int64 `json:"due,omitempty"`
|
||||
Scheduled *int64 `json:"scheduled,omitempty"`
|
||||
Wait *int64 `json:"wait,omitempty"`
|
||||
Until *int64 `json:"until,omitempty"`
|
||||
RecurrenceDuration *int64 `json:"recurrence_duration,omitempty"`
|
||||
ParentUUID *uuid.UUID `json:"parent_uuid,omitempty"`
|
||||
Annotations []Annotation `json:"annotations,omitempty"`
|
||||
Tags []string `json:"tags"`
|
||||
Urgency float64 `json:"urgency"`
|
||||
}
|
||||
|
||||
var raw taskJSON
|
||||
@@ -184,6 +196,7 @@ func (t *Task) UnmarshalJSON(data []byte) error {
|
||||
t.Wait = fromUnix(raw.Wait)
|
||||
t.Until = fromUnix(raw.Until)
|
||||
t.ParentUUID = raw.ParentUUID
|
||||
t.Annotations = raw.Annotations
|
||||
t.Tags = raw.Tags
|
||||
t.Urgency = raw.Urgency
|
||||
|
||||
@@ -263,6 +276,32 @@ func uuidPtrToSQL(u *uuid.UUID) interface{} {
|
||||
return u.String()
|
||||
}
|
||||
|
||||
func annotationsToSQL(annotations []Annotation) interface{} {
|
||||
if len(annotations) == 0 {
|
||||
return nil
|
||||
}
|
||||
data, err := json.Marshal(annotations)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
return string(data)
|
||||
}
|
||||
|
||||
func sqlToAnnotations(v interface{}) []Annotation {
|
||||
if v == nil {
|
||||
return nil
|
||||
}
|
||||
str, ok := v.(string)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
var annotations []Annotation
|
||||
if err := json.Unmarshal([]byte(str), &annotations); err != nil {
|
||||
return nil
|
||||
}
|
||||
return annotations
|
||||
}
|
||||
|
||||
func sqlToUUIDPtr(v interface{}) *uuid.UUID {
|
||||
if v == nil {
|
||||
return nil
|
||||
@@ -337,25 +376,26 @@ func GetTask(taskUUID uuid.UUID) (*Task, error) {
|
||||
query := `
|
||||
SELECT id, uuid, status, description, project, priority,
|
||||
created, modified, start, end, due, scheduled, wait, until_date,
|
||||
recurrence_duration, parent_uuid
|
||||
recurrence_duration, parent_uuid, annotations
|
||||
FROM tasks
|
||||
WHERE uuid = ?
|
||||
`
|
||||
|
||||
task := &Task{}
|
||||
var (
|
||||
uuidStr string
|
||||
project interface{}
|
||||
created int64
|
||||
modified int64
|
||||
start interface{}
|
||||
end interface{}
|
||||
due interface{}
|
||||
scheduled interface{}
|
||||
wait interface{}
|
||||
until interface{}
|
||||
recurDuration interface{}
|
||||
parentUUIDStr interface{}
|
||||
uuidStr string
|
||||
project interface{}
|
||||
created int64
|
||||
modified int64
|
||||
start interface{}
|
||||
end interface{}
|
||||
due interface{}
|
||||
scheduled interface{}
|
||||
wait interface{}
|
||||
until interface{}
|
||||
recurDuration interface{}
|
||||
parentUUIDStr interface{}
|
||||
annotationsStr interface{}
|
||||
)
|
||||
|
||||
err := db.QueryRow(query, taskUUID.String()).Scan(
|
||||
@@ -375,6 +415,7 @@ func GetTask(taskUUID uuid.UUID) (*Task, error) {
|
||||
&until,
|
||||
&recurDuration,
|
||||
&parentUUIDStr,
|
||||
&annotationsStr,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
@@ -401,6 +442,7 @@ func GetTask(taskUUID uuid.UUID) (*Task, error) {
|
||||
task.Until = sqlToTime(until)
|
||||
task.RecurrenceDuration = sqlToDuration(recurDuration)
|
||||
task.ParentUUID = sqlToUUIDPtr(parentUUIDStr)
|
||||
task.Annotations = sqlToAnnotations(annotationsStr)
|
||||
|
||||
// Load tags
|
||||
tags, err := task.GetTags()
|
||||
@@ -429,10 +471,10 @@ func GetTasks(filter *Filter) ([]*Task, error) {
|
||||
query := fmt.Sprintf(`
|
||||
SELECT id, uuid, status, description, project, priority,
|
||||
created, modified, start, end, due, scheduled, wait, until_date,
|
||||
recurrence_duration, parent_uuid
|
||||
recurrence_duration, parent_uuid, annotations
|
||||
FROM tasks
|
||||
WHERE %s
|
||||
ORDER BY
|
||||
ORDER BY
|
||||
CASE WHEN due IS NULL THEN 1 ELSE 0 END,
|
||||
due ASC,
|
||||
priority DESC
|
||||
@@ -449,18 +491,19 @@ func GetTasks(filter *Filter) ([]*Task, error) {
|
||||
for rows.Next() {
|
||||
task := &Task{}
|
||||
var (
|
||||
uuidStr string
|
||||
project interface{}
|
||||
created int64
|
||||
modified int64
|
||||
start interface{}
|
||||
end interface{}
|
||||
due interface{}
|
||||
scheduled interface{}
|
||||
wait interface{}
|
||||
until interface{}
|
||||
recurDuration interface{}
|
||||
parentUUIDStr interface{}
|
||||
uuidStr string
|
||||
project interface{}
|
||||
created int64
|
||||
modified int64
|
||||
start interface{}
|
||||
end interface{}
|
||||
due interface{}
|
||||
scheduled interface{}
|
||||
wait interface{}
|
||||
until interface{}
|
||||
recurDuration interface{}
|
||||
parentUUIDStr interface{}
|
||||
annotationsStr interface{}
|
||||
)
|
||||
|
||||
err := rows.Scan(
|
||||
@@ -480,6 +523,7 @@ func GetTasks(filter *Filter) ([]*Task, error) {
|
||||
&until,
|
||||
&recurDuration,
|
||||
&parentUUIDStr,
|
||||
&annotationsStr,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
@@ -506,6 +550,7 @@ func GetTasks(filter *Filter) ([]*Task, error) {
|
||||
task.Until = sqlToTime(until)
|
||||
task.RecurrenceDuration = sqlToDuration(recurDuration)
|
||||
task.ParentUUID = sqlToUUIDPtr(parentUUIDStr)
|
||||
task.Annotations = sqlToAnnotations(annotationsStr)
|
||||
|
||||
// Load tags
|
||||
tags, err := task.GetTags()
|
||||
@@ -543,7 +588,7 @@ func (t *Task) Save() error {
|
||||
status = ?, description = ?, project = ?, priority = ?,
|
||||
modified = ?, start = ?, end = ?, due = ?,
|
||||
scheduled = ?, wait = ?, until_date = ?,
|
||||
recurrence_duration = ?, parent_uuid = ?
|
||||
recurrence_duration = ?, parent_uuid = ?, annotations = ?
|
||||
WHERE uuid = ?
|
||||
`
|
||||
|
||||
@@ -561,6 +606,7 @@ func (t *Task) Save() error {
|
||||
timeToSQL(t.Until),
|
||||
durationToSQL(t.RecurrenceDuration),
|
||||
uuidPtrToSQL(t.ParentUUID),
|
||||
annotationsToSQL(t.Annotations),
|
||||
t.UUID.String(),
|
||||
)
|
||||
|
||||
@@ -573,8 +619,8 @@ func (t *Task) Save() error {
|
||||
INSERT INTO tasks (
|
||||
uuid, status, description, project, priority,
|
||||
created, modified, start, end, due, scheduled, wait, until_date,
|
||||
recurrence_duration, parent_uuid
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
recurrence_duration, parent_uuid, annotations
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
`
|
||||
|
||||
result, err := db.Exec(query,
|
||||
@@ -593,6 +639,7 @@ func (t *Task) Save() error {
|
||||
timeToSQL(t.Until),
|
||||
durationToSQL(t.RecurrenceDuration),
|
||||
uuidPtrToSQL(t.ParentUUID),
|
||||
annotationsToSQL(t.Annotations),
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
|
||||
@@ -0,0 +1,347 @@
|
||||
package engine
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
const undoStackLimit = 10
|
||||
|
||||
// RecordUndo records a CLI operation as undoable.
|
||||
// Called AFTER the mutation so the change_log entry exists.
|
||||
func RecordUndo(opType string, taskUUID uuid.UUID) error {
|
||||
db := GetDB()
|
||||
if db == nil {
|
||||
return fmt.Errorf("database not initialized")
|
||||
}
|
||||
|
||||
// Find the change_log entry just created by this mutation
|
||||
var changeLogID int64
|
||||
err := db.QueryRow(
|
||||
"SELECT MAX(id) FROM change_log WHERE task_uuid = ?",
|
||||
taskUUID.String(),
|
||||
).Scan(&changeLogID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to find change_log entry: %w", err)
|
||||
}
|
||||
|
||||
// Insert into undo_stack
|
||||
_, err = db.Exec(
|
||||
"INSERT INTO undo_stack (created_at, op_type, task_uuid, change_log_id) VALUES (?, ?, ?, ?)",
|
||||
timeNow().Unix(), opType, taskUUID.String(), changeLogID,
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to record undo: %w", err)
|
||||
}
|
||||
|
||||
// Evict old entries beyond the limit
|
||||
_, err = db.Exec(
|
||||
"DELETE FROM undo_stack WHERE id NOT IN (SELECT id FROM undo_stack ORDER BY id DESC LIMIT ?)",
|
||||
undoStackLimit,
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to evict old undo entries: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// PopUndo pops the most recent undo entry and reverts the task.
|
||||
// Returns a description of what was undone.
|
||||
func PopUndo() (string, error) {
|
||||
db := GetDB()
|
||||
if db == nil {
|
||||
return "", fmt.Errorf("database not initialized")
|
||||
}
|
||||
|
||||
// Get the most recent undo entry
|
||||
var (
|
||||
undoID int64
|
||||
opType string
|
||||
taskUUIDStr string
|
||||
changeLogID int64
|
||||
)
|
||||
err := db.QueryRow(
|
||||
"SELECT id, op_type, task_uuid, change_log_id FROM undo_stack ORDER BY id DESC LIMIT 1",
|
||||
).Scan(&undoID, &opType, &taskUUIDStr, &changeLogID)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("nothing to undo")
|
||||
}
|
||||
|
||||
taskUUID, err := uuid.Parse(taskUUIDStr)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("invalid task UUID in undo stack: %w", err)
|
||||
}
|
||||
|
||||
// Remove the entry from the stack
|
||||
_, err = db.Exec("DELETE FROM undo_stack WHERE id = ?", undoID)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to pop undo entry: %w", err)
|
||||
}
|
||||
|
||||
// Perform the revert based on op type
|
||||
switch opType {
|
||||
case "add":
|
||||
return undoAdd(taskUUID)
|
||||
case "done", "delete", "modify", "start", "stop":
|
||||
return undoRestore(opType, taskUUID, changeLogID)
|
||||
default:
|
||||
return "", fmt.Errorf("unknown undo operation: %s", opType)
|
||||
}
|
||||
}
|
||||
|
||||
// undoAdd reverts an add by hard-deleting the task.
|
||||
// For recurring tasks, also deletes the template.
|
||||
func undoAdd(taskUUID uuid.UUID) (string, error) {
|
||||
db := GetDB()
|
||||
|
||||
task, err := GetTask(taskUUID)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to load task for undo: %w", err)
|
||||
}
|
||||
|
||||
desc := task.Description
|
||||
|
||||
// If this is a recurring instance, also delete the template
|
||||
if task.ParentUUID != nil {
|
||||
_, err = db.Exec("DELETE FROM tasks WHERE uuid = ?", task.ParentUUID.String())
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to delete recurring template: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Hard delete the task
|
||||
_, err = db.Exec("DELETE FROM tasks WHERE uuid = ?", taskUUID.String())
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to delete task: %w", err)
|
||||
}
|
||||
|
||||
return fmt.Sprintf("Undid add: removed \"%s\"", desc), nil
|
||||
}
|
||||
|
||||
// undoRestore reverts a mutation by restoring the prior state from change_log.
|
||||
func undoRestore(opType string, taskUUID uuid.UUID, changeLogID int64) (string, error) {
|
||||
db := GetDB()
|
||||
|
||||
// Find the change_log entry BEFORE this one for the same task
|
||||
var priorData string
|
||||
err := db.QueryRow(
|
||||
"SELECT data FROM change_log WHERE task_uuid = ? AND id < ? ORDER BY id DESC LIMIT 1",
|
||||
taskUUID.String(), changeLogID,
|
||||
).Scan(&priorData)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("no prior state found in change_log (cannot undo)")
|
||||
}
|
||||
|
||||
// Parse the prior state
|
||||
task, err := GetTask(taskUUID)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to load task: %w", err)
|
||||
}
|
||||
|
||||
// Apply the prior state from change_log data
|
||||
if err := applyChangeLogData(task, priorData); err != nil {
|
||||
return "", fmt.Errorf("failed to restore prior state: %w", err)
|
||||
}
|
||||
|
||||
// Save the restored task
|
||||
if err := task.Save(); err != nil {
|
||||
return "", fmt.Errorf("failed to save restored task: %w", err)
|
||||
}
|
||||
|
||||
// Reconcile tags
|
||||
if err := reconcileTagsFromChangeLog(task, priorData); err != nil {
|
||||
return "", fmt.Errorf("failed to reconcile tags: %w", err)
|
||||
}
|
||||
|
||||
return fmt.Sprintf("Undid %s: restored \"%s\"", opType, task.Description), nil
|
||||
}
|
||||
|
||||
// applyChangeLogData parses change_log data and applies it to a task.
|
||||
// The data format is "key: value\n" lines (same format used by sync).
|
||||
func applyChangeLogData(task *Task, data string) error {
|
||||
lines := strings.Split(data, "\n")
|
||||
for _, line := range lines {
|
||||
line = strings.TrimSpace(line)
|
||||
if line == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
parts := strings.SplitN(line, ": ", 2)
|
||||
if len(parts) != 2 {
|
||||
continue
|
||||
}
|
||||
|
||||
key := parts[0]
|
||||
value := parts[1]
|
||||
|
||||
switch key {
|
||||
case "description":
|
||||
task.Description = value
|
||||
case "status":
|
||||
switch value {
|
||||
case "pending":
|
||||
task.Status = StatusPending
|
||||
case "completed":
|
||||
task.Status = StatusCompleted
|
||||
case "deleted":
|
||||
task.Status = StatusDeleted
|
||||
case "recurring":
|
||||
task.Status = StatusRecurring
|
||||
}
|
||||
case "priority":
|
||||
switch value {
|
||||
case "H":
|
||||
task.Priority = PriorityHigh
|
||||
case "M":
|
||||
task.Priority = PriorityMedium
|
||||
case "L":
|
||||
task.Priority = PriorityLow
|
||||
default:
|
||||
task.Priority = PriorityDefault
|
||||
}
|
||||
case "project":
|
||||
task.Project = &value
|
||||
case "created":
|
||||
if ts, err := strconv.ParseInt(value, 10, 64); err == nil {
|
||||
task.Created = time.Unix(ts, 0)
|
||||
}
|
||||
case "modified":
|
||||
// Don't restore modified — it'll be set by Save()
|
||||
case "start":
|
||||
if ts, err := strconv.ParseInt(value, 10, 64); err == nil {
|
||||
t := time.Unix(ts, 0)
|
||||
task.Start = &t
|
||||
}
|
||||
case "end":
|
||||
if ts, err := strconv.ParseInt(value, 10, 64); err == nil {
|
||||
t := time.Unix(ts, 0)
|
||||
task.End = &t
|
||||
}
|
||||
case "due":
|
||||
if ts, err := strconv.ParseInt(value, 10, 64); err == nil {
|
||||
t := time.Unix(ts, 0)
|
||||
task.Due = &t
|
||||
}
|
||||
case "scheduled":
|
||||
if ts, err := strconv.ParseInt(value, 10, 64); err == nil {
|
||||
t := time.Unix(ts, 0)
|
||||
task.Scheduled = &t
|
||||
}
|
||||
case "wait":
|
||||
if ts, err := strconv.ParseInt(value, 10, 64); err == nil {
|
||||
t := time.Unix(ts, 0)
|
||||
task.Wait = &t
|
||||
}
|
||||
case "until":
|
||||
if ts, err := strconv.ParseInt(value, 10, 64); err == nil {
|
||||
t := time.Unix(ts, 0)
|
||||
task.Until = &t
|
||||
}
|
||||
case "recurrence":
|
||||
if ns, err := strconv.ParseInt(value, 10, 64); err == nil {
|
||||
d := time.Duration(ns)
|
||||
task.RecurrenceDuration = &d
|
||||
}
|
||||
case "parent_uuid":
|
||||
if u, err := uuid.Parse(value); err == nil {
|
||||
task.ParentUUID = &u
|
||||
}
|
||||
case "annotations":
|
||||
// Annotations are stored as JSON in the change_log
|
||||
task.Annotations = sqlToAnnotations(value)
|
||||
case "tags":
|
||||
// Tags are handled separately by reconcileTagsFromChangeLog
|
||||
}
|
||||
}
|
||||
|
||||
// Clear fields that aren't present in the change_log data (they were NULL)
|
||||
fieldPresent := make(map[string]bool)
|
||||
for _, line := range lines {
|
||||
parts := strings.SplitN(strings.TrimSpace(line), ": ", 2)
|
||||
if len(parts) == 2 {
|
||||
fieldPresent[parts[0]] = true
|
||||
}
|
||||
}
|
||||
if !fieldPresent["project"] {
|
||||
task.Project = nil
|
||||
}
|
||||
if !fieldPresent["start"] {
|
||||
task.Start = nil
|
||||
}
|
||||
if !fieldPresent["end"] {
|
||||
task.End = nil
|
||||
}
|
||||
if !fieldPresent["due"] {
|
||||
task.Due = nil
|
||||
}
|
||||
if !fieldPresent["scheduled"] {
|
||||
task.Scheduled = nil
|
||||
}
|
||||
if !fieldPresent["wait"] {
|
||||
task.Wait = nil
|
||||
}
|
||||
if !fieldPresent["until"] {
|
||||
task.Until = nil
|
||||
}
|
||||
if !fieldPresent["recurrence"] {
|
||||
task.RecurrenceDuration = nil
|
||||
}
|
||||
if !fieldPresent["parent_uuid"] {
|
||||
task.ParentUUID = nil
|
||||
}
|
||||
if !fieldPresent["annotations"] {
|
||||
task.Annotations = nil
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// reconcileTagsFromChangeLog restores tags from change_log data.
|
||||
func reconcileTagsFromChangeLog(task *Task, data string) error {
|
||||
// Parse desired tags from change_log
|
||||
var desiredTags []string
|
||||
for _, line := range strings.Split(data, "\n") {
|
||||
line = strings.TrimSpace(line)
|
||||
if strings.HasPrefix(line, "tags: ") {
|
||||
tagStr := strings.TrimPrefix(line, "tags: ")
|
||||
for _, tag := range strings.Split(tagStr, ",") {
|
||||
tag = strings.TrimSpace(tag)
|
||||
if tag != "" {
|
||||
desiredTags = append(desiredTags, tag)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Get current tags
|
||||
currentTags, _ := task.GetTags()
|
||||
|
||||
// Remove tags not in desired set
|
||||
desired := make(map[string]bool)
|
||||
for _, t := range desiredTags {
|
||||
desired[t] = true
|
||||
}
|
||||
for _, tag := range currentTags {
|
||||
if !desired[tag] {
|
||||
task.RemoveTag(tag)
|
||||
}
|
||||
}
|
||||
|
||||
// Add missing tags
|
||||
current := make(map[string]bool)
|
||||
for _, t := range currentTags {
|
||||
current[t] = true
|
||||
}
|
||||
for _, tag := range desiredTags {
|
||||
if !current[tag] {
|
||||
task.AddTag(tag)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user