Update for MPS ProtectedRoomsManager.

This commit is contained in:
gnuxie
2024-05-02 14:51:48 +01:00
committed by Gnuxie
parent a85d292841
commit 0049dfd367
12 changed files with 27 additions and 50 deletions

View File

@@ -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)) {

View File

@@ -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({});
}

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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,

View File

@@ -42,7 +42,7 @@ defineInterfaceCommand({
summary: "List all of the protected rooms.",
parameters: parameters([]),
command: async function (this: DraupnirContext, _keywrods): Promise<ActionResult<MatrixRoomID[]>> {
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;
}

View File

@@ -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);
}

View File

@@ -87,7 +87,7 @@ export async function draupnirStatusInfo(draupnir: Draupnir): Promise<StatusInfo
const protectedWatchedLists = watchedListInfo.filter((info) => 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,

View File

@@ -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<ActionResult<ProtectedRoomsConfig>> {
return await MjolnirProtectedRoomsConfig.createFromStore(
new BotSDKMatrixAccountData(
@@ -61,27 +62,8 @@ async function makeProtectedRoomsConfig(
MjolnirProtectedRoomsEvent,
client
),
roomJoiner
);
}
async function makeSetMembership(
roomMembershipManager: RoomMembershipManager,
protectedRoomsConfig: ProtectedRoomsConfig
): Promise<ActionResult<SetMembership>> {
return await StandardSetMembership.create(
roomMembershipManager,
protectedRoomsConfig
);
}
async function makeSetRoomState(
roomStateManager: RoomStateManager,
protectedRoomsConfig: ProtectedRoomsConfig
): Promise<ActionResult<SetRoomState>> {
return await StandardSetRoomState.create(
roomStateManager,
protectedRoomsConfig
roomResolver,
loggableConfigTracker
);
}
@@ -150,23 +132,20 @@ export async function makeProtectedRoomsSet(
config: IConfig,
loggableConfigTracker: LoggableConfigTracker,
): Promise<ActionResult<ProtectedRoomsSet>> {
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);

View File

@@ -36,7 +36,7 @@ export class RedactionSynchronisationProtection extends AbstractProtection<Redac
public redactForNewUserPolicy(policy: PolicyRule): void {
const rooms: StringRoomID[] = [];
if (policy.isGlob()) {
this.protectedRoomsSet.protectedRoomsConfig.allRooms.forEach(room => 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);

View File

@@ -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

View File

@@ -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 => {