diff --git a/package.json b/package.json index 4d84251..6f464e2 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ "matrix-server-config": "src/matrix-server-config.js", "matrix-receive": "src/matrix-receive.js", "matrix-send-message": "src/matrix-send-message.js", + "matrix-send-message": "src/matrix-delete-message.js", "matrix-send-file": "src/matrix-send-file.js", "matrix-send-image": "src/matrix-send-image.js", "matrix-react": "src/matrix-react.js", diff --git a/src/matrix-delete-message.html b/src/matrix-delete-message.html new file mode 100644 index 0000000..e12e08f --- /dev/null +++ b/src/matrix-delete-message.html @@ -0,0 +1,84 @@ + + + + + diff --git a/src/matrix-delete-message.js b/src/matrix-delete-message.js new file mode 100644 index 0000000..362064d --- /dev/null +++ b/src/matrix-delete-message.js @@ -0,0 +1,74 @@ +module.exports = function(RED) { + function MatrixDeleteMessage(n) { + RED.nodes.createNode(this,n); + + var node = this; + + this.name = n.name; + this.server = RED.nodes.getNode(n.server); + this.roomId = n.roomId; + this.reason = n.reason + + if (!node.server) { + node.warn("No configuration node"); + return; + } + + node.status({ fill: "red", shape: "ring", text: "disconnected" }); + + 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', function(msg) { + + if(!msg.eventId) { + node.error("eventId is missing"); + return; + } + + if (!node.server || !node.server.matrixClient) { + node.warn("No matrix server selected"); + return; + } + + if(!node.server.isConnected()) { + node.error("Matrix server connection is currently closed"); + node.send([null, msg]); + return; + } + + msg.topic = node.roomId || msg.topic; + if(!msg.topic) { + node.warn("Room must be specified in msg.topic or in configuration"); + return; + } + + msg.reason = node.reason || msg.reason; + + if(!msg.reason) { + msg.reason = ''; + } + + node.server.matrixClient.redactEvent(msg.topic,msg.eventId,undefined ,{ + reason: msg.reason + }) + + .then(function(e) { + msg.deleted = true + node.send([msg, null]); + }) + .catch(function(e){ + node.warn("Error sending message " + e); + msg.error = e; + msg.deleted = false + node.send([null, msg]); + }); + }); + } + RED.nodes.registerType("matrix-delete-message",MatrixDeleteMessage); +}