@@ -18,7 +18,6 @@ import {
1818 extractRequestError ,
1919 LogLevel ,
2020 LogService ,
21- MatrixClient ,
2221 MembershipEvent ,
2322 Permalinks ,
2423} from "matrix-bot-sdk" ;
@@ -39,6 +38,7 @@ import ManagementRoomOutput from "./ManagementRoomOutput";
3938import { ProtectionManager } from "./protections/ProtectionManager" ;
4039import { RoomMemberManager } from "./RoomMembers" ;
4140import ProtectedRoomsConfig from "./ProtectedRoomsConfig" ;
41+ import { MatrixEmitter , MatrixSendClient } from "./MatrixEmitter" ;
4242
4343export const STATE_NOT_STARTED = "not_started" ;
4444export const STATE_CHECKING_PERMISSIONS = "checking_permissions" ;
@@ -88,15 +88,15 @@ export class Mjolnir {
8888
8989 /**
9090 * Adds a listener to the client that will automatically accept invitations.
91- * @param {MatrixClient } client
91+ * @param {MatrixSendClient } client
9292 * @param options By default accepts invites from anyone.
9393 * @param {string } options.managementRoom The room to report ignored invitations to if `recordIgnoredInvites` is true.
9494 * @param {boolean } options.recordIgnoredInvites Whether to report invites that will be ignored to the `managementRoom`.
9595 * @param {boolean } options.autojoinOnlyIfManager Whether to only accept an invitation by a user present in the `managementRoom`.
9696 * @param {string } options.acceptInvitesFromSpace A space of users to accept invites from, ignores invites form users not in this space.
9797 */
98- private static addJoinOnInviteListener ( mjolnir : Mjolnir , client : MatrixClient , options : { [ key : string ] : any } ) {
99- client . on ( "room.invite" , async ( roomId : string , inviteEvent : any ) => {
98+ private static addJoinOnInviteListener ( mjolnir : Mjolnir , client : MatrixSendClient , options : { [ key : string ] : any } ) {
99+ mjolnir . matrixEmitter . on ( "room.invite" , async ( roomId : string , inviteEvent : any ) => {
100100 const membershipEvent = new MembershipEvent ( inviteEvent ) ;
101101
102102 const reportInvite = async ( ) => {
@@ -130,17 +130,16 @@ export class Mjolnir {
130130 } ) ;
131131 if ( ! spaceUserIds . includes ( membershipEvent . sender ) ) return reportInvite ( ) ; // ignore invite
132132 }
133-
134133 return client . joinRoom ( roomId ) ;
135134 } ) ;
136135 }
137136
138137 /**
139138 * Create a new Mjolnir instance from a client and the options in the configuration file, ready to be started.
140- * @param {MatrixClient } client The client for Mjolnir to use.
139+ * @param {MatrixSendClient } client The client for Mjolnir to use.
141140 * @returns A new Mjolnir instance that can be started without further setup.
142141 */
143- static async setupMjolnirFromConfig ( client : MatrixClient , config : IConfig ) : Promise < Mjolnir > {
142+ static async setupMjolnirFromConfig ( client : MatrixSendClient , matrixEmitter : MatrixEmitter , config : IConfig ) : Promise < Mjolnir > {
144143 const policyLists : PolicyList [ ] = [ ] ;
145144 const joinedRooms = await client . getJoinedRooms ( ) ;
146145
@@ -152,15 +151,16 @@ export class Mjolnir {
152151 }
153152
154153 const ruleServer = config . web . ruleServer ? new RuleServer ( ) : null ;
155- const mjolnir = new Mjolnir ( client , await client . getUserId ( ) , managementRoomId , config , policyLists , ruleServer ) ;
154+ const mjolnir = new Mjolnir ( client , await client . getUserId ( ) , matrixEmitter , managementRoomId , config , policyLists , ruleServer ) ;
156155 await mjolnir . managementRoomOutput . logMessage ( LogLevel . INFO , "index" , "Mjolnir is starting up. Use !mjolnir to query status." ) ;
157156 Mjolnir . addJoinOnInviteListener ( mjolnir , client , config ) ;
158157 return mjolnir ;
159158 }
160159
161160 constructor (
162- public readonly client : MatrixClient ,
161+ public readonly client : MatrixSendClient ,
163162 private readonly clientUserId : string ,
163+ public readonly matrixEmitter : MatrixEmitter ,
164164 public readonly managementRoomId : string ,
165165 public readonly config : IConfig ,
166166 private policyLists : PolicyList [ ] ,
@@ -171,9 +171,9 @@ export class Mjolnir {
171171
172172 // Setup bot.
173173
174- client . on ( "room.event" , this . handleEvent . bind ( this ) ) ;
174+ matrixEmitter . on ( "room.event" , this . handleEvent . bind ( this ) ) ;
175175
176- client . on ( "room.message" , async ( roomId , event ) => {
176+ matrixEmitter . on ( "room.message" , async ( roomId , event ) => {
177177 if ( roomId !== this . managementRoomId ) return ;
178178 if ( ! event [ 'content' ] ) return ;
179179
@@ -208,11 +208,11 @@ export class Mjolnir {
208208 }
209209 } ) ;
210210
211- client . on ( "room.join" , ( roomId : string , event : any ) => {
211+ matrixEmitter . on ( "room.join" , ( roomId : string , event : any ) => {
212212 LogService . info ( "Mjolnir" , `Joined ${ roomId } ` ) ;
213213 return this . resyncJoinedRooms ( ) ;
214214 } ) ;
215- client . on ( "room.leave" , ( roomId : string , event : any ) => {
215+ matrixEmitter . on ( "room.leave" , ( roomId : string , event : any ) => {
216216 LogService . info ( "Mjolnir" , `Left ${ roomId } ` ) ;
217217 return this . resyncJoinedRooms ( ) ;
218218 } ) ;
@@ -234,7 +234,7 @@ export class Mjolnir {
234234 this . reportPoller = new ReportPoller ( this , this . reportManager ) ;
235235 }
236236 // Setup join/leave listener
237- this . roomJoins = new RoomMemberManager ( this . client ) ;
237+ this . roomJoins = new RoomMemberManager ( this . matrixEmitter ) ;
238238 this . taskQueue = new ThrottlingQueue ( this , config . backgroundDelayMS ) ;
239239
240240 this . protectionManager = new ProtectionManager ( this ) ;
@@ -303,7 +303,7 @@ export class Mjolnir {
303303 }
304304
305305 // Start the bot.
306- await this . client . start ( ) ;
306+ await this . matrixEmitter . start ( ) ;
307307
308308 this . currentState = STATE_SYNCING ;
309309 if ( this . config . syncOnStartup ) {
@@ -331,7 +331,7 @@ export class Mjolnir {
331331 */
332332 public stop ( ) {
333333 LogService . info ( "Mjolnir" , "Stopping Mjolnir..." ) ;
334- this . client . stop ( ) ;
334+ this . matrixEmitter . stop ( ) ;
335335 this . webapis . stop ( ) ;
336336 this . reportPoller ?. stop ( ) ;
337337 }
0 commit comments