@@ -32,14 +32,12 @@ import { applyServerAcls } from "./actions/ApplyAcl";
3232import { RoomUpdateError } from "./models/RoomUpdateError" ;
3333import { COMMAND_PREFIX , handleCommand } from "./commands/CommandHandler" ;
3434import { applyUserBans } from "./actions/ApplyBan" ;
35- import config from "./config" ;
3635import ErrorCache , { ERROR_KIND_FATAL , ERROR_KIND_PERMISSION } from "./ErrorCache" ;
3736import { Protection } from "./protections/IProtection" ;
3837import { PROTECTIONS } from "./protections/protections" ;
3938import { ConsequenceType , Consequence } from "./protections/consequence" ;
4039import { ProtectionSettingValidationError } from "./protections/ProtectionSettings" ;
4140import { UnlistedUserRedactionQueue } from "./queues/UnlistedUserRedactionQueue" ;
42- import { Healthz } from "./health/healthz" ;
4341import { EventRedactionQueue , RedactUserInRoom } from "./queues/EventRedactionQueue" ;
4442import { htmlEscape } from "./utils" ;
4543import { ReportManager } from "./report/ReportManager" ;
@@ -50,6 +48,7 @@ import RuleServer from "./models/RuleServer";
5048import { RoomMemberManager } from "./RoomMembers" ;
5149import { ProtectedRoomActivityTracker } from "./queues/ProtectedRoomActivityTracker" ;
5250import { ThrottlingQueue } from "./queues/ThrottlingQueue" ;
51+ import { IConfig } from "./config" ;
5352import PolicyList , { ListRuleChange } from "./models/PolicyList" ;
5453
5554const levelToFn = {
@@ -162,7 +161,7 @@ export class Mjolnir {
162161 * @param {MatrixClient } client The client for Mjolnir to use.
163162 * @returns A new Mjolnir instance that can be started without further setup.
164163 */
165- static async setupMjolnirFromConfig ( client : MatrixClient ) : Promise < Mjolnir > {
164+ static async setupMjolnirFromConfig ( client : MatrixClient , config : IConfig ) : Promise < Mjolnir > {
166165 const policyLists : PolicyList [ ] = [ ] ;
167166 const protectedRooms : { [ roomId : string ] : string } = { } ;
168167 const joinedRooms = await client . getJoinedRooms ( ) ;
@@ -188,7 +187,7 @@ export class Mjolnir {
188187 }
189188
190189 const ruleServer = config . web . ruleServer ? new RuleServer ( ) : null ;
191- const mjolnir = new Mjolnir ( client , managementRoomId , protectedRooms , policyLists , ruleServer ) ;
190+ const mjolnir = new Mjolnir ( client , managementRoomId , config , protectedRooms , policyLists , ruleServer ) ;
192191 await mjolnir . logMessage ( LogLevel . INFO , "index" , "Mjolnir is starting up. Use !mjolnir to query status." ) ;
193192 Mjolnir . addJoinOnInviteListener ( mjolnir , client , config ) ;
194193 return mjolnir ;
@@ -197,6 +196,7 @@ export class Mjolnir {
197196 constructor (
198197 public readonly client : MatrixClient ,
199198 public readonly managementRoomId : string ,
199+ public readonly config : IConfig ,
200200 /*
201201 * All the rooms that Mjolnir is protecting and their permalinks.
202202 * If `config.protectAllJoinedRooms` is specified, then `protectedRooms` will be all joined rooms except watched banlists that we can't protect (because they aren't curated by us).
@@ -208,7 +208,7 @@ export class Mjolnir {
208208 ) {
209209 this . explicitlyProtectedRoomIds = Object . keys ( this . protectedRooms ) ;
210210
211- for ( const reason of config . automaticallyRedactForReasons ) {
211+ for ( const reason of this . config . automaticallyRedactForReasons ) {
212212 this . automaticRedactionReasons . push ( new MatrixGlob ( reason . toLowerCase ( ) ) ) ;
213213 }
214214
@@ -276,7 +276,7 @@ export class Mjolnir {
276276 console . log ( "Creating Web APIs" ) ;
277277 const reportManager = new ReportManager ( this ) ;
278278 reportManager . on ( "report.new" , this . handleReport . bind ( this ) ) ;
279- this . webapis = new WebAPIs ( reportManager , this . ruleServer ) ;
279+ this . webapis = new WebAPIs ( reportManager , this . config , this . ruleServer ) ;
280280 if ( config . pollReports ) {
281281 this . reportPoller = new ReportPoller ( this , reportManager ) ;
282282 }
@@ -356,20 +356,19 @@ export class Mjolnir {
356356 await this . buildWatchedPolicyLists ( ) ;
357357 this . applyUnprotectedRooms ( ) ;
358358
359- if ( config . verifyPermissionsOnStartup ) {
359+ if ( this . config . verifyPermissionsOnStartup ) {
360360 await this . logMessage ( LogLevel . INFO , "Mjolnir@startup" , "Checking permissions..." ) ;
361- await this . verifyPermissions ( config . verboseLogging ) ;
361+ await this . verifyPermissions ( this . config . verboseLogging ) ;
362362 }
363363
364364 this . currentState = STATE_SYNCING ;
365- if ( config . syncOnStartup ) {
365+ if ( this . config . syncOnStartup ) {
366366 await this . logMessage ( LogLevel . INFO , "Mjolnir@startup" , "Syncing lists..." ) ;
367- await this . syncLists ( config . verboseLogging ) ;
367+ await this . syncLists ( this . config . verboseLogging ) ;
368368 await this . registerProtections ( ) ;
369369 }
370370
371371 this . currentState = STATE_RUNNING ;
372- Healthz . isHealthy = true ;
373372 await this . logMessage ( LogLevel . INFO , "Mjolnir@startup" , "Startup complete. Now monitoring rooms." ) ;
374373 } catch ( err ) {
375374 try {
@@ -399,14 +398,13 @@ export class Mjolnir {
399398 if ( ! additionalRoomIds ) additionalRoomIds = [ ] ;
400399 if ( ! Array . isArray ( additionalRoomIds ) ) additionalRoomIds = [ additionalRoomIds ] ;
401400
402- if ( config . RUNTIME . client && ( config . verboseLogging || LogLevel . INFO . includes ( level ) ) ) {
401+ if ( this . config . verboseLogging || LogLevel . INFO . includes ( level ) ) {
403402 let clientMessage = message ;
404403 if ( level === LogLevel . WARN ) clientMessage = `⚠ | ${ message } ` ;
405404 if ( level === LogLevel . ERROR ) clientMessage = `‼ | ${ message } ` ;
406405
407- const client = config . RUNTIME . client ;
408- const managementRoomId = await client . resolveRoom ( config . managementRoom ) ;
409- const roomIds = [ managementRoomId , ...additionalRoomIds ] ;
406+ const client = this . client ;
407+ const roomIds = [ this . managementRoomId , ...additionalRoomIds ] ;
410408
411409 let evContent : TextualMessageEventContent = {
412410 body : message ,
@@ -418,7 +416,7 @@ export class Mjolnir {
418416 evContent = await replaceRoomIdsWithPills ( this , clientMessage , new Set ( roomIds ) , "m.notice" ) ;
419417 }
420418
421- await client . sendMessage ( managementRoomId , evContent ) ;
419+ await client . sendMessage ( this . managementRoomId , evContent ) ;
422420 }
423421
424422 levelToFn [ level . toString ( ) ] ( module , message ) ;
@@ -443,7 +441,7 @@ export class Mjolnir {
443441 const rooms = ( additionalProtectedRooms ?. rooms ?? [ ] ) ;
444442 rooms . push ( roomId ) ;
445443 await this . client . setAccountData ( PROTECTED_ROOMS_EVENT_TYPE , { rooms : rooms } ) ;
446- await this . syncLists ( config . verboseLogging ) ;
444+ await this . syncLists ( this . config . verboseLogging ) ;
447445 }
448446
449447 public async removeProtectedRoom ( roomId : string ) {
@@ -465,7 +463,7 @@ export class Mjolnir {
465463 }
466464
467465 private async resyncJoinedRooms ( withSync = true ) {
468- if ( ! config . protectAllJoinedRooms ) return ;
466+ if ( ! this . config . protectAllJoinedRooms ) return ;
469467
470468 const joinedRoomIds = ( await this . client . getJoinedRooms ( ) ) . filter ( r => r !== this . managementRoomId ) ;
471469 const oldRoomIdsSet = new Set ( this . protectedJoinedRoomIds ) ;
@@ -491,7 +489,7 @@ export class Mjolnir {
491489 this . applyUnprotectedRooms ( ) ;
492490
493491 if ( withSync ) {
494- await this . syncLists ( config . verboseLogging ) ;
492+ await this . syncLists ( this . config . verboseLogging ) ;
495493 }
496494 }
497495
@@ -718,7 +716,7 @@ export class Mjolnir {
718716 }
719717
720718 public async warnAboutUnprotectedPolicyListRoom ( roomId : string ) {
721- if ( ! config . protectAllJoinedRooms ) return ; // doesn't matter
719+ if ( ! this . config . protectAllJoinedRooms ) return ; // doesn't matter
722720 if ( this . explicitlyProtectedRoomIds . includes ( roomId ) ) return ; // explicitly protected
723721
724722 const createEvent = new CreateEvent ( await this . client . getRoomStateEvent ( roomId , "m.room.create" , "" ) ) ;
0 commit comments