forked from d33mobile/dday
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi.go
More file actions
165 lines (158 loc) · 5.49 KB
/
Copy pathapi.go
File metadata and controls
165 lines (158 loc) · 5.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// JSON endpoints: the public capacity counter and the two token-guarded feeds
// the bot polls.
package main
import (
"crypto/subtle"
"encoding/json"
"net/http"
"strconv"
"strings"
"github.com/d33mobile/dday/internal/regwindow"
)
func (d deps) handleCount(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
methodNotAllowed(w)
return
}
count := 0
if d.store != nil {
var err error
count, err = d.store.Count()
if err != nil {
d.serverError(w, "count", err)
return
}
}
confirmed := count
if confirmed > d.seatLimit {
confirmed = d.seatLimit
}
waitlistCount := count - d.seatLimit
if waitlistCount < 0 {
waitlistCount = 0
}
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Cache-Control", "no-store")
// count/limit are kept for backward compatibility; confirmed/waitlist*
// expose the two-tier capacity so the landing page can render both bars.
// The *At / *Text fields make regwindow the single source of the dates: the
// landing page overwrites its hardcoded fallbacks with these on every load.
_ = json.NewEncoder(w).Encode(map[string]any{
"count": count,
"limit": d.seatLimit,
"waitlist": d.waitlistLimit,
"confirmed": confirmed,
"waitlistCount": waitlistCount,
"full": count >= d.total(),
"open": d.isOpen(),
"openAt": regwindow.OpenAt().Unix(),
"eventStartAt": regwindow.EventStart().Unix(),
"eventEndAt": regwindow.EventEnd().Unix(),
"openText": regwindow.OpenStartText(),
"openHowto": regwindow.OpenHowtoText(),
"openShort": regwindow.OpenShort(),
"openShortTime": regwindow.OpenShortTime(),
"eventText": regwindow.EventText(),
"eventShort": regwindow.EventShort(),
"eventShortTime": regwindow.EventShortTime(),
"eventBadge": regwindow.EventBadge(),
})
}
// handleRegistered answers the bot's internal "is this handle registered?"
// query. It is guarded by a shared bearer token: when internalToken is empty
// the endpoint is disabled (404) so the registration list can never leak; a
// missing or wrong token is 401; a missing handle is 400. On success it returns
// {"registered": bool, "number": int}.
func (d deps) handleRegistered(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
methodNotAllowed(w)
return
}
if d.internalToken == "" {
http.NotFound(w, r)
return
}
want := "Bearer " + d.internalToken
if subtle.ConstantTimeCompare([]byte(r.Header.Get("Authorization")), []byte(want)) != 1 {
http.Error(w, "unauthorized", http.StatusUnauthorized)
return
}
handle := strings.TrimSpace(r.URL.Query().Get("h"))
if handle == "" {
http.Error(w, "missing handle", http.StatusBadRequest)
return
}
if d.store == nil {
http.Error(w, "registration unavailable", http.StatusServiceUnavailable)
return
}
number, registered, err := d.store.Number(handle)
if err != nil {
d.serverError(w, "registered", err)
return
}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(map[string]any{
"registered": registered,
"number": number,
})
}
// registrationItem is one entry of the internal GET /api/registrations feed the
// bot polls to announce new signups. It deliberately carries no personal data
// beyond the public Matrix handle and the self-chosen nick: e-mail and city
// must never reach a public room, so they are not part of this shape at all.
type registrationItem struct {
ID int `json:"id"`
Handle string `json:"handle"`
Nick string `json:"nick"`
Rank int `json:"rank"`
Confirmed bool `json:"confirmed"`
WaitlistPos int `json:"waitlistPos"`
}
// handleRegistrations serves the announcement feed: every registration with
// id > since, so the bot can post about the ones it has not announced yet.
// Auth mirrors /api/registered — an empty internalToken disables the endpoint
// (404), a missing/wrong bearer is 401, and only GET is allowed. A missing or
// unparsable "since" means 0 (everything). The response body is
// {"registrations":[{id,handle,nick,rank,confirmed,waitlistPos}, ...]} with
// rank taken from the position in the id-ordered list, so a withdrawal ahead of
// a row promotes it exactly like /admin and /panel do.
func (d deps) handleRegistrations(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
methodNotAllowed(w)
return
}
if d.internalToken == "" {
http.NotFound(w, r)
return
}
want := "Bearer " + d.internalToken
if subtle.ConstantTimeCompare([]byte(r.Header.Get("Authorization")), []byte(want)) != 1 {
http.Error(w, "unauthorized", http.StatusUnauthorized)
return
}
if d.store == nil {
http.Error(w, "registration unavailable", http.StatusServiceUnavailable)
return
}
// A malformed "since" is treated as 0 rather than 400: the caller is our own
// bot, and announcing from the start is a safer failure than a hard error.
since, _ := strconv.Atoi(strings.TrimSpace(r.URL.Query().Get("since")))
regs, err := d.store.List()
if err != nil {
d.serverError(w, "registrations", err)
return
}
items := make([]registrationItem, 0, len(regs))
for i, reg := range regs {
if reg.ID <= since {
continue
}
pos := d.waitlistPos(i + 1)
items = append(items, registrationItem{ID: reg.ID, Handle: reg.Handle,
Nick: reg.Nick, Rank: i + 1, Confirmed: pos == 0, WaitlistPos: pos})
}
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Cache-Control", "no-store")
_ = json.NewEncoder(w).Encode(map[string]any{"registrations": items})
}