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 "encoding/json"
5 "time"
6
7 "amadan.net/oficina/equipo/internal/wire"
8)
9
10type alertText struct {
11 Title string `json:"title"`
12 Body string `json:"body"`
13}
14
15type aps struct {
16 Alert *alertText `json:"alert,omitempty"`
17 Badge *int `json:"badge,omitempty"`
18 Sound string `json:"sound,omitempty"`
19 Category string `json:"category,omitempty"`
20 ThreadID string `json:"thread-id,omitempty"`
21 InterruptionLevel string `json:"interruption-level,omitempty"`
22 Timestamp int64 `json:"timestamp,omitempty"`
23 Event string `json:"event,omitempty"`
24 ContentState *wire.LiveActivityState `json:"content-state,omitempty"`
25 AttributesType string `json:"attributes-type,omitempty"`
26 Attributes *wire.LiveActivityAttributes `json:"attributes,omitempty"`
27 DismissalDate int64 `json:"dismissal-date,omitempty"`
28}
29
30type payload struct {
31 APS aps `json:"aps"`
32 Equipo *wire.PushData `json:"equipo,omitempty"`
33}
34
35// EndShown is how long an ended task's Live Activity stays on the Lock
36// Screen; one replaced by a newer task's goes at once.
37const EndShown = time.Hour
38
39// Render is an intent's APNs payload and push type, its words from copy
40// (Intent literals first), badge the person's Needs you count for a badge.
41func Render(in Intent, copy func(string) string, badge int, now time.Time) ([]byte, string, error) {
42 text := func(literal, key string) string {
43 if literal != "" {
44 return literal
45 }
46 if key == "" {
47 return ""
48 }
49 return copy(key)
50 }
51 p := payload{}
52 typ := TypeAlert
53 switch in.Kind {
54 case KindAlert, KindPassive:
55 p.APS.Alert = &alertText{Title: text(in.Title, in.TitleKey), Body: text(in.Body, in.BodyKey)}
56 p.APS.Category = in.Category
57 data := in.Data
58 p.Equipo = &data
59 if in.Kind == KindPassive {
60 p.APS.InterruptionLevel = "passive"
61 } else {
62 p.APS.Sound = "default"
63 }
64 if in.Data.TaskID != "" {
65 p.APS.ThreadID = "task:" + in.Data.TaskID
66 }
67 case KindBadge:
68 n := badge
69 p.APS.Badge = &n
70 case KindActivityStart, KindActivityUpdate, KindActivityEnd:
71 typ = TypeLiveActivity
72 p.APS.Timestamp = now.Unix()
73 p.APS.ContentState = in.State
74 switch in.Kind {
75 case KindActivityStart:
76 p.APS.Event = "start"
77 p.APS.AttributesType = wire.LiveActivityAttributesType
78 p.APS.Attributes = &wire.LiveActivityAttributes{TaskID: in.TaskID, Origin: in.Origin}
79 case KindActivityUpdate:
80 p.APS.Event = "update"
81 case KindActivityEnd:
82 p.APS.Event = "end"
83 p.APS.DismissalDate = now.Add(EndShown).Unix()
84 if in.State != nil && in.State.Outcome == wire.PushOutcomeReplaced {
85 p.APS.DismissalDate = now.Unix()
86 }
87 }
88 if title := text(in.Title, in.TitleKey); title != "" {
89 p.APS.Alert = &alertText{Title: title, Body: text(in.Body, in.BodyKey)}
90 p.APS.Sound = "default"
91 }
92 }
93 raw, err := json.Marshal(p)
94 if err == nil && len(raw) > MaxPayload {
95 return nil, typ, ErrTooLarge
96 }
97 return raw, typ, err
98}
99
100// TopicFor is the apns-topic of a push type.
101func TopicFor(pushType string) string {
102 if pushType == TypeLiveActivity {
103 return Topic + ".push-type.liveactivity"
104 }
105 return Topic
106}
107
108// Expiration is how long Apple keeps trying a push of a kind: an alert or
109// badge a day, a Live Activity change an hour (a newer one follows).
110func Expiration(kind string, created time.Time) time.Time {
111 switch kind {
112 case KindActivityStart, KindActivityUpdate, KindActivityEnd:
113 return created.Add(time.Hour)
114 }
115 return created.Add(24 * time.Hour)
116}
117