Fix reportPollerTest (it works!)

This commit is contained in:
gnuxie
2024-04-06 20:03:36 +01:00
parent 2198c34b55
commit 8044a78fa6
2 changed files with 47 additions and 25 deletions
+9 -5
View File
@@ -30,9 +30,8 @@ import { LogService, UserID } from "matrix-bot-sdk";
import { htmlToText } from "html-to-text";
import { htmlEscape } from "../utils";
import { JSDOM } from 'jsdom';
import { EventEmitter } from 'events';
import { Draupnir } from "../Draupnir";
import { ReactionContent, RoomEvent, StringEventID, StringRoomID, Task, Value, isError } from "matrix-protection-suite";
import { ReactionContent, RoomEvent, StringEventID, StringRoomID, StringUserID, Task, Value, isError } from "matrix-protection-suite";
/// Regexp, used to extract the action label from an action reaction
/// such as `⚽ Kick user @foobar:localhost from room [kick-user]`.
@@ -86,10 +85,9 @@ enum Kind {
/**
* A class designed to respond to abuse reports.
*/
export class ReportManager extends EventEmitter {
export class ReportManager {
private displayManager: DisplayManager;
constructor(public draupnir: Draupnir) {
super();
this.displayManager = new DisplayManager(this);
}
@@ -117,7 +115,13 @@ export class ReportManager extends EventEmitter {
* @param reason A reason provided by the reporter.
*/
public async handleServerAbuseReport({ roomID, reporterId, event, reason }: { roomID: StringRoomID, reporterId: string, event: RoomEvent, reason?: string }) {
this.emit("report.new", { roomID: roomID, reporterId: reporterId, event: event, reason: reason });
this.draupnir.handleEventReport({
event_id: event.event_id,
room_id: roomID,
sender: reporterId as StringUserID,
event: event,
reason: reason
})
if (this.draupnir.config.displayReports) {
return this.displayManager.displayReportAndUI({ kind: Kind.SERVER_ABUSE_REPORT, event, reporterId, reason, moderationroomID: this.draupnir.managementRoomID });
}
+38 -20
View File
@@ -1,33 +1,51 @@
import { Mjolnir } from "../../src/Mjolnir";
import { Protection } from "../../src/protections/Protection";
import { MatrixClient } from "matrix-bot-sdk";
import { newTestUser } from "./clientHelper";
import { DraupnirTestContext } from "./mjolnirSetupUtils";
import { ActionResult, DEFAULT_CONSEQUENCE_PROVIDER, MatrixRoomReference, Ok, Protection, ProtectionDescription, StandardProtectionSettings, StringRoomID, findConsequenceProvider } from "matrix-protection-suite";
describe("Test: Report polling", function() {
let client;
let client: MatrixClient;
this.beforeEach(async function () {
client = await newTestUser(this.config.homeserverUrl, { name: { contains: "protection-settings" }});
})
it("Mjolnir correctly retrieves a report from synapse", async function() {
it("Mjolnir correctly retrieves a report from synapse", async function(this: DraupnirTestContext) {
this.timeout(40000);
let protectedRoomId = await this.mjolnir.client.createRoom({ invite: [await client.getUserId()] });
const draupnir = this.draupnir;
if (draupnir === undefined) {
throw new TypeError(`Test didn't setup properly`);
}
let protectedRoomId = await draupnir.client.createRoom({ invite: [await client.getUserId()] });
await client.joinRoom(protectedRoomId);
await this.mjolnir.addProtectedRoom(protectedRoomId);
await draupnir.protectedRoomsSet.protectedRoomsConfig.addRoom(MatrixRoomReference.fromRoomID(protectedRoomId as StringRoomID));
const eventId = await client.sendMessage(protectedRoomId, {msgtype: "m.text", body: "uwNd3q"});
await new Promise(async resolve => {
await this.mjolnir.protectionManager.registerProtection(new class extends Protection {
name = "jYvufI";
description = "A test protection";
settings = { };
handleEvent = async (mjolnir: Mjolnir, roomId: string, event: any) => { };
handleReport = async (mjolnir: Mjolnir, roomId: string, reporterId: string, event: any, reason?: string) => {
if (reason === "x5h1Je") {
resolve(null);
}
};
});
await this.mjolnir.protectionManager.enableProtection("jYvufI");
const testProtectionDescription: ProtectionDescription = {
name: "jYvufI",
description: "A test protection",
factory: function (description, consequenceProvider, protectedRoomsSet, context, settings): ActionResult<Protection> {
return Ok({
handleEventReport(report) {
if (report.reason === "x5h1Je") {
resolve(null);
}
return Promise.resolve(Ok(undefined));
},
description: testProtectionDescription,
requiredEventPermissions: [],
requiredPermissions: []
})
},
protectionSettings: new StandardProtectionSettings(
{},
{}
)
}
const defaultConsequenceProvider = findConsequenceProvider(DEFAULT_CONSEQUENCE_PROVIDER);
if (defaultConsequenceProvider === undefined) {
throw new TypeError(`Default consequence provider should be defined mate`);
}
await draupnir.protectedRoomsSet.protections.addProtection(testProtectionDescription, defaultConsequenceProvider, draupnir.protectedRoomsSet, draupnir);
await client.doRequest(
"POST",
`/_matrix/client/r0/rooms/${encodeURIComponent(protectedRoomId)}/report/${encodeURIComponent(eventId)}`, "", {
@@ -42,5 +60,5 @@ describe("Test: Report polling", function() {
// Ok, well apparently that needs a big refactor to change, but if you change the config before running this test,
// then you can ensure that report polling works. https://github.com/matrix-org/mjolnir/issues/326.
await new Promise(resolve => setTimeout(resolve, 1000));
});
} as unknown as Mocha.AsyncFunc);
});