oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1// Package push is Equipo's push notifications for the native apps (plan
2// 2026-11-20-equipo-native-slice-2.md): device registrations, the outbox
3// the conversation engine writes in the transaction of each change, the
4// rule table deciding what pushes whom, the APNs payloads, and the sender
5// that drains the outbox through a Transport.
6//
7// Tokens are secrets: they live only in the registration tables and are
8// handed to the transport. They are never logged, audited or echoed.
9package push
10
11import (
12 "context"
13 "errors"
14 "time"
15)
16
17// Topic is the apps' bundle ID; a Live Activity's topic adds
18// ".push-type.liveactivity".
19const Topic = "works.oficina.equipo"
20
21// Kinds of push, each one outbox row.
22const (
23 KindAlert = "alert"
24 KindPassive = "passive"
25 KindBadge = "badge"
26 KindActivityStart = "activity_start"
27 KindActivityUpdate = "activity_update"
28 KindActivityEnd = "activity_end"
29)
30
31// APNs push types (apns-push-type).
32const (
33 TypeAlert = "alert"
34 TypeLiveActivity = "liveactivity"
35)
36
37// Targets: which of a device's tokens a row goes to.
38const (
39 TargetDevice = "device" // the registration's APNs token
40 TargetStart = "start" // its Live Activity push-to-start token
41 TargetActivity = "activity" // a running activity's own update token
42)
43
44// Message is one push as the transport sends it. ID is the outbox row's,
45// a canonical lowercase UUID stable across retries: the transport sends it
46// as apns-id, so a retried send is the same notification.
47type Message struct {
48 ID string
49 Token string
50 Environment string
51 Topic string
52 PushType string
53 Priority int
54 CollapseID string
55 Expiration time.Time
56 Payload []byte
57}
58
59// Result is one send's answer. When Apple answered, Status is Apple's HTTP
60// status, Reason the reason it gave on a refusal (BadDeviceToken,
61// Unregistered, ...), APNsID the apns-id it answered with and Timestamp,
62// on a 410, when it last knew the token valid. When the platform refused
63// or could not reach Apple (CARLOS's non-200 answers), Refused is the
64// platform's HTTP status, Code its code (PAUSED, RATE, ...), and
65// RetryAfter its Retry-After.
66type Result struct {
67 Status int
68 Reason string
69 APNsID string
70 Timestamp time.Time
71 Refused int
72 Code string
73 RetryAfter time.Duration
74}
75
76// Apple's reasons that mean the token is gone for good.
77const (
78 ReasonUnregistered = "Unregistered"
79 ReasonExpiredToken = "ExpiredToken"
80 ReasonBadDeviceToken = "BadDeviceToken"
81 ReasonDeviceTokenNotForTopic = "DeviceTokenNotForTopic"
82)
83
84// Apple reports whether Apple answered (the platform neither
85// refused the push nor failed to reach Apple).
86func (r Result) Apple() bool { return r.Refused == 0 && r.Code == "" }
87
88// Gone reports whether Apple's answer means the token will never work
89// again: the registration (or activity token) is deleted.
90func (r Result) Gone() bool {
91 if !r.Apple() {
92 return false
93 }
94 switch r.Reason {
95 case ReasonUnregistered, ReasonExpiredToken, ReasonBadDeviceToken, ReasonDeviceTokenNotForTopic:
96 return true
97 }
98 return r.Status == 410
99}
100
101// Transport delivers one push. An error is a delivery that did not reach
102// Apple or the platform (retried); a Result is their answer, whatever it
103// was. An error's text must never carry the token: the sender scrubs it
104// anyway before it is logged.
105type Transport interface {
106 Send(ctx context.Context, m Message) (Result, error)
107}
108
109// ErrNoActivity: an activity update or end with no running activity's
110// token on file yet.
111var ErrNoActivity = errors.New("push: no activity token")
112
113// The device checks' answers (Sender.Check, the engine's own): ErrGone,
114// the device was revoked or its person removed, so its registration is
115// deleted; ErrNotCurrent, it cannot take a push now (its credential
116// expired) but stays registered. Any other error is a lookup that failed:
117// the row is tried again later.
118var (
119 ErrGone = errors.New("push: device revoked or person removed")
120 ErrNotCurrent = errors.New("push: device not current")
121)
122
123// Box seals the tokens and payloads push keeps at rest, with the instance
124// key (Authority.Seal and Open), so neither reaches SQL text, and so
125// neither reaches the database's slow or failed query log.
126type Box struct {
127 Seal func(domain string, v any) (string, error)
128 Open func(domain, sealed string, v any) error
129}
130
131// Sealing domains.
132const (
133 domainToken = "push.token"
134 domainPayload = "push.payload"
135)
136
137func (b Box) seal(domain, v string) (string, error) {
138 if v == "" {
139 return "", nil
140 }
141 if b.Seal == nil {
142 return "", ErrNoBox
143 }
144 return b.Seal(domain, v)
145}
146
147func (b Box) open(domain, sealed string) (string, error) {
148 if sealed == "" {
149 return "", nil
150 }
151 if b.Open == nil {
152 return "", ErrNoBox
153 }
154 var v string
155 err := b.Open(domain, sealed, &v)
156 return v, err
157}
158
159// OpenPayload is a row's payload as sent.
160func (b Box) OpenPayload(sealed string) (string, error) { return b.open(domainPayload, sealed) }
161
162// ErrNoBox: push has no instance key to seal with; nothing is kept.
163var ErrNoBox = errors.New("push: no sealing")
164
165// MaxPayload is APNs' (and the CARLOS verb's) limit on a payload, minified.
166const MaxPayload = 4096
167
168// ErrTooLarge is a payload over MaxPayload: it is never written or sent.
169var ErrTooLarge = errors.New("push: payload too large")
170