- Messages used to get ignored if they were over 1 second old. This caused issues with messages being ignored on servers that took longer than a second to respond (*cough* matrix.org *cough*). Now it accepts all new messages. Moving forward any messages that are sent before Node-RED starts or gets deployed will be ignored (so that old messages don't pop up since the cache clears every deploy/node-red restart so old messages look like new ones).

- When the matrix-server node would shut down (from a deployment) it wasn't properly shutting the old instance of the client down. This would cause messages to start duplicating X times (X being however many times you deployed since you last started Node-RED). This is now fixed.
- Upgraded to matrix-js-sdk 15.3.0
- Message processing is now done on the matrix-server node. Before if you had multiple matrix-receive nodes it would output one line per matrix-receive node into the log. Now it only outputs from the matrix-server node. Note that all messages get logged that are sent to rooms the bot is in whereas before it would only log message in rooms the matrix-receive node was in.
- Fixed Shared secret registration failing if user_type was defined.
This commit is contained in:
Skylar Sadlier 2022-01-14 12:56:14 -07:00
parent 0b9c59a7be
commit 25c92b787a
6 changed files with 7771 additions and 2072 deletions

9748
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,12 +1,12 @@
{ {
"name": "node-red-contrib-matrix-chat", "name": "node-red-contrib-matrix-chat",
"version": "0.2.7", "version": "0.3.0",
"description": "Matrix chat server client for Node-RED", "description": "Matrix chat server client for Node-RED",
"dependencies": { "dependencies": {
"fs-extra": "^9.1.0", "fs-extra": "^9.1.0",
"got": "^11.8.2", "got": "^11.8.2",
"isomorphic-webcrypto": "^2.3.8", "isomorphic-webcrypto": "^2.3.8",
"matrix-js-sdk": "^12.2.0", "matrix-js-sdk": "^15.3.0",
"node-localstorage": "^2.2.1", "node-localstorage": "^2.2.1",
"olm": "https://packages.matrix.org/npm/olm/olm-3.2.1.tgz", "olm": "https://packages.matrix.org/npm/olm/olm-3.2.1.tgz",
"process": "^0.11.10", "process": "^0.11.10",

View File

@ -22,7 +22,7 @@ module.exports = function(RED) {
return; return;
} }
node.server.on("disconnected", function() { node.server.on("disconnected", function(){
node.status({ fill: "red", shape: "ring", text: "disconnected" }); node.status({ fill: "red", shape: "ring", text: "disconnected" });
}); });
@ -30,43 +30,12 @@ module.exports = function(RED) {
node.status({ fill: "green", shape: "ring", text: "connected" }); node.status({ fill: "green", shape: "ring", text: "connected" });
}); });
node.server.on("Room.timeline", async function(event, room, toStartOfTimeline, data) { node.server.on("Room.timeline", async function(event, room, toStartOfTimeline, removed, data, msg) {
if (toStartOfTimeline) {
return; // ignore paginated results
}
if (!event.getSender() || event.getSender() === node.server.userId) {
return; // ignore our own messages
}
if (!event.getUnsigned() || event.getUnsigned().age > 1000) {
return; // ignore old messages
}
// if node has a room ID set we only listen on that room // if node has a room ID set we only listen on that room
if(node.roomIds.length && node.roomIds.indexOf(room.roomId) === -1) { if(node.roomIds.length && node.roomIds.indexOf(room.roomId) === -1) {
return; return;
} }
try {
await node.server.matrixClient.decryptEventIfNeeded(event);
} catch (error) {
node.error(error);
return;
}
let msg = {
encrypted : event.isEncrypted(),
redacted : event.isRedacted(),
content : event.getContent(),
type : (event.getContent()['msgtype'] || event.getType()) || null,
payload : (event.getContent()['body'] || event.getContent()) || null,
userId : event.getSender(),
topic : event.getRoomId(),
eventId : event.getId(),
event : event,
};
node.log("Received" + (msg.encrypted ? ' encrypted' : '') +" timeline event [" + msg.type + "]: (" + room.name + ") " + event.getSender() + " :: " + msg.content.body);
switch(msg.type) { switch(msg.type) {
case 'm.emote': case 'm.emote':
if(!node.acceptEmotes) return; if(!node.acceptEmotes) return;

View File

@ -35,6 +35,7 @@ module.exports = function(RED) {
this.enableE2ee = n.enableE2ee || false; this.enableE2ee = n.enableE2ee || false;
this.e2ee = (this.enableE2ee && this.deviceId); this.e2ee = (this.enableE2ee && this.deviceId);
this.globalAccess = n.global; this.globalAccess = n.global;
this.initializedAt = new Date();
if(!this.credentials.accessToken) { if(!this.credentials.accessToken) {
node.log("Matrix connection failed: missing access token."); node.log("Matrix connection failed: missing access token.");
@ -85,7 +86,6 @@ module.exports = function(RED) {
node.on('close', function(done) { node.on('close', function(done) {
if(node.matrixClient) { if(node.matrixClient) {
node.matrixClient.close();
node.matrixClient.stopClient(); node.matrixClient.stopClient();
node.setConnected(false); node.setConnected(false);
} }
@ -97,8 +97,41 @@ module.exports = function(RED) {
return node.connected; return node.connected;
}; };
node.matrixClient.on("Room.timeline", async function(event, room, toStartOfTimeline, data) { node.matrixClient.on("Room.timeline", async function(event, room, toStartOfTimeline, removed, data) {
node.emit("Room.timeline", event, room, toStartOfTimeline, data); if (toStartOfTimeline) {
return; // ignore paginated results
}
if (!event.getSender() || event.getSender() === node.userId) {
return; // ignore our own messages
}
if (!data || !data.liveEvent) {
return; // ignore old message (we only want live events)
}
if(node.initializedAt > event.getDate()) {
return; // skip events that occurred before our client initialized
}
try {
await node.matrixClient.decryptEventIfNeeded(event);
} catch (error) {
node.error(error);
return;
}
let msg = {
encrypted : event.isEncrypted(),
redacted : event.isRedacted(),
content : event.getContent(),
type : (event.getContent()['msgtype'] || event.getType()) || null,
payload : (event.getContent()['body'] || event.getContent()) || null,
userId : event.getSender(),
topic : event.getRoomId(),
eventId : event.getId(),
event : event,
};
node.log("Received" + (msg.encrypted ? ' encrypted' : '') +" timeline event [" + msg.type + "]: (" + room.name + ") " + event.getSender() + " :: " + msg.content.body + (data.liveEvent ? ' [LIVE]' : '') + (toStartOfTimeline ? ' [PAGINATED]' : ''));
node.emit("Room.timeline", event, room, toStartOfTimeline, removed, data, msg);
}); });
// node.matrixClient.on("RoomMember.typing", async function(event, member) { // node.matrixClient.on("RoomMember.typing", async function(event, member) {
@ -223,7 +256,9 @@ module.exports = function(RED) {
node.matrixClient.setGlobalErrorOnUnknownDevices(false); node.matrixClient.setGlobalErrorOnUnknownDevices(false);
} }
node.log("Connecting to Matrix server..."); node.log("Connecting to Matrix server...");
await node.matrixClient.startClient({ initialSyncLimit: 8 }); await node.matrixClient.startClient({
initialSyncLimit: 8
});
} catch(error){ } catch(error){
node.error(error); node.error(error);
} }

View File

@ -78,7 +78,10 @@
<dt class="optional">msg.payload.user_type <dt class="optional">msg.payload.user_type
<span class="property-type">string | null</span> <span class="property-type">string | null</span>
</dt> </dt>
<dd> Set the user type. Leave this to null if you don't know what it is for. Check <a href="https://github.com/matrix-org/synapse/blob/master/synapse/api/constants.py">here</a> and look for <code>class UserTypes</code> to figure out what is valid.</dd> <dd>
Set the user type. Leave this to null if you don't know what it is for. Check <a href="https://github.com/matrix-org/synapse/blob/master/synapse/api/constants.py">here</a> and look for <code>class UserTypes</code> to figure out what is valid.<br>
NOTE: Testing shows that "bot" and "support" do not work for this. Generate a normal user to use as a bot.
</dd>
</dl> </dl>
<h3>Outputs</h3> <h3>Outputs</h3>

View File

@ -62,7 +62,13 @@ module.exports = function(RED) {
.update(utf8.encode(msg.payload.password)) .update(utf8.encode(msg.payload.password))
.update("\x00") .update("\x00")
.update(msg.payload.admin ? "admin" : "notadmin") .update(msg.payload.admin ? "admin" : "notadmin")
.digest('hex');
if(msg.payload.user_type || null) {
hmac.update("\x00")
.update(msg.payload.user_type);
}
hmac = hmac.digest('hex');
try { try {
response = await got.post(this.server + '/_synapse/admin/v1/register', { response = await got.post(this.server + '/_synapse/admin/v1/register', {