Skip to content

Commit

Permalink
Don't shut down protected rooms (#554)
Browse files Browse the repository at this point in the history
* don't shutdown a room mjolnir is currently protecting

* test

* make protectedRoomsConfig readonly

Co-authored-by: Travis Ralston <travisr@matrix.org>

---------

Co-authored-by: Travis Ralston <travisr@matrix.org>
  • Loading branch information
H-Shay and turt2live authored Oct 23, 2024
1 parent e1dcfc2 commit f53bc84
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Mjolnir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export class Mjolnir {
*/
private unlistedUserRedactionQueue = new UnlistedUserRedactionQueue();

private protectedRoomsConfig: ProtectedRoomsConfig;
public readonly protectedRoomsConfig: ProtectedRoomsConfig;
public readonly protectedRoomsTracker: ProtectedRoomsSet;
private webapis: WebAPIs;
private openMetrics: OpenMetrics;
Expand Down
17 changes: 16 additions & 1 deletion src/commands/ShutdownRoomCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ limitations under the License.
*/

import { Mjolnir } from "../Mjolnir";
import { RichReply } from "@vector-im/matrix-bot-sdk";
import { LogLevel, RichReply } from "@vector-im/matrix-bot-sdk";

// !mjolnir shutdown room <room> [<message>]
export async function execShutdownRoomCommand(roomId: string, event: any, mjolnir: Mjolnir, parts: string[]) {
Expand All @@ -31,6 +31,21 @@ export async function execShutdownRoomCommand(roomId: string, event: any, mjolni
return;
}

let protectedRooms;
if (mjolnir.config.protectAllJoinedRooms) {
protectedRooms = await mjolnir.client.getJoinedRooms();
} else {
protectedRooms = mjolnir.protectedRoomsConfig.getExplicitlyProtectedRooms();
}
if (protectedRooms.includes(target)) {
await mjolnir.managementRoomOutput.logMessage(
LogLevel.INFO,
"ShutdownRoomCommand",
"You are attempting to shutdown a room that mjolnir currently protects, aborting.",
);
return;
}

await mjolnir.shutdownSynapseRoom(await mjolnir.client.resolveRoom(target), reason);
await mjolnir.client.unstableApis.addReactionToEvent(roomId, event["event_id"], "✅");
}
44 changes: 42 additions & 2 deletions test/integration/commands/shutdownCommandTest.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { strict as assert } from "assert";

import { newTestUser } from "../clientHelper";
import { getFirstReaction } from "./commandUtils";
import { MatrixClient } from "@vector-im/matrix-bot-sdk";

describe("Test: shutdown command", function () {
let client;
let client: MatrixClient;
this.beforeEach(async function () {
client = await newTestUser(this.config.homeserverUrl, { name: { contains: "shutdown-command" } });
await client.start();
Expand Down Expand Up @@ -34,7 +36,7 @@ describe("Test: shutdown command", function () {
});

const reply2 = new Promise((resolve, reject) => {
this.mjolnir.client.on("room.event", (roomId, event) => {
this.mjolnir.client.on("room.event", (roomId: string, event: any) => {
if (
roomId !== this.mjolnir.managementRoomId &&
roomId !== badRoom &&
Expand All @@ -54,4 +56,42 @@ describe("Test: shutdown command", function () {
return e.message.endsWith('{"errcode":"M_UNKNOWN","error":"This room has been blocked on this server"}');
});
});
it("Mjolnir will not shutdown a room it is protecting.", async function () {
this.timeout(20000);
const targetRoom = await client.createRoom({ preset: "public_chat" });
await client.joinRoom(this.mjolnir.managementRoomId);
const otherUser = await newTestUser(this.config.homeserverUrl, {
name: { contains: "shutdown-command-extra" },
});

await getFirstReaction(client, this.mjolnir.managementRoomId, "✅", async () => {
return await client.sendMessage(this.mjolnir.managementRoomId, {
msgtype: "m.text",
body: `!mjolnir rooms add ${targetRoom}`,
});
});

await client.sendMessage(this.mjolnir.managementRoomId, {
msgtype: "m.text",
body: `!mjolnir shutdown room ${targetRoom}`,
});

let reply = new Promise((resolve, reject) => {
client.on("room.message", (roomId: string, event: any) => {
console.log(JSON.stringify(event));
if (
roomId === this.mjolnir.managementRoomId &&
event.content?.body.includes(
"You are attempting to shutdown a room that mjolnir currently protects, aborting",
)
) {
resolve(event);
}
});
});
await reply;
// room should not be shutdown and available to join
const joined = await otherUser.joinRoom(targetRoom);
await otherUser.sendMessage(joined, { msgtype: "m.text", body: "it's fine to interact with this room" });
});
});

0 comments on commit f53bc84

Please sign in to comment.