diff --git a/src/commands/Help.tsx b/src/commands/Help.tsx index a078f5cc..fbf911e9 100644 --- a/src/commands/Help.tsx +++ b/src/commands/Help.tsx @@ -53,9 +53,6 @@ const oldHelpMenu = "" + "!mjolnir config add . [value] - Add a value to a list protection setting\n" + "!mjolnir config remove . [value] - Remove a value from a list protection setting\n" + "!mjolnir config get [protection] - List protection settings\n" + -"!mjolnir rooms - Lists all the protected rooms\n" + -"!mjolnir rooms add - Adds a protected room (may cause high server load)\n" + -"!mjolnir rooms remove - Removes a protected room\n" + "!mjolnir resolve - Resolves a room alias to a room ID\n" + "!mjolnir since / [rooms...] [reason] - Apply an action ('kick', 'ban', 'mute', 'unmute' or 'show') to all users who joined a room since / (up to users)\n" + "!mjolnir powerlevel [room alias/ID] - Sets the power level of the user in the specified room (or all protected rooms)\n" + diff --git a/src/commands/Rooms.tsx b/src/commands/Rooms.tsx index 88c9aed2..d5cd70a2 100644 --- a/src/commands/Rooms.tsx +++ b/src/commands/Rooms.tsx @@ -117,6 +117,7 @@ defineInterfaceCommand({ ]), command: async function (this: MjolnirContext, _keywords, roomRef: MatrixRoomReference): Promise> { const roomID = await roomRef.resolve(this.mjolnir.client); + await this.mjolnir.removeProtectedRoom(roomID.toRoomIdOrAlias()); try { await this.mjolnir.client.leaveRoom(roomID.toRoomIdOrAlias()); } catch (exception) { diff --git a/test/integration/commands/roomsTest.ts b/test/integration/commands/roomsTest.ts new file mode 100644 index 00000000..57ae0ec9 --- /dev/null +++ b/test/integration/commands/roomsTest.ts @@ -0,0 +1,39 @@ +import { strict as assert } from "assert"; +import { newTestUser } from "../clientHelper"; +import { getFirstReaction, getFirstReply } from "./commandUtils"; + + describe("Test: The rooms commands", function () { + // If a test has a timeout while awaitng on a promise then we never get given control back. + afterEach(function() { this.moderator?.stop(); }); + + it('Mjolnir can protect a room, show that it is protected and then stop protecting the room.', async function() { + // Create a few users and a room. + const mjolnir = this.mjolnir.client; + let mjolnirUserId = await mjolnir.getUserId(); + let moderator = await newTestUser(this.config.homeserverUrl, { name: { contains: "moderator" } }); + this.moderator = moderator; + await moderator.joinRoom(this.config.managementRoom); + let targetRoom = await moderator.createRoom({ invite: [mjolnirUserId]}); + await moderator.setUserPowerLevel(mjolnirUserId, targetRoom, 100); + + try { + await moderator.start(); + await getFirstReaction(moderator, this.mjolnir.managementRoomId, '✅', async () => { + return await moderator.sendMessage(this.mjolnir.managementRoomId, {msgtype: 'm.text', body: `!mjolnir rooms add ${targetRoom}`}); + }); + let protectedRoomsMessage = await getFirstReply(moderator, this.mjolnir.managementRoomId, async () => { + return await moderator.sendMessage(this.mjolnir.managementRoomId, {msgtype: 'm.text', body: `!mjolnir rooms`}); + }) + assert.equal(protectedRoomsMessage['content']?.['body']?.includes('1'), true, "There should be one protected room"); + await getFirstReaction(moderator, this.mjolnir.managementRoomId, '✅', async () => { + return await moderator.sendMessage(this.mjolnir.managementRoomId, {msgtype: 'm.text', body: `!mjolnir rooms remove ${targetRoom}`}); + }); + protectedRoomsMessage = await getFirstReply(moderator, this.mjolnir.managementRoomId, async () => { + return await moderator.sendMessage(this.mjolnir.managementRoomId, {msgtype: 'm.text', body: `!mjolnir rooms`}); + }) + assert.equal(protectedRoomsMessage['content']?.['body']?.includes('0'), true, "There room should no longer be protected"); + } finally { + moderator.stop(); + } + }) +}) diff --git a/test/integration/fixtures.ts b/test/integration/fixtures.ts index 1634c716..72e1599a 100644 --- a/test/integration/fixtures.ts +++ b/test/integration/fixtures.ts @@ -1,4 +1,5 @@ import { read as configRead } from "../../src/config"; +import { WATCHED_LISTS_EVENT_TYPE } from "../../src/models/PolicyList"; import { patchMatrixClient } from "../../src/utils"; import { makeMjolnir, teardownManagementRoom } from "./mjolnirSetupUtils"; @@ -22,7 +23,7 @@ export const mochaHooks = { config.RUNTIME.client = this.mjolnir.client; await Promise.all([ this.mjolnir.client.setAccountData('org.matrix.mjolnir.protected_rooms', { rooms: [] }), - this.mjolnir.client.setAccountData('org.matrix.mjolnir.watched_lists', { references: [] }), + this.mjolnir.client.setAccountData(WATCHED_LISTS_EVENT_TYPE, { references: [] }), ]); await this.mjolnir.start(); console.log("mochaHooks.beforeEach DONE"); @@ -34,7 +35,7 @@ export const mochaHooks = { await this.mjolnir.stop(); await Promise.all([ this.mjolnir.client.setAccountData('org.matrix.mjolnir.protected_rooms', { rooms: [] }), - this.mjolnir.client.setAccountData('org.matrix.mjolnir.watched_lists', { references: [] }), + this.mjolnir.client.setAccountData(WATCHED_LISTS_EVENT_TYPE, { references: [] }), ]); // remove alias from management room and leave it. await teardownManagementRoom(this.mjolnir.client, this.mjolnir.managementRoomId, this.managementRoomAlias);