diff --git a/package.json b/package.json index 1084edf..b16db3f 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,8 @@ "matrix-synapse-create-edit-user": "src/matrix-synapse-create-edit-user.js", "matrix-synapse-deactivate-user": "src/matrix-synapse-deactivate-user.js", "matrix-synapse-join-room": "src/matrix-synapse-join-room.js", - "matrix-whois-user": "src/matrix-whois-user.js" + "matrix-whois-user": "src/matrix-whois-user.js", + "matrix-typing": "src/matrix-typing.js" } }, "engines": { diff --git a/src/matrix-room-state-events.js b/src/matrix-room-state-events.js index 11e31bc..951a634 100644 --- a/src/matrix-room-state-events.js +++ b/src/matrix-room-state-events.js @@ -74,24 +74,19 @@ module.exports = function(RED) { if (rule.tot === "msg") { value = RED.util.getMessageProperty(msg,rule.to); } else if ((rule.tot === 'flow') || (rule.tot === 'global')) { - RED.util.evaluateNodeProperty(rule.to, rule.tot, node, msg, (err,value) => { - if (err) { - throw new Error("Invalid value evaluation"); - } else { - return value; - } - }); - return + try { + value = RED.util.evaluateNodeProperty(rule.to, rule.tot, node, msg); + } catch(e2) { + throw new Error("Invalid value evaluation"); + } } else if (rule.tot === 'date') { value = Date.now(); } else if (rule.tot === 'jsonata') { - RED.util.evaluateJSONataExpression(rule.to,msg, (err, value) => { - if (err) { - throw new Error("Invalid expression"); - } else { - return value; - } - }); + try { + value = RED.util.evaluateJSONataExpression(rule.to,msg); + } catch(e3) { + throw new Error("Invalid expression"); + } return; } return value; diff --git a/src/matrix-typing.html b/src/matrix-typing.html new file mode 100644 index 0000000..0be8db8 --- /dev/null +++ b/src/matrix-typing.html @@ -0,0 +1,108 @@ + + + + + \ No newline at end of file diff --git a/src/matrix-typing.js b/src/matrix-typing.js new file mode 100644 index 0000000..3facb78 --- /dev/null +++ b/src/matrix-typing.js @@ -0,0 +1,86 @@ +module.exports = function(RED) { + function MatrixTyping(n) { + RED.nodes.createNode(this, n); + + let node = this; + + this.name = n.name; + this.server = RED.nodes.getNode(n.server); + this.roomId = n.roomId; + this.roomType = n.roomType; + this.roomValue = n.roomValue; + this.typingType = n.typingType; + this.typingValue = n.typingValue; + this.timeoutMsType = n.timeoutMsType; + this.timeoutMsValue = n.timeoutMsValue; + + node.status({ fill: "red", shape: "ring", text: "disconnected" }); + + if (!node.server) { + node.error("No configuration node", {}); + return; + } + node.server.register(node); + + node.server.on("disconnected", function(){ + node.status({ fill: "red", shape: "ring", text: "disconnected" }); + }); + + node.server.on("connected", function() { + node.status({ fill: "green", shape: "ring", text: "connected" }); + }); + + node.on('input', async function(msg) { + if (! node.server || ! node.server.matrixClient) { + node.error("No matrix server selected", msg); + return; + } + + if(!node.server.isConnected()) { + node.error("Matrix server connection is currently closed", msg); + node.send([null, msg]); + return; + } + + function getToValue(msg, type, property) { + let value = property; + if (type === "msg") { + value = RED.util.getMessageProperty(msg, property); + } else if ((type === 'flow') || (type === 'global')) { + try { + value = RED.util.evaluateNodeProperty(property, type, node, msg); + } catch(e2) { + throw new Error("Invalid value evaluation"); + } + } else if(type === "bool") { + value = (property === 'true'); + } + return value; + } + + try { + let roomId = getToValue(msg, node.roomType, node.roomValue), + typing = getToValue(msg, node.typingType, node.typingValue), + timeoutMs = getToValue(msg, node.timeoutMsType, node.timeoutMsValue); + + if(!roomId) { + node.error('No room provided in msg.topic', msg); + return; + } + + console.log("sending typing",roomId, typing, timeoutMs); + await node.server.matrixClient.sendTyping(roomId, typing, timeoutMs); + node.send([msg, null]); + } catch(e) { + node.error("Failed to send typing event " + msg.topic + ": " + e, msg); + msg.payload = e; + node.send([null, msg]); + } + }); + + node.on("close", function() { + node.server.deregister(node); + }); + } + RED.nodes.registerType("matrix-typing", MatrixTyping); +} \ No newline at end of file