oficina / meet Public

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

Plain git — no account needed to clone.

Download

Download this file

1// Package bridge is the deliberately small gomobile surface. It imports only
2// Calls' client core, never the database, server, or at-rest encryption keys.
3package bridge
4
5import (
6 "context"
7 "encoding/base64"
8 "encoding/json"
9 "errors"
10 "net/http"
11 "net/http/cookiejar"
12 "net/url"
13
14 "amadan.net/oficina/calls/callscore"
15 "amadan.net/oficina/calls/protocol"
16)
17
18// Listener receives JSON events on a Go callback thread. Enqueue on the main
19// actor before touching Apple UI or WebRTC. Never block or call Close inline.
20type Listener interface{ OnEvent(event string) }
21
22type Client struct{ room *callscore.Room }
23
24// sessionJar turns the cookies the sign-in handoff returned into the jar
25// every authenticated call in this package is made on. It is shared by
26// every function here that takes cookies, so the rules about what counts
27// as a session — one cookie, the framework's name, thirty-two decoded
28// bytes — are written once. Cookies are scoped to the exact supplied
29// origin, and the jar lives only as long as the call that made it:
30// keeping the session between launches is the host's business, and
31// CallsKit keeps it in the Keychain.
32func sessionJar(origin, cookiesJSON string) (*cookiejar.Jar, error) {
33 u, err := url.Parse(origin)
34 if err != nil {
35 return nil, errors.New("invalid Calls origin")
36 }
37 var cookies []struct {
38 Name string `json:"name"`
39 Value string `json:"value"`
40 }
41 if len(cookiesJSON) > 8192 || json.Unmarshal([]byte(cookiesJSON), &cookies) != nil {
42 return nil, errors.New("invalid session cookies")
43 }
44 jar, _ := cookiejar.New(nil)
45 session := false
46 for _, c := range cookies {
47 if c.Name != "__Host-rastrillo_session" && c.Name != "rastrillo_session" {
48 continue
49 }
50 if session {
51 return nil, errors.New("ambiguous Calls session")
52 }
53 raw, err := base64.RawURLEncoding.DecodeString(c.Value)
54 if err != nil || len(raw) != 32 || base64.RawURLEncoding.EncodeToString(raw) != c.Value {
55 return nil, errors.New("invalid Calls session")
56 }
57 session = true
58 jar.SetCookies(u, []*http.Cookie{{Name: c.Name, Value: c.Value, Path: "/", Secure: u.Scheme == "https", HttpOnly: true}})
59 }
60 if !session {
61 return nil, errors.New("sign in to Calls first")
62 }
63 return jar, nil
64}
65
66// StartMeeting creates a meeting on an instance the app is signed in to
67// and answers its identifier, for the app that has no link because the
68// meeting does not exist yet. The caller joins it exactly as it joins a
69// meeting someone sent: NewClient with the same origin and cookies.
70//
71// zone is the device's IANA time zone, for the title the server writes.
72func StartMeeting(origin, cookiesJSON, zone string) (string, error) {
73 jar, err := sessionJar(origin, cookiesJSON)
74 if err != nil {
75 return "", err
76 }
77 return callscore.StartMeetingIn(context.Background(), origin, zone, &http.Client{Jar: jar})
78}
79
80// NewClient uses the ordinary session returned by the browser sign-in handoff.
81// This call does not mint, exchange, or accept an agent bearer token.
82func NewClient(origin, room, cookiesJSON string, listener Listener) (*Client, error) {
83 if listener == nil {
84 return nil, errors.New("a listener is required")
85 }
86 jar, err := sessionJar(origin, cookiesJSON)
87 if err != nil {
88 return nil, err
89 }
90 r, err := callscore.New(origin, room, &http.Client{Jar: jar}, func(event callscore.Event) {
91 b, err := json.Marshal(event)
92 if err == nil {
93 listener.OnEvent(string(b))
94 }
95 })
96 if err != nil {
97 return nil, err
98 }
99 return &Client{room: r}, nil
100}
101func (c *Client) Start() error { return c.room.Start() }
102func (c *Client) ShareLink() (string, error) { return c.room.ShareLink() }
103func (c *Client) Send(typ, to string, generation int64, payload string) error {
104 if generation < 0 {
105 return errors.New("invalid generation")
106 }
107 return c.room.Send(typ, to, uint64(generation), payload)
108}
109func (c *Client) Leave() { c.room.Leave() }
110
111// RefreshICE asks for a fresh /ice answer now (spec §15.28), delivered as
112// the usual "ice" event.
113func (c *Client) RefreshICE() { c.room.RefreshICE() }
114
115// SetClientInfo is the app and its version, said on every request the
116// core makes (spec §15.28), so the people in a call can tell when
117// someone's app is too old. Call it before NewClient; false is refused.
118func SetClientInfo(app, version string) bool { return callscore.SetClient(app, version) }
119func (c *Client) Close() { c.room.Close() }
120
121// SetMedia is this app's own microphone and camera state (spec §15.19).
122// The core reports it once admitted, again whenever a roster says the
123// server has not got it, and at once on every change.
124func (c *Client) SetMedia(muted, cameraOff bool) error { return c.room.SetMedia(muted, cameraOff) }
125
126// SetNoSoundFrom is spec §15.20's receiving half: the peers whose audio
127// is reaching this app as silence, as a JSON array of peer ids (gomobile
128// carries no slices). The core keeps only peers in its roster and
129// reports the list with the rest of this app's media state; "[]" or ""
130// clears it.
131func (c *Client) SetNoSoundFrom(peersJSON string) error {
132 var ids []protocol.PeerID
133 if peersJSON != "" {
134 if err := json.Unmarshal([]byte(peersJSON), &ids); err != nil {
135 return errors.New("invalid peer list")
136 }
137 }
138 return c.room.SetNoSoundFrom(ids)
139}
140
141// SetScreenStalled is spec §15.21's receiving half: the peers whose shared
142// screen has stopped moving here, as a JSON array of peer ids. Reported
143// with the rest of this app's media state; "[]" or "" clears it.
144func (c *Client) SetScreenStalled(peersJSON string) error {
145 var ids []protocol.PeerID
146 if peersJSON != "" {
147 if err := json.Unmarshal([]byte(peersJSON), &ids); err != nil {
148 return errors.New("invalid peer list")
149 }
150 }
151 return c.room.SetScreenStalled(ids)
152}
153
154// SetHearing is spec §15.24: the peers whose audio this app is actually
155// receiving and playing, as a JSON array of peer ids. Reported with the
156// rest of this app's media state; "[]" or "" is "nobody yet", which is
157// still said — the key's presence is what tells the others this app
158// reports it.
159func (c *Client) SetHearing(peersJSON string) error {
160 ids := []protocol.PeerID{}
161 if peersJSON != "" {
162 if err := json.Unmarshal([]byte(peersJSON), &ids); err != nil {
163 return errors.New("invalid peer list")
164 }
165 }
166 return c.room.SetHearing(ids)
167}
168
169// SetVideo is spec §15.30: what this app, as a receiver, asks each sender
170// for, as a JSON object of peer id to "reduced", "minimal" or "audio".
171// Only peers asked for less than normal are listed; "{}" or "" is normal
172// from everyone. Reported with the rest of this app's media state.
173func (c *Client) SetVideo(asksJSON string) error {
174 var asks map[protocol.PeerID]protocol.VideoLevel
175 if asksJSON != "" {
176 if err := json.Unmarshal([]byte(asksJSON), &asks); err != nil {
177 return errors.New("invalid video asks")
178 }
179 }
180 return c.room.SetVideo(asks)
181}
182
183// SendQuality posts one quality report (spec §15.26), a
184// protocol.QualityReport as JSON, with the hello's report token. It
185// answers what became of it, as a word — gomobile hands Swift every error
186// as the same NSError, so an outcome the host acts on is a value:
187//
188// - "sent": taken;
189// - "stop": the server's kill switch; send no more this call;
190// - "too-many", "gone", "failed": skipped; the next window tries again.
191//
192// It works after Leave and Close (the final report goes at leave), and
193// blocks for up to ten seconds: call it off the main thread.
194func (c *Client) SendQuality(report string) string {
195 stop, err := c.room.SendQuality([]byte(report))
196 if err == nil && stop {
197 return string(callscore.ReportStop)
198 }
199 return string(callscore.OutcomeOf(err))
200}
201
202// SendProblem posts "Report a problem" (spec §15.26), a
203// protocol.ProblemReport as JSON, and answers JSON: {"ref":"K7Q2M9PD"}
204// when the server took it, or {"outcome":"too-many"|"gone"|"failed"}.
205// Like SendQuality it works for as long as the server keeps the token —
206// ten minutes past the call — and blocks; call it off the main thread.
207func (c *Client) SendProblem(report string) string {
208 ref, err := c.room.SendProblem([]byte(report))
209 var out struct {
210 Ref string `json:"ref,omitempty"`
211 Outcome string `json:"outcome,omitempty"`
212 }
213 if err == nil {
214 out.Ref = ref
215 } else {
216 out.Outcome = string(callscore.OutcomeOf(err))
217 }
218 b, _ := json.Marshal(out)
219 return string(b)
220}
221
222// SendAutoReport posts the automatic report (spec §15.29, 2026-10-28), a
223// protocol.AutoReport as JSON, and answers JSON: {"ref":"K7Q2M9PD"} when
224// the server took it, or {"outcome":"stop"|"too-many"|"gone"|"failed"} —
225// "stop" being the kill switch, as SendQuality's is: send no more quality
226// or automatic reports this call. It blocks like the others; call it off
227// the main thread.
228func (c *Client) SendAutoReport(report string) string {
229 ref, stop, err := c.room.SendAutoReport([]byte(report))
230 var out struct {
231 Ref string `json:"ref,omitempty"`
232 Outcome string `json:"outcome,omitempty"`
233 }
234 switch {
235 case err != nil:
236 out.Outcome = string(callscore.OutcomeOf(err))
237 case stop:
238 out.Outcome = string(callscore.ReportStop)
239 default:
240 out.Ref = ref
241 }
242 b, _ := json.Marshal(out)
243 return string(b)
244}
245
246// RegisterDevice hands the server an Apple device token so a knock can
247// reach this device when the app is closed. Best effort: the caller
248// ignores the error beyond logging, because a device that did not
249// register is simply one that will not be pushed to.
250func RegisterDevice(origin, cookiesJSON, token, platform string) error {
251 jar, err := sessionJar(origin, cookiesJSON)
252 if err != nil {
253 return err
254 }
255 return callscore.RegisterDevice(context.Background(), origin, &http.Client{Jar: jar}, token, platform)
256}
257
258// SignedOut is CheckSession's third answer, beside callscore's Member
259// and Participant: the instance says this session no longer signs
260// anyone in. It is a value rather than an error because the host acts
261// on it — forgets the copy it kept — and gomobile hands Swift every
262// error as the same NSError, told apart only by its words.
263const SignedOut = "signed-out"
264
265// CheckSession answers "member", "participant" or "signed-out" for a
266// session the host held on to, and an error only when the instance
267// could not be asked or its answer could not be read. The host keeps
268// its credential through an error and forgets it on "signed-out".
269//
270// Cookies this package would refuse to send are "signed-out" too: a
271// stored copy that no longer parses is no more use than an expired one.
272func CheckSession(origin, cookiesJSON string) (string, error) {
273 jar, err := sessionJar(origin, cookiesJSON)
274 if err != nil {
275 return SignedOut, nil
276 }
277 standing, err := callscore.CheckSession(context.Background(), origin, &http.Client{Jar: jar})
278 if errors.Is(err, callscore.ErrSignedOut) {
279 return SignedOut, nil
280 }
281 if err != nil {
282 return "", err
283 }
284 return string(standing), nil
285}
286
287// OpenLink resolves a meeting or invitation address on the instance the
288// host is signed in to, without the browser, and answers JSON:
289// {"room": "..."} — empty for the instance's own address —,
290// {"signedOut": true} when the session has gone and the address needs a
291// fresh sign-in, or {"noAccess": true} when the session is fine but is
292// not someone who may enter that meeting. JSON rather than a bare room
293// so that "no meeting" and "no session" cannot be mistaken for each other.
294func OpenLink(origin, cookiesJSON, address string) (string, error) {
295 type answer struct {
296 Room string `json:"room"`
297 SignedOut bool `json:"signedOut,omitempty"`
298 NoAccess bool `json:"noAccess,omitempty"`
299 }
300 var out answer
301 jar, err := sessionJar(origin, cookiesJSON)
302 if err == nil {
303 out.Room, err = callscore.OpenLink(context.Background(), origin, address, &http.Client{Jar: jar})
304 } else {
305 err = callscore.ErrSignedOut
306 }
307 if errors.Is(err, callscore.ErrSignedOut) {
308 out = answer{SignedOut: true}
309 } else if errors.Is(err, callscore.ErrNoAccess) {
310 out = answer{NoAccess: true}
311 } else if err != nil {
312 return "", err
313 }
314 b, err := json.Marshal(out)
315 return string(b), err
316}
317
318// SignOut asks the instance to revoke the session. The host forgets its
319// copy whatever this answers; see callscore.SignOut.
320func SignOut(origin, cookiesJSON string) error {
321 jar, err := sessionJar(origin, cookiesJSON)
322 if err != nil {
323 return err
324 }
325 return callscore.SignOut(context.Background(), origin, &http.Client{Jar: jar})
326}
327