Update for ClientsInRoomsMap rework (MPS).

This commit is contained in:
gnuxie
2024-02-22 12:43:47 +00:00
parent c1215ab045
commit 380f4b1a77
6 changed files with 21 additions and 62 deletions
+11 -2
View File
@@ -102,7 +102,6 @@ export class Draupnir implements Client {
if (config.pollReports) {
this.reportPoller = new ReportPoller(this, this.reportManager);
}
this.clientRooms.on('timeline', this.timelineEventListener);
this.commandContext = {
draupnir: this, roomID: this.managementRoomID, client: this.client, reactionHandler: this.reactionHandler, clientPlatform: this.clientPlatform
@@ -283,8 +282,13 @@ export class Draupnir implements Client {
}
}
/**
* Start responding to events.
* This will not start the appservice from listening and responding
* to events. Nor will it start any syncing client.
*/
public async start(): Promise<void> {
// FIXME: This method needs to be removed it probably won't be called at all.
this.clientRooms.on('timeline', this.timelineEventListener);
if (this.reportPoller) {
const reportPollSetting = await ReportPoller.getReportPollSetting(
this.client,
@@ -294,6 +298,11 @@ export class Draupnir implements Client {
}
}
public stop(): void {
this.clientRooms.off('timeline', this.timelineEventListener);
this.reportPoller?.stop()
}
public createRoomReference(roomID: StringRoomID): MatrixRoomID {
return new MatrixRoomID(
roomID,
@@ -1,52 +0,0 @@
/**
* Copyright (C) 2023-2024 Gnuxie <Gnuxie@protonmail.com>
* All rights reserved.
*/
import { ActionException, ActionExceptionKind, ActionResult, ClientRooms, JoinedRoomsRevision, JoinedRoomsSafe, Ok, RoomStateManager, StandardClientRooms, StandardJoinedRoomsRevision, StringUserID, isError } from "matrix-protection-suite";
export class DraupnirClientRooms extends StandardClientRooms implements ClientRooms {
private constructor(
roomStateManager: RoomStateManager,
joinedRoomsThunk: JoinedRoomsSafe,
clientUserID: StringUserID,
joinedRoomsRevision: JoinedRoomsRevision
) {
super(
roomStateManager,
joinedRoomsThunk,
clientUserID,
joinedRoomsRevision
);
}
public static async makeClientRooms(
roomStateManager: RoomStateManager,
joinedRoomsThunk: JoinedRoomsSafe,
clientUserID: StringUserID,
): Promise<ActionResult<DraupnirClientRooms>> {
try {
const joinedRooms = await joinedRoomsThunk();
if (isError(joinedRooms)) {
return joinedRooms;
}
const revision = StandardJoinedRoomsRevision.blankRevision(
clientUserID
).reviseFromJoinedRooms(joinedRooms.ok);
return Ok(new DraupnirClientRooms(
roomStateManager,
joinedRoomsThunk,
clientUserID,
revision
))
} catch (exception) {
return ActionException.Result(
`Couldn't create client rooms`,
{
exception,
exceptionKind: ActionExceptionKind.Unknown
}
)
}
}
}
+2 -5
View File
@@ -6,7 +6,6 @@
import { ActionResult, ClientsInRoomMap, MatrixRoomID, Ok, StringUserID, isError } from "matrix-protection-suite";
import { Draupnir } from "../Draupnir";
import { ClientCapabilityFactory, ClientForUserID, RoomStateManagerFactory, joinedRoomsSafe } from "matrix-protection-suite-for-matrix-bot-sdk";
import { DraupnirClientRooms } from "./DraupnirClientRooms";
import { IConfig } from "../config";
import { makeProtectedRoomsSet } from "./DraupnirProtectedRoomsSet";
@@ -25,15 +24,13 @@ export class DraupnirFactory {
const policyRoomManager = await this.roomStateManagerFactory.getPolicyRoomManager(clientUserID);
const roomMembershipManager = await this.roomStateManagerFactory.getRoomMembershipManager(clientUserID);
const client = await this.clientProvider(clientUserID);
const clientRooms = await DraupnirClientRooms.makeClientRooms(
roomStateManager,
const clientRooms = await this.clientsInRoomMap.makeClientRooms(
clientUserID,
async () => joinedRoomsSafe(client),
clientUserID
);
if (isError(clientRooms)) {
return clientRooms;
}
this.clientsInRoomMap.addClientRooms(clientRooms.ok);
const clientPlatform = this.clientCapabilityFactory.makeClientPlatform(clientUserID, client);
const protectedRoomsSet = await makeProtectedRoomsSet(
managementRoom,
@@ -105,7 +105,7 @@ export class StandardDraupnirManager {
if (draupnir === undefined) {
throw new TypeError(`Trying to start a draupnir that hasn't been created ${clientUserID}`);
}
this.clientsInRooms.addClientRooms(draupnir.clientRooms);
draupnir.start();
this.listeningDraupnirs.set(clientUserID, draupnir);
this.readyDraupnirs.delete(clientUserID);
}
@@ -117,7 +117,7 @@ export class StandardDraupnirManager {
if (draupnir === undefined) {
return;
} else {
this.clientsInRooms.removeClientRooms(draupnir.clientRooms);
draupnir.stop();
this.listeningDraupnirs.delete(clientUserID);
this.readyDraupnirs.set(clientUserID, draupnir);
}
+5 -1
View File
@@ -45,6 +45,7 @@ import { constructWebAPIs, makeDraupnirBotModeFromConfig } from "./DraupnirBotMo
import { Draupnir } from "./Draupnir";
import { SafeMatrixEmitterWrapper } from "matrix-protection-suite-for-matrix-bot-sdk";
import { DefaultEventDecoder } from "matrix-protection-suite";
import { WebAPIs } from "./webapis/WebAPIs";
(async function () {
@@ -68,6 +69,7 @@ import { DefaultEventDecoder } from "matrix-protection-suite";
}
let bot: Draupnir | null = null;
let apis: WebAPIs | null = null;
try {
const storagePath = path.isAbsolute(config.dataPath) ? config.dataPath : path.join(__dirname, '../', config.dataPath);
const storage = new SimpleFsStorageProvider(path.join(storagePath, "bot.json"));
@@ -90,6 +92,7 @@ import { DefaultEventDecoder } from "matrix-protection-suite";
config.RUNTIME.client = client;
bot = await makeDraupnirBotModeFromConfig(client, new SafeMatrixEmitterWrapper(client, DefaultEventDecoder), config);
apis = constructWebAPIs(bot);
} catch (err) {
console.error(`Failed to setup mjolnir from the config ${config.dataPath}: ${err}`);
throw err;
@@ -97,11 +100,12 @@ import { DefaultEventDecoder } from "matrix-protection-suite";
try {
await bot.start();
await config.RUNTIME.client.start();
const apis = constructWebAPIs(bot);
await apis.start();
healthz.isHealthy = true;
} catch (err) {
console.error(`Mjolnir failed to start: ${err}`);
bot.stop();
apis.stop();
throw err;
}
})();
+1
View File
@@ -38,6 +38,7 @@ export const mochaHooks = {
this.timeout(10000)
this.apis?.stop();
draupnirClient()?.stop();
this.draupnir?.stop();
// remove alias from management room and leave it.
if (this.draupnir !== undefined) {