Skip to content

Commit

Permalink
remove undoableness, add integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jesopo committed Feb 21, 2022
1 parent f5c6a9a commit 7d9fe14
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 42 deletions.
45 changes: 3 additions & 42 deletions src/Mjolnir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -793,25 +793,21 @@ export class Mjolnir {
}

private async handleConsequence(protection: IProtection, roomId: string, event: any, consequence: Consequence) {
let undoable = false;
switch (consequence.type) {
case ConsequenceType.alert:
break;
case ConsequenceType.redact:
await this.client.redactEvent(roomId, event["event_id"], "abuse detected");
break;
case ConsequenceType.ban:
undoable = true;
//await this.client.banUser(event["sender"], roomId, "abuse detected");
await this.client.banUser(event["sender"], roomId, "abuse detected");
break;
}

console.log(consequence.type)
let message = `protection ${protection.name} enacting ${ConsequenceType[consequence.type]} against ${event["sender"]}`;
if (consequence.reason !== undefined) {
message += ` (reason: ${consequence.reason})`
}
if (undoable) {
message += "\n👎 to undo"
message += ` (reason: ${consequence.reason})`;
}

await this.client.sendMessage(this.managementRoomId, {
Expand All @@ -825,15 +821,6 @@ export class Mjolnir {
});
}

private async handleUndoConsequence(roomId: string, victim: string, type: ConsequenceType) {
switch (type) {
case ConsequenceType.ban:
await this.client.unbanUser(victim, roomId);
break;
}
}


private async handleEvent(roomId: string, event: any) {
// Check for UISI errors
if (roomId === this.managementRoomId) {
Expand Down Expand Up @@ -902,32 +889,6 @@ export class Mjolnir {
await this.printActionResult(redactionErrors);
}
}

let relation: { key?: string, event_id?: string } | null = event?.content?.["m.relates_to"];
if (
event["type"] === "m.reaction"
&& roomId == this.managementRoomId
&& relation !== null
&& relation.event_id !== undefined
) {
// we've got a reaction

const relEvent = await this.client.getEvent(roomId, relation.event_id);
const consequenceData = relEvent?.content?.[CONSEQUENCE_EVENT_DATA];
if (
consequenceData !== undefined
&& relation.key === '👎'
&& consequenceData.who !== undefined
&& consequenceData.room !== undefined
) {
// someone's asked to undo a consequence
await this.handleUndoConsequence(
consequenceData.room,
consequenceData.who,
ConsequenceType[consequenceData.type as keyof typeof ConsequenceType]
);
}
}
}

/**
Expand Down
108 changes: 108 additions & 0 deletions test/integration/standardConsequenceTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import { strict as assert } from "assert";

import config from "../../src/config";
import { Mjolnir } from "../../src/Mjolnir";
import { IProtection } from "../../src/protections/IProtection";
import { newTestUser, noticeListener } from "./clientHelper";
import { matrixClient, mjolnir } from "./mjolnirSetupUtils";
import { ConsequenceType, Consequence } from "../../src/protections/consequence";

describe("Test: standard consequences", function() {
let badUser;
this.beforeEach(async function () {
badUser = await newTestUser({ name: { contains: "standard-consequences" }});
await badUser.start();
})
this.afterEach(async function () {
await badUser.stop();
})
it("Mjolnir applies a standard consequence redaction", async function() {
this.timeout(20000);

let protectedRoomId = await this.mjolnir.client.createRoom({ invite: [await badUser.getUserId()] });
await badUser.joinRoom(this.mjolnir.managementRoomId);
await badUser.joinRoom(protectedRoomId);
await this.mjolnir.addProtectedRoom(protectedRoomId);

await this.mjolnir.registerProtection(new class implements IProtection {
name = "JY2TPN";
description = "A test protection";
settings = { };
handleEvent = async (mjolnir: Mjolnir, roomId: string, event: any) => {
if (event.content.body === "ngmWkF") {
return new Consequence(ConsequenceType.redact, "asd");
}
};
});
await this.mjolnir.enableProtection("JY2TPN");

let reply = new Promise(async (resolve, reject) => {
const messageId = await badUser.sendMessage(protectedRoomId, {msgtype: "m.text", body: "ngmWkF"});
let redaction;
badUser.on('room.event', (roomId, event) => {
if (
roomId === protectedRoomId
&& event?.type === "m.room.redaction"
&& event.redacts === messageId
) {
redaction = event
}
if (
roomId === this.mjolnir.managementRoomId
&& event?.type === "m.room.message"
&& event?.content?.body?.startsWith("protection JY2TPN enacting redact against ")
&& redaction !== undefined
) {
resolve([redaction, event])
}
});
});

const [eventRedact, eventMessage] = await reply
});
it("Mjolnir applies a standard consequence ban", async function() {
this.timeout(20000);

let protectedRoomId = await this.mjolnir.client.createRoom({ invite: [await badUser.getUserId()] });
await badUser.joinRoom(this.mjolnir.managementRoomId);
await badUser.joinRoom(protectedRoomId);
await this.mjolnir.addProtectedRoom(protectedRoomId);

await this.mjolnir.registerProtection(new class implements IProtection {
name = "0LxMTy";
description = "A test protection";
settings = { };
handleEvent = async (mjolnir: Mjolnir, roomId: string, event: any) => {
if (event.content.body === "7Uga3d") {
return new Consequence(ConsequenceType.ban, "asd");
}
};
});
await this.mjolnir.enableProtection("0LxMTy");

let reply = new Promise(async (resolve, reject) => {
const messageId = await badUser.sendMessage(protectedRoomId, {msgtype: "m.text", body: "7Uga3d"});
let redaction;
badUser.on('room.event', (roomId, event) => {
if (
roomId === protectedRoomId
&& event?.type === "m.room.redaction"
&& event.redacts === messageId
) {
redaction = event
}
if (
roomId === this.mjolnir.managementRoomId
&& event?.type === "m.room.message"
&& event?.content?.body?.startsWith("protection 0LxMTy enacting ban against ")
&& redaction !== undefined
) {
resolve([redaction, event])
}
});
});

const [eventRedact, eventMessage] = await reply
});
});

0 comments on commit 7d9fe14

Please sign in to comment.