Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't shut down protected rooms #554

Merged
merged 3 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 protectedRoomsConfig: ProtectedRoomsConfig;
H-Shay marked this conversation as resolved.
Show resolved Hide resolved
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" });
});
});
Loading