Stop managing client from utils and require the caller to (tests)

Specifically `onReactionTo` was starting and stopping the client
argument it was given, which could be problematic if the caller
didn't expect that or didn't want it to.
This commit is contained in:
gnuxie 2021-10-20 11:44:25 +01:00
parent 7773949631
commit c52084c58a
2 changed files with 28 additions and 14 deletions

View File

@ -3,6 +3,7 @@ import { MatrixClient } from "matrix-bot-sdk";
/**
* Returns a promise that resolves to an event that is reacting to the event produced by targetEventThunk.
* @param client A MatrixClient that is already in the targetRoom that can be started to listen for the event produced by targetEventThunk.
* This function assumes that the start() has already been called on the client.
* @param targetRoom The room to listen for the reaction in.
* @param reactionKey The reaction key to wait for.
* @param targetEventThunk A function that produces an event ID when called. This event ID is then used to listen for a reaction.
@ -18,7 +19,6 @@ export async function onReactionTo(client: MatrixClient, targetRoom: string, rea
let targetCb;
try {
client.on('room.event', addEvent)
client.start();
const targetEventId = await targetEventThunk();
for (let event of reactionEvents) {
const relates_to = event.content['m.relates_to'];
@ -38,7 +38,6 @@ export async function onReactionTo(client: MatrixClient, targetRoom: string, rea
client.on('room.event', targetCb);
});
} finally {
client.stop()
client.removeListener('room.event', addEvent);
if (targetCb) {
client.removeListener('room.event', targetCb);

View File

@ -6,7 +6,7 @@ import { getMessagesByUserIn } from "../../../src/utils";
import { LogService } from "matrix-bot-sdk";
import { onReactionTo } from "./commandUtils";
describe("Test: The redaction command", async () => {
describe("Test: The redaction command", function () {
it('Mjölnir redacts all of the events sent by a spammer when instructed to by giving their id and a room id.', async function() {
this.timeout(20000);
// Create a few users and a room.
@ -32,9 +32,15 @@ import { onReactionTo } from "./commandUtils";
await Promise.all([...Array(50).keys()].map((i) => moderator.sendMessage(targetRoom, {msgtype: 'm.text.', body: `Irrelevant Message #${i}`})));
await badUser.sendMessage(targetRoom, {msgtype: 'm.text', body: "Very Bad Stuff"});
try {
moderator.start();
await onReactionTo(moderator, config.managementRoom, '✅', async () => {
return await moderator.sendMessage(config.managementRoom, { msgtype: 'm.text', body: `!mjolnir redact ${badUserId} ${targetRoom}` });
});
} finally {
moderator.stop();
}
await getMessagesByUserIn(moderator, badUserId, targetRoom, 1000, function(events) {
events.map(e => {
if (e.type === 'm.room.member') {
@ -55,7 +61,7 @@ import { onReactionTo } from "./commandUtils";
let moderator = await newTestUser(false, "moderator");
this.moderator = moderator;
await moderator.joinRoom(config.managementRoom);
let targetRooms = [];
let targetRooms: string[] = [];
for (let i = 0; i < 5; i++) {
let targetRoom = await moderator.createRoom({ invite: [await badUser.getUserId(), mjolnirUserId]});
await moderator.setUserPowerLevel(mjolnirUserId, targetRoom, 100);
@ -73,9 +79,14 @@ import { onReactionTo } from "./commandUtils";
await badUser.sendMessage(targetRoom, {msgtype: 'm.text', body: "Very Bad Stuff"});
}
try {
moderator.start();
await onReactionTo(moderator, config.managementRoom, '✅', async () => {
return await moderator.sendMessage(config.managementRoom, { msgtype: 'm.text', body: `!mjolnir redact ${badUserId}` });
});
} finally {
moderator.stop();
}
targetRooms.map(async targetRoom => {
await getMessagesByUserIn(moderator, badUserId, targetRoom, 1000, function(events) {
@ -93,7 +104,6 @@ import { onReactionTo } from "./commandUtils";
this.timeout(20000);
// Create a few users and a room.
let badUser = await newTestUser(false, "spammer-needs-redacting");
let badUserId = await badUser.getUserId();
const mjolnir = config.RUNTIME.client!
let mjolnirUserId = await mjolnir.getUserId();
let moderator = await newTestUser(false, "moderator");
@ -105,9 +115,14 @@ import { onReactionTo } from "./commandUtils";
moderator.sendMessage(config.managementRoom, {msgtype: 'm.text.', body: `!mjolnir rooms add ${targetRoom}`});
let eventToRedact = await badUser.sendMessage(targetRoom, {msgtype: 'm.text', body: "Very Bad Stuff"});
try {
moderator.start();
await onReactionTo(moderator, config.managementRoom, '✅', async () => {
return await moderator.sendMessage(config.managementRoom, {msgtype: 'm.text', body: `!mjolnir redact https://matrix.to/#/${encodeURIComponent(targetRoom)}/${encodeURIComponent(eventToRedact)}`});
});
} finally {
moderator.stop();
}
let redactedEvent = await moderator.getEvent(targetRoom, eventToRedact);
assert.equal(Object.keys(redactedEvent.content).length, 0, "This event should have been redacted");