2021-09-14 04:31:59 -04:00
|
|
|
/*
|
|
|
|
Copyright 2021 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.
|
|
|
|
*/
|
2021-09-08 11:44:06 -04:00
|
|
|
import {
|
|
|
|
MatrixClient,
|
2021-09-22 06:57:45 -04:00
|
|
|
PantalaimonClient,
|
2021-09-30 10:32:20 -04:00
|
|
|
MemoryStorageProvider,
|
|
|
|
LogService,
|
|
|
|
LogLevel,
|
|
|
|
RichConsoleLogger
|
2021-09-08 11:44:06 -04:00
|
|
|
} from "matrix-bot-sdk";
|
2021-09-27 10:52:28 -04:00
|
|
|
import { Mjolnir} from '../../src/Mjolnir';
|
2022-01-25 07:19:44 -05:00
|
|
|
import { overrideRatelimitForUser, registerUser } from "./clientHelper";
|
2022-01-06 06:19:19 -05:00
|
|
|
import { patchMatrixClient } from "../../src/utils";
|
2022-08-09 06:29:27 -04:00
|
|
|
import { IConfig } from "../../src/config";
|
2021-09-08 11:44:06 -04:00
|
|
|
|
2021-09-24 05:12:18 -04:00
|
|
|
/**
|
|
|
|
* Ensures that a room exists with the alias, if it does not exist we create it.
|
|
|
|
* @param client The MatrixClient to use to resolve or create the aliased room.
|
|
|
|
* @param alias The alias of the room.
|
|
|
|
* @returns The room ID of the aliased room.
|
|
|
|
*/
|
|
|
|
export async function ensureAliasedRoomExists(client: MatrixClient, alias: string): Promise<string> {
|
2022-01-06 06:19:19 -05:00
|
|
|
try {
|
|
|
|
return await client.resolveRoom(alias);
|
|
|
|
} catch (e) {
|
2021-09-08 11:44:06 -04:00
|
|
|
if (e?.body?.errcode === 'M_NOT_FOUND') {
|
|
|
|
console.info(`${alias} hasn't been created yet, so we're making it now.`)
|
2021-11-09 07:15:49 -05:00
|
|
|
let roomId = await client.createRoom({
|
|
|
|
visibility: "public",
|
|
|
|
});
|
2021-11-19 12:09:58 -05:00
|
|
|
await client.createRoomAlias(alias, roomId);
|
2021-09-24 05:12:18 -04:00
|
|
|
return roomId
|
2021-09-08 11:44:06 -04:00
|
|
|
}
|
|
|
|
throw e;
|
2022-01-06 06:19:19 -05:00
|
|
|
}
|
2021-09-22 06:57:45 -04:00
|
|
|
}
|
|
|
|
|
2022-08-09 06:29:27 -04:00
|
|
|
async function configureMjolnir(config: IConfig) {
|
2021-10-07 08:42:08 -04:00
|
|
|
try {
|
2022-01-18 06:34:38 -05:00
|
|
|
await registerUser(config.pantalaimon.username, config.pantalaimon.username, config.pantalaimon.password, true)
|
2021-10-07 08:42:08 -04:00
|
|
|
} catch (e) {
|
2022-01-24 12:04:21 -05:00
|
|
|
if (e?.body?.errcode === 'M_USER_IN_USE') {
|
|
|
|
console.log(`${config.pantalaimon.username} already registered, skipping`);
|
|
|
|
return;
|
2021-09-22 06:57:45 -04:00
|
|
|
}
|
2021-10-07 08:42:08 -04:00
|
|
|
throw e;
|
|
|
|
};
|
2021-09-22 06:57:45 -04:00
|
|
|
}
|
|
|
|
|
2021-10-07 08:42:08 -04:00
|
|
|
export function mjolnir(): Mjolnir | null {
|
|
|
|
return globalMjolnir;
|
|
|
|
}
|
|
|
|
export function matrixClient(): MatrixClient | null {
|
|
|
|
return globalClient;
|
|
|
|
}
|
|
|
|
let globalClient: MatrixClient | null
|
|
|
|
let globalMjolnir: Mjolnir | null;
|
2021-09-24 05:12:18 -04:00
|
|
|
|
2021-10-07 08:42:08 -04:00
|
|
|
/**
|
|
|
|
* Return a test instance of Mjolnir.
|
|
|
|
*/
|
2022-08-09 06:29:27 -04:00
|
|
|
export async function makeMjolnir(config: IConfig): Promise<Mjolnir> {
|
|
|
|
await configureMjolnir(config);
|
2021-09-30 10:32:20 -04:00
|
|
|
LogService.setLogger(new RichConsoleLogger());
|
|
|
|
LogService.setLevel(LogLevel.fromString(config.logLevel, LogLevel.DEBUG));
|
|
|
|
LogService.info("test/mjolnirSetupUtils", "Starting bot...");
|
2021-09-22 12:59:11 -04:00
|
|
|
const pantalaimon = new PantalaimonClient(config.homeserverUrl, new MemoryStorageProvider());
|
2022-01-05 04:43:43 -05:00
|
|
|
const client = await pantalaimon.createClientWithCredentials(config.pantalaimon.username, config.pantalaimon.password);
|
2022-01-25 07:19:44 -05:00
|
|
|
await overrideRatelimitForUser(await client.getUserId());
|
2022-01-06 06:19:19 -05:00
|
|
|
patchMatrixClient();
|
2021-09-24 05:12:18 -04:00
|
|
|
await ensureAliasedRoomExists(client, config.managementRoom);
|
2022-08-09 06:29:27 -04:00
|
|
|
let mj = await Mjolnir.setupMjolnirFromConfig(client, config);
|
2021-10-07 08:42:08 -04:00
|
|
|
globalClient = client;
|
2022-02-24 09:46:15 -05:00
|
|
|
globalMjolnir = mj;
|
|
|
|
return mj;
|
2021-09-23 11:12:08 -04:00
|
|
|
}
|
|
|
|
|
2021-09-24 05:12:18 -04:00
|
|
|
/**
|
|
|
|
* Remove the alias and leave the room, can't be implicitly provided from the config because Mjolnir currently mutates it.
|
|
|
|
* @param client The client to use to leave the room.
|
|
|
|
* @param roomId The roomId of the room to leave.
|
|
|
|
* @param alias The alias to remove from the room.
|
|
|
|
*/
|
2021-09-23 11:12:08 -04:00
|
|
|
export async function teardownManagementRoom(client: MatrixClient, roomId: string, alias: string) {
|
|
|
|
await client.deleteRoomAlias(alias);
|
|
|
|
await client.leaveRoom(roomId);
|
2021-09-24 05:12:18 -04:00
|
|
|
}
|