2021-10-19 04:55:28 -04:00
|
|
|
import { strict as assert } from "assert";
|
|
|
|
|
|
|
|
import { newTestUser } from "../clientHelper";
|
|
|
|
import { getMessagesByUserIn } from "../../../src/utils";
|
|
|
|
import { LogService } from "matrix-bot-sdk";
|
2021-12-03 12:17:23 -05:00
|
|
|
import { getFirstReaction } from "./commandUtils";
|
2021-10-19 04:55:28 -04:00
|
|
|
|
2021-10-20 06:44:25 -04:00
|
|
|
describe("Test: The redaction command", function () {
|
2022-01-28 06:03:20 -05:00
|
|
|
// If a test has a timeout while awaitng on a promise then we never get given control back.
|
|
|
|
afterEach(function() { this.moderator?.stop(); });
|
|
|
|
|
2021-10-19 04:55:28 -04:00
|
|
|
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() {
|
2022-01-06 06:19:19 -05:00
|
|
|
this.timeout(60000);
|
2021-10-19 04:55:28 -04:00
|
|
|
// Create a few users and a room.
|
2022-08-16 10:51:18 -04:00
|
|
|
let badUser = await newTestUser(this.config.homeserverUrl, { name: { contains: "spammer-needs-redacting" } });
|
2021-10-19 04:55:28 -04:00
|
|
|
let badUserId = await badUser.getUserId();
|
2022-08-16 10:51:18 -04:00
|
|
|
const mjolnir = this.config.RUNTIME.client!
|
2021-10-19 04:55:28 -04:00
|
|
|
let mjolnirUserId = await mjolnir.getUserId();
|
2022-08-16 10:51:18 -04:00
|
|
|
let moderator = await newTestUser(this.config.homeserverUrl, { name: { contains: "moderator" } });
|
2021-10-19 04:55:28 -04:00
|
|
|
this.moderator = moderator;
|
2022-08-16 10:51:18 -04:00
|
|
|
await moderator.joinRoom(this.config.managementRoom);
|
2021-10-19 04:55:28 -04:00
|
|
|
let targetRoom = await moderator.createRoom({ invite: [await badUser.getUserId(), mjolnirUserId]});
|
|
|
|
await moderator.setUserPowerLevel(mjolnirUserId, targetRoom, 100);
|
|
|
|
await badUser.joinRoom(targetRoom);
|
2022-01-17 11:24:12 -05:00
|
|
|
moderator.sendMessage(this.mjolnir.managementRoomId, {msgtype: 'm.text.', body: `!mjolnir rooms add ${targetRoom}`});
|
2021-10-19 04:55:28 -04:00
|
|
|
|
2022-08-16 10:51:18 -04:00
|
|
|
LogService.debug("redactionTest", `targetRoom: ${targetRoom}, managementRoom: ${this.config.managementRoom}`);
|
2021-10-19 04:55:28 -04:00
|
|
|
// Sandwich irrelevant messages in bad messages.
|
|
|
|
await badUser.sendMessage(targetRoom, {msgtype: 'm.text', body: "Very Bad Stuff"});
|
|
|
|
await Promise.all([...Array(50).keys()].map((i) => moderator.sendMessage(targetRoom, {msgtype: 'm.text.', body: `Irrelevant Message #${i}`})));
|
|
|
|
for (let i = 0; i < 5; i++) {
|
|
|
|
await badUser.sendMessage(targetRoom, {msgtype: 'm.text', body: "Very Bad Stuff"});
|
|
|
|
}
|
|
|
|
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"});
|
|
|
|
|
2021-10-20 06:44:25 -04:00
|
|
|
try {
|
2022-01-26 11:25:43 -05:00
|
|
|
await moderator.start();
|
2021-12-03 12:17:23 -05:00
|
|
|
await getFirstReaction(moderator, this.mjolnir.managementRoomId, '✅', async () => {
|
2022-01-17 11:24:12 -05:00
|
|
|
return await moderator.sendMessage(this.mjolnir.managementRoomId, { msgtype: 'm.text', body: `!mjolnir redact ${badUserId} ${targetRoom}` });
|
2021-10-20 06:44:25 -04:00
|
|
|
});
|
|
|
|
} finally {
|
|
|
|
moderator.stop();
|
|
|
|
}
|
|
|
|
|
2021-10-19 04:55:28 -04:00
|
|
|
await getMessagesByUserIn(moderator, badUserId, targetRoom, 1000, function(events) {
|
|
|
|
events.map(e => {
|
|
|
|
if (e.type === 'm.room.member') {
|
|
|
|
assert.equal(Object.keys(e.content).length, 1, "Only membership should be left on the membership even when it has been redacted.")
|
|
|
|
} else if (Object.keys(e.content).length !== 0) {
|
|
|
|
throw new Error(`This event should have been redacted: ${JSON.stringify(e, null, 2)}`)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
});
|
|
|
|
})
|
2022-01-28 06:03:20 -05:00
|
|
|
|
2021-10-19 04:55:28 -04:00
|
|
|
it('Mjölnir redacts all of the events sent by a spammer when instructed to by giving their id in multiple rooms.', async function() {
|
2022-01-06 06:19:19 -05:00
|
|
|
this.timeout(60000);
|
2021-10-19 04:55:28 -04:00
|
|
|
// Create a few users and a room.
|
2022-08-16 10:51:18 -04:00
|
|
|
let badUser = await newTestUser(this.config.homeserverUrl, { name: { contains: "spammer-needs-redacting" } });
|
2021-10-19 04:55:28 -04:00
|
|
|
let badUserId = await badUser.getUserId();
|
2022-08-16 10:51:18 -04:00
|
|
|
const mjolnir = this.config.RUNTIME.client!
|
2021-10-19 04:55:28 -04:00
|
|
|
let mjolnirUserId = await mjolnir.getUserId();
|
2022-08-16 10:51:18 -04:00
|
|
|
let moderator = await newTestUser(this.config.homeserverUrl, { name: { contains: "moderator" } });
|
2021-10-19 04:55:28 -04:00
|
|
|
this.moderator = moderator;
|
2022-08-16 10:51:18 -04:00
|
|
|
await moderator.joinRoom(this.config.managementRoom);
|
2021-10-20 06:44:25 -04:00
|
|
|
let targetRooms: string[] = [];
|
2021-10-19 04:55:28 -04:00
|
|
|
for (let i = 0; i < 5; i++) {
|
|
|
|
let targetRoom = await moderator.createRoom({ invite: [await badUser.getUserId(), mjolnirUserId]});
|
|
|
|
await moderator.setUserPowerLevel(mjolnirUserId, targetRoom, 100);
|
|
|
|
await badUser.joinRoom(targetRoom);
|
2022-01-17 11:24:12 -05:00
|
|
|
await moderator.sendMessage(this.mjolnir.managementRoomId, {msgtype: 'm.text.', body: `!mjolnir rooms add ${targetRoom}`});
|
2021-10-19 04:55:28 -04:00
|
|
|
targetRooms.push(targetRoom);
|
|
|
|
|
|
|
|
// Sandwich irrelevant messages in bad messages.
|
|
|
|
await badUser.sendMessage(targetRoom, {msgtype: 'm.text', body: "Very Bad Stuff"});
|
|
|
|
await Promise.all([...Array(50).keys()].map((j) => moderator.sendMessage(targetRoom, {msgtype: 'm.text.', body: `Irrelevant Message #${j}`})));
|
|
|
|
for (let j = 0; j < 5; j++) {
|
|
|
|
await badUser.sendMessage(targetRoom, {msgtype: 'm.text', body: "Very Bad Stuff"});
|
|
|
|
}
|
|
|
|
await Promise.all([...Array(50).keys()].map((j) => moderator.sendMessage(targetRoom, {msgtype: 'm.text.', body: `Irrelevant Message #${j}`})));
|
|
|
|
await badUser.sendMessage(targetRoom, {msgtype: 'm.text', body: "Very Bad Stuff"});
|
|
|
|
}
|
|
|
|
|
2021-10-20 06:44:25 -04:00
|
|
|
try {
|
2022-01-26 11:25:43 -05:00
|
|
|
await moderator.start();
|
2021-12-03 12:17:23 -05:00
|
|
|
await getFirstReaction(moderator, this.mjolnir.managementRoomId, '✅', async () => {
|
2022-01-17 11:24:12 -05:00
|
|
|
return await moderator.sendMessage(this.mjolnir.managementRoomId, { msgtype: 'm.text', body: `!mjolnir redact ${badUserId}` });
|
2021-10-20 06:44:25 -04:00
|
|
|
});
|
|
|
|
} finally {
|
|
|
|
moderator.stop();
|
|
|
|
}
|
2021-10-19 04:55:28 -04:00
|
|
|
|
|
|
|
targetRooms.map(async targetRoom => {
|
|
|
|
await getMessagesByUserIn(moderator, badUserId, targetRoom, 1000, function(events) {
|
|
|
|
events.map(e => {
|
|
|
|
if (e.type === 'm.room.member') {
|
|
|
|
assert.equal(Object.keys(e.content).length, 1, "Only membership should be left on the membership even when it has been redacted.")
|
|
|
|
} else if (Object.keys(e.content).length !== 0) {
|
|
|
|
throw new Error(`This event should have been redacted: ${JSON.stringify(e, null, 2)}`)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it("Redacts a single event when instructed to.", async function () {
|
2022-01-06 06:19:19 -05:00
|
|
|
this.timeout(60000);
|
2021-10-19 04:55:28 -04:00
|
|
|
// Create a few users and a room.
|
2022-08-16 10:51:18 -04:00
|
|
|
let badUser = await newTestUser(this.config.homeserverUrl, { name: { contains: "spammer-needs-redacting" } });
|
|
|
|
const mjolnir = this.config.RUNTIME.client!
|
2021-10-19 04:55:28 -04:00
|
|
|
let mjolnirUserId = await mjolnir.getUserId();
|
2022-08-16 10:51:18 -04:00
|
|
|
let moderator = await newTestUser(this.config.homeserverUrl, { name: { contains: "moderator" } });
|
2021-10-19 04:55:28 -04:00
|
|
|
this.moderator = moderator;
|
2022-08-16 10:51:18 -04:00
|
|
|
await moderator.joinRoom(this.config.managementRoom);
|
2021-10-19 04:55:28 -04:00
|
|
|
let targetRoom = await moderator.createRoom({ invite: [await badUser.getUserId(), mjolnirUserId]});
|
|
|
|
await moderator.setUserPowerLevel(mjolnirUserId, targetRoom, 100);
|
|
|
|
await badUser.joinRoom(targetRoom);
|
2022-01-17 11:24:12 -05:00
|
|
|
moderator.sendMessage(this.mjolnir.managementRoomId, {msgtype: 'm.text.', body: `!mjolnir rooms add ${targetRoom}`});
|
2021-10-19 04:55:28 -04:00
|
|
|
let eventToRedact = await badUser.sendMessage(targetRoom, {msgtype: 'm.text', body: "Very Bad Stuff"});
|
|
|
|
|
2021-10-20 06:44:25 -04:00
|
|
|
try {
|
2022-01-26 11:25:43 -05:00
|
|
|
await moderator.start();
|
2021-12-03 12:17:23 -05:00
|
|
|
await getFirstReaction(moderator, this.mjolnir.managementRoomId, '✅', async () => {
|
2022-01-17 11:24:12 -05:00
|
|
|
return await moderator.sendMessage(this.mjolnir.managementRoomId, {msgtype: 'm.text', body: `!mjolnir redact https://matrix.to/#/${encodeURIComponent(targetRoom)}/${encodeURIComponent(eventToRedact)}`});
|
2021-10-20 06:44:25 -04:00
|
|
|
});
|
|
|
|
} finally {
|
|
|
|
moderator.stop();
|
|
|
|
}
|
2021-10-19 04:55:28 -04:00
|
|
|
|
|
|
|
let redactedEvent = await moderator.getEvent(targetRoom, eventToRedact);
|
|
|
|
assert.equal(Object.keys(redactedEvent.content).length, 0, "This event should have been redacted");
|
|
|
|
})
|
|
|
|
});
|