-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathinternal.controller.ts
More file actions
92 lines (72 loc) · 2.79 KB
/
internal.controller.ts
File metadata and controls
92 lines (72 loc) · 2.79 KB
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
import express from 'express'
import Room from '../models/room'
import StoredRoom from '../schemas/room.schema'
import { PortalAllocationStatus } from '../models/room/defs'
import WSMessage from '../server/websocket/models/message'
import authenticate from '../server/middleware/authenticate.internal.middleware'
import { signApertureToken } from '../utils/aperture.utils'
import { handleError, RoomNotFound } from '../utils/errors.utils'
const app = express()
/**
* Assign New Portal ID to Room
*/
app.post('/portal', authenticate, async (req, res) => {
const { id, roomId } = req.body as { id: string, roomId: string }
try {
const room = await new Room().load(roomId)
await room.setPortalId(id)
res.sendStatus(200)
} catch (error) {
handleError(error, res)
}
})
/**
* Existing Portal Status Update
*/
app.put('/portal', authenticate, async (req, res) => {
const { id, status, janusId, janusIp } = req.body as { id: string, status: PortalAllocationStatus, janusId?: number, janusIp?: string }
//console.log('recieved', id, status, 'from portal microservice, finding room...')
try {
const doc = await StoredRoom.findOne({ 'info.portal.id': id })
if (!doc)
return RoomNotFound
//console.log('room found, updating status...')
const room = new Room(doc)
const { portal: allocation } = await room.updatePortalAllocation({ janusId, janusIp, status }),
{ online } = await room.fetchOnlineMemberIds()
//console.log('status updated and online members fetched:', online)
if (online.length > 0) {
/**
* Broadcast allocation to all online clients
*/
const updateMessage = new WSMessage(0, allocation, 'PORTAL_UPDATE')
await updateMessage.broadcastRoom(room)
if (status === 'open')
if (allocation.janusId) {
const token = signApertureToken(id),
apertureMessage = new WSMessage(0, { ws: process.env.APERTURE_WS_URL, t: token }, 'APERTURE_CONFIG')
await apertureMessage.broadcastRoom(room)
} else {
const janusMessage = new WSMessage(0, { id: janusId, ip: janusIp }, 'JANUS_CONFIG')
await janusMessage.broadcastRoom(room)
}
}
res.sendStatus(200)
} catch(error) {
handleError(error, res)
}
})
app.post('/queue', authenticate, (req, res) => {
const { queue } = req.body as { queue: string[] }
queue.forEach(async (id, i) => {
try {
const op = 0, d = { pos: i, len: queue.length }, t = 'PORTAL_QUEUE_UPDATE',
message = new WSMessage(op, d, t)
await message.broadcastRoom(id)
} catch (error) {
handleError(error, res)
}
})
res.sendStatus(200)
})
export default app