-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsession.ts
91 lines (73 loc) · 2.67 KB
/
session.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
import { Response } from "express";
import { Request as JwtRequest } from "express-jwt";
import { State } from "./globals";
import { Session } from "express-session"
// Check that session exist and has not been created during this request.
export function isValidSession(session: Session): boolean {
return session && (session.created + 500) < Date.now();
}
// Check that the user has not already seen this scene recently.
export function isValidImpression(session: Session, id: string): boolean {
// Rate limit user impressions
const timeoutMs = 10000;
let isValidRate = true;
const sceneImpression = session?.impressions?.find(x => x.scene_id == id);
if (sceneImpression) {
isValidRate = (sceneImpression.last + timeoutMs) < Date.now();
}
return isValidSession(session) && isValidRate;
}
// Add an impression to the session
export function tryAddImpressionToSession(session: Session, id: string): boolean {
const now = Date.now();
if (isValidImpression(session, id)) {
if (!session.impressions) {
session.impressions = [];
}
const sceneImpression = session?.impressions?.find(x => x.scene_id == id);
if (sceneImpression) {
sceneImpression.last = now;
} else {
session.impressions.push({ scene_id: id, last: now })
}
return true;
}
return false;
}
// Check that the session has not liked the given scene
export function isValidLike(session: Session, id: string) {
return isValidSession(session) && (!session?.likes?.some(l => l.scene_id == id) ?? true);
}
// Add a like to the session
export function tryAddLikeToSession(session: Session, id: string): boolean {
if (isValidLike(session, id)) {
if (!session.likes) {
session.likes = [];
}
session.likes.push({ scene_id: id });
return true;
}
return false;
}
// Check that the session has a like for the given scene
export function isValidRemoveLike(session: Session, id: string) {
return session?.likes?.some(scene => scene.scene_id == id) ?? false;
}
// Remove a like from the session
export function tryRemoveLikeFromSession(session: Session, id: string): boolean {
if (isValidRemoveLike(session, id)) {
session.likes = session.likes.filter(l => l.scene_id != id);
return true;
}
return false;
}
// POST /session/init - Initialize a session
export function initializeSessionEndpoints(state: State) {
state.app.post("/session/init", async (req: JwtRequest, res: Response) => {
req.session.created = Date.now();
res.statusCode = 200;
res.json({
error: false,
});
});
}