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 "bytes"
5 "context"
6 "encoding/json"
7 "errors"
8 "log/slog"
9 "strconv"
10 "strings"
11 "time"
12)
13
14// Logged is the local and staging transport: it sends nothing and logs
15// that a push would have gone, with its kind and size but never its
16// token, anything derived from it, or its payload (a payload can quote a
17// person's card). Boot allows it only on loopback or a staging origin
18// (EQUIPO_ALLOW_LOGGED_PUSH), as EQUIPO_ALLOW_LOGGED_MAIL is for mail.
19type Logged struct {
20 Logger *slog.Logger
21 // Sent sees each message (tests and the local drive); nil is none.
22 Sent func(Message)
23}
24
25// Send logs the push and answers as Apple would to a delivered one.
26func (l Logged) Send(_ context.Context, m Message) (Result, error) {
27 if l.Sent != nil {
28 l.Sent(m)
29 }
30 if l.Logger != nil {
31 l.Logger.Info("push logged", "id", m.ID, "type", m.PushType, "priority", m.Priority, "collapse", m.CollapseID, "environment", m.Environment, "bytes", len(m.Payload))
32 }
33 return Result{Status: 200, APNsID: m.ID}, nil
34}
35
36// CarlosPushEnv is set to "submit" by the CARLOS agent when the platform
37// sends this instance's pushes (D1: the Apple key stays on the CARLOS box).
38const CarlosPushEnv = "CARLOS_PUSH"
39
40// ErrCarlosPushUnavailable refuses boot when CARLOS_PUSH=submit asks for a
41// transport this build does not have yet.
42var ErrCarlosPushUnavailable = errors.New("push: CARLOS push submission is not available in this build")
43
44// FromCarlos is the CARLOS platform push submission transport: POST
45// /v1/push on the instance's control socket ($CARLOS_CONTROL_SOCKET) with
46// Authorization: Bearer $CARLOS_ADMIN_TOKEN, as mail's submit
47// (mail.FromCarlos). The contract (carlos/platform, design/platform-push-
48// submission, docs/superpowers/specs/2026-11-20-platform-push-submission-
49// design.md §4, awaiting Paul) is here already: the body is
50// CarlosRequestOf(m), the answer CarlosResult(status, Retry-After, body),
51// and the Sender acts on every outcome (Judge).
52//
53// SEAM (2026-11-20): neither the CARLOS verb nor rastrillo's push client
54// exists yet. When they land, build the client here from the process
55// environment and return it as a Transport: it sends CarlosRequestOf(m)
56// and returns CarlosResult of the answer, retrying nothing itself (the
57// outbox retries). Until then CARLOS_PUSH=submit refuses boot rather than
58// dropping pushes silently.
59func FromCarlos(getenv func(string) string) (Transport, error) {
60 if getenv(CarlosPushEnv) != "submit" {
61 return nil, nil
62 }
63 return nil, ErrCarlosPushUnavailable
64}
65
66// CarlosMaxBody is the verb's limit on a request body.
67const CarlosMaxBody = 8 << 10
68
69// CarlosRequest is the verb's request body (§4.1).
70type CarlosRequest struct {
71 DeviceToken string `json:"device_token"`
72 Topic string `json:"topic"`
73 PushType string `json:"push_type"`
74 Priority int `json:"priority"`
75 CollapseID string `json:"collapse_id,omitempty"`
76 Expiration *int64 `json:"expiration,omitempty"`
77 APNsID string `json:"apns_id,omitempty"`
78 Environment string `json:"environment"`
79 Payload json.RawMessage `json:"payload"`
80}
81
82// CarlosRequestOf is a message as the verb takes it: the payload a
83// compact JSON object of at most MaxPayload bytes, the whole body at most
84// CarlosMaxBody.
85func CarlosRequestOf(m Message) ([]byte, error) {
86 var payload bytes.Buffer
87 if err := json.Compact(&payload, m.Payload); err != nil || payload.Len() == 0 || payload.Bytes()[0] != '{' {
88 return nil, errors.New("push: payload is not a JSON object")
89 }
90 if payload.Len() > MaxPayload {
91 return nil, ErrTooLarge
92 }
93 r := CarlosRequest{DeviceToken: m.Token, Topic: m.Topic, PushType: m.PushType, Priority: m.Priority, CollapseID: m.CollapseID, APNsID: m.ID, Environment: m.Environment, Payload: payload.Bytes()}
94 if !m.Expiration.IsZero() {
95 at := m.Expiration.Unix()
96 r.Expiration = &at
97 }
98 raw, err := json.Marshal(r)
99 if err == nil && len(raw) > CarlosMaxBody {
100 return nil, ErrTooLarge
101 }
102 return raw, err
103}
104
105// carlosAnswer is the verb's answer body: Apple's answer on a 200
106// (status, apns_id, reason, timestamp in milliseconds on a 410), the
107// platform's code otherwise.
108type carlosAnswer struct {
109 Status int `json:"status"`
110 APNsID string `json:"apns_id"`
111 Reason string `json:"reason"`
112 Timestamp int64 `json:"timestamp"`
113 Code string `json:"code"`
114}
115
116// CarlosResult reads the verb's answer (§4.2): a 200 is Apple's answer,
117// passed through; anything else is the platform's refusal, its code (a
118// 401's body is plain text: no code) and Retry-After in seconds.
119func CarlosResult(status int, retryAfter string, body []byte) (Result, error) {
120 var a carlosAnswer
121 if status == 200 {
122 if err := json.Unmarshal(body, &a); err != nil || a.Status == 0 {
123 return Result{}, errors.New("push: CARLOS answered 200 without Apple's answer")
124 }
125 r := Result{Status: a.Status, Reason: a.Reason, APNsID: a.APNsID}
126 if a.Timestamp > 0 {
127 r.Timestamp = time.UnixMilli(a.Timestamp).UTC()
128 }
129 return r, nil
130 }
131 _ = json.Unmarshal(body, &a)
132 r := Result{Refused: status, Code: a.Code}
133 if n, err := strconv.Atoi(strings.TrimSpace(retryAfter)); err == nil && n > 0 {
134 r.RetryAfter = time.Duration(n) * time.Second
135 }
136 return r, nil
137}
138