add reply support

This commit is contained in:
Evert Prants 2024-04-07 20:50:19 +03:00
parent e900c0e65a
commit a517031290
Signed by: evert
GPG Key ID: 1688DA83D222D0B5
5 changed files with 72 additions and 23 deletions

View File

@ -3,7 +3,7 @@
"name": "matrix", "name": "matrix",
"description": "Matrix.org Service for Squeebot 3", "description": "Matrix.org Service for Squeebot 3",
"tags": ["service", "matrix"], "tags": ["service", "matrix"],
"version": "1.3.1", "version": "1.3.3",
"dependencies": ["control?"], "dependencies": ["control?"],
"npmDependencies": ["matrix-bot-sdk@0.6.6"] "npmDependencies": ["matrix-bot-sdk@0.6.6"]
} }

View File

@ -15,10 +15,11 @@ import {
LogService, LogService,
LogLevel, LogLevel,
RustSdkCryptoStorageProvider, RustSdkCryptoStorageProvider,
RichReply,
} from 'matrix-bot-sdk'; } from 'matrix-bot-sdk';
import { StoreType } from '@matrix-org/matrix-sdk-crypto-nodejs'; 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 { import {
EMessageType, EMessageType,
Formatter, Formatter,
@ -37,6 +38,7 @@ import { resolve, join } from 'path';
class MatrixMessageAdapter implements IMessage { class MatrixMessageAdapter implements IMessage {
public time: Date = new Date(); public time: Date = new Date();
public resolved = false; public resolved = false;
public isReply = false;
constructor( constructor(
public type: EMessageType, public type: EMessageType,
@ -73,6 +75,11 @@ class MatrixMessageAdapter implements IMessage {
this.source.resolve(this, error.message); this.source.resolve(this, error.message);
} }
public reply(...args: any[]): void {
this.isReply = true;
this.resolve(...args);
}
public mention(user: IMessageTarget): string { public mention(user: IMessageTarget): string {
return `<a href="https://matrix.to/#/${user.id}">${user.id}</a>`; return `<a href="https://matrix.to/#/${user.id}">${user.id}</a>`;
} }
@ -135,6 +142,7 @@ class MatrixProtocol extends Protocol {
ProtocolFeatureFlag.THREADS, ProtocolFeatureFlag.THREADS,
ProtocolFeatureFlag.REACTIONS, ProtocolFeatureFlag.REACTIONS,
ProtocolFeatureFlag.MENTION, ProtocolFeatureFlag.MENTION,
ProtocolFeatureFlag.REPLY,
ProtocolFeatureFlag.OPTIONAL_ENCRYPTION, ProtocolFeatureFlag.OPTIONAL_ENCRYPTION,
ProtocolFeatureFlag.EVENT_MESSAGE, ProtocolFeatureFlag.EVENT_MESSAGE,
ProtocolFeatureFlag.EVENT_ROOM_JOIN, ProtocolFeatureFlag.EVENT_ROOM_JOIN,
@ -249,7 +257,7 @@ class MatrixProtocol extends Protocol {
type: 'm.id.user', type: 'm.id.user',
user: username, user: username,
}, },
password: password, password,
initial_device_display_name: deviceName, initial_device_display_name: deviceName,
}; };
@ -354,19 +362,61 @@ class MatrixProtocol extends Protocol {
this.emit('stopped'); this.emit('stopped');
} }
public resolve(msg: MatrixMessageAdapter, ...data: any[]): void { public async resolve(
msg: MatrixMessageAdapter,
...data: any[]
): Promise<void> {
if (!msg.target) { if (!msg.target) {
return; 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<boolean> { public async sendTo(target: string, ...data: any[]): Promise<boolean> {
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)); let response = util.format(data[0], ...data.slice(1));
const rxSplit = target.split('/'); const rxSplit = target.split('/');
if (!response || !target || rxSplit.length !== 3) { if (!response || !target || rxSplit.length !== 3) {
return false; return undefined;
} }
if (Array.isArray(data[0])) { if (Array.isArray(data[0])) {
@ -378,7 +428,7 @@ class MatrixProtocol extends Protocol {
this.fullName, this.fullName,
e.message e.message
); );
return false; return undefined;
} }
} }
@ -392,14 +442,13 @@ class MatrixProtocol extends Protocol {
response = response.substring(9, response.length - 10); response = response.substring(9, response.length - 10);
} }
await this.client?.sendMessage(rxSplit[2], { return {
roomId: rxSplit[2],
msgtype, msgtype,
body: this.format.strip(response), response,
format: 'org.matrix.custom.html', text: this.format.strip(response),
formatted_body: response.replace(/\n/g, '<br />'), html: response.replace(/\n/g, '<br />'),
}); };
return true;
} }
} }

14
package-lock.json generated
View File

@ -9,7 +9,7 @@
"version": "1.0.0", "version": "1.0.0",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"@squeebot/core": "^3.6.0-1", "@squeebot/core": "^3.6.3",
"matrix-bot-sdk": "^0.6.6", "matrix-bot-sdk": "^0.6.6",
"typescript": "^5.1.6" "typescript": "^5.1.6"
} }
@ -40,9 +40,9 @@
} }
}, },
"node_modules/@squeebot/core": { "node_modules/@squeebot/core": {
"version": "3.6.0-1", "version": "3.6.3",
"resolved": "https://registry.npmjs.org/@squeebot/core/-/core-3.6.0-1.tgz", "resolved": "https://registry.npmjs.org/@squeebot/core/-/core-3.6.3.tgz",
"integrity": "sha512-Q9My45N9Z4irJc5/SvZJoSogLHKkqmqQkP6XdOEKerFAsPW/+x0gUNb45R0P7YpDjYe84nedtSuqlHxYzVnlhA==", "integrity": "sha512-lZArEZUiY2iN9poSxrXsowdIKGf9PzBjqNMgtQxf63UJ9es1A52DDlhpv7qzCVsSJ4wV0aZrY+crk4yTsqISXA==",
"dependencies": { "dependencies": {
"fs-extra": "^11.1.1", "fs-extra": "^11.1.1",
"reflect-metadata": "^0.1.13", "reflect-metadata": "^0.1.13",
@ -2062,9 +2062,9 @@
} }
}, },
"@squeebot/core": { "@squeebot/core": {
"version": "3.6.0-1", "version": "3.6.3",
"resolved": "https://registry.npmjs.org/@squeebot/core/-/core-3.6.0-1.tgz", "resolved": "https://registry.npmjs.org/@squeebot/core/-/core-3.6.3.tgz",
"integrity": "sha512-Q9My45N9Z4irJc5/SvZJoSogLHKkqmqQkP6XdOEKerFAsPW/+x0gUNb45R0P7YpDjYe84nedtSuqlHxYzVnlhA==", "integrity": "sha512-lZArEZUiY2iN9poSxrXsowdIKGf9PzBjqNMgtQxf63UJ9es1A52DDlhpv7qzCVsSJ4wV0aZrY+crk4yTsqISXA==",
"requires": { "requires": {
"fs-extra": "^11.1.1", "fs-extra": "^11.1.1",
"reflect-metadata": "^0.1.13", "reflect-metadata": "^0.1.13",

View File

@ -11,7 +11,7 @@
"author": "", "author": "",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"@squeebot/core": "^3.6.0-1", "@squeebot/core": "^3.6.3",
"matrix-bot-sdk": "^0.6.6", "matrix-bot-sdk": "^0.6.6",
"typescript": "^5.1.6" "typescript": "^5.1.6"
} }

View File

@ -3,7 +3,7 @@
"plugins": [ "plugins": [
{ {
"name": "matrix", "name": "matrix",
"version": "1.3.1" "version": "1.3.3"
} }
], ],
"typescript": true "typescript": true