Add name to account model

Add the name of the account that's connected to the control link on the
web interface via an additional property on the ready payload for the
control link.
This commit is contained in:
Hornwitser committed 2021-03-18 23:05:47 +01:00
1 parent e1549c1b0c
commit 6458810571
6 files changed
+50 -5

No files matched your search

+1 -1
View File
@@ -401,7 +401,7 @@ class WebSocketClientConnector extends WebSocketBaseConnector {
}
this._startedReconnect = null;
this._connected = true;
this.emit("connect");
this.emit("connect", data);
} else if (type === "continue") {
logger.verbose("SOCKET | resuming existing session");
+7
View File
@@ -102,6 +102,13 @@ const serverHandshake = ajv.compile({
properties: {
"session_token": { type: "string" },
"heartbeat_interval": { type: "number" },
"account": {
additionalProperties: false,
required: ["name"],
properties: {
"name": { type: "string" },
},
},
},
},
},
+9 -2
View File
@@ -1371,8 +1371,10 @@ class WebSocketServerConnector extends libLink.WebSocketBaseConnector {
*
* @param {string} sessionToken -
* the session token to send to the client.
* @param {Object=} additionalData -
* extra properties to send along the ready message.
*/
ready(sessionToken) {
ready(sessionToken, additionalData) {
this._heartbeatInterval = masterConfig.get("master.heartbeat_interval");
this._socket.send(JSON.stringify({
seq: null,
@@ -1380,6 +1382,7 @@ class WebSocketServerConnector extends libLink.WebSocketBaseConnector {
data: {
session_token: sessionToken,
heartbeat_interval: this._heartbeatInterval,
...additionalData,
},
}));
@@ -1650,6 +1653,7 @@ async function handleHandshake(message, socket, req, attachHandler) {
let connector = new WebSocketServerConnector(socket, sessionId);
activeConnectors.set(sessionId, connector);
let additionalReadyData = {};
if (type === "register_slave") {
let connection = slaveConnections.get(data.id);
if (connection) {
@@ -1664,9 +1668,12 @@ async function handleHandshake(message, socket, req, attachHandler) {
} else if (type === "register_control") {
logger.verbose(`SOCKET | registered control from ${req.socket.remoteAddress}`);
controlConnections.push(new ControlConnection(data, connector, user));
additionalReadyData["account"] = {
name: user.name,
};
}
connector.ready(sessionToken);
connector.ready(sessionToken, additionalReadyData);
}
wss.on("connection", (socket, req) => {
@@ -52,6 +52,8 @@ export default function SiteLayout(props) {
}
let accountMenu = <Menu>
<Menu.ItemGroup title={account.name}/>
<Menu.Divider/>
<Menu.Item danger onClick={() => { account.logOut(); }}>Log out</Menu.Item>
</Menu>;
+19 -1
View File
@@ -1,17 +1,35 @@
import { useContext } from "react";
import { useContext, useEffect, useState } from "react";
import { notifyErrorHandler } from "../util/notify";
import ControlContext from "../components/ControlContext";
/**
* @typedef {Object} Account
* @property {?string} name - Name of the currently logged in account.
* @property {function()} logout - Logs out of the web interface.
*/
export function useAccount() {
let control = useContext(ControlContext);
let [name, setName] = useState(control.accountName);
useEffect(() => {
function onConnect(data) {
setName(data.account.name);
}
function onClose() {
setName(null);
}
control.connector.on("connect", onConnect);
control.connector.on("close", onClose);
return () => {
control.connector.off("connect", onConnect);
control.connector.off("close", onClose);
};
}, [control]);
return {
name,
logOut() {
control.loggingOut = true;
control.shutdown(notifyErrorHandler("Error logging out"));
+12 -1
View File
@@ -55,13 +55,24 @@ export class Control extends libLink.Link {
*/
this.loggingOut = false;
/**
* Name of the account this control link is connected as.
* @type {?string}
*/
this.accountName = null;
this.instanceLogHandlers = new Map();
this.connector.on("connect", () => {
this.connector.on("connect", data => {
this.accountName = data.account.name;
this.updateLogSubscriptions().catch(err => logger.error(
`Unexpected error updating log subscriptions:\n${err.stack}`
));
});
this.connector.on("close", () => {
this.accountName = null;
});
}
async logMessageEventHandler(message) {