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'
};
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>`;
}
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 = [];
for (const i in objs) {

View File

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

View File

@ -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;
}
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;
}
}
public async getRoomDisplayName(id: string): Promise<string> {
if (this.nameCache.has(id)) {
return this.nameCache.get(id) as string;
}
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;
}
}
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], {
msgtype: 'm.text',
msgtype,
body: this.format.strip(response),
format: 'org.matrix.custom.html',
formatted_body: response.replace('\n', '<br />'),

View File

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