diff --git a/src/Mjolnir.ts b/src/Mjolnir.ts index a2224a2..b3108f9 100644 --- a/src/Mjolnir.ts +++ b/src/Mjolnir.ts @@ -43,7 +43,7 @@ import { Healthz } from "./health/healthz"; import { EventRedactionQueue, RedactUserInRoom } from "./queues/EventRedactionQueue"; import { htmlEscape } from "./utils"; import { ReportManager } from "./report/ReportManager"; -import { ReportPoll } from "./report/ReportPoll"; +import { ReportPoller } from "./report/ReportPoller"; import { WebAPIs } from "./webapis/WebAPIs"; import { replaceRoomIdsWithPills } from "./utils"; import RuleServer from "./models/RuleServer"; @@ -102,7 +102,7 @@ export class Mjolnir { /* * Config-enabled polling of reports in Synapse, so Mjolnir can react to reports */ - private reportPoll: ReportPoll | undefined; + private reportPoller?: ReportPoller; /** * Adds a listener to the client that will automatically accept invitations. * @param {MatrixClient} client @@ -264,7 +264,7 @@ export class Mjolnir { reportManager.on("report.new", this.handleReport); this.webapis = new WebAPIs(reportManager, this.ruleServer); if (config.pollReports) { - this.reportPoll = new ReportPoll(this, reportManager); + this.reportPoller = new ReportPoller(this, reportManager); } // Setup join/leave listener this.roomJoins = new RoomMemberManager(this.client); @@ -311,16 +311,18 @@ export class Mjolnir { console.log("Starting web server"); await this.webapis.start(); - if (this.reportPoll !== undefined) { + if (this.reportPoller) { let reportPollSetting: { from: number } = { from: 0 }; try { reportPollSetting = await this.client.getAccountData(REPORT_POLL_EVENT_TYPE); } catch (err) { if (err.body?.errcode !== "M_NOT_FOUND") { throw err; - } else { /* setting probably doesn't exist yet */ } + } else { + this.logMessage(LogLevel.INFO, "Mjolnir@startup", "report poll setting does not exist yet"); + } } - this.reportPoll.start(reportPollSetting.from); + this.reportPoller.start(reportPollSetting.from); } // Load the state. @@ -379,7 +381,7 @@ export class Mjolnir { LogService.info("Mjolnir", "Stopping Mjolnir..."); this.client.stop(); this.webapis.stop(); - this.reportPoll?.stop(); + this.reportPoller?.stop(); } public async logMessage(level: LogLevel, module: string, message: string | any, additionalRoomIds: string[] | string | null = null, isRecursive = false): Promise { diff --git a/src/report/ReportPoll.ts b/src/report/ReportPoller.ts similarity index 95% rename from src/report/ReportPoll.ts rename to src/report/ReportPoller.ts index 6a79129..e0c8cc8 100644 --- a/src/report/ReportPoll.ts +++ b/src/report/ReportPoller.ts @@ -20,7 +20,7 @@ import { LogLevel } from "matrix-bot-sdk"; class InvalidStateError extends Error {} -export class ReportPoll { +export class ReportPoller { /* * https://matrix-org.github.io/synapse/latest/admin_api/event_reports.html * "from" is an opaque token that is returned from the API to paginate reports @@ -28,12 +28,11 @@ export class ReportPoll { private from = 0; private timeout: ReturnType | null = null; - /* + /** * A class to poll synapse's report endpoint, so we can act on new reports * - * @param client The Matrix client underpinning the running Mjolnir + * @param mjolnir The running Mjolnir instance * @param manager The report manager in to which we feed new reports - * @param save An abstract function to persist where we got to in report reading */ constructor( private mjolnir: Mjolnir, @@ -63,7 +62,7 @@ export class ReportPoll { let response_: { event_reports: { room_id: string, event_id: string, sender: string, reason: string }[], next_token: number | undefined - } | undefined = undefined; + } | undefined; try { response_ = await this.mjolnir.client.doRequest( "GET", diff --git a/test/integration/reportPollingTest.ts b/test/integration/reportPollingTest.ts index d6cec5d..2debd72 100644 --- a/test/integration/reportPollingTest.ts +++ b/test/integration/reportPollingTest.ts @@ -18,7 +18,7 @@ describe("Test: Report polling", function() { this.afterEach(async function () { await client.stop(); }) - it("Mjolnir correctly retreives a report from synapse", async function() { + it("Mjolnir correctly retrieves a report from synapse", async function() { this.timeout(20000); const reportPromise = new Promise(async (resolve, reject) => { @@ -27,7 +27,9 @@ describe("Test: Report polling", function() { description = "A test protection"; settings = { }; handleReport = (mjolnir: Mjolnir, roomId: string, reporterId: string, event: any, reason?: string) => { - resolve(null); + if (reason === "x5h1Je") { + resolve(null); + } }; }); }); @@ -37,11 +39,11 @@ describe("Test: Report polling", function() { await this.mjolnir.client.inviteUser(await client.getUserId(), roomId); await client.joinRoom(roomId); - const eventId = await badUser.sendMesosage(roomId, {msgtype: "m.text", body: "uwNd3q"}); + const eventId = await badUser.sendMessaage(roomId, {msgtype: "m.text", body: "uwNd3q"}); await client.doRequest( "POST", `/_matrix/client/r0/rooms/${encodeURIComponent(roomId)}/report/${encodeURIComponent(eventId)}`, "", { - reason: "dont like it :(" + reason: "x5h1Je" } );