Skip to content
Extraits de code Groupes Projets
Valider 7b02c4f1 rédigé par Tadeusz Sośnierz's avatar Tadeusz Sośnierz
Parcourir les fichiers

Lookup DM rooms without using account data

parent 09a36313
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
......@@ -30,8 +30,6 @@ import { Response } from "express";
const log = new Log("DiscordAS");
const ADMIN_ROOMS_KEY = 'uk.half-shot.discord.admin-rooms';
const commandOptions = [
{ name: "config", alias: "c", type: String },
{ name: "url", alias: "u", type: String },
......@@ -230,20 +228,44 @@ async function run(): Promise<void> {
}
interface AdminRooms {
[mxid: string]: string;
async function findDMRoom(client: MatrixClient, targetMxid: string): Promise<string|undefined> {
const rooms = await client.getJoinedRooms();
const roomsWithMembers = await Promise.all(rooms.map(async (id) => {
return {
id,
memberships: await client.getRoomMembers(id, undefined, ['join', 'invite']),
}
}));
return roomsWithMembers.find(
room => room.memberships.length == 2
&& !!room.memberships.find(member => member.stateKey === targetMxid)
)?.id;
}
async function notifyBridgeAdmin(client: MatrixClient, adminMxid: string, message: string) {
log.verbose(`Looking up admin room for ${adminMxid}`);
let adminRooms = await client.getAccountData<AdminRooms>(ADMIN_ROOMS_KEY).catch(() => null) || {};
if (!adminRooms[adminMxid]) {
log.verbose(`Creating an admin room for ${adminMxid}`);
adminRooms[adminMxid] = await client.createRoom();
await client.inviteUser(adminMxid, adminRooms[adminMxid]);
await client.setAccountData(ADMIN_ROOMS_KEY, adminRooms);
async function ensureDMRoom(client: MatrixClient, mxid: string): Promise<string> {
const existing = await findDMRoom(client, mxid);
if (existing) {
log.verbose(`Found existing DM room with ${mxid}: ${existing}`);
return existing;
}
const roomId = await client.createRoom();
try {
await client.inviteUser(mxid, roomId);
} catch (err) {
log.verbose(`Failed to invite ${mxid} to ${roomId}, cleaning up`);
client.leaveRoom(roomId); // no point awaiting it, nothing we can do if we fail
throw err;
}
await client.sendText(adminRooms[adminMxid], message)
log.verbose(`Created ${roomId} to DM with ${mxid}`);
return roomId;
}
async function notifyBridgeAdmin(client: MatrixClient, adminMxid: string, message: string) {
const roomId = await ensureDMRoom(client, adminMxid);
await client.sendText(roomId, message)
}
let adminNotified = false;
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter