From 804dfa0e336f488f7c67089abf11d674cecdc8a5 Mon Sep 17 00:00:00 2001 From: gnuxie Date: Tue, 27 May 2025 14:39:42 +0100 Subject: [PATCH] Return the new protection instead of playing about. --- .../NotificationRoom/NotificationRoom.ts | 95 ++++++++++--------- .../RoomTakedown/RoomTakedownProtection.ts | 11 ++- 2 files changed, 59 insertions(+), 47 deletions(-) diff --git a/src/protections/NotificationRoom/NotificationRoom.ts b/src/protections/NotificationRoom/NotificationRoom.ts index 677030e..1e189a6 100644 --- a/src/protections/NotificationRoom/NotificationRoom.ts +++ b/src/protections/NotificationRoom/NotificationRoom.ts @@ -2,7 +2,7 @@ // // SPDX-License-Identifier: AFL-3.0 -import { Ok, Result, ResultError } from "@gnuxie/typescript-result"; +import { Ok, Result } from "@gnuxie/typescript-result"; import { MatrixRoomID, StringRoomID, @@ -12,6 +12,7 @@ import { isError, Logger, ProtectedRoomsManager, + Protection, ProtectionDescription, RoomCreator, RoomMembershipManager, @@ -19,14 +20,18 @@ import { } from "matrix-protection-suite"; import { Draupnir } from "../../Draupnir"; -export type SettingChangeAndProtectionEnableCB = ( +export type SettingChangeAndProtectionEnableCB< + TProtectionDescription extends ProtectionDescription = ProtectionDescription, +> = ( roomID: StringRoomID -) => Promise>; +) => Promise>>; -export class NotificationRoomCreator { +export class NotificationRoomCreator< + TProtectionDescription extends ProtectionDescription = ProtectionDescription, +> { public constructor( private readonly protectedRoomsManager: ProtectedRoomsManager, - private readonly settingChangeCB: SettingChangeAndProtectionEnableCB, + private readonly settingChangeCB: SettingChangeAndProtectionEnableCB, private readonly roomCreateCapability: RoomCreator, private readonly roomStateEventCapability: RoomStateEventSender, private readonly roomName: string, @@ -56,14 +61,17 @@ export class NotificationRoomCreator { ); } - public static async createNotificationRoomFromDraupnir( + public static async createNotificationRoomFromDraupnir< + TProtectionDescription extends + ProtectionDescription = ProtectionDescription, + >( draupnir: Draupnir, - description: ProtectionDescription, + description: TProtectionDescription, settings: Record, notificationRoomSettingName: string, notificationRoomName: string, log: Logger - ): Promise> { + ): Promise>> { const moderators = await NotificationRoomCreator.extractMembersFromManagementRoom( draupnir.managementRoom, @@ -73,41 +81,44 @@ export class NotificationRoomCreator { if (isError(moderators)) { return moderators.elaborate("Unable to find the draupnir moderators"); } - const notificationRoomCreator = new NotificationRoomCreator( - draupnir.protectedRoomsSet.protectedRoomsManager, - async function (roomID: StringRoomID) { - const newSettings = description.protectionSettings - .toMirror() - .setValue(settings, notificationRoomSettingName, roomID); - if (isError(newSettings)) { - return newSettings; - } - const result = - await draupnir.protectedRoomsSet.protections.changeProtectionSettings( - description as unknown as ProtectionDescription, - draupnir.protectedRoomsSet, - draupnir, - newSettings.ok - ); - if (isError(result)) { - return result.elaborate( - "Unable to add the notification room to the protection settings" - ); - } - return Ok(undefined); - }, - draupnir.clientPlatform.toRoomCreator(), - draupnir.clientPlatform.toRoomStateEventSender(), - notificationRoomName, - draupnir.clientUserID, - draupnir.managementRoomID, - moderators.ok, - log - ); + const notificationRoomCreator = + new NotificationRoomCreator( + draupnir.protectedRoomsSet.protectedRoomsManager, + async function (roomID: StringRoomID) { + const newSettings = description.protectionSettings + .toMirror() + .setValue(settings, notificationRoomSettingName, roomID); + if (isError(newSettings)) { + return newSettings; + } + const result = + await draupnir.protectedRoomsSet.protections.changeProtectionSettings( + description, + draupnir.protectedRoomsSet, + draupnir, + newSettings.ok + ); + if (isError(result)) { + return result.elaborate( + "Unable to add the notification room to the protection settings" + ); + } + return result; + }, + draupnir.clientPlatform.toRoomCreator(), + draupnir.clientPlatform.toRoomStateEventSender(), + notificationRoomName, + draupnir.clientUserID, + draupnir.managementRoomID, + moderators.ok, + log + ); return await notificationRoomCreator.createMissingNotificationRoom(); } - public async createMissingNotificationRoom(): Promise> { + public async createMissingNotificationRoom(): Promise< + Result> + > { const roomTitle = `${this.draupnirUserID}'s ${this.roomName}`; const newRoom = await this.roomCreateCapability.createRoom({ preset: "private_chat", @@ -163,8 +174,6 @@ export class NotificationRoomCreator { ); return protectionEnableResult; } - return ResultError.Result( - `A notification room titled "${roomTitle}" has been created for this protection's messages, and the protection has been restarted separately` - ); + return protectionEnableResult; } } diff --git a/src/protections/RoomTakedown/RoomTakedownProtection.ts b/src/protections/RoomTakedown/RoomTakedownProtection.ts index f69ff02..847e5cb 100644 --- a/src/protections/RoomTakedown/RoomTakedownProtection.ts +++ b/src/protections/RoomTakedown/RoomTakedownProtection.ts @@ -26,7 +26,7 @@ import { SynapseAdminRoomDetailsProvider, SynapseAdminRoomTakedownCapability, } from "../../capabilities/SynapseAdminRoomTakedown/SynapseAdminRoomTakedown"; -import { isError, ResultError } from "@gnuxie/typescript-result"; +import { isError, Result, ResultError } from "@gnuxie/typescript-result"; import { RoomDiscovery, RoomDiscoveryListener, @@ -172,19 +172,22 @@ describeProtection< draupnir, capabilitySet, settings - ) { + ): Promise> { if ( settings.discoveryNotificationEnabled && settings.discoveryNotificationRoom === undefined ) { - return await NotificationRoomCreator.createNotificationRoomFromDraupnir( + // FIXME: The type parameters are really fucked for the protection system + // and that needs fixing. The problem is that the protection system was written + // before we knew how to do this properly. + return (await NotificationRoomCreator.createNotificationRoomFromDraupnir( draupnir, description as unknown as ProtectionDescription, settings, "discoveryNotificationRoom", "Room Discovery Notification", log - ); + )) as Result; } if ( draupnir.stores.hashStore === undefined ||