Add commands for managing room aliases and the room directory

This commit is contained in:
Travis Ralston 2020-02-13 12:40:44 -07:00
parent f6b4376eeb
commit aee562455a
3 changed files with 92 additions and 0 deletions

View File

@ -0,0 +1,43 @@
/*
Copyright 2020 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import { Mjolnir } from "../Mjolnir";
import { RichReply } from "matrix-bot-sdk";
async function addRemoveFromDirectory(inRoomId: string, event: any, mjolnir: Mjolnir, roomRef: string, visibility: "public" | "private") {
const isAdmin = await mjolnir.isSynapseAdmin();
if (!isAdmin) {
const message = "I am not a Synapse administrator, or the endpoint is blocked";
const reply = RichReply.createFor(inRoomId, event, message, message);
reply['msgtype'] = "m.notice";
return mjolnir.client.sendMessage(inRoomId, reply);
}
const targetRoomId = await mjolnir.client.resolveRoom(roomRef);
await mjolnir.client.setDirectoryVisibility(targetRoomId, visibility);
await mjolnir.client.unstableApis.addReactionToEvent(inRoomId, event['event_id'], '✅');
}
// !mjolnir directory add <room>
export async function execAddRoomToDirectoryCommand(roomId: string, event: any, mjolnir: Mjolnir, parts: string[]) {
await addRemoveFromDirectory(roomId, event, mjolnir, parts[3], "public");
}
// !mjolnir directory remove <room>
export async function execRemoveRoomFromDirectoryCommand(roomId: string, event: any, mjolnir: Mjolnir, parts: string[]) {
await addRemoveFromDirectory(roomId, event, mjolnir, parts[3], "private");
}

View File

@ -31,6 +31,8 @@ import { execDeactivateCommand } from "./DeactivateCommand";
import { execDisableProtection, execEnableProtection, execListProtections } from "./ProtectionsCommands";
import { execListProtectedRooms } from "./ListProtectedRoomsCommand";
import { execAddProtectedRoom, execRemoveProtectedRoom } from "./AddRemoveProtectedRoomsCommand";
import { execMoveAliasCommand } from "./MoveAliasCommand";
import { execAddRoomToDirectoryCommand, execRemoveRoomFromDirectoryCommand } from "./AddRemoveRoomFromDirectoryCommand";
export const COMMAND_PREFIX = "!mjolnir";
@ -77,6 +79,12 @@ export async function handleCommand(roomId: string, event: any, mjolnir: Mjolnir
return await execRemoveProtectedRoom(roomId, event, mjolnir, parts);
} else if (parts[1] === 'rooms' && parts.length === 2) {
return await execListProtectedRooms(roomId, event, mjolnir);
} else if (parts[1] === 'move' && parts.length > 3) {
return await execMoveAliasCommand(roomId, event, mjolnir, parts);
} else if (parts[1] === 'directory' && parts.length > 3 && parts[2] === 'add') {
return await execAddRoomToDirectoryCommand(roomId, event, mjolnir, parts);
} else if (parts[1] === 'directory' && parts.length > 3 && parts[2] === 'remove') {
return await execRemoveRoomFromDirectoryCommand(roomId, event, mjolnir, parts);
} else {
// Help menu
const menu = "" +
@ -100,6 +108,9 @@ export async function handleCommand(roomId: string, event: any, mjolnir: Mjolnir
"!mjolnir rooms - Lists all the protected rooms\n" +
"!mjolnir rooms add <room alias/ID> - Adds a protected room (may cause high server load)\n" +
"!mjolnir rooms remove <room alias/ID> - Removes a protected room\n" +
"!mjolnir move <room alias> <room alias/ID> - Moves a <room alias> to a new <room ID>\n" +
"!mjolnir directory add <room alias/ID> - Publishes a room in the server's room directory\n" +
"!mjolnir directory remove <room alias/ID> - Removes a room from the server's room directory\n" +
"!mjolnir help - This menu\n";
const html = `<b>Mjolnir help:</b><br><pre><code>${htmlEscape(menu)}</code></pre>`;
const text = `Mjolnir help:\n${menu}`;

View File

@ -0,0 +1,38 @@
/*
Copyright 2020 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import { Mjolnir } from "../Mjolnir";
import { RichReply } from "matrix-bot-sdk";
// !mjolnir move <alias> <new room ID>
export async function execMoveAliasCommand(roomId: string, event: any, mjolnir: Mjolnir, parts: string[]) {
const movingAlias = parts[2];
const targetRoom = parts[3];
const isAdmin = await mjolnir.isSynapseAdmin();
if (!isAdmin) {
const message = "I am not a Synapse administrator, or the endpoint is blocked";
const reply = RichReply.createFor(roomId, event, message, message);
reply['msgtype'] = "m.notice";
return mjolnir.client.sendMessage(roomId, reply);
}
await mjolnir.client.deleteRoomAlias(movingAlias);
const newRoomId = await mjolnir.client.resolveRoom(targetRoom);
await mjolnir.client.createRoomAlias(movingAlias, newRoomId);
await mjolnir.client.unstableApis.addReactionToEvent(roomId, event['event_id'], '✅');
}