mirror of
https://github.com/Skylar-Tech/node-red-contrib-matrix-chat.git
synced 2025-04-19 20:43:04 -06:00
- #100 add node for sending typing state to rooms
- fix global and flow variable in getters for Matrix Room States node
This commit is contained in:
parent
2e9633e113
commit
785e0cd7be
@ -39,7 +39,8 @@
|
|||||||
"matrix-synapse-create-edit-user": "src/matrix-synapse-create-edit-user.js",
|
"matrix-synapse-create-edit-user": "src/matrix-synapse-create-edit-user.js",
|
||||||
"matrix-synapse-deactivate-user": "src/matrix-synapse-deactivate-user.js",
|
"matrix-synapse-deactivate-user": "src/matrix-synapse-deactivate-user.js",
|
||||||
"matrix-synapse-join-room": "src/matrix-synapse-join-room.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": {
|
"engines": {
|
||||||
|
@ -74,24 +74,19 @@ module.exports = function(RED) {
|
|||||||
if (rule.tot === "msg") {
|
if (rule.tot === "msg") {
|
||||||
value = RED.util.getMessageProperty(msg,rule.to);
|
value = RED.util.getMessageProperty(msg,rule.to);
|
||||||
} else if ((rule.tot === 'flow') || (rule.tot === 'global')) {
|
} else if ((rule.tot === 'flow') || (rule.tot === 'global')) {
|
||||||
RED.util.evaluateNodeProperty(rule.to, rule.tot, node, msg, (err,value) => {
|
try {
|
||||||
if (err) {
|
value = RED.util.evaluateNodeProperty(rule.to, rule.tot, node, msg);
|
||||||
|
} catch(e2) {
|
||||||
throw new Error("Invalid value evaluation");
|
throw new Error("Invalid value evaluation");
|
||||||
} else {
|
|
||||||
return value;
|
|
||||||
}
|
}
|
||||||
});
|
|
||||||
return
|
|
||||||
} else if (rule.tot === 'date') {
|
} else if (rule.tot === 'date') {
|
||||||
value = Date.now();
|
value = Date.now();
|
||||||
} else if (rule.tot === 'jsonata') {
|
} else if (rule.tot === 'jsonata') {
|
||||||
RED.util.evaluateJSONataExpression(rule.to,msg, (err, value) => {
|
try {
|
||||||
if (err) {
|
value = RED.util.evaluateJSONataExpression(rule.to,msg);
|
||||||
|
} catch(e3) {
|
||||||
throw new Error("Invalid expression");
|
throw new Error("Invalid expression");
|
||||||
} else {
|
|
||||||
return value;
|
|
||||||
}
|
}
|
||||||
});
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
return value;
|
return value;
|
||||||
|
108
src/matrix-typing.html
Normal file
108
src/matrix-typing.html
Normal file
@ -0,0 +1,108 @@
|
|||||||
|
<script type="text/javascript">
|
||||||
|
RED.nodes.registerType('matrix-typing', {
|
||||||
|
category: 'matrix',
|
||||||
|
color: '#00b7ca',
|
||||||
|
icon: "matrix.png",
|
||||||
|
outputLabels: ["success", "error"],
|
||||||
|
inputs: 1,
|
||||||
|
outputs: 2,
|
||||||
|
defaults: {
|
||||||
|
name: { value: null },
|
||||||
|
server: { type: "matrix-server-config" },
|
||||||
|
roomType: { value: "msg" },
|
||||||
|
roomValue: { value: "topic" },
|
||||||
|
typingType: { value: "bool" },
|
||||||
|
typingValue: { value: true },
|
||||||
|
timeoutMsType: { value: "num" },
|
||||||
|
timeoutMsValue: { value: 20000 },
|
||||||
|
},
|
||||||
|
label: function() {
|
||||||
|
return this.name || "Typing";
|
||||||
|
},
|
||||||
|
oneditprepare: function() {
|
||||||
|
$("#node-input-room").typedInput({
|
||||||
|
type: this.roomType,
|
||||||
|
types:['msg','flow','global','str'],
|
||||||
|
}).typedInput('value', this.roomValue);
|
||||||
|
|
||||||
|
$("#node-input-typing").typedInput({
|
||||||
|
types:['msg','flow','global','bool'],
|
||||||
|
})
|
||||||
|
.typedInput('value', this.typingValue)
|
||||||
|
.typedInput('type', this.typingType);
|
||||||
|
|
||||||
|
$("#node-input-timeoutMs").typedInput({
|
||||||
|
types:['msg','flow','global','num'],
|
||||||
|
})
|
||||||
|
.typedInput('value', this.timeoutMsValue)
|
||||||
|
.typedInput('type', this.timeoutMsType);
|
||||||
|
},
|
||||||
|
oneditsave: function() {
|
||||||
|
this.roomType = $("#node-input-room").typedInput('type');
|
||||||
|
this.roomValue = $("#node-input-room").typedInput('value');
|
||||||
|
this.typingType = $("#node-input-typing").typedInput('type');
|
||||||
|
this.typingValue = $("#node-input-typing").typedInput('value');
|
||||||
|
this.timeoutMsType = $("#node-input-timeoutMs").typedInput('type');
|
||||||
|
this.timeoutMsValue = $("#node-input-timeoutMs").typedInput('value');
|
||||||
|
},
|
||||||
|
paletteLabel: 'Typing'
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<script type="text/html" data-template-name="matrix-typing">
|
||||||
|
<div class="form-row">
|
||||||
|
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
|
||||||
|
<input type="text" id="node-input-name" placeholder="Name">
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="form-row">
|
||||||
|
<label for="node-input-server"><i class="fa fa-user"></i> Matrix Server Config</label>
|
||||||
|
<input type="text" id="node-input-server">
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="form-row">
|
||||||
|
<label for="node-input-room"><i class="fa fa-comments"></i> Room</label>
|
||||||
|
<input type="text" id="node-input-room">
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="form-row">
|
||||||
|
<label for="node-input-room"><i class="fa fa-commenting-o"></i> Is Typing</label>
|
||||||
|
<input type="text" id="node-input-typing">
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="form-row">
|
||||||
|
<label for="node-input-room"><i class="fa fa-clock-o"></i> Timeout Milliseconds</label>
|
||||||
|
<input type="text" id="node-input-timeoutMs">
|
||||||
|
</div>
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<script type="text/html" data-help-name="matrix-typing">
|
||||||
|
<h3>Details</h3>
|
||||||
|
<p>
|
||||||
|
Sends typing event to a room
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<h3>Inputs</h3>
|
||||||
|
<dl class="message-properties">
|
||||||
|
<dt>dynamic
|
||||||
|
<span class="property-type">any</span>
|
||||||
|
</dt>
|
||||||
|
<dd> The inputs are configurable on the node.</dd>
|
||||||
|
</dl>
|
||||||
|
|
||||||
|
<h3>Outputs</h3>
|
||||||
|
<ol class="node-ports">
|
||||||
|
<li>Success
|
||||||
|
<dl class="message-properties">
|
||||||
|
<dd>Returns from first output on success</dd>
|
||||||
|
</dl>
|
||||||
|
</li>
|
||||||
|
<li>Error
|
||||||
|
<dl class="message-properties">
|
||||||
|
<dt>msg.error <span class="property-type">string</span></dt>
|
||||||
|
<dd>the error that occurred.</dd>
|
||||||
|
</dl>
|
||||||
|
</li>
|
||||||
|
</ol>
|
||||||
|
|
||||||
|
</script>
|
86
src/matrix-typing.js
Normal file
86
src/matrix-typing.js
Normal file
@ -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);
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user