This repository has been archived by the owner on Sep 27, 2022. It is now read-only.
forked from aullman/opentok-meet
-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathroomstore.js
104 lines (102 loc) · 3.73 KB
/
roomstore.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
const OpenTok = require('opentok');
const ONE_MINUTE_IN_MS = 60 * 1000;
const INACTIVITY_TIME_IN_MINS = parseInt(process.env.INACTIVITY_TIME_IN_MINS, 10) || 180;
const ROOM_INACTIVITY_TIME_LIMIT = INACTIVITY_TIME_IN_MINS * ONE_MINUTE_IN_MS;
module.exports = (redis, ot, config) => {
const roomStore = {
isP2P(room) {
return room.toLowerCase().indexOf('p2p') >= 0;
},
getRooms(callback) {
redis.hkeys('rooms', callback);
},
clearRooms(callback) {
// This deletes all of the rooms. Only use this when migrating to
// a different environment
redis.del('rooms', callback);
},
updateRoomTimestamp(room, sessionId) {
redis.hset('rooms', room, `${sessionId}:${Date.now()}`);
},
getRoom(room, apiKey, secret, req = { session: {} }) {
console.log(`getRoom: ${room} ${apiKey} ${secret}`);
const goToRoom = arguments[arguments.length - 1]; // eslint-disable-line
// Lookup the mapping of rooms to sessionIds
redis.hget('rooms', room, (err, sidWithTs) => {
if (!sidWithTs) {
if (config.clientId) {
req.session.redirectUrl = req.originalUrl;
if (!req.user) {
return goToRoom({ message: 'AUTH-REQUIRED' });
}
}
const props = {
mediaMode: 'routed',
};
if (roomStore.isP2P(room)) {
props.mediaMode = 'relayed';
}
if (req.query.e2ee) {
props.e2ee = true;
}
let otSDK = ot;
// If there's a custom apiKey and secret use that
if (apiKey && secret) {
otSDK = new OpenTok(apiKey, secret);
}
// Create the session
otSDK.createSession(props, (createErr, session) => {
if (createErr) {
goToRoom(createErr);
} else {
const { sessionId } = session;
// Store the room to sessionId mapping
redis.hset('rooms', room, `${sessionId}:${Date.now()}`, (setErr) => {
if (setErr) {
console.error('Failed to set room', setErr);
goToRoom(setErr);
} else if (apiKey && secret) {
// If there's a custom apiKey and secret store that
redis.hset(
'apiKeys', room, JSON.stringify({
apiKey,
secret,
}),
(apiKeyErr) => {
if (apiKeyErr) {
console.error('Failed to set apiKey', apiKeyErr);
goToRoom(apiKeyErr);
} else {
goToRoom(null, sessionId, apiKey, secret);
}
}
);
} else {
goToRoom(null, sessionId);
}
});
}
});
} else {
const [sessionId, timestamp] = sidWithTs.split(':');
if (timestamp && Date.now() - parseInt(timestamp, 10) > ROOM_INACTIVITY_TIME_LIMIT) {
redis.hdel('rooms', room);
goToRoom({ message: 'SESSION-EXPIRED' });
} else {
this.updateRoomTimestamp(room, sessionId);
// Lookup if there's a custom apiKey for this room
redis.hget('apiKeys', room, (getErr, apiKeySecret) => {
if (getErr || !apiKeySecret) {
goToRoom(null, sessionId);
} else {
const parsedApiKeySecret = JSON.parse(apiKeySecret);
goToRoom(null, sessionId, parsedApiKeySecret.apiKey, parsedApiKeySecret.secret);
}
});
}
}
});
},
};
return roomStore;
};