diff --git a/src/Draupnir.ts b/src/Draupnir.ts index b876fe7..040ac52 100644 --- a/src/Draupnir.ts +++ b/src/Draupnir.ts @@ -173,7 +173,7 @@ export class Draupnir implements Client { } // we need to make sure that we are protecting the management room so we // have immediate access to its membership (for accepting invitations). - const managementRoomProtectResult = await draupnir.protectedRoomsSet.protectedRoomsConfig.addRoom( + const managementRoomProtectResult = await draupnir.protectedRoomsSet.protectedRoomsManager.addRoom( managementRoom ); if (isError(managementRoomProtectResult)) { diff --git a/src/appservice/Api.ts b/src/appservice/Api.ts index 3170a36..9d4e578 100644 --- a/src/appservice/Api.ts +++ b/src/appservice/Api.ts @@ -218,7 +218,7 @@ export class Api { } await mjolnir.client.joinRoom(roomId); - await mjolnir.protectedRoomsSet.protectedRoomsConfig.addRoom(roomId); + await mjolnir.protectedRoomsSet.protectedRoomsManager.addRoom(roomId); response.status(200).json({}); } diff --git a/src/appservice/AppServiceDraupnirManager.ts b/src/appservice/AppServiceDraupnirManager.ts index 83ac9c2..8ac0f21 100644 --- a/src/appservice/AppServiceDraupnirManager.ts +++ b/src/appservice/AppServiceDraupnirManager.ts @@ -307,7 +307,7 @@ async function createFirstList(draupnir: Draupnir, draupnirOwnerID: StringUserID if (isError(policyRoom)) { throw policyRoom.error; } - const addRoomResult = await draupnir.protectedRoomsSet.protectedRoomsConfig.addRoom(policyRoom.ok); + const addRoomResult = await draupnir.protectedRoomsSet.protectedRoomsManager.addRoom(policyRoom.ok); if (isError(addRoomResult)) { return addRoomResult; } diff --git a/src/commands/CreateBanListCommand.ts b/src/commands/CreateBanListCommand.ts index 0f86856..11a368a 100644 --- a/src/commands/CreateBanListCommand.ts +++ b/src/commands/CreateBanListCommand.ts @@ -54,7 +54,7 @@ export async function createList( if (isError(watchResult)) { return watchResult; } - const protectResult = await this.draupnir.protectedRoomsSet.protectedRoomsConfig.addRoom(newList.ok); + const protectResult = await this.draupnir.protectedRoomsSet.protectedRoomsManager.addRoom(newList.ok); if (isError(protectResult)) { return protectResult; } diff --git a/src/commands/RedactCommand.ts b/src/commands/RedactCommand.ts index bfb3f03..54c9147 100644 --- a/src/commands/RedactCommand.ts +++ b/src/commands/RedactCommand.ts @@ -65,7 +65,7 @@ export async function redactCommand( if (restrictToRoom !== undefined && isError(restrictToRoom)) { return restrictToRoom; } - const roomsToRedactWithin = restrictToRoom === undefined ? this.draupnir.protectedRoomsSet.protectedRoomsConfig.allRooms : [restrictToRoom.ok]; + const roomsToRedactWithin = restrictToRoom === undefined ? this.draupnir.protectedRoomsSet.allProtectedRooms : [restrictToRoom.ok]; await redactUserMessagesIn( this.client, this.draupnir.managementRoomOutput, diff --git a/src/commands/Rooms.tsx b/src/commands/Rooms.tsx index 906dd5d..ca032ab 100644 --- a/src/commands/Rooms.tsx +++ b/src/commands/Rooms.tsx @@ -42,7 +42,7 @@ defineInterfaceCommand({ summary: "List all of the protected rooms.", parameters: parameters([]), command: async function (this: DraupnirContext, _keywrods): Promise> { - return Ok(this.draupnir.protectedRoomsSet.protectedRoomsConfig.allRooms); + return Ok(this.draupnir.protectedRoomsSet.allProtectedRooms); } }) @@ -91,7 +91,7 @@ defineInterfaceCommand({ Try an alias or the "share room" button in your client to obtain a valid reference to the room.`, ); } - return await this.draupnir.protectedRoomsSet.protectedRoomsConfig.addRoom(room.ok); + return await this.draupnir.protectedRoomsSet.protectedRoomsManager.addRoom(room.ok); }, }) @@ -114,7 +114,7 @@ defineInterfaceCommand({ Try an alias or the "share room" button in your client to obtain a valid reference to the room.`, ); }; - const removeResult = await this.draupnir.protectedRoomsSet.protectedRoomsConfig.removeRoom(room.ok); + const removeResult = await this.draupnir.protectedRoomsSet.protectedRoomsManager.removeRoom(room.ok); if (isError(removeResult)) { return removeResult; } diff --git a/src/commands/SetPowerLevelCommand.ts b/src/commands/SetPowerLevelCommand.ts index 0f82583..37512bc 100644 --- a/src/commands/SetPowerLevelCommand.ts +++ b/src/commands/SetPowerLevelCommand.ts @@ -48,7 +48,7 @@ async function setPowerLevelCommand( resolvedGivenRooms.push(resolvedResult.ok); } } - const rooms = givenRooms.length === 0 ? this.draupnir.protectedRoomsSet.protectedRoomsConfig.allRooms : resolvedGivenRooms; + const rooms = givenRooms.length === 0 ? this.draupnir.protectedRoomsSet.allProtectedRooms : resolvedGivenRooms; for (const room of rooms) { await this.draupnir.client.setUserPowerLevel(user.toString(), room.toRoomIDOrAlias(), parsedLevel); } diff --git a/src/commands/StatusCommand.tsx b/src/commands/StatusCommand.tsx index 732d468..afc17ab 100644 --- a/src/commands/StatusCommand.tsx +++ b/src/commands/StatusCommand.tsx @@ -87,7 +87,7 @@ export async function draupnirStatusInfo(draupnir: Draupnir): Promise draupnir.protectedRoomsSet.isProtectedRoom(info.revision.room.toRoomIDOrAlias())); const unprotectedListProfiles = watchedListInfo.filter((info) => !draupnir.protectedRoomsSet.isProtectedRoom(info.revision.room.toRoomIDOrAlias())); return { - numberOfProtectedRooms: draupnir.protectedRoomsSet.protectedRoomsConfig.allRooms.length, + numberOfProtectedRooms: draupnir.protectedRoomsSet.allProtectedRooms.length, subscribedLists: unprotectedListProfiles, subscribedAndProtectedLists: protectedWatchedLists, version: SOFTWARE_VERSION, diff --git a/src/draupnirfactory/DraupnirProtectedRoomsSet.ts b/src/draupnirfactory/DraupnirProtectedRoomsSet.ts index 5990931..f08b3a5 100644 --- a/src/draupnirfactory/DraupnirProtectedRoomsSet.ts +++ b/src/draupnirfactory/DraupnirProtectedRoomsSet.ts @@ -25,7 +25,7 @@ limitations under the License. * are NOT distributed, contributed, committed, or licensed under the Apache License. */ -import { ActionResult, ClientPlatform, LoggableConfigTracker, Logger, MJOLNIR_PROTECTED_ROOMS_EVENT_TYPE, MJOLNIR_WATCHED_POLICY_ROOMS_EVENT_TYPE, MatrixRoomID, MissingProtectionCB, MjolnirEnabledProtectionsEvent, MjolnirEnabledProtectionsEventType, MjolnirPolicyRoomsConfig, MjolnirProtectedRoomsConfig, MjolnirProtectedRoomsEvent, MjolnirProtectionSettingsEventType, MjolnirProtectionsConfig, MjolnirWatchedPolicyRoomsEvent, Ok, PolicyListConfig, PolicyRoomManager, ProtectedRoomsConfig, ProtectedRoomsSet, ProtectionsManager, RoomJoiner, RoomMembershipManager, RoomStateManager, SetMembership, SetRoomState, StandardProtectedRoomsSet, StandardProtectionsManager, StandardSetMembership, StandardSetRoomState, StringUserID, isError } from "matrix-protection-suite"; +import { ActionResult, ClientPlatform, LoggableConfigTracker, Logger, MJOLNIR_PROTECTED_ROOMS_EVENT_TYPE, MJOLNIR_WATCHED_POLICY_ROOMS_EVENT_TYPE, MatrixRoomID, MissingProtectionCB, MjolnirEnabledProtectionsEvent, MjolnirEnabledProtectionsEventType, MjolnirPolicyRoomsConfig, MjolnirProtectedRoomsConfig, MjolnirProtectedRoomsEvent, MjolnirProtectionSettingsEventType, MjolnirProtectionsConfig, MjolnirWatchedPolicyRoomsEvent, Ok, PolicyListConfig, PolicyRoomManager, ProtectedRoomsConfig, ProtectedRoomsSet, ProtectionsManager, RoomJoiner, RoomMembershipManager, RoomResolver, RoomStateManager, StandardProtectedRoomsManager, StandardProtectedRoomsSet, StandardProtectionsManager, StandardSetMembership, StandardSetRoomState, StringUserID, isError } from "matrix-protection-suite"; import { BotSDKMatrixAccountData, BotSDKMatrixStateData, MatrixSendClient } from "matrix-protection-suite-for-matrix-bot-sdk"; import { DefaultEnabledProtectionsMigration } from "../protections/DefaultEnabledProtectionsMigration"; import '../protections/DraupnirProtectionsIndex'; @@ -53,7 +53,8 @@ async function makePolicyListConfig( async function makeProtectedRoomsConfig( client: MatrixSendClient, - roomJoiner: RoomJoiner, + roomResolver: RoomResolver, + loggableConfigTracker: LoggableConfigTracker ): Promise> { return await MjolnirProtectedRoomsConfig.createFromStore( new BotSDKMatrixAccountData( @@ -61,27 +62,8 @@ async function makeProtectedRoomsConfig( MjolnirProtectedRoomsEvent, client ), - roomJoiner - ); -} - -async function makeSetMembership( - roomMembershipManager: RoomMembershipManager, - protectedRoomsConfig: ProtectedRoomsConfig -): Promise> { - return await StandardSetMembership.create( - roomMembershipManager, - protectedRoomsConfig - ); -} - -async function makeSetRoomState( - roomStateManager: RoomStateManager, - protectedRoomsConfig: ProtectedRoomsConfig -): Promise> { - return await StandardSetRoomState.create( - roomStateManager, - protectedRoomsConfig + roomResolver, + loggableConfigTracker ); } @@ -150,23 +132,20 @@ export async function makeProtectedRoomsSet( config: IConfig, loggableConfigTracker: LoggableConfigTracker, ): Promise> { - const protectedRoomsConfig = await makeProtectedRoomsConfig(client, clientPlatform.toRoomJoiner()) + const protectedRoomsConfig = await makeProtectedRoomsConfig(client, clientPlatform.toRoomResolver(), loggableConfigTracker) if (isError(protectedRoomsConfig)) { return protectedRoomsConfig; } - const setRoomState = await makeSetRoomState( + const protectedRoomsManager = await StandardProtectedRoomsManager.create( + protectedRoomsConfig.ok, roomStateManager, - protectedRoomsConfig.ok - ); - if (isError(setRoomState)) { - return setRoomState; - } - const membershipSet = await makeSetMembership( roomMembershipManager, - protectedRoomsConfig.ok + clientPlatform.toRoomJoiner(), + StandardSetMembership.blankSet(), + StandardSetRoomState.blankSet(), ); - if (isError(membershipSet)) { - return membershipSet; + if (isError(protectedRoomsManager)) { + return protectedRoomsManager; } const policyListConfig = await makePolicyListConfig(client, policyRoomManager, clientPlatform.toRoomJoiner()); if (isError(policyListConfig)) { @@ -184,10 +163,8 @@ export async function makeProtectedRoomsSet( } const protectedRoomsSet = new StandardProtectedRoomsSet( policyListConfig.ok, - protectedRoomsConfig.ok, + protectedRoomsManager.ok, protectionsConfig.ok, - membershipSet.ok, - setRoomState.ok, userID, ); return Ok(protectedRoomsSet); diff --git a/src/protections/RedactionSynchronisation.ts b/src/protections/RedactionSynchronisation.ts index ad827f5..4932c14 100644 --- a/src/protections/RedactionSynchronisation.ts +++ b/src/protections/RedactionSynchronisation.ts @@ -36,7 +36,7 @@ export class RedactionSynchronisationProtection extends AbstractProtection rooms.push(room.toRoomIDOrAlias())); + this.protectedRoomsSet.allProtectedRooms.forEach(room => rooms.push(room.toRoomIDOrAlias())); } else { for (const roomMembership of this.protectedRoomsSet.setMembership.allRooms) { const membership = roomMembership.membershipForUser(policy.entity as StringUserID); diff --git a/test/integration/banPropagationTest.ts b/test/integration/banPropagationTest.ts index a8de1a6..ff8bb98 100644 --- a/test/integration/banPropagationTest.ts +++ b/test/integration/banPropagationTest.ts @@ -29,7 +29,7 @@ describe("Ban propagation test", function() { const room = await moderator.createRoom({ invite: [draupnir.clientUserID] }); await draupnir.client.joinRoom(room); await moderator.setUserPowerLevel(draupnir.clientUserID, room, 100); - await draupnir.protectedRoomsSet.protectedRoomsConfig.addRoom(MatrixRoomReference.fromRoomID(room as StringRoomID)); + await draupnir.protectedRoomsSet.protectedRoomsManager.addRoom(MatrixRoomReference.fromRoomID(room as StringRoomID)); return room; })); // create a policy list so that we can check it for a user rule later diff --git a/test/integration/reportPollingTest.ts b/test/integration/reportPollingTest.ts index 41ce2c1..ab3a2d7 100644 --- a/test/integration/reportPollingTest.ts +++ b/test/integration/reportPollingTest.ts @@ -16,7 +16,7 @@ describe("Test: Report polling", function() { } let protectedRoomId = await draupnir.client.createRoom({ invite: [await client.getUserId()] }); await client.joinRoom(protectedRoomId); - await draupnir.protectedRoomsSet.protectedRoomsConfig.addRoom(MatrixRoomReference.fromRoomID(protectedRoomId as StringRoomID)); + await draupnir.protectedRoomsSet.protectedRoomsManager.addRoom(MatrixRoomReference.fromRoomID(protectedRoomId as StringRoomID)); const eventId = await client.sendMessage(protectedRoomId, {msgtype: "m.text", body: "uwNd3q"}); await new Promise(async resolve => {