From ffb7a91a8448c58d62017b946338d4b7c4bc49d0 Mon Sep 17 00:00:00 2001 From: Evert Prants Date: Mon, 7 Aug 2023 16:55:11 +0300 Subject: [PATCH] DM check, m.emote --- matrix/format.ts | 9 +++++-- matrix/plugin.json | 2 +- matrix/plugin.ts | 64 +++++++++++++++++++++++++++++++++------------- squeebot.repo.json | 2 +- 4 files changed, 55 insertions(+), 22 deletions(-) diff --git a/matrix/format.ts b/matrix/format.ts index 746d4d0..5179b21 100644 --- a/matrix/format.ts +++ b/matrix/format.ts @@ -21,11 +21,16 @@ export class MatrixFormatter extends HTMLFormatter { white: '#ffffff' }; - public color(color: string, msg: string): string { + public override color(color: string, msg: string): string { return `${msg}`; } - compose(objs: any): any { + public override format(method: string, msg: string): string { + if (method === 'action') return `${msg}`; + return super.format(method, msg); + } + + override compose(objs: any): any { const str = []; for (const i in objs) { diff --git a/matrix/plugin.json b/matrix/plugin.json index 555bc2e..334aca9 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.2.0", + "version": "1.3.0", "dependencies": ["control?"], "npmDependencies": ["matrix-bot-sdk@0.6.6"] } diff --git a/matrix/plugin.ts b/matrix/plugin.ts index a71e806..d438171 100644 --- a/matrix/plugin.ts +++ b/matrix/plugin.ts @@ -37,14 +37,14 @@ import { resolve, join } from 'path'; class MatrixMessageAdapter implements IMessage { public time: Date = new Date(); public resolved = false; - public direct = false; constructor( public type: EMessageType, public data: any, public source: Protocol, public sender: IMessageTarget, - public target?: IMessageTarget + public target?: IMessageTarget, + public direct = false ) {} public get fullSenderID(): string { @@ -158,24 +158,28 @@ class MatrixProtocol extends Protocol { } // Don't handle non-text events - if (event.content.msgtype !== 'm.text') { + if (!['m.text', 'm.emote'].includes(event.content.msgtype)) { return; } const msg = event.content.body; + const direct = this.client.dms.isDm(roomId); // filter out events sent by the bot itself if (event.sender === this.me.id || !msg) { return; } const senderName = await this.getUserDisplayName(event.sender); - const roomName = await this.getRoomDisplayName(roomId); + const roomName = direct + ? senderName + : await this.getRoomDisplayName(roomId); const newMessage = new MatrixMessageAdapter( EMessageType.message, event, this, { id: event.sender, name: senderName }, - { id: roomId, name: roomName } + { id: roomId, name: roomName }, + direct ); this.plugin.stream.emitTo('channel', 'message', newMessage); } @@ -184,28 +188,42 @@ class MatrixProtocol extends Protocol { if (this.nameCache.has(id)) { return this.nameCache.get(id) as string; } - const profile = await this.client?.getUserProfile(id); - if (!profile || !profile.displayname) { + + try { + const profile = await this.client?.getUserProfile(id); + if (!profile || !profile.displayname) { + return id; + } + + this.nameCache.set(id, profile.displayname); + return profile.displayname; + } catch { return id; } - this.nameCache.set(id, profile.displayname); - return profile.displayname; } public async getRoomDisplayName(id: string): Promise { if (this.nameCache.has(id)) { return this.nameCache.get(id) as string; } - const roomState = await this.client?.getRoomStateEvent( - id, - 'm.room.name', - '' - ); - if (!roomState || !roomState.name) { + + try { + const roomState = await this.client?.getRoomStateEvent( + id, + 'm.room.name', + '' + ); + + if (!roomState || !roomState.name) { + return id; + } + + this.nameCache.set(id, roomState.name); + + return roomState.name; + } catch { return id; } - this.nameCache.set(id, roomState.name); - return roomState.name; } private async getSelf(): Promise { @@ -364,8 +382,18 @@ class MatrixProtocol extends Protocol { } } + const msgtype = + response.startsWith('') && response.endsWith('') + ? 'm.emote' + : 'm.text'; + + // TODO: make more generic + if (msgtype === 'm.emote') { + response = response.substring(9, response.length - 10); + } + await this.client?.sendMessage(rxSplit[2], { - msgtype: 'm.text', + msgtype, body: this.format.strip(response), format: 'org.matrix.custom.html', formatted_body: response.replace('\n', '
'), diff --git a/squeebot.repo.json b/squeebot.repo.json index 24e312c..5fe8c38 100644 --- a/squeebot.repo.json +++ b/squeebot.repo.json @@ -3,7 +3,7 @@ "plugins": [ { "name": "matrix", - "version": "1.2.0" + "version": "1.3.0" } ], "typescript": true