- Reduce margin between checkboxes for matrix-receive node

- matrix-send-message node can now be used to reply in a thread (closes #104)
- matrix-receive node now returns msg.mentions for easier access to who was mentioned in message
- matrix-receive node now returns boolean msg.isThread based on whether the message is a thread reply or not
This commit is contained in:
Skylar Sadlier 2023-11-25 05:38:09 -07:00
parent fd174e32ff
commit 000c28e3b8
5 changed files with 85 additions and 22 deletions

View File

@ -45,7 +45,7 @@
<div class="form-row" style="margin-left: 100px;margin-top:10px;font-weight:bold;">
Timeline event filters
</div>
<div class="form-row">
<div class="form-row" style="margin-bottom:0;">
<input
type="checkbox"
id="node-input-acceptText"
@ -55,7 +55,7 @@
Accept text <code style="text-transform: none;">m.text</code>
</label>
</div>
<div class="form-row">
<div class="form-row" style="margin-bottom:0;">
<input
type="checkbox"
id="node-input-acceptEmotes"
@ -65,7 +65,7 @@
Accept emotes <code style="text-transform: none;">m.emote</code>
</label>
</div>
<div class="form-row">
<div class="form-row" style="margin-bottom:0;">
<input
type="checkbox"
id="node-input-acceptStickers"
@ -75,7 +75,7 @@
Accept stickers <code style="text-transform: none;">m.sticker</code>
</label>
</div>
<div class="form-row">
<div class="form-row" style="margin-bottom:0;">
<input
type="checkbox"
id="node-input-acceptReactions"
@ -85,7 +85,7 @@
Accept reactions <code style="text-transform: none;">m.reaction</code>
</label>
</div>
<div class="form-row">
<div class="form-row" style="margin-bottom:0;">
<input
type="checkbox"
id="node-input-acceptFiles"
@ -95,7 +95,7 @@
Accept files <code style="text-transform: none;">m.file</code>
</label>
</div>
<div class="form-row">
<div class="form-row" style="margin-bottom:0;">
<input
type="checkbox"
id="node-input-acceptAudio"
@ -105,7 +105,7 @@
Accept files <code style="text-transform: none;">m.audio</code>
</label>
</div>
<div class="form-row">
<div class="form-row" style="margin-bottom:0;">
<input
type="checkbox"
id="node-input-acceptImages"
@ -115,7 +115,7 @@
Accept images <code style="text-transform: none;">m.image</code>
</label>
</div>
<div class="form-row">
<div class="form-row" style="margin-bottom:0;">
<input
type="checkbox"
id="node-input-acceptVideos"
@ -125,7 +125,7 @@
Accept videos <code style="text-transform: none;">m.video</code>
</label>
</div>
<div class="form-row">
<div class="form-row" style="margin-bottom:0;">
<input
type="checkbox"
id="node-input-acceptLocations"

View File

@ -1,3 +1,4 @@
const {RelationType} = require("matrix-js-sdk");
module.exports = function(RED) {
function MatrixReceiveMessage(n) {
RED.nodes.createNode(this, n);

View File

@ -6,6 +6,7 @@
outputLabels: ["success", "error"],
inputs:1,
outputs:2,
paletteLabel: 'Send Message',
defaults: {
name: { value: null },
server: { type: "matrix-server-config" },
@ -13,12 +14,24 @@
message: { value: null },
messageType: { value: 'm.text' },
messageFormat: { value: '' },
replaceMessage : { value: false }
replaceMessage : { value: false },
threadReplyType: { value: "msg" },
threadReplyValue: { value: "isThread" },
},
label: function() {
return this.name || "Send Message";
},
paletteLabel: 'Send Message'
oneditprepare: function() {
$("#node-input-threadReply").typedInput({
types:['msg','flow','global','bool'],
})
.typedInput('value', this.threadReplyValue || "isThread")
.typedInput('type', this.threadReplyType || "msg");
},
oneditsave: function() {
this.threadReplyType = $("#node-input-threadReply").typedInput('type');
this.threadReplyValue = $("#node-input-threadReply").typedInput('value');
},
});
</script>
@ -72,6 +85,11 @@
It's recommended to use m.notice for bots because the message will render in a lighter text (at least in Element client) for users to distinguish bot and real user messages.
</div>
<div class="form-row">
<label for="node-input-threadReply"><i class="fa fa-commenting-o"></i> Thread Reply</label>
<input type="text" id="node-input-threadReply">
</div>
<div class="form-row">
<label for="node-input-messageFormat">
Message Format

View File

@ -13,6 +13,8 @@ module.exports = function(RED) {
this.messageFormat = n.messageFormat;
this.replaceMessage = n.replaceMessage;
this.message = n.message;
this.threadReplyType = n.threadReplyType || null;
this.threadReplyValue = n.threadReplyValue || null;
// taken from https://github.com/matrix-org/synapse/blob/master/synapse/push/mailer.py
this.allowedTags = [
@ -67,8 +69,27 @@ module.exports = function(RED) {
});
node.on("input", function (msg) {
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');
} else if(type === "num") {
value = Number(property);
}
return value;
}
let msgType = node.messageType,
msgFormat = node.messageFormat;
msgFormat = node.messageFormat,
threadReply = getToValue(msg, node.threadReplyType, node.threadReplyValue);
if (!node.server || !node.server.matrixClient) {
node.warn("No matrix server selected");
@ -143,6 +164,25 @@ module.exports = function(RED) {
event_id: msg.eventId
};
content['body'] = ' * ' + content['body'];
} else if(threadReply) {
// if incoming message is a reply to a thread we fetch the thread parent from the m.relates_to property
// otherwise fallback to msg.eventId
console.log(msg);
console.log(msg?.content?.['m.relates_to']?.rel_type);
console.log(msg?.content?.['m.relates_to']?.event_id);
let threadParent = (msg?.content?.['m.relates_to']?.rel_type === RelationType.Thread ? msg?.content?.['m.relates_to']?.event_id : null) || msg.eventId;
if(threadParent) {
content["m.relates_to"] = {
"rel_type": RelationType.Thread,
"event_id": threadParent,
"is_falling_back": true,
};
if(msg.eventId !== threadParent) {
content["m.relates_to"]["m.in_reply_to"] = {
"event_id": msg.eventId
};
}
}
}
}

View File

@ -1,3 +1,5 @@
const {RelationType} = require("matrix-js-sdk");
global.Olm = require('olm');
const fs = require("fs-extra");
const sdk = require("matrix-js-sdk");
@ -231,10 +233,12 @@ module.exports = function(RED) {
type : (event.getContent()['msgtype'] || event.getType()) || null,
payload : (event.getContent()['body'] || event.getContent()) || null,
isDM : isDmRoom(room),
isThread : event.getContent()?.['m.relates_to']?.rel_type === RelationType.Thread,
mentions : event.getContent()["m.mentions"] || null,
userId : event.getSender(),
topic : event.getRoomId(),
eventId : event.getId(),
event : event
event : event,
};
node.log("Received" + (msg.encrypted ? ' encrypted' : '') +" timeline event [" + msg.type + "]: (" + room.name + ") " + event.getSender() + " :: " + msg.content.body + (toStartOfTimeline ? ' [PAGINATED]' : ''));