Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for messages in threads #104

Closed
justyns opened this issue Nov 23, 2023 · 2 comments · Fixed by #96
Closed

Support for messages in threads #104

justyns opened this issue Nov 23, 2023 · 2 comments · Fixed by #96

Comments

@justyns
Copy link

justyns commented Nov 23, 2023

Hello! Thanks for working on this package, it's been super useful for me. One feature request I have is for native support of matrix threads.

As an example, this is something I hacked together with a function node:

let matrixClient = global.get("matrixClient['@botid']"),
    matrixOnline = global.get("matrixClientOnline['@botid']");

// TODO: For some reason, I can't normally access matrixEvent.decrypted
let parsedEvent = JSON.parse(JSON.stringify(msg.event));

// Determine if the message is part of an existing thread
msg.isPartOfThread = parsedEvent.decrypted?.content['m.relates_to']?.['rel_type'] === 'm.thread';

// Determine the thread ID if part of a thread
const threadEventId = msg.isPartOfThread ? parsedEvent.decrypted?.content['m.relates_to']?.event_id : null;

// Initialize the content object
const content = {
    "body": msg.payload,
    "formatted_body": msg.payload,
    "format": "org.matrix.custom.html",
    "msgtype": "m.text"
};

// Handle thread replies
if (msg.isPartOfThread) {
    // Use the original thread's event_id for the reply
    const threadEventId2 = parsedEvent.decrypted.content['m.relates_to'].event_id;
    content["m.relates_to"] = {
        "rel_type": "m.thread",
        "event_id": threadEventId2,
        "is_falling_back": false,
    };
} else {
    // Start a new thread if not part of an existing thread
    content["m.relates_to"] = {
        "rel_type": "m.thread",
        "event_id": msg.eventId,
        "is_falling_back": false,
    };
}

// Send the message
matrixClient.sendEvent(msg.topic, "m.room.message", content, "").then((res) => {
    msg.response = res;
    return [null, msg];
}).catch((error) => {
    msg.error = error;
    return [null, msg];
});

It basically always either starts a new thread or replies to an existing thread if one already exists. The input is always from a receive node. When I don't need threads, I just use the normal matrix send node.

I'm not actually sure what the best way to handle this would be, but maybe simply a config checkbox on the send node that is something like "reply in thread"?

@skylord123
Copy link
Contributor

One feature request I have is for native support of matrix threads.

Ah yeah it looks like the MSC for thread support was finally approved. Last time I looked into this it was still a proposal/in beta and wasn't official. Looks like we can now implement it.

I'm not actually sure what the best way to handle this would be, but maybe simply a config checkbox on the send node that is something like "reply in thread"?

Yeah that is most likely the best way to handle this. I'll probably make it so you can either set it directly to true/false inside the node config or point it to a property on a msg so that you have options to do it either way. For the next release I have already updated a few existing nodes so that their options can be determined by a property on the msg so it makes sense to do it for this as well. That way you don't need two send message nodes (one for sending a regular message and one for replying).

It may be good to update the receive node to set an is_reply boolean property that tells us if the message we just received is a reply to another message. You can determine this based on the full message object but if we set it to a property directly we could default to reading this property on the send message node to determine if we should reply back to the reply. Not sure on this though, I will have to do some testing to see if it makes sense. I would also have to make it backwards compatible so that we don't just default to replying to replies for people's existing flows. Maybe if the node already exists we default to setting it to false and if the node was newly created we default to it replying to replies.

skylord123 added a commit that referenced this issue Nov 25, 2023
- 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
@skylord123
Copy link
Contributor

@justyns I just pushed up some changes to the dev branch with this feature. Want to try it out and give me feedback? Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants