1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
|
package handlers
import (
"context"
"log"
"sort"
"strings"
"time"
"task-dashboard/internal/config"
"task-dashboard/internal/models"
"task-dashboard/internal/store"
)
// setChainBadge populates item.ChainPosition/ChainTotal (1-indexed) when
// task belongs to a chain. Only the currently-unlocked task in a chain ever
// reaches BuildTimeline (locked tasks are excluded at the store layer), so
// this runs at most once per chain per call -- no memoization needed.
func setChainBadge(s *store.Store, item *models.TimelineItem, task models.Task) {
if task.ChainID == "" {
return
}
tasks, err := s.GetChainTasks(task.ChainID)
if err != nil {
return
}
item.ChainPosition = task.ChainPosition + 1
item.ChainTotal = len(tasks)
}
// BuildTimeline aggregates and normalizes data into a timeline structure
func BuildTimeline(ctx context.Context, s *store.Store, start, end time.Time) ([]models.TimelineItem, error) {
var items []models.TimelineItem
now := config.Now()
// Fetch all projects once into a color lookup map
projectColors := make(map[string]string)
if projects, err := s.GetProjects(); err != nil {
log.Printf("Warning: failed to read projects: %v", err)
} else {
for _, p := range projects {
projectColors[p.ID] = p.Color
}
}
// 1. Fetch Meals - combine multiple items for same date+mealType
meals, err := s.GetMealsByDateRange(start, end)
if err != nil {
return nil, err
}
for _, cm := range groupMeals(meals) {
mealTime := cm.Date
switch cm.MealType {
case "breakfast":
mealTime = time.Date(mealTime.Year(), mealTime.Month(), mealTime.Day(), config.BreakfastHour, 0, 0, 0, mealTime.Location())
case "lunch":
mealTime = time.Date(mealTime.Year(), mealTime.Month(), mealTime.Day(), config.LunchHour, 0, 0, 0, mealTime.Location())
case "dinner":
mealTime = time.Date(mealTime.Year(), mealTime.Month(), mealTime.Day(), config.DinnerHour, 0, 0, 0, mealTime.Location())
default:
mealTime = time.Date(mealTime.Year(), mealTime.Month(), mealTime.Day(), config.LunchHour, 0, 0, 0, mealTime.Location())
}
item := models.TimelineItem{
ID: cm.ID,
Type: models.TimelineItemTypeMeal,
Title: strings.Join(cm.RecipeNames, " + "),
Time: mealTime,
URL: cm.RecipeURL,
OriginalItem: cm.Meals,
IsCompleted: false,
Source: "plantoeat",
}
item.ComputeDaySection(now)
items = append(items, item)
}
// 3. Fetch Cards
cards, err := s.GetCardsByDateRange(start, end)
if err != nil {
return nil, err
}
for _, card := range cards {
if card.DueDate == nil {
continue
}
item := models.TimelineItem{
ID: card.ID,
Type: models.TimelineItemTypeCard,
Title: card.Name,
Time: *card.DueDate,
URL: card.URL,
OriginalItem: card,
IsCompleted: false, // Cards in timeline are not completed (closed cards filtered out)
Source: "trello",
}
item.ComputeDaySection(now)
items = append(items, item)
}
// 4. Fetch Events from store cache (populated by fetchCalendarEvents)
events, err := s.GetCalendarEventsByDateRange(start, end)
if err != nil {
log.Printf("Warning: failed to read cached calendar events: %v", err)
} else {
for _, event := range events {
endTime := event.End
item := models.TimelineItem{
ID: event.ID,
Type: models.TimelineItemTypeEvent,
Title: event.Summary,
Time: event.Start,
EndTime: &endTime,
Description: event.Description,
URL: event.HTMLLink,
OriginalItem: event,
IsCompleted: false,
Source: "calendar",
RecurringEventID: event.RecurringEventID,
}
item.ComputeDaySection(now)
items = append(items, item)
}
}
// 5. Fetch Google Tasks from store cache
gTasks, err := s.GetGoogleTasksByDateRange(start, end)
if err != nil {
log.Printf("Warning: failed to read cached Google Tasks: %v", err)
} else {
for _, gTask := range gTasks {
// Tasks without due date are placed in today section
taskTime := now
if gTask.DueDate != nil {
taskTime = *gTask.DueDate
}
item := models.TimelineItem{
ID: gTask.ID,
Type: models.TimelineItemTypeGTask,
Title: gTask.Title,
Time: taskTime,
Description: gTask.Notes,
URL: gTask.URL,
OriginalItem: gTask,
IsCompleted: gTask.Completed,
Source: "gtasks",
ListID: gTask.ListID,
// No due date -- flag IsAllDay like nativeUndated tasks below so
// TimelineItemToWidgetItem leaves Start nil instead of pinning it
// to "now", which otherwise lands it among scheduledEvents and
// renders it via EventBlock/HourRow -- rows whose click handler
// always opens the source URL/Google Calendar, not the task's
// detail sheet.
IsAllDay: gTask.DueDate == nil,
}
item.ComputeDaySection(now)
items = append(items, item)
}
}
// 6. Fetch native (doot-owned) tasks due within the range, plus any
// overdue tasks (due before the range's start) so they still surface --
// GetNativeTasksByDateRange's lower bound would otherwise drop them
// before ComputeDaySection ever gets a chance to mark them IsOverdue.
nativeDated, err := s.GetNativeTasksByDateRange(start, end)
if err != nil {
log.Printf("Warning: failed to read native tasks: %v", err)
} else {
for _, task := range nativeDated {
if task.DueDate == nil {
continue
}
item := models.TimelineItem{
ID: task.ID,
Type: models.TimelineItemTypeTask,
Title: task.Content,
Time: *task.DueDate,
Description: task.Description,
OriginalItem: task,
IsCompleted: task.Completed,
Source: "doot",
ProjectColor: projectColors[task.ProjectID],
}
setChainBadge(s, &item, task)
item.BucketState = task.BucketState
item.ComputeDaySection(now)
items = append(items, item)
}
}
nativeOverdue, err := s.GetOverdueNativeTasks(start)
if err != nil {
log.Printf("Warning: failed to read overdue native tasks: %v", err)
} else {
for _, task := range nativeOverdue {
if task.DueDate == nil {
continue
}
item := models.TimelineItem{
ID: task.ID,
Type: models.TimelineItemTypeTask,
Title: task.Content,
Time: *task.DueDate,
Description: task.Description,
OriginalItem: task,
IsCompleted: task.Completed,
Source: "doot",
ProjectColor: projectColors[task.ProjectID],
}
setChainBadge(s, &item, task)
item.BucketState = task.BucketState
item.ComputeDaySection(now)
items = append(items, item)
}
}
nativeUndated, err := s.GetUndatedNativeTasks()
if err != nil {
log.Printf("Warning: failed to read undated native tasks: %v", err)
} else {
for _, task := range nativeUndated {
item := models.TimelineItem{
ID: task.ID,
Type: models.TimelineItemTypeTask,
Title: task.Content,
Time: now,
Description: task.Description,
OriginalItem: task,
IsCompleted: task.Completed,
Source: "doot",
IsAllDay: true,
ProjectColor: projectColors[task.ProjectID],
}
setChainBadge(s, &item, task)
item.BucketState = task.BucketState
item.ComputeDaySection(now)
items = append(items, item)
}
}
// Sort items by Time
sort.Slice(items, func(i, j int) bool {
return items[i].Time.Before(items[j].Time)
})
return items, nil
}
|