add (default false) option to enable report polling

This commit is contained in:
jesopo 2022-04-11 15:06:02 +00:00
parent e518541fd0
commit 126791908d
2 changed files with 17 additions and 11 deletions

View File

@ -99,7 +99,7 @@ export class Mjolnir {
private webapis: WebAPIs;
private protectedRoomActivityTracker: ProtectedRoomActivityTracker;
public taskQueue: ThrottlingQueue;
private reportPoll: ReportPoll;
private reportPoll: ReportPoll | undefined;
/**
* Adds a listener to the client that will automatically accept invitations.
* @param {MatrixClient} client
@ -260,7 +260,9 @@ export class Mjolnir {
const reportManager = new ReportManager(this);
reportManager.on("report.new", this.handleReport);
this.webapis = new WebAPIs(reportManager, this.ruleServer);
this.reportPoll = new ReportPoll(this, reportManager);
if (config.pollReports) {
this.reportPoll = new ReportPoll(this, reportManager);
}
// Setup join/leave listener
this.roomJoins = new RoomMemberManager(this.client);
this.taskQueue = new ThrottlingQueue(this, config.backgroundDelayMS);
@ -306,15 +308,17 @@ export class Mjolnir {
console.log("Starting web server");
await this.webapis.start();
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 */ }
if (this.reportPoll !== undefined) {
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 */ }
}
this.reportPoll.start(reportPollSetting.from);
}
this.reportPoll.start(reportPollSetting.from);
// Load the state.
this.currentState = STATE_CHECKING_PERMISSIONS;
@ -372,7 +376,7 @@ export class Mjolnir {
LogService.info("Mjolnir", "Stopping Mjolnir...");
this.client.stop();
this.webapis.stop();
this.reportPoll.stop();
this.reportPoll?.stop();
}
public async logMessage(level: LogLevel, module: string, message: string | any, additionalRoomIds: string[] | string | null = null, isRecursive = false): Promise<any> {

View File

@ -53,6 +53,7 @@ interface IConfig {
* of one background task and the start of the next one.
*/
backgroundDelayMS: number;
pollReports: boolean;
admin?: {
enableMakeRoomAdminCommand?: boolean;
}
@ -122,6 +123,7 @@ const defaultConfig: IConfig = {
automaticallyRedactForReasons: ["spam", "advertising"],
protectAllJoinedRooms: false,
backgroundDelayMS: 500,
pollReports: false,
commands: {
allowNoPrefix: false,
additionalPrefixes: [],