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",
"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"]
}

View File

@ -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 `<a href="https://matrix.to/#/${user.id}">${user.id}</a>`;
}
@ -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<void> {
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<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));
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, '<br />'),
});
return true;
response,
text: this.format.strip(response),
html: response.replace(/\n/g, '<br />'),
};
}
}

14
package-lock.json generated
View File

@ -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",

View File

@ -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"
}

View File

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