1// SPDX-FileCopyrightText: Amolith <amolith@secluded.site>
2//
3// SPDX-License-Identifier: AGPL-3.0-or-later
4
5package lunatask
6
7import (
8 "context"
9 "time"
10)
11
12// Task is a task in Lunatask. Name and Note are encrypted client-side
13// and will be null when read back from the API.
14type Task struct {
15 ID string `json:"id"`
16 AreaID *string `json:"area_id"`
17 GoalID *string `json:"goal_id"`
18 Name *string `json:"name"`
19 Note *string `json:"note"`
20 Status *TaskStatus `json:"status"`
21 PreviousStatus *TaskStatus `json:"previous_status"`
22 Estimate *int `json:"estimate"`
23 Priority *int `json:"priority"`
24 Progress *int `json:"progress"`
25 Motivation *Motivation `json:"motivation"`
26 Eisenhower *int `json:"eisenhower"`
27 Sources []Source `json:"sources"`
28 ScheduledOn *Date `json:"scheduled_on"`
29 CompletedAt *time.Time `json:"completed_at"`
30 CreatedAt time.Time `json:"created_at"`
31 UpdatedAt time.Time `json:"updated_at"`
32}
33
34// createTaskRequest defines a new task for JSON serialization.
35type createTaskRequest struct {
36 Name string `json:"name"`
37 AreaID *string `json:"area_id,omitempty"`
38 GoalID *string `json:"goal_id,omitempty"`
39 Note *string `json:"note,omitempty"`
40 Status *TaskStatus `json:"status,omitempty"`
41 Motivation *Motivation `json:"motivation,omitempty"`
42 Estimate *int `json:"estimate,omitempty"`
43 Priority *int `json:"priority,omitempty"`
44 Eisenhower *int `json:"eisenhower,omitempty"`
45 ScheduledOn *Date `json:"scheduled_on,omitempty"`
46 CompletedAt *time.Time `json:"completed_at,omitempty"`
47 Source *string `json:"source,omitempty"`
48 SourceID *string `json:"source_id,omitempty"`
49}
50
51// updateTaskRequest specifies which fields to change on a task.
52type updateTaskRequest struct {
53 Name *string `json:"name,omitempty"`
54 AreaID *string `json:"area_id,omitempty"`
55 GoalID *string `json:"goal_id,omitempty"`
56 Note *string `json:"note,omitempty"`
57 Status *TaskStatus `json:"status,omitempty"`
58 Motivation *Motivation `json:"motivation,omitempty"`
59 Estimate *int `json:"estimate,omitempty"`
60 Priority *int `json:"priority,omitempty"`
61 Eisenhower *int `json:"eisenhower,omitempty"`
62 ScheduledOn *Date `json:"scheduled_on,omitempty"`
63 CompletedAt *time.Time `json:"completed_at,omitempty"`
64}
65
66// taskResponse wraps a single task from the API.
67type taskResponse struct {
68 Task Task `json:"task"`
69}
70
71// tasksResponse wraps a list of tasks from the API.
72type tasksResponse struct {
73 Tasks []Task `json:"tasks"`
74}
75
76// ListTasksOptions filters tasks by source integration.
77type ListTasksOptions struct {
78 Source *string
79 SourceID *string
80}
81
82// GetSource implements [SourceFilter].
83func (o *ListTasksOptions) GetSource() *string { return o.Source }
84
85// GetSourceID implements [SourceFilter].
86func (o *ListTasksOptions) GetSourceID() *string { return o.SourceID }
87
88// ListTasks returns all tasks, optionally filtered. Pass nil for all.
89func (c *Client) ListTasks(ctx context.Context, opts *ListTasksOptions) ([]Task, error) {
90 var filter SourceFilter
91 if opts != nil {
92 filter = opts
93 }
94
95 return list(ctx, c, "/tasks", filter, func(r tasksResponse) []Task { return r.Tasks })
96}
97
98// GetTask fetches a task by ID. Name and Note will be null (E2EE).
99func (c *Client) GetTask(ctx context.Context, taskID string) (*Task, error) {
100 return get(ctx, c, "/tasks", taskID, "task", func(r taskResponse) Task { return r.Task })
101}
102
103// DeleteTask removes a task and returns its final state.
104func (c *Client) DeleteTask(ctx context.Context, taskID string) (*Task, error) {
105 return del(ctx, c, "/tasks", taskID, "task", func(r taskResponse) Task { return r.Task })
106}
107
108// TaskBuilder constructs and creates a task via method chaining.
109//
110// task, err := client.NewTask("Review PR").
111// InArea(areaID).
112// WithStatus(lunatask.StatusNext).
113// WithEstimate(30).
114// Create(ctx)
115type TaskBuilder struct {
116 client *Client
117 req createTaskRequest
118}
119
120// NewTask starts building a task with the given name.
121func (c *Client) NewTask(name string) *TaskBuilder {
122 return &TaskBuilder{client: c, req: createTaskRequest{Name: name}}
123}
124
125// InArea assigns the task to an area. IDs are in the area's settings in the app.
126func (b *TaskBuilder) InArea(areaID string) *TaskBuilder {
127 b.req.AreaID = &areaID
128
129 return b
130}
131
132// InGoal assigns the task to a goal. IDs are in the goal's settings in the app.
133func (b *TaskBuilder) InGoal(goalID string) *TaskBuilder {
134 b.req.GoalID = &goalID
135
136 return b
137}
138
139// WithNote attaches a Markdown note to the task.
140func (b *TaskBuilder) WithNote(note string) *TaskBuilder {
141 b.req.Note = ¬e
142
143 return b
144}
145
146// WithStatus sets the workflow status.
147// Use one of the Status* constants (e.g., [StatusNext]).
148func (b *TaskBuilder) WithStatus(status TaskStatus) *TaskBuilder {
149 b.req.Status = &status
150
151 return b
152}
153
154// WithMotivation sets why this task matters.
155// Use one of the Motivation* constants (e.g., [MotivationMust]).
156func (b *TaskBuilder) WithMotivation(motivation Motivation) *TaskBuilder {
157 b.req.Motivation = &motivation
158
159 return b
160}
161
162// WithEstimate sets the expected duration in minutes (0–720).
163func (b *TaskBuilder) WithEstimate(minutes int) *TaskBuilder {
164 b.req.Estimate = &minutes
165
166 return b
167}
168
169// WithPriority sets importance from -2 (lowest) to 2 (highest).
170func (b *TaskBuilder) WithPriority(priority int) *TaskBuilder {
171 b.req.Priority = &priority
172
173 return b
174}
175
176// WithEisenhower sets the Eisenhower matrix quadrant:
177// 0=uncategorized, 1=urgent+important, 2=urgent, 3=important, 4=neither.
178func (b *TaskBuilder) WithEisenhower(eisenhower int) *TaskBuilder {
179 b.req.Eisenhower = &eisenhower
180
181 return b
182}
183
184// ScheduledOn sets when the task should appear on your schedule.
185func (b *TaskBuilder) ScheduledOn(date Date) *TaskBuilder {
186 b.req.ScheduledOn = &date
187
188 return b
189}
190
191// CompletedAt marks the task completed at a specific time.
192func (b *TaskBuilder) CompletedAt(t time.Time) *TaskBuilder {
193 b.req.CompletedAt = &t
194
195 return b
196}
197
198// FromSource tags the task with a free-form origin identifier, useful for
199// tracking tasks created by scripts or external integrations.
200func (b *TaskBuilder) FromSource(source, sourceID string) *TaskBuilder {
201 b.req.Source = &source
202 b.req.SourceID = &sourceID
203
204 return b
205}
206
207// Create sends the task to Lunatask. Returns (nil, nil) if a not-completed
208// task already exists in the same area with matching source/source_id.
209func (b *TaskBuilder) Create(ctx context.Context) (*Task, error) {
210 return create(ctx, b.client, "/tasks", b.req, func(r taskResponse) Task { return r.Task })
211}
212
213// TaskUpdateBuilder constructs and updates a task via method chaining.
214// Only fields you set will be modified; others remain unchanged.
215//
216// task, err := client.NewTaskUpdate(taskID).
217// WithStatus(lunatask.StatusCompleted).
218// CompletedAt(time.Now()).
219// Update(ctx)
220type TaskUpdateBuilder struct {
221 client *Client
222 taskID string
223 req updateTaskRequest
224}
225
226// NewTaskUpdate starts building a task update for the given task ID.
227func (c *Client) NewTaskUpdate(taskID string) *TaskUpdateBuilder {
228 return &TaskUpdateBuilder{client: c, taskID: taskID}
229}
230
231// Name changes the task's name.
232func (b *TaskUpdateBuilder) Name(name string) *TaskUpdateBuilder {
233 b.req.Name = &name
234
235 return b
236}
237
238// InArea moves the task to an area. IDs are in the area's settings in the app.
239func (b *TaskUpdateBuilder) InArea(areaID string) *TaskUpdateBuilder {
240 b.req.AreaID = &areaID
241
242 return b
243}
244
245// InGoal moves the task to a goal. IDs are in the goal's settings in the app.
246func (b *TaskUpdateBuilder) InGoal(goalID string) *TaskUpdateBuilder {
247 b.req.GoalID = &goalID
248
249 return b
250}
251
252// WithNote replaces the task's Markdown note.
253func (b *TaskUpdateBuilder) WithNote(note string) *TaskUpdateBuilder {
254 b.req.Note = ¬e
255
256 return b
257}
258
259// WithStatus sets the workflow status.
260// Use one of the Status* constants (e.g., [StatusNext]).
261func (b *TaskUpdateBuilder) WithStatus(status TaskStatus) *TaskUpdateBuilder {
262 b.req.Status = &status
263
264 return b
265}
266
267// WithMotivation sets why this task matters.
268// Use one of the Motivation* constants (e.g., [MotivationMust]).
269func (b *TaskUpdateBuilder) WithMotivation(motivation Motivation) *TaskUpdateBuilder {
270 b.req.Motivation = &motivation
271
272 return b
273}
274
275// WithEstimate sets the expected duration in minutes (0–720).
276func (b *TaskUpdateBuilder) WithEstimate(minutes int) *TaskUpdateBuilder {
277 b.req.Estimate = &minutes
278
279 return b
280}
281
282// WithPriority sets importance from -2 (lowest) to 2 (highest).
283func (b *TaskUpdateBuilder) WithPriority(priority int) *TaskUpdateBuilder {
284 b.req.Priority = &priority
285
286 return b
287}
288
289// WithEisenhower sets the Eisenhower matrix quadrant:
290// 0=uncategorized, 1=urgent+important, 2=urgent, 3=important, 4=neither.
291func (b *TaskUpdateBuilder) WithEisenhower(eisenhower int) *TaskUpdateBuilder {
292 b.req.Eisenhower = &eisenhower
293
294 return b
295}
296
297// ScheduledOn sets when the task should appear on your schedule.
298func (b *TaskUpdateBuilder) ScheduledOn(date Date) *TaskUpdateBuilder {
299 b.req.ScheduledOn = &date
300
301 return b
302}
303
304// CompletedAt marks the task completed at a specific time.
305func (b *TaskUpdateBuilder) CompletedAt(t time.Time) *TaskUpdateBuilder {
306 b.req.CompletedAt = &t
307
308 return b
309}
310
311// Update sends the changes to Lunatask.
312func (b *TaskUpdateBuilder) Update(ctx context.Context) (*Task, error) {
313 return update(ctx, b.client, "/tasks", b.taskID, "task", b.req, func(r taskResponse) Task { return r.Task })
314}