review feedback

This commit is contained in:
jesopo 2022-06-27 13:04:20 +00:00
parent e0ed4fb77e
commit bcf0f127d1
3 changed files with 19 additions and 16 deletions

View File

@ -43,7 +43,7 @@ import { Healthz } from "./health/healthz";
import { EventRedactionQueue, RedactUserInRoom } from "./queues/EventRedactionQueue"; import { EventRedactionQueue, RedactUserInRoom } from "./queues/EventRedactionQueue";
import { htmlEscape } from "./utils"; import { htmlEscape } from "./utils";
import { ReportManager } from "./report/ReportManager"; import { ReportManager } from "./report/ReportManager";
import { ReportPoll } from "./report/ReportPoll"; import { ReportPoller } from "./report/ReportPoller";
import { WebAPIs } from "./webapis/WebAPIs"; import { WebAPIs } from "./webapis/WebAPIs";
import { replaceRoomIdsWithPills } from "./utils"; import { replaceRoomIdsWithPills } from "./utils";
import RuleServer from "./models/RuleServer"; 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 * 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. * Adds a listener to the client that will automatically accept invitations.
* @param {MatrixClient} client * @param {MatrixClient} client
@ -264,7 +264,7 @@ export class Mjolnir {
reportManager.on("report.new", this.handleReport); reportManager.on("report.new", this.handleReport);
this.webapis = new WebAPIs(reportManager, this.ruleServer); this.webapis = new WebAPIs(reportManager, this.ruleServer);
if (config.pollReports) { if (config.pollReports) {
this.reportPoll = new ReportPoll(this, reportManager); this.reportPoller = new ReportPoller(this, reportManager);
} }
// Setup join/leave listener // Setup join/leave listener
this.roomJoins = new RoomMemberManager(this.client); this.roomJoins = new RoomMemberManager(this.client);
@ -311,16 +311,18 @@ export class Mjolnir {
console.log("Starting web server"); console.log("Starting web server");
await this.webapis.start(); await this.webapis.start();
if (this.reportPoll !== undefined) { if (this.reportPoller) {
let reportPollSetting: { from: number } = { from: 0 }; let reportPollSetting: { from: number } = { from: 0 };
try { try {
reportPollSetting = await this.client.getAccountData(REPORT_POLL_EVENT_TYPE); reportPollSetting = await this.client.getAccountData(REPORT_POLL_EVENT_TYPE);
} catch (err) { } catch (err) {
if (err.body?.errcode !== "M_NOT_FOUND") { if (err.body?.errcode !== "M_NOT_FOUND") {
throw err; 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. // Load the state.
@ -379,7 +381,7 @@ export class Mjolnir {
LogService.info("Mjolnir", "Stopping Mjolnir..."); LogService.info("Mjolnir", "Stopping Mjolnir...");
this.client.stop(); this.client.stop();
this.webapis.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<any> { public async logMessage(level: LogLevel, module: string, message: string | any, additionalRoomIds: string[] | string | null = null, isRecursive = false): Promise<any> {

View File

@ -20,7 +20,7 @@ import { LogLevel } from "matrix-bot-sdk";
class InvalidStateError extends Error {} class InvalidStateError extends Error {}
export class ReportPoll { export class ReportPoller {
/* /*
* https://matrix-org.github.io/synapse/latest/admin_api/event_reports.html * 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 * "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 from = 0;
private timeout: ReturnType<typeof setTimeout> | null = null; private timeout: ReturnType<typeof setTimeout> | null = null;
/* /**
* A class to poll synapse's report endpoint, so we can act on new reports * 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 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( constructor(
private mjolnir: Mjolnir, private mjolnir: Mjolnir,
@ -63,7 +62,7 @@ export class ReportPoll {
let response_: { let response_: {
event_reports: { room_id: string, event_id: string, sender: string, reason: string }[], event_reports: { room_id: string, event_id: string, sender: string, reason: string }[],
next_token: number | undefined next_token: number | undefined
} | undefined = undefined; } | undefined;
try { try {
response_ = await this.mjolnir.client.doRequest( response_ = await this.mjolnir.client.doRequest(
"GET", "GET",

View File

@ -18,7 +18,7 @@ describe("Test: Report polling", function() {
this.afterEach(async function () { this.afterEach(async function () {
await client.stop(); 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); this.timeout(20000);
const reportPromise = new Promise(async (resolve, reject) => { const reportPromise = new Promise(async (resolve, reject) => {
@ -27,7 +27,9 @@ describe("Test: Report polling", function() {
description = "A test protection"; description = "A test protection";
settings = { }; settings = { };
handleReport = (mjolnir: Mjolnir, roomId: string, reporterId: string, event: any, reason?: string) => { 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 this.mjolnir.client.inviteUser(await client.getUserId(), roomId);
await client.joinRoom(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( await client.doRequest(
"POST", "POST",
`/_matrix/client/r0/rooms/${encodeURIComponent(roomId)}/report/${encodeURIComponent(eventId)}`, "", { `/_matrix/client/r0/rooms/${encodeURIComponent(roomId)}/report/${encodeURIComponent(eventId)}`, "", {
reason: "dont like it :(" reason: "x5h1Je"
} }
); );