oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package equipo
2
3import (
4 "fmt"
5 "slices"
6 "strings"
7 "time"
8
9 "amadan.net/oficina/equipo/internal/equipo/chat"
10 "amadan.net/oficina/equipo/internal/wire"
11)
12
13// The Projects page's order: starred projects pinned at the top, then the
14// active ones by what needs the viewer and then their latest activity,
15// and the quiet ones (no open tasks, nothing new for quietAfter) folded
16// into Quiet (N). The order is automatic; Equipo also stars on two
17// rules, deterministically (starSignals), and says so.
18
19// quietAfter is how long a project with no open tasks stays active.
20const quietAfter = 14 * 24 * time.Hour
21
22// descLimit is the most a row's description runs to: about two lines.
23const descLimit = 140
24
25// The auto-star rules (see starSignals).
26const (
27 // starWaitingMin: the project with the most tasks waiting on the
28 // viewer, when that is at least this many.
29 starWaitingMin = 2
30 // starBusyMin within starBusyWindow: the viewer noted at least this
31 // many tasks in the project from their conversation.
32 starBusyMin = 3
33 starBusyWindow = 7 * 24 * time.Hour
34)
35
36const (
37 starWaiting = "waiting"
38 starBusy = "busy"
39)
40
41// starSignals are the auto-star rules for viewer, from what their Tasks
42// page shows them. No model turn: plain counts.
43//
44// 1. waiting: the project with the most tasks waiting on the viewer's
45// own decision (their Needs you), when that is at least
46// starWaitingMin; projects tied at the most are all starred.
47// 2. busy: a project where the viewer noted starBusyMin or more tasks
48// from their conversation in the last starBusyWindow.
49//
50// A project both rules pick is starred for waiting, the more urgent.
51func starSignals(tasks []taskLine, viewer string, now time.Time) map[string]chat.StarSignal {
52 waiting, noted := map[string]int{}, map[string]int{}
53 for _, t := range tasks {
54 if t.ProjectID == "" {
55 continue
56 }
57 if t.Group == "needs" {
58 waiting[t.ProjectID]++
59 }
60 if t.Task.Creator == viewer && t.SourceMessageID != "" && now.Sub(t.CreatedAt) < starBusyWindow {
61 noted[t.ProjectID]++
62 }
63 }
64 out := map[string]chat.StarSignal{}
65 most := 0
66 for _, n := range waiting {
67 most = max(most, n)
68 }
69 for id, n := range noted {
70 if n >= starBusyMin {
71 out[id] = chat.StarSignal{Reason: starBusy, N: n}
72 }
73 }
74 if most >= starWaitingMin {
75 for id, n := range waiting {
76 if n == most {
77 out[id] = chat.StarSignal{Reason: starWaiting, N: n}
78 }
79 }
80 }
81 return out
82}
83
84// starWhy is an auto star's reason in words: "2 tasks are waiting on
85// you", "You started 3 tasks here this week"; one is singular.
86func starWhy(s chat.Star) string {
87 key := "work.star.why." + s.Reason
88 if s.N == 1 {
89 key += "_one"
90 }
91 return fmt.Sprintf(copyText(key), s.N)
92}
93
94// starLine is a row's star: on or off, and who set it, for the hover.
95type starLine struct {
96 On bool
97 Label, Who string
98 Auto, Fresh bool
99}
100
101func starLineOf(p projectLine, s chat.Star, ok bool, now time.Time) starLine {
102 l := starLine{Label: fmt.Sprintf(copyText("work.star.add"), p.Title)}
103 if !ok || !s.Starred {
104 l.Who = l.Label
105 return l
106 }
107 l.On, l.Label = true, fmt.Sprintf(copyText("work.star.remove"), p.Title)
108 switch s.By {
109 case chat.StarByAuto:
110 l.Auto, l.Who = true, fmt.Sprintf(copyText("work.star.by_auto"), starWhy(s))
111 l.Fresh = s.ToldAt != nil && now.Sub(*s.ToldAt) < 24*time.Hour
112 case chat.StarByCoordinator:
113 l.Who = copyText("work.star.by_coordinator")
114 default:
115 l.Who = copyText("work.star.by_you")
116 }
117 return l
118}
119
120// starReceipt is "Starred Woodstar: 2 tasks are waiting on you" with Undo,
121// for a day after the person is first shown it.
122type starReceipt struct {
123 ProjectID, Text string
124}
125
126// projectGroups is the Projects page's list: the starred, the active and
127// the quiet, each in order; Key the states present, for the list's key.
128type projectGroups struct {
129 Starred, Active, Quiet []projectLine
130 Receipts []starReceipt
131 Key []string
132 // Filter is the filter box's text; Filtering whether it shows.
133 Filter string
134 Filtering bool
135 Total int
136}
137
138// orderProjects sorts lines into the page's groups. Within Starred and
139// Active: most tasks waiting on the viewer first, then the latest
140// activity, then the title; Quiet by latest activity.
141func orderProjects(lines []projectLine, now time.Time) projectGroups {
142 var g projectGroups
143 g.Total = len(lines)
144 present := map[string]bool{}
145 for _, p := range lines {
146 for _, s := range p.Spread {
147 present[s.Key] = true
148 }
149 switch {
150 case p.Star.On:
151 g.Starred = append(g.Starred, p)
152 case p.Open == 0 && (p.LastAt.IsZero() || now.Sub(p.LastAt) >= quietAfter):
153 g.Quiet = append(g.Quiet, p)
154 default:
155 g.Active = append(g.Active, p)
156 }
157 if p.Star.Fresh {
158 g.Receipts = append(g.Receipts, starReceipt{ProjectID: p.ID, Text: fmt.Sprintf(copyText("work.star.receipt"), p.Title, p.StarWhy)})
159 }
160 }
161 byNeed := func(a, b projectLine) int {
162 if a.NeedsYou != b.NeedsYou {
163 return b.NeedsYou - a.NeedsYou
164 }
165 if c := b.LastAt.Compare(a.LastAt); c != 0 {
166 return c
167 }
168 return strings.Compare(strings.ToLower(a.Title), strings.ToLower(b.Title))
169 }
170 slices.SortStableFunc(g.Starred, byNeed)
171 slices.SortStableFunc(g.Active, byNeed)
172 slices.SortStableFunc(g.Quiet, func(a, b projectLine) int {
173 if c := b.LastAt.Compare(a.LastAt); c != 0 {
174 return c
175 }
176 return strings.Compare(strings.ToLower(a.Title), strings.ToLower(b.Title))
177 })
178 for _, k := range workGroups {
179 if present[k] {
180 g.Key = append(g.Key, k)
181 }
182 }
183 return g
184}
185
186// filterProjects keeps the projects whose title, name or description
187// holds q, case-insensitively.
188func filterProjects(lines []projectLine, q string) []projectLine {
189 q = strings.ToLower(strings.TrimSpace(q))
190 if q == "" {
191 return lines
192 }
193 var out []projectLine
194 for _, p := range lines {
195 if strings.Contains(strings.ToLower(p.Title+" "+p.Name+" "+p.Description+" "+strings.Join(p.Names, " ")+" "+strings.Join(p.PersonalNames, " ")), q) {
196 out = append(out, p)
197 }
198 }
199 return out
200}
201
202// shortDescription is a row's description: its first sentence when that
203// fits, else cut on a whole word with an ellipsis, never mid-word.
204func shortDescription(s string) string {
205 s = wholeWords(s)
206 if i := sentenceEnd(s); i > 0 && i <= descLimit {
207 return s[:i]
208 }
209 return clipWords(s, descLimit)
210}
211
212// wholeWords drops a word the runner's catalog cut short: a description
213// at the catalog's limit ends mid-word ("proo"), so it ends on the last
214// whole word with an ellipsis instead.
215func wholeWords(s string) string {
216 s = strings.TrimSpace(s)
217 if len(s) < wire.MaxRepoString {
218 return s
219 }
220 return clipWords(s, len(s)-1)
221}
222
223// sentenceEnd is the index just after the first sentence's full stop,
224// or 0 when s is one sentence.
225func sentenceEnd(s string) int {
226 for i := 0; i+1 < len(s); i++ {
227 if (s[i] == '.' || s[i] == '!' || s[i] == '?') && s[i+1] == ' ' {
228 // Not after a one-letter abbreviation ("e.g. ").
229 if i >= 2 && s[i-2] == '.' {
230 continue
231 }
232 return i + 1
233 }
234 }
235 return 0
236}
237