| 1 | package equipo |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "net/url" |
| 7 | "slices" |
| 8 | "sort" |
| 9 | "time" |
| 10 | |
| 11 | "amadan.net/oficina/equipo/internal/equipo/chat" |
| 12 | "amadan.net/oficina/equipo/internal/wire" |
| 13 | ) |
| 14 | |
| 15 | // The daily roll-up (Paul, 2026-11-12): "the value of the scrollback |
| 16 | // diminishes and just becomes a distraction". Once a day is over in the |
| 17 | // person's zone it shows as one day box: its date, what was started, |
| 18 | // merged or finished, what the person decided and what is still open, |
| 19 | // all read from records (no model, no tokens), and "Show the |
| 20 | // conversation", which opens the day's transcript in place. Today is |
| 21 | // never rolled up. |
| 22 | // |
| 23 | // The transcript opens inline rather than on a page of its own: the |
| 24 | // day's cards keep working where they stand, a link to one of its |
| 25 | // messages (/conversation?message=) lands in the one conversation with |
| 26 | // the composer under it, and the live feed keeps an opened day open |
| 27 | // across its refreshes. Without script the same link is a plain |
| 28 | // /conversation?day= page with that day open. |
| 29 | // |
| 30 | // Whatever still needs the person is never rolled away: cards still |
| 31 | // asking and tasks still underway from earlier days stand together at |
| 32 | // the top of today. |
| 33 | |
| 34 | // earlierDayBoxes is how many earlier days with messages the page shows. |
| 35 | const earlierDayBoxes = 14 |
| 36 | |
| 37 | // maxDayMessages bounds one day's transcript, and today's. |
| 38 | const maxDayMessages = 1000 |
| 39 | |
| 40 | // dayListMax is how many items a day box names in one row. |
| 41 | const dayListMax = 5 |
| 42 | |
| 43 | // chatDay is one earlier day on the conversation page. |
| 44 | type chatDay struct { |
| 45 | Key, Label string |
| 46 | Messages string |
| 47 | // Rows are what the day held, in a fixed order: started, merged, |
| 48 | // finished, decided, still open. A day with none is Quiet: talk only. |
| 49 | Rows []dayRow |
| 50 | Quiet bool |
| 51 | // Expanded days carry their transcript; Toggle is the link that |
| 52 | // opens or closes it (the page's other open days kept). |
| 53 | Expanded bool |
| 54 | Toggle string |
| 55 | Feed chatFeed |
| 56 | } |
| 57 | |
| 58 | // chatFeed is a run of conversation lines and whose they are. |
| 59 | type chatFeed struct { |
| 60 | Lines []chatLine |
| 61 | Address string |
| 62 | } |
| 63 | |
| 64 | // Today is today's lines; Earlier what still needs the person from |
| 65 | // earlier days. |
| 66 | func (v chatView) Today(address string) chatFeed { return chatFeed{v.Messages, address} } |
| 67 | func (v chatView) Earlier(address string) chatFeed { return chatFeed{v.Open, address} } |
| 68 | |
| 69 | // dayRow is a row of a day box: its label, its items, and how many more |
| 70 | // there are. A stacked row (decisions, what is still open) puts each |
| 71 | // item on its own line. |
| 72 | type dayRow struct { |
| 73 | Width int |
| 74 | Count int |
| 75 | Key, Label string |
| 76 | Items []dayItem |
| 77 | More string |
| 78 | Stack bool |
| 79 | } |
| 80 | |
| 81 | // dayItem names a task (a link) or, with no task, says what was asked; |
| 82 | // Note is what a decision decided. |
| 83 | type dayItem struct { |
| 84 | TaskID, Text, Note string |
| 85 | } |
| 86 | |
| 87 | func (r *dayRow) add(it dayItem) { |
| 88 | if slices.Contains(r.Items, it) { |
| 89 | return |
| 90 | } |
| 91 | r.Items = append(r.Items, it) |
| 92 | } |
| 93 | |
| 94 | func (r *dayRow) trim() { |
| 95 | r.Count = len(r.Items) |
| 96 | if n := len(r.Items) - dayListMax; n > 0 { |
| 97 | r.Items, r.More = r.Items[:dayListMax], fmt.Sprintf(copyText("chat.day.more"), n) |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | // dayRowOrder is the order of a day box's rows. |
| 102 | var dayRowOrder = []string{"started", "merged", "finished", "decided", "open"} |
| 103 | |
| 104 | func newDayRows() map[string]*dayRow { |
| 105 | out := map[string]*dayRow{} |
| 106 | for _, key := range dayRowOrder { |
| 107 | out[key] = &dayRow{Key: key, Label: copyText("chat.day." + key), Stack: key == "decided" || key == "open"} |
| 108 | } |
| 109 | return out |
| 110 | } |
| 111 | |
| 112 | // dayKey is the day's name in URLs and ids. |
| 113 | func dayKey(start time.Time, loc *time.Location) string { return start.In(loc).Format("2006-01-02") } |
| 114 | |
| 115 | // parseDay reads a day key as the start of that day in loc. |
| 116 | func parseDay(key string, loc *time.Location) (time.Time, bool) { |
| 117 | t, err := time.ParseInLocation("2006-01-02", key, loc) |
| 118 | if err != nil { |
| 119 | return time.Time{}, false |
| 120 | } |
| 121 | return t.UTC(), true |
| 122 | } |
| 123 | |
| 124 | // dayLabel is how a day is named: "Tuesday 11 November", with the year |
| 125 | // when it is not this one. |
| 126 | func dayLabel(start, today time.Time, loc *time.Location) string { |
| 127 | l := start.In(loc) |
| 128 | if l.Year() != today.In(loc).Year() { |
| 129 | return l.Format("Monday 2 January 2006") |
| 130 | } |
| 131 | return l.Format("Monday 2 January") |
| 132 | } |
| 133 | |
| 134 | // dayToggle is the link that opens (or closes) one day, keeping the |
| 135 | // other open days open. |
| 136 | func dayToggle(key string, open map[string]bool) string { |
| 137 | var keys []string |
| 138 | for k := range open { |
| 139 | if k != key { |
| 140 | keys = append(keys, k) |
| 141 | } |
| 142 | } |
| 143 | if !open[key] { |
| 144 | keys = append(keys, key) |
| 145 | } |
| 146 | sort.Strings(keys) |
| 147 | path := "/conversation" |
| 148 | if len(keys) > 0 { |
| 149 | path += "?" + url.Values{"day": keys}.Encode() |
| 150 | } |
| 151 | return path + "#day-" + key |
| 152 | } |
| 153 | |
| 154 | // asksPerson is whether a card from an earlier day still needs the |
| 155 | // person: a question, decision or access card still open, or the card of |
| 156 | // a task still underway. |
| 157 | func asksPerson(c wire.Card, v chatCard, tasks map[string]wire.Task) bool { |
| 158 | if c.Kind == wire.CardTask { |
| 159 | t, ok := tasks[c.TaskID] |
| 160 | return ok && v.Task != nil && slices.Contains(chat.Underway, t.Stage) |
| 161 | } |
| 162 | return v.Open |
| 163 | } |
| 164 | |
| 165 | // receiptKind is whether a card shows as a quiet receipt line. |
| 166 | func receiptKind(c wire.Card) bool { |
| 167 | return c.Kind == wire.CardProject || c.Kind == wire.CardTask || c.Kind == wire.CardCombined || c.Kind == wire.CardHousekeeping || c.Kind == wire.CardHome && c.State != wire.CardHomeAsk |
| 168 | } |
| 169 | |
| 170 | // decisionNote is what an answered card decided, in the words its card |
| 171 | // shows once answered. |
| 172 | func decisionNote(c wire.Card) string { |
| 173 | key := "" |
| 174 | switch c.Kind { |
| 175 | case wire.CardMerge: |
| 176 | key = "chat.card.merge_chose_" + c.Choice |
| 177 | case wire.CardDirection: |
| 178 | key = "chat.card.direction_chose_" + c.Choice |
| 179 | case wire.CardMakerRemoved: |
| 180 | key = "chat.card.maker_removed_chose_" + c.Choice |
| 181 | case wire.CardPublish: |
| 182 | key = "chat.card.publish_chose_" + c.Choice |
| 183 | case wire.CardSpecialist: |
| 184 | key = "chat.card.specialist_chose_" + c.Choice |
| 185 | case wire.CardResearchSources: |
| 186 | key = "chat.card.research_chose_" + c.Choice |
| 187 | case wire.CardResearchInventory: |
| 188 | key = "chat.card.inventory_chose_" + c.Choice |
| 189 | case wire.CardMergeShips: |
| 190 | key = "chat.card.ships_chose_" + c.Choice |
| 191 | case wire.CardMergePolicy: |
| 192 | key = "chat.card.policy_chose_" + c.Choice |
| 193 | } |
| 194 | if text := copyText(key); key != "" && text != "" { |
| 195 | return text |
| 196 | } |
| 197 | return fmt.Sprintf(copyText("chat.card.answered"), c.Choice) |
| 198 | } |
| 199 | |
| 200 | // earlierDays is the page's earlier days, oldest first, with the ones |
| 201 | // asked for (expand) open, and what still needs the person from before |
| 202 | // today, as loose lines for the top of today. |
| 203 | func (a *application) earlierDays(ctx context.Context, address string, loc *time.Location, today time.Time, expand map[time.Time]bool, choices []string, tasks map[string]wire.Task) ([]chatDay, []chatLine, error) { |
| 204 | chats := a.store.Chat() |
| 205 | starts, err := chats.EarlierDays(ctx, address, today, earlierDayBoxes) |
| 206 | if err != nil { |
| 207 | return nil, nil, err |
| 208 | } |
| 209 | for d := range expand { |
| 210 | if !slices.ContainsFunc(starts, d.Equal) { |
| 211 | starts = append(starts, d) |
| 212 | } |
| 213 | } |
| 214 | slices.SortFunc(starts, func(x, y time.Time) int { return x.Compare(y) }) |
| 215 | open, err := chats.StillOpen(ctx, address, today) |
| 216 | if err != nil { |
| 217 | return nil, nil, err |
| 218 | } |
| 219 | for _, t := range open.Tasks { |
| 220 | tasks[t.ID] = t |
| 221 | } |
| 222 | // What still needs the person: loose lines, in the order they came. |
| 223 | var loose []chatLine |
| 224 | shown := map[string]bool{} |
| 225 | byDay := map[time.Time][]wire.Card{} |
| 226 | for _, c := range open.Cards { |
| 227 | v := cardView(c, choices, tasks) |
| 228 | if !asksPerson(c, v, tasks) { |
| 229 | continue |
| 230 | } |
| 231 | v.Receipt = receiptKind(c) |
| 232 | at := c.CreatedAt |
| 233 | if c.VisibleAt != nil && c.VisibleAt.After(at) { |
| 234 | at = *c.VisibleAt |
| 235 | } |
| 236 | loose = append(loose, chatLine{ID: c.ID, CardOnly: true, Cards: []chatCard{v}, CreatedAt: at}) |
| 237 | shown[c.ID] = true |
| 238 | byDay[chat.DayStart(at, loc)] = append(byDay[chat.DayStart(at, loc)], c) |
| 239 | } |
| 240 | if len(starts) == 0 { |
| 241 | return nil, loose, nil |
| 242 | } |
| 243 | roll, err := chats.Rollup(ctx, address, starts[0], today) |
| 244 | if err != nil { |
| 245 | return nil, nil, err |
| 246 | } |
| 247 | counts := map[time.Time]int{} |
| 248 | for _, at := range roll.Messages { |
| 249 | counts[chat.DayStart(at, loc)]++ |
| 250 | } |
| 251 | // A day asked for that held no message is no day of the person's. |
| 252 | starts = slices.DeleteFunc(starts, func(d time.Time) bool { return counts[d] == 0 }) |
| 253 | if len(starts) == 0 { |
| 254 | return nil, loose, nil |
| 255 | } |
| 256 | days := make([]chatDay, len(starts)) |
| 257 | rows := make([]map[string]*dayRow, len(starts)) |
| 258 | index := map[time.Time]int{} |
| 259 | opened := map[string]bool{} |
| 260 | for _, d := range starts { |
| 261 | if expand[d] { |
| 262 | opened[dayKey(d, loc)] = true |
| 263 | } |
| 264 | } |
| 265 | for i, d := range starts { |
| 266 | index[d] = i |
| 267 | key := dayKey(d, loc) |
| 268 | days[i] = chatDay{Key: key, Label: dayLabel(d, today, loc), Expanded: expand[d], Toggle: dayToggle(key, opened)} |
| 269 | rows[i] = newDayRows() |
| 270 | days[i].Messages = copyText("chat.day.messages_one") |
| 271 | if n := counts[d]; n != 1 { |
| 272 | days[i].Messages = fmt.Sprintf(copyText("chat.day.messages"), n) |
| 273 | } |
| 274 | } |
| 275 | in := func(at *time.Time) (int, bool) { |
| 276 | if at == nil { |
| 277 | return 0, false |
| 278 | } |
| 279 | i, ok := index[chat.DayStart(*at, loc)] |
| 280 | return i, ok |
| 281 | } |
| 282 | for _, t := range roll.Tasks { |
| 283 | it := dayItem{TaskID: t.ID, Text: t.Title} |
| 284 | if i, ok := in(&t.CreatedAt); ok { |
| 285 | rows[i]["started"].add(it) |
| 286 | } |
| 287 | if i, ok := in(t.MergedAt); ok { |
| 288 | rows[i]["merged"].add(it) |
| 289 | } else if i, ok := in(t.DoneAt); ok && t.Stage == wire.StageDone { |
| 290 | rows[i]["finished"].add(it) |
| 291 | } |
| 292 | } |
| 293 | for _, d := range roll.Decisions { |
| 294 | i, ok := in(&d.At) |
| 295 | if !ok { |
| 296 | continue |
| 297 | } |
| 298 | it := dayItem{Text: d.Card.Question, Note: decisionNote(d.Card)} |
| 299 | if d.Task != nil { |
| 300 | it.TaskID, it.Text = d.Task.ID, d.Task.Title |
| 301 | } |
| 302 | rows[i]["decided"].add(it) |
| 303 | } |
| 304 | for start, cards := range byDay { |
| 305 | i, ok := index[start] |
| 306 | if !ok { |
| 307 | continue |
| 308 | } |
| 309 | for _, c := range cards { |
| 310 | if t, ok := tasks[c.TaskID]; ok && c.TaskID != "" { |
| 311 | rows[i]["open"].add(dayItem{TaskID: t.ID, Text: t.Title}) |
| 312 | } else { |
| 313 | rows[i]["open"].add(dayItem{Text: cardView(c, choices, tasks).Line}) |
| 314 | } |
| 315 | } |
| 316 | } |
| 317 | for i := range days { |
| 318 | d := &days[i] |
| 319 | for _, key := range dayRowOrder { |
| 320 | if r := rows[i][key]; len(r.Items) > 0 { |
| 321 | r.trim() |
| 322 | d.Rows = append(d.Rows, *r) |
| 323 | } |
| 324 | } |
| 325 | total := 0 |
| 326 | for _, r := range d.Rows { |
| 327 | total += r.Count |
| 328 | } |
| 329 | for j := range d.Rows { |
| 330 | d.Rows[j].Width = max(1, (d.Rows[j].Count*100+total/2)/total) |
| 331 | } |
| 332 | d.Quiet = len(d.Rows) == 0 |
| 333 | if !d.Expanded { |
| 334 | continue |
| 335 | } |
| 336 | start := starts[i] |
| 337 | win, err := chats.Window(ctx, address, start, chat.NextDay(start, loc), maxDayMessages) |
| 338 | if err != nil { |
| 339 | return nil, nil, err |
| 340 | } |
| 341 | for _, t := range win.Tasks { |
| 342 | tasks[t.ID] = t |
| 343 | } |
| 344 | // A card still asking stands at the top of today, once. |
| 345 | cards := slices.DeleteFunc(win.Cards, func(c wire.Card) bool { return shown[c.ID] }) |
| 346 | d.Feed = chatFeed{Lines: feedLines(win.Messages, cards, choices, tasks, loc), Address: address} |
| 347 | } |
| 348 | return days, loose, nil |
| 349 | } |
| 350 | |
| 351 | // feedLines places a stretch's messages and cards as the page shows |
| 352 | // them: a receipt sits where its action happened, before the reply of |
| 353 | // its turn (or after the person's message while the turn runs); other |
| 354 | // cards come after the turn's last message, in event order; a card no |
| 355 | // turn produced stands at its own time. |
| 356 | func feedLines(msgs []wire.Message, cards []wire.Card, choices []string, tasks map[string]wire.Task, loc *time.Location) []chatLine { |
| 357 | var out []chatLine |
| 358 | last := map[string]int{} |
| 359 | for i, m := range msgs { |
| 360 | out = append(out, chatLine{ID: m.ID, Text: m.Text, TurnID: m.TurnID, Own: m.Role == wire.RolePerson, CreatedAt: m.CreatedAt, Switched: switchedText(m.Switched, loc), Files: m.Attachments}) |
| 361 | last[m.TurnID] = i |
| 362 | } |
| 363 | reply := map[string]int{} |
| 364 | for i, m := range out { |
| 365 | if !m.Own { |
| 366 | if _, ok := reply[m.TurnID]; !ok { |
| 367 | reply[m.TurnID] = i |
| 368 | } |
| 369 | } |
| 370 | } |
| 371 | var loose []chatLine |
| 372 | for _, c := range cards { |
| 373 | v := cardView(c, choices, tasks) |
| 374 | if c.Kind == wire.CardTask && v.Task == nil || c.Kind == wire.CardAdvice && c.State == wire.CardResolved { |
| 375 | continue |
| 376 | } |
| 377 | v.Receipt = receiptKind(c) |
| 378 | if i, ok := reply[c.TurnID]; ok && v.Receipt && c.TurnID != "" { |
| 379 | out[i].Receipts = append(out[i].Receipts, v) |
| 380 | } else if i, ok := last[c.TurnID]; ok && c.TurnID != "" { |
| 381 | out[i].Cards = append(out[i].Cards, v) |
| 382 | } else if c.TurnID == "" { |
| 383 | // A receipt held through quiet hours stands where it showed. |
| 384 | at := c.CreatedAt |
| 385 | if c.VisibleAt != nil && c.VisibleAt.After(at) { |
| 386 | at = *c.VisibleAt |
| 387 | } |
| 388 | loose = append(loose, chatLine{ID: c.ID, CardOnly: true, Cards: []chatCard{v}, CreatedAt: at}) |
| 389 | } |
| 390 | } |
| 391 | for _, l := range loose { |
| 392 | at := sort.Search(len(out), func(i int) bool { return out[i].CreatedAt.After(l.CreatedAt) }) |
| 393 | out = slices.Insert(out, at, l) |
| 394 | } |
| 395 | return out |
| 396 | } |
| 397 | |