DM check, m.emote

This commit is contained in:
Evert Prants 2023-08-07 16:55:11 +03:00
parent 11958b8db0
commit ffb7a91a84
Signed by: evert
GPG Key ID: 1688DA83D222D0B5
4 changed files with 55 additions and 22 deletions

View File

@ -21,11 +21,16 @@ export class MatrixFormatter extends HTMLFormatter {
white: '#ffffff' white: '#ffffff'
}; };
public color(color: string, msg: string): string { public override color(color: string, msg: string): string {
return `<span data-mx-color="${this.colors[color] || '#000000'}">${msg}</span>`; return `<span data-mx-color="${this.colors[color] || '#000000'}">${msg}</span>`;
} }
compose(objs: any): any { public override format(method: string, msg: string): string {
if (method === 'action') return `<m.emote>${msg}</m.emote>`;
return super.format(method, msg);
}
override compose(objs: any): any {
const str = []; const str = [];
for (const i in objs) { for (const i in objs) {

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.2.0", "version": "1.3.0",
"dependencies": ["control?"], "dependencies": ["control?"],
"npmDependencies": ["matrix-bot-sdk@0.6.6"] "npmDependencies": ["matrix-bot-sdk@0.6.6"]
} }

View File

@ -37,14 +37,14 @@ 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 direct = false;
constructor( constructor(
public type: EMessageType, public type: EMessageType,
public data: any, public data: any,
public source: Protocol, public source: Protocol,
public sender: IMessageTarget, public sender: IMessageTarget,
public target?: IMessageTarget public target?: IMessageTarget,
public direct = false
) {} ) {}
public get fullSenderID(): string { public get fullSenderID(): string {
@ -158,24 +158,28 @@ class MatrixProtocol extends Protocol {
} }
// Don't handle non-text events // Don't handle non-text events
if (event.content.msgtype !== 'm.text') { if (!['m.text', 'm.emote'].includes(event.content.msgtype)) {
return; return;
} }
const msg = event.content.body; const msg = event.content.body;
const direct = this.client.dms.isDm(roomId);
// filter out events sent by the bot itself // filter out events sent by the bot itself
if (event.sender === this.me.id || !msg) { if (event.sender === this.me.id || !msg) {
return; return;
} }
const senderName = await this.getUserDisplayName(event.sender); 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( const newMessage = new MatrixMessageAdapter(
EMessageType.message, EMessageType.message,
event, event,
this, this,
{ id: event.sender, name: senderName }, { id: event.sender, name: senderName },
{ id: roomId, name: roomName } { id: roomId, name: roomName },
direct
); );
this.plugin.stream.emitTo('channel', 'message', newMessage); this.plugin.stream.emitTo('channel', 'message', newMessage);
} }
@ -184,28 +188,42 @@ class MatrixProtocol extends Protocol {
if (this.nameCache.has(id)) { if (this.nameCache.has(id)) {
return this.nameCache.get(id) as string; return this.nameCache.get(id) as string;
} }
try {
const profile = await this.client?.getUserProfile(id); const profile = await this.client?.getUserProfile(id);
if (!profile || !profile.displayname) { if (!profile || !profile.displayname) {
return id; return id;
} }
this.nameCache.set(id, profile.displayname); this.nameCache.set(id, profile.displayname);
return profile.displayname; return profile.displayname;
} catch {
return id;
}
} }
public async getRoomDisplayName(id: string): Promise<string> { public async getRoomDisplayName(id: string): Promise<string> {
if (this.nameCache.has(id)) { if (this.nameCache.has(id)) {
return this.nameCache.get(id) as string; return this.nameCache.get(id) as string;
} }
try {
const roomState = await this.client?.getRoomStateEvent( const roomState = await this.client?.getRoomStateEvent(
id, id,
'm.room.name', 'm.room.name',
'' ''
); );
if (!roomState || !roomState.name) { if (!roomState || !roomState.name) {
return id; return id;
} }
this.nameCache.set(id, roomState.name); this.nameCache.set(id, roomState.name);
return roomState.name; return roomState.name;
} catch {
return id;
}
} }
private async getSelf(): Promise<void> { private async getSelf(): Promise<void> {
@ -364,8 +382,18 @@ class MatrixProtocol extends Protocol {
} }
} }
const msgtype =
response.startsWith('<m.emote>') && response.endsWith('</m.emote>')
? '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], { await this.client?.sendMessage(rxSplit[2], {
msgtype: 'm.text', msgtype,
body: this.format.strip(response), body: this.format.strip(response),
format: 'org.matrix.custom.html', format: 'org.matrix.custom.html',
formatted_body: response.replace('\n', '<br />'), formatted_body: response.replace('\n', '<br />'),

View File

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