Factor interuser, servertimelinestats

Noah Zalev committed Dec 8, 2021 at 00:14 UTC ca751731525584f3c5320b573517e23cd2055b9c
1 file changed +37 -39
meshuser.js
+37 -39
@@ -631,45 +631,6 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
631 // pass through to switch statement until refactoring complete
632
633 switch (command.action) {
634 - case 'interuser':
635 - {
636 - // Sends data between users only if allowed. Only a user in the "interUserMessaging": [] list, in the settings section of the config.json can receive and send inter-user messages from and to all users.
637 - if ((parent.parent.config.settings.interusermessaging == null) || (parent.parent.config.settings.interusermessaging == false) || (command.data == null)) return;
638 - if (typeof command.sessionid == 'string') { var userSessionId = command.sessionid.split('/'); if (userSessionId.length != 4) return; command.userid = userSessionId[0] + '/' + userSessionId[1] + '/' + userSessionId[2]; }
639 - if (common.validateString(command.userid, 0, 2014) == false) return;
640 - var userSplit = command.userid.split('/');
641 - if (userSplit.length == 1) { command.userid = 'user/' + domain.id + '/' + command.userid; userSplit = command.userid.split('/'); }
642 - if ((userSplit.length != 3) || (userSplit[0] != 'user') || (userSplit[1] != domain.id) || (parent.users[command.userid] == null)) return; // Make sure the target userid is valid and within the domain
643 - const allowed = ((parent.parent.config.settings.interusermessaging === true) || (parent.parent.config.settings.interusermessaging.indexOf(obj.user._id) >= 0) || (parent.parent.config.settings.interusermessaging.indexOf(command.userid) >= 0));
644 - if (allowed == false) return;
645 -
646 - // Get sessions
647 - var sessions = parent.wssessions[command.userid];
648 - if (sessions == null) break;
649 -
650 - // Create the notification message and send on all sessions except our own (no echo back).
651 - var notification = JSON.stringify({ action: 'interuser', sessionid: ws.sessionId, data: command.data, scope: (command.sessionid != null)?'session':'user' });
652 - for (var i in sessions) {
653 - if ((command.sessionid != null) && (sessions[i].sessionId != command.sessionid)) continue; // Send to a specific session
654 - if (sessions[i] != obj.ws) { try { sessions[i].send(notification); } catch (ex) { } }
655 - }
656 -
657 - // TODO: Send the message of user sessions connected to other servers.
658 -
659 - break;
660 - }
661 - case 'servertimelinestats':
662 - {
663 - // Only accept if the "My Server" tab is allowed for this domain
664 - if (domain.myserver === false) break;
665 -
666 - if ((user.siteadmin & 21) == 0) return; // Only site administrators with "site backup" or "site restore" or "site update" permissions can use this.
667 - if (common.validateInt(command.hours, 0, 24 * 30) == false) return;
668 - db.GetServerStats(command.hours, function (err, docs) {
669 - if (err == null) { try { ws.send(JSON.stringify({ action: 'servertimelinestats', events: docs })); } catch (ex) { } }
670 - });
671 - break;
672 - }
634 case 'nodes':
635 {
636 var links = [], extraids = null, err = null;
@@ -5422,6 +5383,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
5383 'getnetworkinfo': serverCommandGetNetworkInfo,
5384 'getsysinfo': serverCommandGetSysInfo,
5385 'intersession': serverCommandInterSession,
5386 + 'interuser': serverCommandInterUser,
5387 'lastconnect': serverCommandLastConnect,
5388 'lastconnects': serverCommandLastConnects,
5389 'logincookie': serverCommandLoginCookie,
@@ -5435,6 +5397,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
5397 'serverconsole': serverCommandServerConsole,
5398 'servererrors': serverCommandServerErrors,
5399 'serverstats': serverCommandServerStats,
5400 + 'servertimelinestats': serverCommandServerTimelineStats,
5401 'serverupdate': serverCommandServerUpdate,
5402 'serverversion': serverCommandServerVersion,
5403 'urlargs': serverCommandUrlArgs,
@@ -5886,6 +5849,30 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
5849 // TODO: Send the message of user sessions connected to other servers.
5850 }
5851
5852 + function serverCommandInterUser(command) {
5853 + // Sends data between users only if allowed. Only a user in the "interUserMessaging": [] list, in the settings section of the config.json can receive and send inter-user messages from and to all users.
5854 + if ((parent.parent.config.settings.interusermessaging == null) || (parent.parent.config.settings.interusermessaging == false) || (command.data == null)) return;
5855 + if (typeof command.sessionid == 'string') { var userSessionId = command.sessionid.split('/'); if (userSessionId.length != 4) return; command.userid = userSessionId[0] + '/' + userSessionId[1] + '/' + userSessionId[2]; }
5856 + if (common.validateString(command.userid, 0, 2014) == false) return;
5857 + var userSplit = command.userid.split('/');
5858 + if (userSplit.length == 1) { command.userid = 'user/' + domain.id + '/' + command.userid; userSplit = command.userid.split('/'); }
5859 + if ((userSplit.length != 3) || (userSplit[0] != 'user') || (userSplit[1] != domain.id) || (parent.users[command.userid] == null)) return; // Make sure the target userid is valid and within the domain
5860 + const allowed = ((parent.parent.config.settings.interusermessaging === true) || (parent.parent.config.settings.interusermessaging.indexOf(obj.user._id) >= 0) || (parent.parent.config.settings.interusermessaging.indexOf(command.userid) >= 0));
5861 + if (allowed == false) return;
5862 +
5863 + // Get sessions
5864 + var sessions = parent.wssessions[command.userid];
5865 + if (sessions == null) return;
5866 +
5867 + // Create the notification message and send on all sessions except our own (no echo back).
5868 + var notification = JSON.stringify({ action: 'interuser', sessionid: ws.sessionId, data: command.data, scope: (command.sessionid != null)?'session':'user' });
5869 + for (var i in sessions) {
5870 + if ((command.sessionid != null) && (sessions[i].sessionId != command.sessionid)) continue; // Send to a specific session
5871 + if (sessions[i] != obj.ws) { try { sessions[i].send(notification); } catch (ex) { } }
5872 + }
5873 + // TODO: Send the message of user sessions connected to other servers.
5874 + }
5875 +
5876 function serverCommandLastConnect(command) {
5877 if (!validNodeIdAndDomain(command)) return;
5878
@@ -6088,6 +6075,17 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
6075 }
6076 }
6077
6078 + function serverCommandServerTimelineStats(command) {
6079 + // Only accept if the "My Server" tab is allowed for this domain
6080 + if (domain.myserver === false) return;
6081 +
6082 + if ((user.siteadmin & 21) == 0) return; // Only site administrators with "site backup" or "site restore" or "site update" permissions can use this.
6083 + if (common.validateInt(command.hours, 0, 24 * 30) == false) return;
6084 + db.GetServerStats(command.hours, function (err, docs) {
6085 + if (err == null) { try { ws.send(JSON.stringify({ action: 'servertimelinestats', events: docs })); } catch (ex) { } }
6086 + });
6087 + }
6088 +
6089 function serverCommandServerUpdate(command) {
6090 // Do not allow this command when logged in using a login token
6091 if (req.session.loginToken != null) return;