diff options
author | Chocobozzz <me@florianbigard.com> | 2021-06-16 15:14:41 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2021-06-17 08:47:03 +0200 |
commit | 8ebf2a5d5d126e6ef9b89109124adf2a5e9e293d (patch) | |
tree | eee3ef972943be629519c2dc97928878dbd2d732 /server/lib/live/live-quota-store.ts | |
parent | fd6584844b070cfc283902aba26f2853473e83d1 (diff) | |
download | PeerTube-8ebf2a5d5d126e6ef9b89109124adf2a5e9e293d.tar.gz PeerTube-8ebf2a5d5d126e6ef9b89109124adf2a5e9e293d.tar.zst PeerTube-8ebf2a5d5d126e6ef9b89109124adf2a5e9e293d.zip |
Refactor live manager
Diffstat (limited to 'server/lib/live/live-quota-store.ts')
-rw-r--r-- | server/lib/live/live-quota-store.ts | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/server/lib/live/live-quota-store.ts b/server/lib/live/live-quota-store.ts new file mode 100644 index 000000000..8ceccde98 --- /dev/null +++ b/server/lib/live/live-quota-store.ts | |||
@@ -0,0 +1,48 @@ | |||
1 | class LiveQuotaStore { | ||
2 | |||
3 | private static instance: LiveQuotaStore | ||
4 | |||
5 | private readonly livesPerUser = new Map<number, { liveId: number, size: number }[]>() | ||
6 | |||
7 | private constructor () { | ||
8 | } | ||
9 | |||
10 | addNewLive (userId: number, liveId: number) { | ||
11 | if (!this.livesPerUser.has(userId)) { | ||
12 | this.livesPerUser.set(userId, []) | ||
13 | } | ||
14 | |||
15 | const currentUserLive = { liveId, size: 0 } | ||
16 | const livesOfUser = this.livesPerUser.get(userId) | ||
17 | livesOfUser.push(currentUserLive) | ||
18 | } | ||
19 | |||
20 | removeLive (userId: number, liveId: number) { | ||
21 | const newLivesPerUser = this.livesPerUser.get(userId) | ||
22 | .filter(o => o.liveId !== liveId) | ||
23 | |||
24 | this.livesPerUser.set(userId, newLivesPerUser) | ||
25 | } | ||
26 | |||
27 | addQuotaTo (userId: number, liveId: number, size: number) { | ||
28 | const lives = this.livesPerUser.get(userId) | ||
29 | const live = lives.find(l => l.liveId === liveId) | ||
30 | |||
31 | live.size += size | ||
32 | } | ||
33 | |||
34 | getLiveQuotaOf (userId: number) { | ||
35 | const currentLives = this.livesPerUser.get(userId) | ||
36 | if (!currentLives) return 0 | ||
37 | |||
38 | return currentLives.reduce((sum, obj) => sum + obj.size, 0) | ||
39 | } | ||
40 | |||
41 | static get Instance () { | ||
42 | return this.instance || (this.instance = new this()) | ||
43 | } | ||
44 | } | ||
45 | |||
46 | export { | ||
47 | LiveQuotaStore | ||
48 | } | ||