@@ -18,6 +18,7 @@ class App extends MatrixPuppetBridgeBase {
18
18
this . slackPrefix = 'slack' ;
19
19
this . servicePrefix = `${ this . slackPrefix } _${ this . teamName } ` ;
20
20
this . notifyToSlack = notify ;
21
+ this . matrixRoomStatus = { } ;
21
22
}
22
23
getServiceName ( ) {
23
24
return "Slack" ;
@@ -136,6 +137,13 @@ class App extends MatrixPuppetBridgeBase {
136
137
user : data . user ,
137
138
} ) ;
138
139
} ) ;
140
+ this . client . on ( 'rename' , ( data ) => {
141
+ console . log ( data ) ;
142
+ // rename channel
143
+ this . renameChannelEvent ( {
144
+ channel : data . channel ,
145
+ } ) ;
146
+ } ) ;
139
147
debug ( 'registered message listener' ) ;
140
148
}
141
149
getPayload ( data ) {
@@ -374,6 +382,30 @@ class App extends MatrixPuppetBridgeBase {
374
382
} ) ;
375
383
} ) ;
376
384
}
385
+
386
+ async _renameChannelEvent ( matrixRoomId , name ) {
387
+ const botIntent = this . getIntentFromApplicationServerBot ( ) ;
388
+ const ret = await botIntent . setRoomName ( matrixRoomId , name ) ;
389
+ this . updateRoomStatesCache ( matrixRoomId , 'name' , name ) ;
390
+ return ret ;
391
+ }
392
+
393
+ async renameChannelEvent ( data ) {
394
+ const payload = this . getPayload ( data ) ;
395
+ const roomAlias = this . getRoomAliasFromThirdPartyRoomId ( payload . roomId ) ;
396
+ try {
397
+ const room = await this . puppet . getClient ( ) . getRoomIdForAlias ( roomAlias ) ;
398
+ return this . _renameChannelEvent ( room . room_id , data . name ) ;
399
+ } catch ( err ) {
400
+ console . error ( err ) ;
401
+ this . sendStatusMsg ( {
402
+ fixedWidthOutput : true ,
403
+ roomAliasLocalPart : `${ this . slackPrefix } _${ this . getStatusRoomPostfix ( ) } `
404
+ } , err . stack ) . catch ( ( err ) => {
405
+ console . error ( err ) ;
406
+ } ) ;
407
+ }
408
+ }
377
409
getThirdPartyRoomDataById ( id ) {
378
410
const directName = ( user ) => this . client . getUserById ( user ) . name ;
379
411
const directTopic = ( ) => `Slack Direct Message (Team: ${ this . teamName } )`
@@ -424,6 +456,44 @@ class App extends MatrixPuppetBridgeBase {
424
456
const filename = this . tagMatrixMessage ( data . filename ) ;
425
457
return this . client . sendFileMessage ( data . url , data . text , filename , id ) ;
426
458
}
459
+
460
+ async getRoomState ( matrixRoomId , type ) {
461
+ // prevent refetch from matrix server after first fetching
462
+ const cache = this . matrixRoomStatus [ matrixRoomId ] || { } ;
463
+ if ( cache [ type ] ) {
464
+ return cache [ type ] ;
465
+ }
466
+ const puppetClient = this . puppet . getClient ( ) ;
467
+ switch ( type ) {
468
+ case 'name' :
469
+ const roomName = await puppetClient . getStateEvent ( matrixRoomId , 'm.room.name' ) ;
470
+ if ( roomName && roomName . name ) {
471
+ this . updateRoomStatesCache ( matrixRoomId , 'name' , roomName . name ) ;
472
+ }
473
+ return roomName . name ;
474
+ // TODO
475
+ }
476
+ }
477
+
478
+ updateRoomStatesCache ( matrixRoomId , type , data ) {
479
+ const roomStatus = this . matrixRoomStatus [ matrixRoomId ] = this . matrixRoomStatus [ matrixRoomId ] || { } ;
480
+ roomStatus [ type ] = data ;
481
+ }
482
+
483
+ // HACK: recheck the old name when after getOrCreateMatrixRoomFromThirdPartyRoomId
484
+ // if room has old name, forcefully update new name after bind
485
+ async getOrCreateMatrixRoomFromThirdPartyRoomId ( thirdPartyRoomId ) {
486
+ const matrixRoomId = await super . getOrCreateMatrixRoomFromThirdPartyRoomId ( thirdPartyRoomId ) ;
487
+ const name = await this . getRoomState ( matrixRoomId , 'name' ) ;
488
+ const chan = this . client . getChannelById ( thirdPartyRoomId ) || { } ;
489
+ if ( ! chan . name ) {
490
+ return matrixRoomId ;
491
+ }
492
+ if ( name !== chan . name ) {
493
+ await this . _renameChannelEvent ( matrixRoomId , chan . name ) ;
494
+ }
495
+ return matrixRoomId ;
496
+ }
427
497
}
428
498
429
499
module . exports = App ;
0 commit comments