| 1 | package conversation |
| 2 | |
| 3 | import ( |
| 4 | sends "amadan.net/oficina/equipo/internal/chatsend" |
| 5 | "context" |
| 6 | "crypto/rand" |
| 7 | "encoding/json" |
| 8 | "errors" |
| 9 | "net/url" |
| 10 | "time" |
| 11 | |
| 12 | "amadan.net/oficina/equipo/internal/equipo/planning" |
| 13 | "amadan.net/oficina/equipo/internal/runnerproto" |
| 14 | "amadan.net/rastrillo/rastrillo/live" |
| 15 | "gorm.io/gorm" |
| 16 | ) |
| 17 | |
| 18 | type MessageView struct { |
| 19 | ClientRequestID string |
| 20 | Message |
| 21 | RouteID, ThreadURL string |
| 22 | AuthorName, AuthorIcon string |
| 23 | Own bool |
| 24 | } |
| 25 | type CardView struct { |
| 26 | ProjectProposal *ProjectProposal |
| 27 | Card |
| 28 | Work *planning.TaskViewData |
| 29 | NativeQuestion *planning.TaskQuestion |
| 30 | Plan *planning.Page |
| 31 | Question *runnerproto.ClarificationQuestion |
| 32 | CanAnswer bool |
| 33 | RequestID, ResetID string |
| 34 | BindingRevision int64 |
| 35 | WaitingLabel string |
| 36 | } |
| 37 | type Page struct { |
| 38 | OwnerMode bool |
| 39 | HasTaskCards bool |
| 40 | ThreadTitle string |
| 41 | Today string |
| 42 | Now time.Time |
| 43 | Projects []planning.ProjectOption |
| 44 | ThreadPlan *planning.Page |
| 45 | Live live.View |
| 46 | Topic, ThreadID, RequestID, NextURL string |
| 47 | Revision int64 |
| 48 | Messages []MessageView |
| 49 | Feed []Entry |
| 50 | Cards []CardView |
| 51 | Tasks []planning.ProjectOption |
| 52 | BindingRevision int64 |
| 53 | } |
| 54 | |
| 55 | func (s *Application) View(ctx context.Context, a Actor, task, cursor string, plans *planning.Application) (out Page, err error) { |
| 56 | a.Address = s.Normalize(a.Address) |
| 57 | out.ThreadID = task |
| 58 | out.Now = s.Now().UTC() |
| 59 | out.Today = out.Now.Format("2006-01-02") |
| 60 | out.RequestID = rand.Text() |
| 61 | err = s.Write(ctx, func(tx *gorm.DB) error { |
| 62 | cs := s.Conversation(tx) |
| 63 | if _, err := cs.deps.Authorize(tx, a); err != nil { |
| 64 | return err |
| 65 | } |
| 66 | if err := tx.Table("projects").Select("id", "name").Order("name, id").Find(&out.Projects).Error; err != nil { |
| 67 | return err |
| 68 | } |
| 69 | var page ConversationPage |
| 70 | if task == "" { |
| 71 | main, e := cs.main(a) |
| 72 | if e != nil { |
| 73 | return e |
| 74 | } |
| 75 | out.Topic = ConversationTopic(main.TeamID, a.Address, main.ID) |
| 76 | page, err = cs.Conversation(a, cursor, 50) |
| 77 | if err != nil { |
| 78 | return err |
| 79 | } |
| 80 | all, e := cs.allCards(a, main) |
| 81 | if e != nil { |
| 82 | return e |
| 83 | } |
| 84 | for _, c := range page.Cards { |
| 85 | if c.Kind == "coordinator" { |
| 86 | all = append(all, c) |
| 87 | } |
| 88 | } |
| 89 | if cursor == "" { |
| 90 | page, out.NextURL, err = cs.recentPage(a, main.ID, main.LastSequence, main.Revision) |
| 91 | } |
| 92 | page.Cards = all |
| 93 | } else { |
| 94 | thread, _, e := cs.thread(a, task) |
| 95 | if e != nil { |
| 96 | return e |
| 97 | } |
| 98 | out.Topic = ConversationTopic(thread.TeamID, "", thread.ID) |
| 99 | page, err = cs.TaskThread(a, task, cursor, 50) |
| 100 | if err == nil && cursor == "" { |
| 101 | page, out.NextURL, err = cs.recentPage(a, thread.ID, thread.LastSequence, thread.Revision) |
| 102 | } |
| 103 | } |
| 104 | if err != nil { |
| 105 | return err |
| 106 | } |
| 107 | if task != "" { |
| 108 | p, e := plans.At(tx).Page(a, task) |
| 109 | if e != nil { |
| 110 | return e |
| 111 | } |
| 112 | out.ThreadPlan = &p |
| 113 | } |
| 114 | out.Revision = page.Revision |
| 115 | if page.NextCursor != "" { |
| 116 | out.NextURL = "?cursor=" + url.QueryEscape(page.NextCursor) |
| 117 | } |
| 118 | requests, err := sends.Requests(tx, a.Address, page.Messages, func(m Message) string { return m.ID }) |
| 119 | if err != nil { |
| 120 | return err |
| 121 | } |
| 122 | for _, m := range page.Messages { |
| 123 | mv := MessageView{Message: m, RouteID: rand.Text(), ClientRequestID: requests[m.ID]} |
| 124 | if task == "" { |
| 125 | var route routeRecord |
| 126 | e := tx.Where("notice_id=?", m.ID).Take(&route).Error |
| 127 | if e == nil { |
| 128 | mv.ThreadURL = "/tasks/" + url.PathEscape(route.TaskID) + "/thread" |
| 129 | } else if !errors.Is(e, gorm.ErrRecordNotFound) { |
| 130 | return e |
| 131 | } |
| 132 | } |
| 133 | out.Messages = append(out.Messages, mv) |
| 134 | } |
| 135 | b, e := cs.Binding(a) |
| 136 | if e != nil && !errors.Is(e, s.NotFound) { |
| 137 | return e |
| 138 | } |
| 139 | out.BindingRevision = b.Revision |
| 140 | if b.ID != "" && s.BindingSelection != nil { |
| 141 | if sel, err := s.BindingSelection(tx, b); err == nil { |
| 142 | out.OwnerMode = sel.Isolation.Mode == "owner mode" |
| 143 | } |
| 144 | } |
| 145 | for _, card := range page.Cards { |
| 146 | v := CardView{Card: card, RequestID: rand.Text(), ResetID: rand.Text(), BindingRevision: b.Revision} |
| 147 | switch card.Kind { |
| 148 | case "task": |
| 149 | p, e := plans.At(tx).Page(a, card.ResourceID) |
| 150 | if e != nil { |
| 151 | return e |
| 152 | } |
| 153 | v.Plan = &p |
| 154 | case "project_setup": |
| 155 | p, e := cs.projectProposal(a, card.ResourceID) |
| 156 | if e != nil { |
| 157 | return e |
| 158 | } |
| 159 | if p.State == "pending" && (p.BindingID != b.ID || p.Generation != b.Generation) { |
| 160 | p.State = "superseded" |
| 161 | } |
| 162 | v.ProjectProposal = &p |
| 163 | v.CanAnswer = p.State == "pending" |
| 164 | case "clarification": |
| 165 | q, can, e := cs.questionView(a, card.ResourceID, s.Now().UTC()) |
| 166 | if e != nil { |
| 167 | return e |
| 168 | } |
| 169 | v.Question = &q |
| 170 | v.CanAnswer = can |
| 171 | case "coordinator": |
| 172 | if card.WaitingTaskID != "" { |
| 173 | var row struct{ Description string } |
| 174 | e := tx.Table("tasks").Where("id=? AND team_id=?", card.WaitingTaskID, b.TeamID).Select("description").Take(&row).Error |
| 175 | if e != nil { |
| 176 | return e |
| 177 | } |
| 178 | v.WaitingLabel = row.Description |
| 179 | } |
| 180 | } |
| 181 | out.Cards = append(out.Cards, v) |
| 182 | } |
| 183 | return nil |
| 184 | }) |
| 185 | return |
| 186 | } |
| 187 | func (s *Store) questionView(a Actor, id string, now time.Time) (q runnerproto.ClarificationQuestion, can bool, err error) { |
| 188 | b, err := s.Binding(a) |
| 189 | if err != nil { |
| 190 | return |
| 191 | } |
| 192 | var rec QuestionRecord |
| 193 | if err = s.tx.Where("id=? AND binding_id=? AND person=?", id, b.ID, a.Address).Take(&rec).Error; err != nil { |
| 194 | return |
| 195 | } |
| 196 | if err = json.Unmarshal([]byte(rec.Payload), &q); err != nil { |
| 197 | return |
| 198 | } |
| 199 | var turn TurnRecord |
| 200 | if err = s.tx.Where("id=? AND binding_id=?", rec.TurnID, b.ID).Take(&turn).Error; err != nil { |
| 201 | return |
| 202 | } |
| 203 | q.State, q.Revision = rec.State, rec.Revision |
| 204 | if q.State == "pending" { |
| 205 | switch { |
| 206 | case !now.Before(q.ExpiresAt): |
| 207 | q.State = "expired" |
| 208 | case rec.Generation != b.Generation || turn.Generation != b.Generation || (turn.State != "running" && turn.State != "delivered"): |
| 209 | q.State = "superseded" |
| 210 | default: |
| 211 | can = true |
| 212 | } |
| 213 | } |
| 214 | return |
| 215 | } |
| 216 | |
| 217 | func (s *Application) Question(ctx context.Context, a Actor, id string) (q runnerproto.ClarificationQuestion, can bool, err error) { |
| 218 | a.Address = s.Normalize(a.Address) |
| 219 | err = s.Write(ctx, func(tx *gorm.DB) error { |
| 220 | var e error |
| 221 | q, can, e = s.Conversation(tx).questionView(a, id, s.Now().UTC()) |
| 222 | if errors.Is(e, gorm.ErrRecordNotFound) { |
| 223 | return s.NotFound |
| 224 | } |
| 225 | return e |
| 226 | }) |
| 227 | return |
| 228 | } |
| 229 | |