| 1 | package conversation |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/equipo/internal/equipo/library" |
| 5 | "amadan.net/oficina/equipo/internal/equipo/planning" |
| 6 | "fmt" |
| 7 | "time" |
| 8 | ) |
| 9 | |
| 10 | func (v *Page) PrepareChat(address string, specialists []library.Specialist, taskView func(string) (planning.TaskViewData, error), copyText func(string) string) error { |
| 11 | for i := range v.Messages { |
| 12 | m := &v.Messages[i] |
| 13 | m.Own = m.AuthorKind == "person" && m.AuthorID == address |
| 14 | m.AuthorName, m.AuthorIcon = m.AuthorID, "users" |
| 15 | switch m.AuthorKind { |
| 16 | case "coordinator": |
| 17 | m.AuthorName, m.AuthorIcon = copyText("conversation.coordinator"), "message-circle" |
| 18 | case "specialist": |
| 19 | m.AuthorIcon = "code" |
| 20 | for _, s := range specialists { |
| 21 | if s.Key == m.AuthorID { |
| 22 | m.AuthorIcon = s.Icon |
| 23 | m.AuthorName = s.Name |
| 24 | if m.AuthorName == "" { |
| 25 | m.AuthorName = copyText("nav." + s.Key) |
| 26 | } |
| 27 | break |
| 28 | } |
| 29 | } |
| 30 | case "person": |
| 31 | default: |
| 32 | m.AuthorName, m.AuthorIcon = copyText("brand"), "info" |
| 33 | } |
| 34 | } |
| 35 | if v.ThreadID != "" { |
| 36 | v.Cards = append(v.Cards, CardView{Card: Card{ID: "thread-task", Kind: "task", ResourceID: v.ThreadID, Sequence: -1}, Plan: v.ThreadPlan}) |
| 37 | } |
| 38 | v.HasTaskCards = false |
| 39 | tasks := map[string]*planning.TaskViewData{} |
| 40 | for i := range v.Cards { |
| 41 | c := &v.Cards[i] |
| 42 | id := c.TaskID |
| 43 | if c.Kind == "task" { |
| 44 | id = c.ResourceID |
| 45 | } |
| 46 | if id == "" { |
| 47 | continue |
| 48 | } |
| 49 | work := tasks[id] |
| 50 | if work == nil { |
| 51 | w, err := taskView(id) |
| 52 | if err != nil { |
| 53 | return err |
| 54 | } |
| 55 | if !w.CreatedAt.IsZero() && copyText != nil { |
| 56 | elapsed := max(time.Duration(0), v.Now.Sub(w.CreatedAt)) |
| 57 | n, key := int(elapsed.Minutes()), "conversation.age_minutes" |
| 58 | if elapsed >= 24*time.Hour { |
| 59 | n, key = int(elapsed.Hours()/24), "conversation.age_days" |
| 60 | } else if elapsed >= time.Hour { |
| 61 | n, key = int(elapsed.Hours()), "conversation.age_hours" |
| 62 | } |
| 63 | w.ChatAge = fmt.Sprintf(copyText(key), n) |
| 64 | } |
| 65 | work = &w |
| 66 | tasks[id] = work |
| 67 | } |
| 68 | if c.Kind == "task" { |
| 69 | v.HasTaskCards = true |
| 70 | } |
| 71 | c.Work = work |
| 72 | if c.Kind == "question" { |
| 73 | for j := range work.Questions { |
| 74 | if work.Questions[j].ID == c.ResourceID { |
| 75 | c.NativeQuestion = &work.Questions[j] |
| 76 | break |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | if work := tasks[v.ThreadID]; work != nil { |
| 82 | v.ThreadTitle = work.Task.Description |
| 83 | for i := range work.Questions { |
| 84 | q := &work.Questions[i] |
| 85 | if q.State == "pending" { |
| 86 | v.Cards = append(v.Cards, CardView{Card: Card{ID: "native:" + q.ID, Kind: "question", TaskID: v.ThreadID, ResourceID: q.ID}, Work: work, NativeQuestion: q}) |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | v.BuildFeed() |
| 91 | return nil |
| 92 | } |
| 93 | |