| 1 | package equipo |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/equipo/internal/equipo/chat" |
| 5 | "amadan.net/oficina/equipo/internal/wire" |
| 6 | "context" |
| 7 | "crypto/rand" |
| 8 | "errors" |
| 9 | "fmt" |
| 10 | "github.com/go-chi/chi/v5" |
| 11 | "net/http" |
| 12 | "slices" |
| 13 | "strconv" |
| 14 | "strings" |
| 15 | "time" |
| 16 | ) |
| 17 | |
| 18 | func (v *chatCard) rosterAuthor(tasks map[string]wire.Task) { |
| 19 | v.Author, v.AuthorIcon, v.Verb = copyText("conversation.coordinator"), "sparkles", copyText("roster.reports") |
| 20 | if !receiptKind(v.Card) { |
| 21 | v.Verb = copyText("roster.asks") |
| 22 | } |
| 23 | who := "" |
| 24 | switch v.Kind { |
| 25 | case wire.CardTask: |
| 26 | who = wire.SpecialistEngineering |
| 27 | if t, ok := tasks[v.TaskID]; ok && !wire.FinalStage(t.Stage) && (t.CopyHold || t.CopyCanSkip) { |
| 28 | who, v.Verb = wire.SpecialistCopy, copyText("roster.holds") |
| 29 | } |
| 30 | case wire.CardDirection: |
| 31 | who = wire.SpecialistEngineering |
| 32 | case wire.CardResearchSources, wire.CardResearchInventory: |
| 33 | who = wire.SpecialistResearch |
| 34 | } |
| 35 | if who != "" { |
| 36 | v.Author = specialistName(who) |
| 37 | v.AuthorIcon = map[string]string{wire.SpecialistEngineering: "wrench", wire.SpecialistResearch: "search", wire.SpecialistCopy: "type"}[who] |
| 38 | } |
| 39 | // A task a data-defined specialist makes (Design) reports as that |
| 40 | // specialist, not as Engineering, whose step kind it works on. |
| 41 | if who == wire.SpecialistEngineering { |
| 42 | t, ok := tasks[v.TaskID] |
| 43 | if !ok && v.Task != nil { |
| 44 | t, ok = v.Task.Task, true |
| 45 | } |
| 46 | if ok && t.Maker != nil { |
| 47 | if n := makerName(t.Maker.Profile); n != "" { |
| 48 | v.Author, v.AuthorIcon = n, makerIcon(t.Maker.Profile) |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | func (v chatCard) Interactive() bool { |
| 55 | return v.Open && (v.Access == nil || !wire.AccessFinal(v.Access.State)) && (!v.Receipt || v.Task != nil && (v.Task.CopyHold || v.Task.CopyCanSkip)) |
| 56 | } |
| 57 | func (v chatCard) SetAside() bool { return v.Interactive() && v.Presentation.Aside } |
| 58 | |
| 59 | func (a *application) presentRoster(ctx context.Context, address string, view *chatView) error { |
| 60 | lane := wire.LaneCoordinator |
| 61 | if view.Job != nil { |
| 62 | lane = wire.JobLane(view.Job.ID) |
| 63 | } |
| 64 | closed, err := a.store.Chat().ResolvedAsideCards(ctx, address, lane, chat.DayStart(a.store.Chat().Now(), a.store.Chat().Zone(address))) |
| 65 | if err != nil { |
| 66 | return err |
| 67 | } |
| 68 | shown := map[string]bool{} |
| 69 | noteShown := func(lines []chatLine) { |
| 70 | for _, line := range lines { |
| 71 | for _, cs := range [][]chatCard{line.Cards, line.Receipts} { |
| 72 | for _, c := range cs { |
| 73 | shown[c.ID] = true |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | noteShown(view.Messages) |
| 79 | noteShown(view.Open) |
| 80 | for _, day := range view.Days { |
| 81 | noteShown(day.Feed.Lines) |
| 82 | } |
| 83 | for _, c := range closed { |
| 84 | if !shown[c.ID] { |
| 85 | v := cardView(c, nil, nil) |
| 86 | v.Job, _ = wire.JobOf(lane) |
| 87 | if v.Note == "" && c.Kind != wire.CardQuestion { |
| 88 | v.Note = copyText("roster.finished") |
| 89 | } |
| 90 | view.Open = append(view.Open, chatLine{ID: c.ID, CardOnly: true, Cards: []chatCard{v}, CreatedAt: c.CreatedAt}) |
| 91 | } |
| 92 | } |
| 93 | slices.SortStableFunc(view.Open, func(a, b chatLine) int { return a.CreatedAt.Compare(b.CreatedAt) }) |
| 94 | var cards []*chatCard |
| 95 | collect := func(lines []chatLine) { |
| 96 | for i := range lines { |
| 97 | for _, cs := range [][]chatCard{lines[i].Cards, lines[i].Receipts} { |
| 98 | for j := range cs { |
| 99 | cards = append(cards, &cs[j]) |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | collect(view.Messages) |
| 105 | collect(view.Open) |
| 106 | for i := range view.Days { |
| 107 | collect(view.Days[i].Feed.Lines) |
| 108 | } |
| 109 | ids := make([]string, 0, len(cards)) |
| 110 | for _, c := range cards { |
| 111 | ids = append(ids, c.ID) |
| 112 | } |
| 113 | ps, err := a.store.Chat().CardPresentations(ctx, address, ids) |
| 114 | if err != nil { |
| 115 | return err |
| 116 | } |
| 117 | for _, c := range cards { |
| 118 | c.Presentation = ps[c.ID] |
| 119 | if c.Presentation.Context.ProjectTitle != "" { |
| 120 | c.ProjectMono = monoOf(c.Presentation.Context.ProjectTitle, c.Presentation.Context.ProjectID) |
| 121 | } |
| 122 | c.ReplyKey = rand.Text() |
| 123 | if n := len(c.Presentation.Activity); n > 0 { |
| 124 | c.ThreadActivity = activityText(c.Presentation.Activity[n-1]) |
| 125 | } |
| 126 | if c.Presentation.ThreadState == wire.TurnFailed { |
| 127 | c.ThreadError = copyText("chat.failed") |
| 128 | } |
| 129 | if c.Presentation.ThreadState == wire.TurnCancelled { |
| 130 | c.ThreadError = copyText("chat.cancelled") |
| 131 | } |
| 132 | } |
| 133 | deciding := map[string]bool{} |
| 134 | for _, c := range cards { |
| 135 | deciding[c.TaskID] = deciding[c.TaskID] || c.TaskID != "" && !c.Receipt && c.Interactive() && !c.SetAside() |
| 136 | } |
| 137 | now, loc := time.Now(), a.store.Chat().Zone(address) |
| 138 | for _, c := range cards { |
| 139 | if c.Task != nil { |
| 140 | c.Task.applyPause(address, now, loc) |
| 141 | c.ReportAction = reportNext(*c.Task, address, cards) |
| 142 | } |
| 143 | c.Compact = c.Receipt && c.Task != nil && deciding[c.TaskID] && !c.Interactive() |
| 144 | } |
| 145 | turns := map[string]bool{} |
| 146 | own := map[string]bool{} |
| 147 | for _, p := range ps { |
| 148 | for _, id := range p.OwnMessages { |
| 149 | own[id] = true |
| 150 | } |
| 151 | for _, id := range p.Turns { |
| 152 | turns[id] = true |
| 153 | } |
| 154 | } |
| 155 | threadMessage := func(m chatLine) bool { |
| 156 | return (own[m.ID] || !m.Own && turns[m.TurnID]) && len(m.Cards) == 0 && len(m.Receipts) == 0 |
| 157 | } |
| 158 | view.Messages = slices.DeleteFunc(view.Messages, threadMessage) |
| 159 | for i := range view.Days { |
| 160 | view.Days[i].Feed.Lines = slices.DeleteFunc(view.Days[i].Feed.Lines, threadMessage) |
| 161 | } |
| 162 | if view.Ended != nil && turns[view.Ended.ID] { |
| 163 | view.Ended = nil |
| 164 | } |
| 165 | view.Waiting = slices.DeleteFunc(view.Waiting, func(t wire.Turn) bool { return turns[t.ID] }) |
| 166 | return nil |
| 167 | } |
| 168 | func (a *application) chatCardVisibility(w http.ResponseWriter, r *http.Request) { |
| 169 | a.chatActionTo(w, r, workBack(r, r.PostFormValue("task")), func(ctx context.Context, address string) error { |
| 170 | revision, err := strconv.ParseInt(r.PostFormValue("revision"), 10, 64) |
| 171 | if err != nil { |
| 172 | return chat.ErrInvalid |
| 173 | } |
| 174 | value := r.PostFormValue("aside") |
| 175 | if value != "true" && value != "false" { |
| 176 | return chat.ErrInvalid |
| 177 | } |
| 178 | return a.store.Chat().SetCardAside(ctx, address, chi.URLParam(r, "id"), revision, value == "true") |
| 179 | }) |
| 180 | } |
| 181 | |
| 182 | func (a *application) chatCardReply(w http.ResponseWriter, r *http.Request) { |
| 183 | a.chatActionTo(w, r, workBack(r, r.PostFormValue("task")), func(ctx context.Context, address string) error { |
| 184 | return a.store.Chat().ReplyCard(ctx, address, chi.URLParam(r, "id"), r.PostFormValue("request_id"), r.PostFormValue("body")) |
| 185 | }) |
| 186 | } |
| 187 | |
| 188 | func rosterTrack(t wire.Task) []trackStop { |
| 189 | if t.Kind == wire.KindExplore { |
| 190 | v := taskLine{} |
| 191 | v.trackOf(t) |
| 192 | out := slices.DeleteFunc(v.Track, func(s trackStop) bool { return s.Stage == wire.StageInvestigated }) |
| 193 | for i := range out { |
| 194 | out[i].Live = false |
| 195 | out[i].At = nil |
| 196 | if out[i].State == "current" { |
| 197 | out[i].Tone = rosterTone(t) |
| 198 | } |
| 199 | } |
| 200 | return out |
| 201 | } |
| 202 | stages := []string{wire.StageNoted, wire.StageInProgress, wire.StageInReview, wire.StageReadyToMerge, wire.StageDone} |
| 203 | current := slices.Index(stages, t.Stage) |
| 204 | if t.Stage == wire.StageMerging { |
| 205 | current = 3 |
| 206 | } |
| 207 | current = max(0, current) |
| 208 | out := make([]trackStop, len(stages)) |
| 209 | for i, s := range stages { |
| 210 | out[i] = trackStop{Stage: s, Label: copyText("work.stage." + s), State: "todo"} |
| 211 | if i < current { |
| 212 | out[i].State = "done" |
| 213 | } |
| 214 | if i == current { |
| 215 | out[i].State, out[i].Tone, out[i].Working = "current", rosterTone(t), workingNow(t) |
| 216 | } |
| 217 | } |
| 218 | return out |
| 219 | } |
| 220 | |
| 221 | func (a *application) chatTurnStop(w http.ResponseWriter, r *http.Request) { |
| 222 | a.chatActionTo(w, r, workBack(r, ""), func(ctx context.Context, address string) error { |
| 223 | err := a.store.Chat().StopConversationTurn(ctx, address, chi.URLParam(r, "id")) |
| 224 | if errors.Is(err, chat.ErrTerminal) { |
| 225 | return nil |
| 226 | } |
| 227 | return err |
| 228 | }) |
| 229 | } |
| 230 | |
| 231 | func (v chatView) HasOpen() bool { |
| 232 | for _, line := range v.Open { |
| 233 | for _, cards := range [][]chatCard{line.Cards, line.Receipts} { |
| 234 | for _, c := range cards { |
| 235 | if c.Open { |
| 236 | return true |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | } |
| 241 | return false |
| 242 | } |
| 243 | |
| 244 | func rosterTone(t wire.Task) string { |
| 245 | if t.Checks == wire.ChecksFailed || t.Verdict == "changes_requested" { |
| 246 | return "negative" |
| 247 | } |
| 248 | if t.NeedsYou != "" { |
| 249 | return "warning" |
| 250 | } |
| 251 | return "positive" |
| 252 | } |
| 253 | |
| 254 | func (v chatCard) AsideTitle() string { |
| 255 | if v.Presentation.Context.TaskTitle != "" { |
| 256 | return v.Presentation.Context.TaskTitle |
| 257 | } |
| 258 | if v.Task != nil { |
| 259 | return v.Task.Title |
| 260 | } |
| 261 | if v.Name != "" { |
| 262 | return v.Name |
| 263 | } |
| 264 | if v.Kind == wire.CardQuestion { |
| 265 | return copyText("roster.question") |
| 266 | } |
| 267 | return copyText("roster.decision") |
| 268 | } |
| 269 | |
| 270 | func (v taskLine) RosterTone() string { return rosterTone(v.Task) } |
| 271 | |
| 272 | // Eyebrow is the report's short state. A task that needs the person |
| 273 | // names its stage: the chip beside it says Needs you, once. |
| 274 | func (v taskLine) Eyebrow() string { |
| 275 | if v.Pause != nil { |
| 276 | // The pause's lead and stop say it. |
| 277 | return "" |
| 278 | } |
| 279 | if v.Moving || v.Stopping { |
| 280 | return placementPendingLine(v.Task) |
| 281 | } |
| 282 | if v.NeedsYou != "" { |
| 283 | for _, s := range v.Track { |
| 284 | if s.State == "current" { |
| 285 | return s.Label |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | return v.StageLine |
| 290 | } |
| 291 | |
| 292 | // Why is the reason a task needs the person, as a sentence under the |
| 293 | // title, without the words the chip already says. |
| 294 | func (v taskLine) Why() string { |
| 295 | if v.NeedsYou == "" || v.Pause != nil || v.StageLine == v.Eyebrow() { |
| 296 | return "" |
| 297 | } |
| 298 | why := strings.TrimPrefix(v.StageLine, copyText("nav.needs-you")+": ") |
| 299 | return strings.ToUpper(why[:1]) + why[1:] |
| 300 | } |
| 301 | func (v taskLine) RosterChecks() string { |
| 302 | if v.Step != nil && v.Step.Specialist == wire.SpecialistChecks && (v.Step.State == wire.StepRunning || v.Step.State == wire.StepQueued) { |
| 303 | return copyText("roster.checks_running") |
| 304 | } |
| 305 | return copyText("roster.checks_" + v.Checks) |
| 306 | } |
| 307 | func (v taskLine) RosterReview() string { |
| 308 | if v.Step != nil && v.Step.Specialist == wire.SpecialistReview && (v.Step.State == wire.StepRunning || v.Step.State == wire.StepQueued) { |
| 309 | return copyText("roster.review_running") |
| 310 | } |
| 311 | return copyText("roster.review_" + v.Verdict) |
| 312 | } |
| 313 | |
| 314 | func (v taskLine) RosterChecksDetail() string { |
| 315 | detail := "" |
| 316 | switch v.Checks { |
| 317 | case wire.ChecksCannotRun: |
| 318 | key := map[string]string{"needs_macos": "roster.checks_needs_macos", "needs_network": "roster.checks_needs_network", "checks_timeout": "roster.checks_timeout"}[v.ChecksReason] |
| 319 | if key == "" { |
| 320 | key = "roster.checks_unavailable" |
| 321 | } |
| 322 | detail = copyText(key) |
| 323 | case wire.ChecksPassed, wire.ChecksFailed: |
| 324 | for _, s := range v.Steps { |
| 325 | if s.Specialist != wire.SpecialistChecks || s.State != wire.StepDone || s.CandidateSHA != v.CandidateSHA { |
| 326 | continue |
| 327 | } |
| 328 | detail = s.Gate |
| 329 | if s.ExitCode != nil && v.Checks == wire.ChecksFailed { |
| 330 | detail = fmt.Sprintf(copyText("roster.checks_exit"), s.Gate, *s.ExitCode) |
| 331 | } |
| 332 | } |
| 333 | if v.ChecksSandbox == wire.SandboxNone { |
| 334 | detail = outsideSandbox(detail, v.ChecksSandbox) |
| 335 | } |
| 336 | } |
| 337 | if v.Step != nil && v.Step.Specialist == wire.SpecialistChecks && (v.Step.State == wire.StepRunning || v.Step.State == wire.StepQueued) { |
| 338 | detail = v.ChecksLine |
| 339 | } |
| 340 | return strings.TrimPrefix(detail, ", ") |
| 341 | } |
| 342 | func (v taskLine) RosterReviewDetail() string { return copyText("work.review." + v.ReviewLabel) } |
| 343 | |
| 344 | func (v chatCard) ContextTask() string { |
| 345 | title := v.Presentation.Context.TaskTitle |
| 346 | if v.Kind == wire.CardTask || (v.Open && strings.Contains(v.Line, title)) { |
| 347 | return "" |
| 348 | } |
| 349 | return title |
| 350 | } |
| 351 | |