From b93a54f04195a3d1d478dbc6b4a0a8371b6d5345 Mon Sep 17 00:00:00 2001 From: Skylar Sadlier Date: Mon, 30 Aug 2021 13:15:22 -0600 Subject: [PATCH] - Fixed not being able to disable e2ee - Session.logged_out events are now processed to display an error from the node (helps user figure out why their login failed). --- src/matrix-server-config.html | 6 +++--- src/matrix-server-config.js | 34 ++++++++++++++++++++++++++++------ 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/src/matrix-server-config.html b/src/matrix-server-config.html index 6589bdf..902435c 100644 --- a/src/matrix-server-config.html +++ b/src/matrix-server-config.html @@ -6,12 +6,12 @@ userId: { type: "text", required: true }, accessToken: { type: "password", required: true }, deviceId: { type: "text", required: true }, - url: { type: "text", required: true }, - enableE2ee: { type: "checkbox", value: true } + url: { type: "text", required: true } }, defaults: { name: { value: null }, - autoAcceptRoomInvites: { value: true } + autoAcceptRoomInvites: { value: true }, + enableE2ee: { type: "checkbox", value: true } }, icon: "matrix.png", label: function() { diff --git a/src/matrix-server-config.js b/src/matrix-server-config.js index 8726ef6..61775f8 100644 --- a/src/matrix-server-config.js +++ b/src/matrix-server-config.js @@ -27,7 +27,7 @@ module.exports = function(RED) { this.deviceId = this.credentials.deviceId || null; this.url = this.credentials.url; this.autoAcceptRoomInvites = n.autoAcceptRoomInvites; - this.enableE2ee = this.credentials.enableE2ee || false; + this.enableE2ee = this.enableE2ee || false; this.e2ee = this.enableE2ee && this.deviceId; if(!this.credentials.accessToken) { @@ -134,15 +134,38 @@ module.exports = function(RED) { } }); + node.matrixClient.on("Session.logged_out", async function(errorObj){ + // Example if user auth token incorrect: + // { + // errcode: 'M_UNKNOWN_TOKEN', + // data: { + // errcode: 'M_UNKNOWN_TOKEN', + // error: 'Invalid macaroon passed.', + // soft_logout: false + // }, + // httpStatus: 401 + // } + + node.error("[Session.logged_out] " + errorObj); + }); + async function run() { if(node.e2ee){ - node.matrixClient.initCrypto() - .catch((error) => node.error(error)); + const initCrypto = ms => new Promise(res => node.matrixClient.initCrypto()); + try { + await initCrypto(); + } catch(error){ + node.error(error); + } node.matrixClient.setGlobalErrorOnUnknownDevices(false); } - node.matrixClient.startClient({ initialSyncLimit: 8 }) - .catch((error) => node.error(error)); + const startClient = ms => new Promise(res => node.matrixClient.startClient({ initialSyncLimit: 8 })); + try { + await startClient(); + } catch(error){ + node.error(error); + } } node.log("Connecting to Matrix server..."); @@ -155,7 +178,6 @@ module.exports = function(RED) { userId: { type:"text", required: true }, accessToken: { type:"text", required: true }, deviceId: { type: "text", required: true }, - enableE2ee: { type: "checkbox", value: true }, url: { type: "text", required: true }, } });