oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package push
2
3import (
4 "strings"
5 "time"
6 "unicode/utf8"
7
8 "amadan.net/oficina/equipo/internal/wire"
9)
10
11// Changes the engine reports, each from the transaction that made it.
12const (
13 ChangeCard = "card" // a card was written (cardEvent)
14 ChangeReply = "reply" // a turn was saved (saveTurn)
15 ChangeTask = "task" // a task was written (taskEvent)
16 ChangeLine = "line" // a running step reported an activity line
17)
18
19// Change is one engine change, as the rules read it. Only the fields of
20// its kind are set.
21type Change struct {
22 Kind string
23 Now time.Time
24 Card Card
25 // Reply is a saved turn; Streaming whether the person has an /events
26 // stream open now.
27 Reply Reply
28 Streaming bool
29 // Before is what the person's push memory held for the task; After the
30 // task now. A line change carries the new line in After.Activity.
31 Before Memory
32 After Task
33}
34
35// Card is a card as the rules read it. It alerts once in its life: its
36// alert's once-only key (Dedupe) is decided once for the person.
37type Card struct {
38 ID, Kind, State, Lane, Question, TaskID string
39 // AccessFinal: an access card whose request already ended.
40 AccessFinal bool
41}
42
43// Reply is a saved turn and its reply's text.
44type Reply struct {
45 TurnID, Lane, Kind, State, Text string
46}
47
48// Task is a task the person started, as a Live Activity shows it.
49type Task struct {
50 ID, Title, Stage, NeedsYou, Specialist, StepID, Activity, Tone string
51 // Working: a step of it is running on a runner (it was dispatched).
52 Working bool
53}
54
55// Final reports whether the task ended.
56func (k Task) Final() bool { return wire.FinalStage(k.Stage) }
57
58// Memory is what the person's devices were last told about a task.
59type Memory struct {
60 Seen bool
61 Title, Tone, Stage, NeedsYou, Specialist, StepID, Activity string
62 // Started is when its Live Activity started; Ended once it ended.
63 Started *time.Time
64 Ended bool
65 // LineAt is when an activity line last updated the Live Activity.
66 LineAt *time.Time
67}
68
69// Task is the task as the memory last showed it.
70func (m Memory) Task(id string) Task {
71 return Task{ID: id, Title: m.Title, Tone: m.Tone, Stage: m.Stage, NeedsYou: m.NeedsYou, Specialist: m.Specialist, StepID: m.StepID, Activity: m.Activity}
72}
73
74// Remember is the memory after the devices were told about k.
75func (m Memory) Remember(k Task) Memory {
76 m.Seen, m.Title, m.Tone, m.Stage, m.NeedsYou, m.Specialist, m.StepID, m.Activity = true, k.Title, k.Tone, k.Stage, k.NeedsYou, k.Specialist, k.StepID, k.Activity
77 return m
78}
79
80// Live reports whether the task's Live Activity is running.
81func (m Memory) Live() bool { return m.Started != nil && !m.Ended }
82
83// Intent is one push a rule asks for: its kind, priority and collapse ID,
84// a once-only key (Dedupe: the push is decided once for the person, ever)
85// or Coalesce (a newer one replaces one not yet
86// sent), its words (literal Title and Body, else copy keys), where a tap
87// goes (Data), and a Live Activity's state.
88type Intent struct {
89 Rule string
90 Kind string
91 Priority int
92 CollapseID string
93 Dedupe string
94 Coalesce bool
95 Title string
96 TitleKey string
97 Body string
98 BodyKey string
99 Category string
100 Data wire.PushData
101 State *wire.LiveActivityState
102 // TaskID names the task a Live Activity push follows.
103 TaskID string
104 // Origin is the instance's origin, named in a start's attributes.
105 Origin string
106}
107
108// LineEvery is how often an activity line may update a Live Activity, to
109// stay inside Apple's update budget.
110const LineEvery = time.Minute
111
112// CategoryNeedsYou is the alert category whose only action is Open (D4).
113const CategoryNeedsYou = "needs_you"
114
115// Rule is one row of the table: when it applies to a change, and the push.
116type Rule struct {
117 Name string
118 When func(Change) bool
119 Push func(Change) Intent
120}
121
122// Rules is the table: every rule that applies to a change pushes.
123var Rules = []Rule{
124 // A card that waits on the person opens: an alert, collapsed by card.
125 {"card_opens", func(c Change) bool {
126 return c.Kind == ChangeCard && c.Card.Lane == wire.LaneCoordinator && waits(c.Card)
127 }, func(c Change) Intent {
128 in := Intent{Kind: KindAlert, Priority: 10, CollapseID: c.Card.ID, Dedupe: "card:" + c.Card.ID, TitleKey: "push.card.title", Body: Clip(c.Card.Question), Category: CategoryNeedsYou, Data: wire.PushData{Kind: wire.PushCard, CardID: c.Card.ID, TaskID: c.Card.TaskID}}
129 if in.Body == "" {
130 in.BodyKey = "push.card.body"
131 }
132 return in
133 }},
134 // The coordinator replied to the person while no stream is open: an
135 // alert with the reply's first line (D2).
136 {"reply_unseen", func(c Change) bool {
137 r := c.Reply
138 return c.Kind == ChangeReply && r.Lane == wire.LaneCoordinator && r.Kind == wire.TurnPerson && r.State == wire.TurnReplied && !c.Streaming && FirstLine(r.Text) != ""
139 }, func(c Change) Intent {
140 return Intent{Kind: KindAlert, Priority: 10, CollapseID: "reply:" + c.Reply.TurnID, Dedupe: "reply:" + c.Reply.TurnID, TitleKey: "push.reply.title", Body: FirstLine(c.Reply.Text), Data: wire.PushData{Kind: wire.PushReply, TurnID: c.Reply.TurnID}}
141 }},
142 // A task the person started ends: a passive alert with its outcome.
143 {"task_ends", func(c Change) bool {
144 return c.Kind == ChangeTask && c.Before.Seen && c.After.Final() && !wire.FinalStage(c.Before.Stage)
145 }, func(c Change) Intent {
146 in := taskAlert(c, outcome(c.After))
147 in.Dedupe = "outcome:" + c.After.ID + ":" + outcome(c.After)
148 return in
149 }},
150 // A step of it failed (it waits for Retry): a passive alert too.
151 {"task_fails", func(c Change) bool {
152 return c.Kind == ChangeTask && c.Before.Seen && !c.After.Final() && c.After.NeedsYou == wire.NeedsRetry && c.Before.NeedsYou != wire.NeedsRetry
153 }, func(c Change) Intent {
154 return taskAlert(c, wire.PushOutcomeFailed)
155 }},
156 // Needs you changed: the badge, the newest count replacing any unsent.
157 {"badge", func(c Change) bool {
158 return c.Kind == ChangeTask && c.Before.Seen && c.Before.NeedsYou != c.After.NeedsYou
159 }, func(c Change) Intent {
160 return Badge()
161 }},
162 // It was dispatched: its Live Activity starts, by push-to-start.
163 {"activity_start", func(c Change) bool {
164 return c.Kind == ChangeTask && c.After.Working && !c.After.Final() && c.Before.Started == nil
165 }, func(c Change) Intent {
166 return Intent{Kind: KindActivityStart, Priority: 10, TaskID: c.After.ID, Title: Clip(c.After.Title), BodyKey: "push.activity.started", State: state(c.After, "")}
167 }},
168 // Its stage, step or needs-you changed: an update, urgent when it now
169 // needs the person. Never with an alert: what needs the person sounds
170 // once, by its card's alert (or a failed step's passive one), not
171 // again on the Lock Screen's activity.
172 {"activity_update", func(c Change) bool {
173 b, a := c.Before, c.After
174 return c.Kind == ChangeTask && b.Live() && !a.Final() && (b.Stage != a.Stage || b.StepID != a.StepID || b.Specialist != a.Specialist || b.NeedsYou != a.NeedsYou)
175 }, func(c Change) Intent {
176 in := Intent{Kind: KindActivityUpdate, Priority: 5, TaskID: c.After.ID, State: state(c.After, "")}
177 if c.After.NeedsYou != "" && c.After.NeedsYou != c.Before.NeedsYou {
178 in.Priority = 10
179 }
180 return in
181 }},
182 // A new activity line: an update at most once a minute a task.
183 {"activity_line", func(c Change) bool {
184 b := c.Before
185 return c.Kind == ChangeLine && b.Live() && c.After.Activity != b.Activity && (b.LineAt == nil || c.Now.Sub(*b.LineAt) >= LineEvery)
186 }, func(c Change) Intent {
187 return Intent{Kind: KindActivityUpdate, Priority: 5, TaskID: c.After.ID, State: state(c.After, "")}
188 }},
189 // It ended: the Live Activity ends with the outcome.
190 {"activity_end", func(c Change) bool {
191 return c.Kind == ChangeTask && c.Before.Live() && c.After.Final()
192 }, func(c Change) Intent {
193 return Intent{Kind: KindActivityEnd, Priority: 10, TaskID: c.After.ID, State: state(c.After, outcome(c.After))}
194 }},
195}
196
197// Decide is every push the table asks for a change, in table order.
198func Decide(c Change) []Intent {
199 var out []Intent
200 for _, r := range Rules {
201 if r.When(c) {
202 in := r.Push(c)
203 in.Rule = r.Name
204 out = append(out, in)
205 }
206 }
207 return out
208}
209
210// Badge is the person's Needs you count, the newest replacing any unsent.
211func Badge() Intent {
212 return Intent{Rule: "badge", Kind: KindBadge, Priority: 5, CollapseID: "badge", Coalesce: true, Data: wire.PushData{Kind: wire.PushBadge}}
213}
214
215// CatchUp is a running activity's whole state, sent when its own token
216// arrives: what it missed while the server had no token for it.
217func CatchUp(k Task) Intent {
218 return Intent{Rule: "activity_token", Kind: KindActivityUpdate, Priority: 10, TaskID: k.ID, State: state(k, "")}
219}
220
221// Replace ends the Live Activity of a task to make room for a newer one.
222func Replace(k Task) Intent {
223 return Intent{Rule: "activity_cap", Kind: KindActivityEnd, Priority: 5, TaskID: k.ID, State: state(k, wire.PushOutcomeReplaced)}
224}
225
226// waits reports whether a card waits on its person: open (or asking), not
227// a receipt, and not an access request that already ended.
228func waits(c Card) bool {
229 if c.State != wire.CardOpen && c.State != wire.CardHomeAsk {
230 return false
231 }
232 switch c.Kind {
233 case wire.CardProject, wire.CardTask, wire.CardCombined, wire.CardHousekeeping, wire.CardDigest, wire.CardMergeReceipt:
234 return false
235 case wire.CardHome:
236 return c.State == wire.CardHomeAsk
237 case wire.CardAccess:
238 return !c.AccessFinal
239 }
240 return true
241}
242
243func taskAlert(c Change, outcome string) Intent {
244 return Intent{Kind: KindPassive, Priority: 5, CollapseID: "task:" + c.After.ID, Title: Clip(c.After.Title), BodyKey: "push.task." + outcome, Data: wire.PushData{Kind: wire.PushTask, TaskID: c.After.ID}}
245}
246
247func outcome(k Task) string {
248 if k.Stage == wire.StageDone {
249 return wire.PushOutcomeDone
250 }
251 return wire.PushOutcomeCancelled
252}
253
254func state(k Task, outcome string) *wire.LiveActivityState {
255 return &wire.LiveActivityState{TaskID: k.ID, Title: Clip(k.Title), Stage: k.Stage, Specialist: k.Specialist, Activity: k.Activity, NeedsYou: k.NeedsYou, Tone: k.Tone, Outcome: outcome}
256}
257
258// MaxText bounds an alert's title or body, so a payload stays well under
259// APNs' 4 KB.
260const MaxText = 178
261
262// Clip trims s to MaxText runes, with an ellipsis when it cut.
263func Clip(s string) string {
264 s = strings.TrimSpace(s)
265 if utf8.RuneCountInString(s) <= MaxText {
266 return s
267 }
268 r := []rune(s)
269 return strings.TrimSpace(string(r[:MaxText-1])) + "…"
270}
271
272// FirstLine is a reply's first line with text, as plain words: leading
273// heading and list marks and emphasis stars dropped, clipped.
274func FirstLine(s string) string {
275 for line := range strings.SplitSeq(s, "\n") {
276 line = strings.TrimSpace(line)
277 line = strings.TrimLeft(line, "#>-*+ ")
278 line = strings.ReplaceAll(line, "**", "")
279 line = strings.ReplaceAll(line, "`", "")
280 if line = strings.TrimSpace(line); line != "" {
281 return Clip(line)
282 }
283 }
284 return ""
285}
286