oficina / meet Public

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

Plain git — no account needed to clone.

Download

Download this file

1package callscore
2
3import (
4 "bytes"
5 "context"
6 "encoding/json"
7 "errors"
8 "io"
9 "net/http"
10 "regexp"
11 "time"
12
13 "amadan.net/oficina/calls/protocol"
14)
15
16// Quality and problem reports (spec §15.26), and the automatic report
17// (§15.29). The host builds the report — it holds the media statistics —
18// and the core carries it, because the core holds the two things the
19// request needs: the session the hello was given to, and the hello's
20// report token.
21//
22// Every request runs on a context of its own rather than the room's, so
23// they still work once the room is closed: the final quality report goes
24// at leave, and "Report a problem" is offered for ten minutes after the
25// call (the server keeps the token that long past the participation).
26
27// reportTimeout bounds one report request. Ten seconds, as every other
28// request this package makes; a report that cannot get through in that
29// time is not worth holding a thread for.
30const reportTimeout = 10 * time.Second
31
32// reportTokenHeader is where the token goes (internal/calls/quality.go).
33const reportTokenHeader = "X-Calls-Report"
34
35var reportToken = regexp.MustCompile(`^[0-9a-f]{32}$`)
36
37// ReportOutcome is what became of a report, in words the host can turn
38// into a sentence for a person, or act on.
39type ReportOutcome string
40
41const (
42 // ReportSent: the server took it.
43 ReportSent ReportOutcome = "sent"
44 // ReportStop: the server's kill switch (CALLS_QUALITY_REPORTS=off)
45 // answered 204. The host sends no more quality reports this call.
46 ReportStop ReportOutcome = "stop"
47 // ReportTooMany: 429, the server's per-participation limit.
48 ReportTooMany ReportOutcome = "too-many"
49 // ReportGone: 404. The token is unknown — the participation ended
50 // more than ten minutes ago, or a new hello replaced it — or there
51 // was never one (a server from before §15.26).
52 ReportGone ReportOutcome = "gone"
53 // ReportFailed: anything else — no answer, a refusal, a bad body.
54 ReportFailed ReportOutcome = "failed"
55)
56
57// ReportError is a report that did not get through, and why.
58type ReportError struct{ Outcome ReportOutcome }
59
60func (e *ReportError) Error() string { return "Calls report not sent (" + string(e.Outcome) + ")" }
61
62// reportTokenNow is the latest hello's token, or "".
63func (r *Room) reportTokenNow() string {
64 r.mu.Lock()
65 defer r.mu.Unlock()
66 return r.report
67}
68
69// SendQuality posts one quality report (protocol.QualityReport, as JSON).
70// stop is the server's 204: send no more for the rest of the call. Every
71// other refusal is a skipped report, and the next window tries again. It
72// blocks for up to ten seconds and belongs on the host's background
73// executor.
74func (r *Room) SendQuality(report []byte) (stop bool, err error) {
75 if len(report) > protocol.MaxQualityBody || !json.Valid(report) {
76 return false, &ReportError{ReportFailed}
77 }
78 status, _, err := r.postReport("quality", report)
79 if err != nil {
80 return false, err
81 }
82 switch status {
83 case http.StatusAccepted, http.StatusOK:
84 return false, nil
85 case http.StatusNoContent:
86 return true, nil
87 }
88 return false, &ReportError{outcomeOf(status)}
89}
90
91// SendProblem posts "Report a problem" (protocol.ProblemReport, as JSON)
92// and answers the reference the server gave it, which the host shows.
93func (r *Room) SendProblem(report []byte) (ref string, err error) {
94 if len(report) > protocol.MaxProblemBody || !json.Valid(report) {
95 return "", &ReportError{ReportFailed}
96 }
97 status, body, err := r.postReport("problem", report)
98 if err != nil {
99 return "", err
100 }
101 if status != http.StatusOK {
102 return "", &ReportError{outcomeOf(status)}
103 }
104 var receipt protocol.ProblemReceipt
105 if json.Unmarshal(body, &receipt) != nil || !validRef(receipt.Ref) {
106 return "", &ReportError{ReportFailed}
107 }
108 return receipt.Ref, nil
109}
110
111// SendAutoReport posts the automatic report (spec §15.29, 2026-10-28), a
112// protocol.AutoReport as JSON, sent with nobody pressing anything when the
113// host judges its call to be struggling. It answers the reference the
114// server gave it, which the host puts on the window's quality report; stop
115// is the server's kill switch (204), as for SendQuality: send no more
116// quality or automatic reports this call. Every other refusal is an
117// automatic report not sent, and the call goes on as before.
118func (r *Room) SendAutoReport(report []byte) (ref string, stop bool, err error) {
119 if len(report) > protocol.MaxProblemBody || !json.Valid(report) {
120 return "", false, &ReportError{ReportFailed}
121 }
122 status, body, err := r.postReport("auto-report", report)
123 if err != nil {
124 return "", false, err
125 }
126 switch status {
127 case http.StatusNoContent:
128 return "", true, nil
129 case http.StatusOK:
130 default:
131 return "", false, &ReportError{outcomeOf(status)}
132 }
133 var receipt protocol.ProblemReceipt
134 if json.Unmarshal(body, &receipt) != nil || !validRef(receipt.Ref) {
135 return "", false, &ReportError{ReportFailed}
136 }
137 return receipt.Ref, false, nil
138}
139
140func outcomeOf(status int) ReportOutcome {
141 switch status {
142 case http.StatusTooManyRequests:
143 return ReportTooMany
144 case http.StatusNotFound:
145 return ReportGone
146 }
147 return ReportFailed
148}
149
150// validRef is the server's reference shape: eight of Crockford's
151// characters. Anything else is not shown to a person as one.
152func validRef(ref string) bool {
153 if len(ref) != 8 {
154 return false
155 }
156 for _, c := range ref {
157 if !(c >= '0' && c <= '9' || c >= 'A' && c <= 'Z') {
158 return false
159 }
160 }
161 return true
162}
163
164// postReport makes the request: the same session, Origin and redirect
165// rules as every other request from the transport, plus the token.
166func (r *Room) postReport(endpoint string, body []byte) (int, []byte, error) {
167 token := r.reportTokenNow()
168 if !reportToken.MatchString(token) {
169 return 0, nil, &ReportError{ReportGone}
170 }
171 ctx, cancel := context.WithTimeout(context.Background(), reportTimeout)
172 defer cancel()
173 t := r.t
174 req, err := http.NewRequestWithContext(ctx, http.MethodPost, t.origin+"/m/"+t.room+"/"+endpoint, bytes.NewReader(body))
175 if err != nil {
176 return 0, nil, &ReportError{ReportFailed}
177 }
178 req.Header.Set("Accept", "application/json")
179 req.Header.Set("Content-Type", "application/json")
180 req.Header.Set("Origin", t.origin)
181 req.Header.Set(reportTokenHeader, token)
182 res, err := t.http.Do(req)
183 if err != nil {
184 return 0, nil, &ReportError{ReportFailed}
185 }
186 defer res.Body.Close()
187 out, err := io.ReadAll(io.LimitReader(res.Body, maxResponse))
188 if err != nil {
189 return 0, nil, &ReportError{ReportFailed}
190 }
191 return res.StatusCode, out, nil
192}
193
194// OutcomeOf is a report error's outcome, ReportFailed for any other
195// error, and ReportSent for none.
196func OutcomeOf(err error) ReportOutcome {
197 if err == nil {
198 return ReportSent
199 }
200 var refusal *ReportError
201 if errors.As(err, &refusal) {
202 return refusal.Outcome
203 }
204 return ReportFailed
205}
206