2020-09-17 13:44:32 +08:00
|
|
|
/* eslint no-param-reassign: 0 */
|
|
|
|
|
2020-03-13 02:28:20 +08:00
|
|
|
/*
|
|
|
|
Description: Create a new socket session or restore previous session
|
|
|
|
*/
|
|
|
|
|
2020-09-08 12:51:47 +08:00
|
|
|
// module support functions
|
|
|
|
const createSessionID = () => {
|
|
|
|
let sessionID = '';
|
2020-09-17 13:44:32 +08:00
|
|
|
for (let i = 0, j = 32; i < j; i += 1) {
|
2020-09-08 12:51:47 +08:00
|
|
|
sessionID += Math.random().toString(36).substr(2, 9);
|
|
|
|
}
|
|
|
|
return sessionID;
|
2020-09-17 13:44:32 +08:00
|
|
|
};
|
2020-09-08 12:51:47 +08:00
|
|
|
|
2020-03-13 02:28:20 +08:00
|
|
|
// module main
|
2020-09-17 13:44:32 +08:00
|
|
|
export async function run({ server, socket }) {
|
2020-09-08 12:51:47 +08:00
|
|
|
// gather connection and channel count
|
|
|
|
let ips = {};
|
|
|
|
let channels = {};
|
2020-09-17 13:44:32 +08:00
|
|
|
// @todo use public channel flag
|
|
|
|
const publicChanCounts = {
|
2020-09-08 12:51:47 +08:00
|
|
|
lounge: 0,
|
|
|
|
meta: 0,
|
|
|
|
math: 0,
|
|
|
|
physics: 0,
|
|
|
|
chemistry: 0,
|
|
|
|
technology: 0,
|
|
|
|
programming: 0,
|
|
|
|
games: 0,
|
|
|
|
banana: 0,
|
|
|
|
chinese: 0,
|
|
|
|
};
|
|
|
|
|
2020-09-17 13:44:32 +08:00
|
|
|
// todo code resuage between here and `morestats`, export function
|
2020-09-08 12:51:47 +08:00
|
|
|
server.clients.forEach((client) => {
|
|
|
|
if (client.channel) {
|
|
|
|
channels[client.channel] = true;
|
|
|
|
ips[client.address] = true;
|
|
|
|
if (typeof publicChanCounts[client.channel] !== 'undefined') {
|
2020-09-17 13:44:32 +08:00
|
|
|
publicChanCounts[client.channel] += 1;
|
2020-09-08 12:51:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const uniqueClientCount = Object.keys(ips).length;
|
|
|
|
const uniqueChannels = Object.keys(channels).length;
|
|
|
|
|
|
|
|
ips = null;
|
|
|
|
channels = null;
|
|
|
|
|
2020-09-17 13:44:32 +08:00
|
|
|
// @todo restore session
|
2020-09-08 12:51:47 +08:00
|
|
|
socket.sessionID = createSessionID();
|
|
|
|
socket.hcProtocol = 2;
|
|
|
|
socket.userid = Math.floor(Math.random() * 9999999999999);
|
|
|
|
|
|
|
|
// dispatch info
|
|
|
|
server.reply({
|
|
|
|
cmd: 'session',
|
|
|
|
users: uniqueClientCount,
|
|
|
|
chans: uniqueChannels,
|
|
|
|
public: publicChanCounts,
|
|
|
|
sessionID: socket.sessionID,
|
|
|
|
restored: false,
|
|
|
|
}, socket);
|
|
|
|
}
|
2020-03-13 02:28:20 +08:00
|
|
|
|
|
|
|
export const info = {
|
|
|
|
name: 'session',
|
|
|
|
description: 'Restore previous state by session id or return new session id (currently unavailable)',
|
|
|
|
usage: `
|
2020-09-17 13:44:32 +08:00
|
|
|
API: { cmd: 'session', id: '<previous session>' }`,
|
2020-03-13 02:28:20 +08:00
|
|
|
};
|