This repository has been archived by the owner on Jul 17, 2020. It is now read-only.
forked from dyc3/opentogethertube
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.js
269 lines (256 loc) · 6.17 KB
/
api.js
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
264
265
266
267
268
269
const express = require('express');
const uuid = require("uuid/v4");
const _ = require("lodash");
const InfoExtract = require("./infoextract");
// eslint-disable-next-line no-unused-vars
module.exports = function(_roommanager, storage) {
const roommanager = _roommanager;
const router = express.Router();
router.get("/room/list", (req, res) => {
let rooms = [];
for (const room of roommanager.rooms) {
if (room.visibility !== "public") {
continue;
}
rooms.push({
name: room.name,
description: room.description,
isTemporary: room.isTemporary,
currentSource: room.currentSource,
users: room.clients.length,
});
}
res.json(rooms);
});
router.get("/room/:name", (req, res) => {
roommanager.getOrLoadRoom(req.params.name).then(room => {
room = _.cloneDeep(room);
for (let client of room.clients) {
client.name = client.session.username;
delete client.session;
delete client.socket;
}
res.json(room);
}).catch(err => {
if (err.name === "RoomNotFoundException") {
res.status(404).json({
success: false,
error: "Room not found",
});
}
else {
console.error("Unhandled exception when getting room:", err);
res.status(500).json({
success: false,
error: "Failed to get room",
});
}
});
});
router.post("/room/create", (req, res) => {
if (!req.body.name) {
console.log(req.body);
res.status(400).json({
success: false,
error: "Missing argument (name)",
});
return;
}
if (req.body.name == "list") {
res.status(400).json({
success: false,
error: "Room name not allowed (reserved)",
});
return;
}
if (req.body.name.length < 3) {
res.status(400).json({
success: false,
error: "Room name not allowed (too short, must be at least 3 characters)",
});
return;
}
if (!(/^[A-za-z0-9_-]+$/).exec(req.body.name)) {
res.status(400).json({
success: false,
error: "Room name not allowed (invalid characters)",
});
return;
}
if (!req.body.temporary) {
req.body.temporary = false;
}
if (!req.body.visibility) {
req.body.visibility = "public";
}
try {
roommanager.createRoom(req.body.name, req.body.temporary, req.body.visibility);
res.json({
success: true,
});
}
catch (e) {
if (e.name === "RoomNameTakenException") {
res.status(400).json({
success: false,
error: "Room with that name already exists",
});
}
else {
res.status(500).json({
success: false,
error: "An unknown error occured when creating this room. Try again later.",
});
}
}
});
router.post("/room/generate", (req, res) => {
let roomName = uuid();
roommanager.createRoom(roomName, true);
res.json({
success: true,
room: roomName,
});
});
router.delete("/room/:name", (req, res) => {
roommanager.getOrLoadRoom(req.params.name).then(room => {
roommanager.unloadRoom(room);
res.json({
success: true,
});
}).catch(err => {
if (err.name === "RoomNotFoundException") {
res.status(404).json({
success: false,
error: "Room not found",
});
}
else {
console.error("Unhandled exception when getting room:", err);
res.status(500).json({
success: false,
error: "Failed to get room",
});
}
});
});
router.post("/room/:name/queue", (req, res) => {
roommanager.getOrLoadRoom(req.params.name).then(room => {
if (req.body.url) {
room.addToQueue({ url: req.body.url }, req.session).then(success => {
res.json({
success,
});
});
}
else if (req.body.service && req.body.id) {
room.addToQueue({ service: req.body.service, id: req.body.id }, req.session).then(success => {
res.json({
success,
});
});
}
else {
res.status(400).json({
success: false,
error: "Invalid parameters",
});
}
}).catch(err => {
if (err.name === "RoomNotFoundException") {
res.status(404).json({
success: false,
error: "Room not found",
});
}
else {
console.error("Unhandled exception when getting room:", err);
res.status(500).json({
success: false,
error: "Failed to get room",
});
}
});
});
router.delete("/room/:name/queue", (req, res) => {
roommanager.getOrLoadRoom(req.params.name).then(room => {
if (req.body.service && req.body.id) {
const success = room.removeFromQueue({ service: req.body.service, id: req.body.id }, req.session);
res.json({
success,
});
}
else {
res.status(400).json({
success: false,
error: "Invalid parameters",
});
}
}).catch(err => {
if (err.name === "RoomNotFoundException") {
res.status(404).json({
success: false,
error: "Room not found",
});
}
else {
console.error("Unhandled exception when getting room:", err);
res.status(500).json({
success: false,
error: "Failed to get room",
});
}
});
});
router.post("/room/:name/undo", (req, res) => {
roommanager.getOrLoadRoom(req.params.name).then(room => {
room.undoEvent(req.body.event);
}).catch(err => {
if (err.name === "RoomNotFoundException") {
res.status(404).json({
success: false,
error: "Room not found",
});
}
else {
console.error("Unhandled exception when getting room:", err);
res.status(500).json({
success: false,
error: "Failed to get room",
});
}
});
});
router.get("/data/previewAdd", (req, res) => {
// FIXME: this endpoint has the potential to be abused.
// TODO: rate limit
console.log("Getting queue add preview for", req.query.input);
try {
InfoExtract.getAddPreview(req.query.input.trim()).then(result => {
res.json(result);
});
}
catch (error) {
if (error.name === "UnsupportedServiceException" || error.name === "InvalidAddPreviewInputException") {
console.error("Unable to get add preview:", error.name);
res.status(400).json({
success: false,
error: error.message,
});
}
else {
console.error("Unable to get add preview:", error);
res.status(500).json({
success: false,
error: "Unknown error occurred.",
});
}
}
});
router.get("/user", (req, res) => {
res.json({
name: req.session.username,
});
});
return router;
};