-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfeatures.ts
331 lines (285 loc) · 10.1 KB
/
features.ts
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
import * as t from "io-ts";
import { isLeft } from "fp-ts/lib/Either.js";
import { PathReporter } from "io-ts/lib/PathReporter.js";
import { Response } from "express";
import { Request as JwtRequest } from "express-jwt";
import { FindCursor, ObjectId, WithId } from "mongodb";
import { State } from "./globals.js";
import { sceneToJson } from "./scenes.js";
import { makeRequireSuperuserOrRoleMiddleware } from "./permissions.js";
export interface MongoSceneFeature {
scene_id: ObjectId;
feature_time: Date;
}
export interface MongoSceneFeatureQueue {
scene_ids: ObjectId[];
}
export interface HydratedSceneFeature {
id?: ObjectId;
scene: Record<string, any>;
feature_time: Date;
}
const QueueRequestBody = t.type({
scene_ids: t.array(t.string),
});
/*
* Right now the feature time is the only thing that it makes sense to update,
* and there's really no sense having an empty update.
* But if that changes in the future, this should probably use `t.partial` instead
*/
const FeatureUpdateRequestBody = t.type({
feature_time: t.number,
});
function timeStrippedDate(date: Date): Date {
const day = new Date(date);
day.setHours(0, 0, 0, 0);
return day;
}
export function getFeaturesForDate(state: State, date: Date): Promise<FindCursor<WithId<MongoSceneFeature>>> {
const day = timeStrippedDate(date);
const nextDay = new Date(day);
nextDay.setDate(nextDay.getDate() + 1);
return getFeaturesForRange(state, day, nextDay);
}
export async function getFeaturesForRange(state: State, startDate: Date, endDate: Date): Promise<FindCursor<WithId<MongoSceneFeature>>> {
return state.features.find({
"$and": [
{ feature_time: { "$gte": startDate } },
{ feature_time: { "$lt": endDate } }
]
});
}
async function hydratedFeature(state: State, feature: WithId<MongoSceneFeature>, req: JwtRequest): Promise<HydratedSceneFeature> {
const scene = await state.scenes.findOne({ "_id": feature.scene_id });
if (scene === null) {
throw new Error(`Database consistency failure, feature ${feature._id} missing scene ${feature.scene_id}`);
}
const sceneJson = await sceneToJson(scene, state, req.session);
return {
id: feature._id,
feature_time: feature.feature_time,
scene: sceneJson
};
}
/*
* Note that `findOneAndUpdate` will return (a Promise resolving to) the "original" document, before the pop operation
* has occurred.
* See https://mongodb.github.io/node-mongodb-native/6.3/classes/Collection.html#findOneAndUpdate
* along with
* https://mongodb.github.io/node-mongodb-native/6.3/interfaces/FindOneAndUpdateOptions.html#returnDocument
*/
export async function tryPopNextQueuedSceneId(state: State): Promise<ObjectId | null> {
const result = await state.featureQueue.findOneAndUpdate(
{ queue: true },
{ "$pop": { "scene_ids": -1 } } // -1 pops the first element: https://www.mongodb.com/docs/manual/reference/operator/update/pop/
);
const queueDoc = result.value;
return queueDoc?.scene_ids[0] ?? null;
}
export async function getCurrentFeaturedSceneID(state: State): Promise<ObjectId | null> {
const features = await getFeaturesForDate(state, new Date());
const firstFeature = await features.next();
return firstFeature?.scene_id ?? tryPopNextQueuedSceneId(state);
}
export function initializeFeatureEndpoints(state: State) {
const FeatureCreation = t.type({
scene_id: t.string,
feature_time: t.Integer
});
type FeatureCreationT = t.TypeOf<typeof FeatureCreation>;
const requireManageFeatures = makeRequireSuperuserOrRoleMiddleware(state, 'manage-features');
state.app.post(
"/feature",
requireManageFeatures,
async (req: JwtRequest, res: Response) => {
const maybe = FeatureCreation.decode(req.body);
if (isLeft(maybe)) {
res.statusCode = 400;
res.json({ error: true, message: `Submission did not match schema: ${PathReporter.report(maybe).join("\n")}` });
return;
}
const input: FeatureCreationT = maybe.right;
const date = new Date(input.feature_time);
if (isNaN(date.getTime())) {
res.status(400).json({
error: true,
message: "Invalid date specified",
});
return;
}
const record: MongoSceneFeature = {
scene_id: new ObjectId(input.scene_id),
feature_time: date,
};
try {
const result = await state.features.insertOne(record);
res.json({
error: false,
id: "" + result.insertedId,
});
} catch (err) {
console.error(`${req.method} ${req.path} exception`, err);
res.statusCode = 500;
res.json({ error: true, message: `error serving ${req.method} ${req.path}` });
}
}
);
state.app.get(
"/features",
requireManageFeatures,
async (req: JwtRequest, res: Response) => {
const startDate = new Date(Number(req.query.start_date));
const endDate = new Date(Number(req.query.end_date));
if (isNaN(startDate.getTime()) || isNaN(endDate.getTime())) {
res.status(400).json({
error: true,
message: "Invalid start and end date formats"
});
return;
}
const features = await getFeaturesForRange(state, startDate, endDate);
const hydratedFeatures: HydratedSceneFeature[] = [];
for await (const feature of features) {
const hydrated = await hydratedFeature(state, feature, req);
hydratedFeatures.push(hydrated);
}
res.json({
error: false,
features: hydratedFeatures
});
});
state.app.get(
"/features/queue",
requireManageFeatures,
async (req: JwtRequest, res: Response) => {
const queueDoc = await state.featureQueue.findOne();
const sceneIDs = queueDoc?.scene_ids ?? [];
const scenes: Record<string, any>[] = [];
for (const id of sceneIDs) {
const scene = await state.scenes.findOne({ "_id": new ObjectId(id) });
if (scene === null) {
throw new Error(`Database consistency failure, feature queue missing scene ${id}`);
}
const sceneJson = await sceneToJson(scene, state, req.session);
scenes.push(sceneJson);
}
res.json({
error: false,
scenes
});
});
state.app.get(
"/features/:id",
requireManageFeatures,
async (req: JwtRequest, res: Response) => {
const objectId = new ObjectId(req.params.id);
const feature = await state.features.findOne({ _id: objectId });
if (feature === null) {
res.status(404).json({
error: true,
message: `Feature with id ${req.params.id} not found`
});
return;
}
const hydrated = await hydratedFeature(state, feature, req);
res.json({
error: false,
feature: hydrated
});
});
state.app.patch(
"/features/:id",
requireManageFeatures,
async (req: JwtRequest, res: Response) => {
const objectId = new ObjectId(req.params.id);
const feature = await state.features.findOne({ _id: objectId });
if (feature === null) {
res.status(404).json({
error: true,
message: `Feature with id ${req.params.id} not found`
});
return;
}
const maybe = FeatureUpdateRequestBody.decode(req.body);
if (isLeft(maybe)) {
res.status(400).json({ error: true, message: `Submission did not match schema: ${PathReporter.report(maybe).join("\n")}` });
return;
}
const update = { feature_time: new Date(maybe.right.feature_time) };
try {
const result = await state.features.updateOne({ _id: objectId }, { "$set": update });
res.json({
error: false,
updated: result.modifiedCount === 1
});
} catch (err) {
console.error(`${req.method} ${req.path} exception`, err);
res.statusCode = 500;
res.json({ error: true, message: `error serving ${req.method} ${req.path}` });
}
});
state.app.delete(
"/features/:id",
requireManageFeatures,
async (req: JwtRequest, res: Response) => {
const objectId = new ObjectId(req.params.id);
const feature = await state.features.findOne({ _id: objectId });
if (feature === null) {
res.status(404).json({
error: true,
message: `Feature with id ${req.params.id} not found`
});
return;
}
try {
const result = await state.features.deleteOne({ _id: objectId });
res.json({
error: false,
deleted: result.deletedCount === 1
});
} catch (err) {
console.error(`${req.method} ${req.path} exception`, err);
res.statusCode = 500;
res.json({ error: true, message: `error serving ${req.method} ${req.path}` });
}
});
state.app.post(
"/features/queue",
requireManageFeatures,
async (req: JwtRequest, res: Response) => {
const maybe = QueueRequestBody.decode(req.body);
if (isLeft(maybe)) {
res.statusCode = 400;
res.json({ error: true, message: `Submission did not match schema: ${PathReporter.report(maybe).join("\n")}` });
return;
}
const ids = maybe.right.scene_ids;
const objectIDs = ids.map((id: string) => new ObjectId(id));
const scenes = await state.scenes.find({ "_id": { "$in": objectIDs } }).toArray();
if (scenes.length !== objectIDs.length) {
res.status(400).json({
error: true,
message: "At least one of the scene IDs does not correspond to a valid scene",
});
return;
}
// Note that this method fully replaces the queue, rather than extending it. This
// behavior makes it convenient to implement drag-n-drop reordering in the
// queue management UI.
const result = await state.featureQueue.updateOne(
{ queue: true },
{ "$set": { scene_ids: objectIDs } },
);
if (result.modifiedCount === 1) {
res.json({
error: false,
message: "Queue updated successfully"
});
} else {
res.status(500).json({
error: true,
message: "Error updating queue in database"
});
}
});
}