This repository has been archived by the owner on Jan 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathendpoint_channels.go
263 lines (197 loc) · 5.63 KB
/
endpoint_channels.go
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package main
import (
"net/http"
)
func endpoint_channels(request *http.Request) *http_status {
subject, err := authenticate(request)
if err != nil {
return &http_status{401, err.Error()}
}
switch request.Method {
case "GET":
rows, err := db.Query("SELECT id, name FROM memberships, channels WHERE person = ? AND channel = id", subject)
if err != nil {
return &http_status{500, err.Error()}
}
var channel Channel
channels := make([]Channel, 0, 16)
for rows.Next() {
if err := rows.Scan(&channel.Id, &channel.Name); err != nil {
return &http_status{500, err.Error()}
}
channels = append(channels, channel)
}
return &http_status{200, channels}
case "POST":
tx, err := db.Begin()
if err != nil {
return &http_status{500, err.Error()}
}
var channel Channel
err = decode_payload(request, &channel)
if err != nil {
return &http_status{400, err.Error()}
}
result, err := tx.Exec("INSERT INTO channels (name) VALUES (?)", channel.Name)
if err != nil {
tx.Rollback()
return &http_status{500, err.Error()}
}
last_insert_id, err := result.LastInsertId()
channel.Id = int(last_insert_id)
if err != nil {
tx.Rollback()
return &http_status{500, err.Error()}
}
channel.Members = append(channel.Members, subject)
for _, person_id := range channel.Members {
if _, err = tx.Exec("INSERT INTO memberships (person, channel) VALUES (?, ?)", person_id, channel.Id); err != nil {
tx.Rollback()
return &http_status{500, err.Error()}
}
}
if err := tx.Commit(); err != nil {
return &http_status{500, err.Error()}
}
for _, person_id := range channel.Members {
event := Message{
Channel: channel.Id,
Author: person_id,
IsEvent: 1,
Content: "join",
}
stamp_message(&event)
hub.Publish(channel.Id, event)
}
return &http_status{200, channel}
default:
return &http_status{405, "method not allowed"}
}
}
func endpoint_channels_with_parameters(request *http.Request) *http_status {
subject, err := authenticate(request)
if err != nil {
return &http_status{401, err.Error()}
}
var (
channel_id int
person_id string
)
err = match(request.URL.Path, "/channels/([0-9]+)(?:/([^/]+))?", &channel_id, &person_id)
if err != nil {
return &http_status{400, err.Error()}
}
if db.QueryRow("SELECT person FROM memberships WHERE channel = ? AND person = ?", channel_id, subject).Scan(&subject) != nil {
return &http_status{403, "not a member"}
}
switch request.Method {
case "GET":
if person_id != "" {
return &http_status{400, "person parameter cannot be specified"}
}
case "POST":
if person_id != "" {
return &http_status{400, "person parameter cannot be specified"}
}
var friends []string
decode_payload(request, &friends)
tx, err := db.Begin()
if err != nil {
return &http_status{500, err.Error()}
}
for _, person_id := range friends {
if _, err := db.Exec("INSERT INTO memberships (channel, person) VALUES (?, ?)", channel_id, person_id); err != nil {
tx.Rollback()
return &http_status{500, err.Error()}
}
}
if err = tx.Commit(); err != nil {
return &http_status{500, err.Error()}
}
for _, person_id := range friends {
event := Message{
Channel: channel_id,
Author: person_id,
IsEvent: 1,
Content: "join",
}
stamp_message(&event)
hub.Publish(channel_id, event)
}
case "PUT":
if _, err := db.Exec("INSERT INTO memberships (channel, person) VALUES (?, ?)", channel_id, person_id); err != nil {
return &http_status{500, err.Error()}
}
event := Message{
Channel: channel_id,
Author: person_id,
IsEvent: 1,
Content: "join",
}
stamp_message(&event)
hub.Publish(channel_id, event)
case "DELETE":
if _, err := db.Exec("DELETE FROM memberships WHERE channel = ? AND person = ?", channel_id, person_id); err != nil {
return &http_status{500, err.Error()}
}
event := Message{
Channel: channel_id,
Author: person_id,
IsEvent: 1,
Content: "leave",
}
stamp_message(&event)
hub.Publish(channel_id, event)
tx, err := db.Begin()
if err != nil {
return &http_status{500, err.Error()}
}
row := tx.QueryRow("SELECT COUNT(*) FROM memberships WHERE channel = ?", channel_id)
var size int
if err := row.Scan(&size); err != nil {
return &http_status{500, err.Error()}
}
if size == 0 {
if _, err := tx.Exec("DELETE FROM channels WHERE id = ?", channel_id); err != nil {
return &http_status{500, err.Error()}
}
}
if err = tx.Commit(); err != nil {
return &http_status{500, err.Error()}
}
if size == 0 {
return &http_status{410, "channel deleted"}
}
case "PATCH":
request.ParseForm()
if _, exists := request.PostForm["name"]; exists {
_, err := db.Exec("UPDATE channels SET name = ? WHERE id = ?", request.PostForm.Get("name"), channel_id)
if err != nil {
return &http_status{500, err.Error()}
}
return &http_status{200, "channel name updated"}
}
return &http_status{200, "everything up to date"}
default:
return &http_status{405, "method not allowed"}
}
row := db.QueryRow("SELECT name FROM channels WHERE id = ?", channel_id)
var channel Channel
channel.Id = channel_id
if err := row.Scan(&channel.Name); err != nil {
return &http_status{500, err.Error()}
}
rows, err := db.Query("SELECT person FROM memberships WHERE channel = ?", channel_id)
channel.Members = make([]string, 0, 16)
if err != nil {
return &http_status{500, err.Error()}
}
var person string
for rows.Next() {
if err := rows.Scan(&person); err != nil {
return &http_status{500, err.Error()}
}
channel.Members = append(channel.Members, person)
}
return &http_status{200, channel}
}