From a5170312903ab915e417361902699048db51ee62 Mon Sep 17 00:00:00 2001 From: Evert Prants Date: Sun, 7 Apr 2024 20:50:19 +0300 Subject: [PATCH] add reply support --- matrix/plugin.json | 2 +- matrix/plugin.ts | 75 ++++++++++++++++++++++++++++++++++++++-------- package-lock.json | 14 ++++----- package.json | 2 +- squeebot.repo.json | 2 +- 5 files changed, 72 insertions(+), 23 deletions(-) diff --git a/matrix/plugin.json b/matrix/plugin.json index 03f374e..6d7de9f 100644 --- a/matrix/plugin.json +++ b/matrix/plugin.json @@ -3,7 +3,7 @@ "name": "matrix", "description": "Matrix.org Service for Squeebot 3", "tags": ["service", "matrix"], - "version": "1.3.1", + "version": "1.3.3", "dependencies": ["control?"], "npmDependencies": ["matrix-bot-sdk@0.6.6"] } diff --git a/matrix/plugin.ts b/matrix/plugin.ts index 514da70..930ced0 100644 --- a/matrix/plugin.ts +++ b/matrix/plugin.ts @@ -15,10 +15,11 @@ import { LogService, LogLevel, RustSdkCryptoStorageProvider, + RichReply, } from 'matrix-bot-sdk'; import { StoreType } from '@matrix-org/matrix-sdk-crypto-nodejs'; -import { Logger, logger } from '@squeebot/core/lib/core'; +import { logger } from '@squeebot/core/lib/core'; import { EMessageType, Formatter, @@ -37,6 +38,7 @@ import { resolve, join } from 'path'; class MatrixMessageAdapter implements IMessage { public time: Date = new Date(); public resolved = false; + public isReply = false; constructor( public type: EMessageType, @@ -73,6 +75,11 @@ class MatrixMessageAdapter implements IMessage { this.source.resolve(this, error.message); } + public reply(...args: any[]): void { + this.isReply = true; + this.resolve(...args); + } + public mention(user: IMessageTarget): string { return `${user.id}`; } @@ -135,6 +142,7 @@ class MatrixProtocol extends Protocol { ProtocolFeatureFlag.THREADS, ProtocolFeatureFlag.REACTIONS, ProtocolFeatureFlag.MENTION, + ProtocolFeatureFlag.REPLY, ProtocolFeatureFlag.OPTIONAL_ENCRYPTION, ProtocolFeatureFlag.EVENT_MESSAGE, ProtocolFeatureFlag.EVENT_ROOM_JOIN, @@ -249,7 +257,7 @@ class MatrixProtocol extends Protocol { type: 'm.id.user', user: username, }, - password: password, + password, initial_device_display_name: deviceName, }; @@ -354,19 +362,61 @@ class MatrixProtocol extends Protocol { this.emit('stopped'); } - public resolve(msg: MatrixMessageAdapter, ...data: any[]): void { + public async resolve( + msg: MatrixMessageAdapter, + ...data: any[] + ): Promise { if (!msg.target) { return; } - this.sendTo(`${this.fullName}/${msg.target.id}`, ...data); + const parts = this.getMessageParts( + `${this.fullName}/${msg.target.id}`, + ...data + ); + if (!parts) { + return; + } + + const { roomId, msgtype, text, html } = parts; + const messageContents = { + // Assign properties generated by reply + ...(msg.isReply + ? RichReply.createFor(roomId, msg.data, text, html) + : { + body: text, + format: 'org.matrix.custom.html', + formatted_body: html, + }), + // Override message type to support notice + msgtype, + }; + + await this.client?.sendMessage(roomId, messageContents); } public async sendTo(target: string, ...data: any[]): Promise { + const parts = this.getMessageParts(target, ...data); + if (!parts) { + return false; + } + + const { roomId, msgtype, text, html } = parts; + await this.client?.sendMessage(roomId, { + msgtype, + body: text, + format: 'org.matrix.custom.html', + formatted_body: html, + }); + + return true; + } + + private getMessageParts(target: string, ...data: any[]) { let response = util.format(data[0], ...data.slice(1)); const rxSplit = target.split('/'); if (!response || !target || rxSplit.length !== 3) { - return false; + return undefined; } if (Array.isArray(data[0])) { @@ -378,7 +428,7 @@ class MatrixProtocol extends Protocol { this.fullName, e.message ); - return false; + return undefined; } } @@ -392,14 +442,13 @@ class MatrixProtocol extends Protocol { response = response.substring(9, response.length - 10); } - await this.client?.sendMessage(rxSplit[2], { + return { + roomId: rxSplit[2], msgtype, - body: this.format.strip(response), - format: 'org.matrix.custom.html', - formatted_body: response.replace(/\n/g, '
'), - }); - - return true; + response, + text: this.format.strip(response), + html: response.replace(/\n/g, '
'), + }; } } diff --git a/package-lock.json b/package-lock.json index ca4247e..f7e096b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,7 @@ "version": "1.0.0", "license": "ISC", "dependencies": { - "@squeebot/core": "^3.6.0-1", + "@squeebot/core": "^3.6.3", "matrix-bot-sdk": "^0.6.6", "typescript": "^5.1.6" } @@ -40,9 +40,9 @@ } }, "node_modules/@squeebot/core": { - "version": "3.6.0-1", - "resolved": "https://registry.npmjs.org/@squeebot/core/-/core-3.6.0-1.tgz", - "integrity": "sha512-Q9My45N9Z4irJc5/SvZJoSogLHKkqmqQkP6XdOEKerFAsPW/+x0gUNb45R0P7YpDjYe84nedtSuqlHxYzVnlhA==", + "version": "3.6.3", + "resolved": "https://registry.npmjs.org/@squeebot/core/-/core-3.6.3.tgz", + "integrity": "sha512-lZArEZUiY2iN9poSxrXsowdIKGf9PzBjqNMgtQxf63UJ9es1A52DDlhpv7qzCVsSJ4wV0aZrY+crk4yTsqISXA==", "dependencies": { "fs-extra": "^11.1.1", "reflect-metadata": "^0.1.13", @@ -2062,9 +2062,9 @@ } }, "@squeebot/core": { - "version": "3.6.0-1", - "resolved": "https://registry.npmjs.org/@squeebot/core/-/core-3.6.0-1.tgz", - "integrity": "sha512-Q9My45N9Z4irJc5/SvZJoSogLHKkqmqQkP6XdOEKerFAsPW/+x0gUNb45R0P7YpDjYe84nedtSuqlHxYzVnlhA==", + "version": "3.6.3", + "resolved": "https://registry.npmjs.org/@squeebot/core/-/core-3.6.3.tgz", + "integrity": "sha512-lZArEZUiY2iN9poSxrXsowdIKGf9PzBjqNMgtQxf63UJ9es1A52DDlhpv7qzCVsSJ4wV0aZrY+crk4yTsqISXA==", "requires": { "fs-extra": "^11.1.1", "reflect-metadata": "^0.1.13", diff --git a/package.json b/package.json index e8e4162..fd1e917 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ "author": "", "license": "ISC", "dependencies": { - "@squeebot/core": "^3.6.0-1", + "@squeebot/core": "^3.6.3", "matrix-bot-sdk": "^0.6.6", "typescript": "^5.1.6" } diff --git a/squeebot.repo.json b/squeebot.repo.json index 4e7b7c9..25062e9 100644 --- a/squeebot.repo.json +++ b/squeebot.repo.json @@ -3,7 +3,7 @@ "plugins": [ { "name": "matrix", - "version": "1.3.1" + "version": "1.3.3" } ], "typescript": true