From 257995f91e8906262507c4c3c69dec97b950a8db Mon Sep 17 00:00:00 2001 From: Denperidge Date: Wed, 18 Jan 2023 15:12:43 +0100 Subject: [PATCH 1/2] Added optional variable to reply into a thread Signed-off-by: Cat --- src/MatrixClient.ts | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/src/MatrixClient.ts b/src/MatrixClient.ts index 9058fdea..c94ed214 100644 --- a/src/MatrixClient.ts +++ b/src/MatrixClient.ts @@ -1212,14 +1212,21 @@ export class MatrixClient extends EventEmitter { * @param {string} roomId the room ID to reply in * @param {any} event the event to reply to * @param {string} text the text to reply with + * @param {boolean} thread whether to reply into a thread * @param {string} html the HTML to reply with, or falsey to use the `text` * @returns {Promise} resolves to the event ID which was sent */ @timedMatrixClientFunctionCall() - public replyText(roomId: string, event: any, text: string, html: string = null): Promise { + public replyText(roomId: string, event: any, text: string, thread = false, html: string = null): Promise { if (!html) html = htmlEncode(text); const reply = RichReply.createFor(roomId, event, text, html); + if (thread) { + reply['m.relates_to'] = { + 'rel_type': 'm.thread', + 'event_id': event['event_id'], + }; + } return this.sendMessage(roomId, reply); } @@ -1229,12 +1236,19 @@ export class MatrixClient extends EventEmitter { * @param {string} roomId the room ID to reply in * @param {any} event the event to reply to * @param {string} html the HTML to reply with. + * @param {boolean} thread whether to reply into a thread * @returns {Promise} resolves to the event ID which was sent */ @timedMatrixClientFunctionCall() - public replyHtmlText(roomId: string, event: any, html: string): Promise { + public replyHtmlText(roomId: string, event: any, html: string, thread = false): Promise { const text = htmlToText(html, { wordwrap: false }); const reply = RichReply.createFor(roomId, event, text, html); + if (thread) { + reply['m.relates_to'] = { + 'rel_type': 'm.thread', + 'event_id': event['event_id'], + }; + } return this.sendMessage(roomId, reply); } @@ -1244,15 +1258,22 @@ export class MatrixClient extends EventEmitter { * @param {string} roomId the room ID to reply in * @param {any} event the event to reply to * @param {string} text the text to reply with + * @param {boolean} thread whether to reply into a thread * @param {string} html the HTML to reply with, or falsey to use the `text` * @returns {Promise} resolves to the event ID which was sent */ @timedMatrixClientFunctionCall() - public replyNotice(roomId: string, event: any, text: string, html: string = null): Promise { + public replyNotice(roomId: string, event: any, text: string, thread = false, html: string = null): Promise { if (!html) html = htmlEncode(text); const reply = RichReply.createFor(roomId, event, text, html); reply['msgtype'] = 'm.notice'; + if (thread) { + reply['m.relates_to'] = { + 'rel_type': 'm.thread', + 'event_id': event['event_id'], + }; + } return this.sendMessage(roomId, reply); } @@ -1262,13 +1283,20 @@ export class MatrixClient extends EventEmitter { * @param {string} roomId the room ID to reply in * @param {any} event the event to reply to * @param {string} html the HTML to reply with. + * @param {boolean} thread whether to reply into a thread * @returns {Promise} resolves to the event ID which was sent */ @timedMatrixClientFunctionCall() - public replyHtmlNotice(roomId: string, event: any, html: string): Promise { + public replyHtmlNotice(roomId: string, event: any, html: string, thread = false): Promise { const text = htmlToText(html, { wordwrap: false }); const reply = RichReply.createFor(roomId, event, text, html); reply['msgtype'] = 'm.notice'; + if (thread) { + reply['m.relates_to'] = { + 'rel_type': 'm.thread', + 'event_id': event['event_id'], + }; + } return this.sendMessage(roomId, reply); } From d34e19246b0bac53d0c68f701c1d5077c454aa0a Mon Sep 17 00:00:00 2001 From: Denperidge Date: Wed, 18 Jan 2023 15:57:05 +0100 Subject: [PATCH 2/2] Added threadStartEventId to reply functions This fixes incorrect event_id when replying to a reply in a thread Signed-off-by: Cat --- src/MatrixClient.ts | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/MatrixClient.ts b/src/MatrixClient.ts index c94ed214..b37550fe 100644 --- a/src/MatrixClient.ts +++ b/src/MatrixClient.ts @@ -1222,9 +1222,10 @@ export class MatrixClient extends EventEmitter { const reply = RichReply.createFor(roomId, event, text, html); if (thread) { + const threadStartEventId = event['content']['m.relates_to'] ? event['content']['m.relates_to']['event_id'] : event['event_id']; reply['m.relates_to'] = { 'rel_type': 'm.thread', - 'event_id': event['event_id'], + 'event_id': threadStartEventId, }; } return this.sendMessage(roomId, reply); @@ -1244,9 +1245,10 @@ export class MatrixClient extends EventEmitter { const text = htmlToText(html, { wordwrap: false }); const reply = RichReply.createFor(roomId, event, text, html); if (thread) { + const threadStartEventId = event['content']['m.relates_to'] ? event['content']['m.relates_to']['event_id'] : event['event_id']; reply['m.relates_to'] = { 'rel_type': 'm.thread', - 'event_id': event['event_id'], + 'event_id': threadStartEventId, }; } return this.sendMessage(roomId, reply); @@ -1269,9 +1271,10 @@ export class MatrixClient extends EventEmitter { const reply = RichReply.createFor(roomId, event, text, html); reply['msgtype'] = 'm.notice'; if (thread) { + const threadStartEventId = event['content']['m.relates_to'] ? event['content']['m.relates_to']['event_id'] : event['event_id']; reply['m.relates_to'] = { 'rel_type': 'm.thread', - 'event_id': event['event_id'], + 'event_id': threadStartEventId, }; } return this.sendMessage(roomId, reply); @@ -1292,9 +1295,10 @@ export class MatrixClient extends EventEmitter { const reply = RichReply.createFor(roomId, event, text, html); reply['msgtype'] = 'm.notice'; if (thread) { + const threadStartEventId = event['content']['m.relates_to'] ? event['content']['m.relates_to']['event_id'] : event['event_id']; reply['m.relates_to'] = { 'rel_type': 'm.thread', - 'event_id': event['event_id'], + 'event_id': threadStartEventId, }; } return this.sendMessage(roomId, reply);