@@ -86,6 +86,7 @@ class ForstaBot {
86
86
}
87
87
fqLabel ( user ) { return `${ this . fqTag ( user ) } (${ this . fqName ( user ) } )` ; }
88
88
89
+
89
90
async onMessage ( ev ) {
90
91
const received = new Date ( ev . data . timestamp ) ;
91
92
const envelope = JSON . parse ( ev . data . message . body ) ;
@@ -197,7 +198,7 @@ class ForstaBot {
197
198
const auth = this . genAuthCode ( 1 ) ;
198
199
this . msgSender . send ( {
199
200
distribution : resolved ,
200
- threadId : await this . getSoloAuthThreadId ( ) ,
201
+ threadId : await this . getGroupAuthThreadId ( ) ,
201
202
text : `${ auth . code } is your authentication code, valid for one minute`
202
203
} ) ;
203
204
const pending = await relay . storage . get ( 'authentication' , 'pending' , { } ) ;
@@ -225,8 +226,79 @@ class ForstaBot {
225
226
delete pending [ userId ] ;
226
227
relay . storage . set ( 'authentication' , 'pending' , pending ) ;
227
228
229
+ await this . broadcastNotice ( 'has successfully authenticated as an administrator' , userId ) ;
228
230
return true ;
229
231
}
232
+
233
+ async getAdministrators ( ) {
234
+ const adminIds = await relay . storage . get ( 'authentication' , 'adminIds' , [ ] ) ;
235
+ const adminUsers = await this . getUsers ( adminIds ) ;
236
+ const admins = adminUsers . map ( u => {
237
+ return {
238
+ id : u . id ,
239
+ label : this . fqLabel ( u )
240
+ } ;
241
+ } ) ;
242
+ return admins ;
243
+ }
244
+
245
+ async broadcastNotice ( action , actorUserId ) {
246
+ const adminIds = await relay . storage . get ( 'authentication' , 'adminIds' , [ ] ) ;
247
+ let added = false ;
248
+ if ( ! adminIds . includes ( actorUserId ) ) {
249
+ adminIds . push ( actorUserId ) ;
250
+ added = true ;
251
+ }
252
+ const adminUsers = await this . getUsers ( adminIds ) ;
253
+ const actor = adminUsers . find ( u => u . id === actorUserId ) ;
254
+ const actorLabel = actor ? this . fqLabel ( actor ) : '<unknown>' ;
255
+ const expression = adminUsers . map ( u => this . fqTag ( u ) ) . join ( ' + ' ) ;
256
+ const distribution = await this . resolveTags ( expression ) ;
257
+
258
+ const adminList = adminUsers . filter ( u => ! ( added && u . id === actorUserId ) ) . map ( u => this . fqLabel ( u ) ) . join ( '\n' ) ;
259
+
260
+ const fullMessage = `Note: ${ actorLabel } ${ action } .\n\nCurrent administrators are:\n${ adminList } ` ;
261
+ const subbedFullMessage = fullMessage . replace ( / < < ( [ ^ > ] * ) > > / g, ( _ , id ) => {
262
+ const user = adminUsers . find ( x => x . id === id ) ;
263
+ return this . fqLabel ( user ) ;
264
+ } ) ;
265
+
266
+ this . msgSender . send ( {
267
+ distribution,
268
+ threadId : await this . getSoloAuthThreadId ( ) ,
269
+ text : subbedFullMessage
270
+ } ) ;
271
+ }
272
+
273
+ async addAdministrator ( { addTag, actorUserId} ) {
274
+ const tag = ( addTag && addTag [ 0 ] === '@' ) ? addTag : '@' + addTag ;
275
+ const resolved = await this . resolveTags ( tag ) ;
276
+ if ( resolved . userids . length === 1 && resolved . warnings . length === 0 ) {
277
+ const uid = resolved . userids [ 0 ] ;
278
+ const adminIds = await relay . storage . get ( 'authentication' , 'adminIds' ) ;
279
+ if ( ! adminIds . includes ( uid ) ) {
280
+ adminIds . push ( uid ) ;
281
+ await relay . storage . set ( 'authentication' , 'adminIds' , adminIds ) ;
282
+ }
283
+ await this . broadcastNotice ( `has added <<${ uid } >> to the administrator list` , actorUserId ) ;
284
+ return this . getAdministrators ( ) ;
285
+ }
286
+ throw { statusCode : 400 , info : { tag : [ 'not a recognized tag, please try again' ] } } ;
287
+ }
288
+
289
+ async removeAdministrator ( { removeId, actorUserId} ) {
290
+ const adminIds = await relay . storage . get ( 'authentication' , 'adminIds' , [ ] ) ;
291
+ const idx = adminIds . indexOf ( removeId ) ;
292
+
293
+ if ( idx < 0 ) {
294
+ throw { statusCode : 400 , info : { id : [ 'administrator id not found' ] } } ;
295
+ }
296
+ adminIds . splice ( idx , 1 ) ;
297
+ await this . broadcastNotice ( `is removing <<${ removeId } >> from the administrator list` , actorUserId ) ;
298
+ await relay . storage . set ( 'authentication' , 'adminIds' , adminIds ) ;
299
+
300
+ return this . getAdministrators ( ) ;
301
+ }
230
302
}
231
303
232
304
module . exports = ForstaBot ;
0 commit comments