@@ -25,19 +25,38 @@ export class MjolnirManager {
2525
2626 }
2727
28+ /**
29+ * Create the mjolnir manager from the datastore and the access control.
30+ * @param dataStore The data store interface that has the details for provisioned mjolnirs.
31+ * @param bridge The bridge abstraction that encapsulates details about the appservice.
32+ * @param accessControl Who has access to the bridge.
33+ * @returns A new mjolnir manager.
34+ */
2835 public static async makeMjolnirManager ( dataStore : DataStore , bridge : Bridge , accessControl : AccessControl ) : Promise < MjolnirManager > {
2936 const mjolnirManager = new MjolnirManager ( dataStore , bridge , accessControl ) ;
30- mjolnirManager . createMjolnirsFromDataStore ( ) ;
37+ await mjolnirManager . createMjolnirsFromDataStore ( ) ;
3138 return mjolnirManager ;
3239 }
3340
34- public getDefaultMjolnirConfig ( managementRoom : string ) : IConfig {
41+ /**
42+ * Gets the default config to give the newly provisioned mjolnirs.
43+ * @param managementRoomId A room that has been created to serve as the mjolnir's management room for the owner.
44+ * @returns A config that can be directly used by the new mjolnir.
45+ */
46+ public getDefaultMjolnirConfig ( managementRoomId : string ) : IConfig {
3547 let config = configRead ( ) ;
36- config . managementRoom = managementRoom ;
48+ config . managementRoom = managementRoomId ;
3749 config . protectedRooms = [ ] ;
3850 return config ;
3951 }
4052
53+ /**
54+ * Creates a new mjolnir for a user.
55+ * @param requestingUserId The user that is requesting this mjolnir and who will own it.
56+ * @param managementRoomId An existing matrix room to act as the management room.
57+ * @param client A client for the appservice virtual user that the new mjolnir should use.
58+ * @returns A new managed mjolnir.
59+ */
4160 public async makeInstance ( requestingUserId : string , managementRoomId : string , client : MatrixClient ) : Promise < ManagedMjolnir > {
4261 const managedMjolnir = new ManagedMjolnir (
4362 requestingUserId ,
@@ -47,6 +66,12 @@ export class MjolnirManager {
4766 return managedMjolnir ;
4867 }
4968
69+ /**
70+ * Gets a mjolnir for the corresponding mxid that is owned by a specific user.
71+ * @param mjolnirId The mxid of the mjolnir we are trying to get.
72+ * @param ownerId The owner of the mjolnir. We ask for it explicitly to not leak access to another user's mjolnir.
73+ * @returns The matching managed mjolnir instance.
74+ */
5075 public getMjolnir ( mjolnirId : string , ownerId : string ) : ManagedMjolnir | undefined {
5176 const mjolnir = this . mjolnirs . get ( mjolnirId ) ;
5277 if ( mjolnir ) {
@@ -60,6 +85,11 @@ export class MjolnirManager {
6085 }
6186 }
6287
88+ /**
89+ * Find all of the mjolnirs that are owned by this specific user.
90+ * @param ownerId An owner of multiple mjolnirs.
91+ * @returns Any mjolnirs that they own.
92+ */
6393 public getOwnedMjolnirs ( ownerId : string ) : ManagedMjolnir [ ] {
6494 // TODO we need to use the database for this but also provide the utility
6595 // for going from a MjolnirRecord to a ManagedMjolnir.
@@ -73,6 +103,11 @@ export class MjolnirManager {
73103 [ ...this . mjolnirs . values ( ) ] . forEach ( ( mj : ManagedMjolnir ) => mj . onEvent ( request ) ) ;
74104 }
75105
106+ /**
107+ * provision a new mjolnir for a matrix user.
108+ * @param requestingUserId The mxid of the user we are creating a mjolnir for.
109+ * @returns The matrix id of the new mjolnir and its management room.
110+ */
76111 public async provisionNewMjolnir ( requestingUserId : string ) : Promise < [ string , string ] > {
77112 const access = this . accessControl . getUserAccess ( requestingUserId ) ;
78113 if ( access . outcome !== Access . Allowed ) {
@@ -104,14 +139,22 @@ export class MjolnirManager {
104139 }
105140 }
106141
142+ /**
143+ * Utility that creates a matrix client for a virtual user on our homeserver with the specified loclapart.
144+ * @param localPart The localpart of the virtual user we need a client for.
145+ * @returns A tuple with the complete mxid of the virtual user and a MatrixClient.
146+ */
107147 private async makeMatrixClient ( localPart : string ) : Promise < [ string , MatrixClient ] > {
108148 // Now we need to make one of the transparent mjolnirs and add it to the monitor.
109149 const mjIntent = await this . bridge . getIntentFromLocalpart ( localPart ) ;
110150 await mjIntent . ensureRegistered ( ) ;
111151 return [ mjIntent . userId , mjIntent . matrixClient ] ;
112152 }
113153
114- // Still think that we should check each time a command is sent or something, rather than like this ...
154+ // TODO: We need to check that an owner still has access to the appservice each time they send a command to the mjolnir or use the web api.
155+ /**
156+ * Used at startup to create all the ManagedMjolnir instances and start them so that they will respond to users.
157+ */
115158 private async createMjolnirsFromDataStore ( ) {
116159 for ( const mjolnirRecord of await this . dataStore . list ( ) ) {
117160 const [ _mjolnirUserId , mjolnirClient ] = await this . makeMatrixClient ( mjolnirRecord . local_part ) ;
@@ -146,7 +189,7 @@ export class ManagedMjolnir {
146189 if ( mxEvent . type === 'm.room.message' ) {
147190 this . mjolnir . client . emit ( 'room.message' , mxEvent . room_id , mxEvent ) ;
148191 }
149- // room.join requires us to know the joined rooms before so lol .
192+ // TODO: We need to figure out how to inform the mjolnir of `room.join` .
150193 }
151194 if ( mxEvent [ 'type' ] === 'm.room.member' ) {
152195 if ( mxEvent [ 'content' ] [ 'membership' ] === 'invite' && mxEvent . state_key === await this . mjolnir . client . getUserId ( ) ) {
@@ -176,4 +219,4 @@ export class ManagedMjolnir {
176219 public get managementRoomId ( ) : string {
177220 return this . mjolnir . managementRoomId ;
178221 }
179- }
222+ }
0 commit comments