@@ -670,7 +670,7 @@ export class Appservice extends EventEmitter {
670
670
671
671
private async decryptAppserivceEvent ( roomId : string , encrypted : EncryptedRoomEvent ) : ReturnType < Appservice [ "processEvent" ] > {
672
672
const existingClient = this . cryptoClientForRoomId . get ( roomId ) ;
673
- const decryptFn = async ( client ) => {
673
+ const decryptFn = async ( client : MatrixClient ) => {
674
674
let event = ( await client . crypto . decryptRoomEvent ( encrypted , roomId ) ) . raw ;
675
675
event = await this . processEvent ( event ) ;
676
676
this . cryptoClientForRoomId . set ( roomId , client ) ;
@@ -682,16 +682,18 @@ export class Appservice extends EventEmitter {
682
682
if ( existingClient ) {
683
683
try {
684
684
return await decryptFn ( existingClient ) ;
685
- } catch ( existingClientError ) {
685
+ } catch ( error ) {
686
+ LogService . debug ( "Appservice" , `Failed to decrypt via cached client ${ await existingClient . getUserId ( ) } ` , error ) ;
686
687
LogService . warn ( "Appservice" , `Cached client was not able to decrypt ${ roomId } ${ encrypted . eventId } - trying other intents` ) ;
687
688
}
688
689
}
689
690
this . cryptoClientForRoomId . delete ( roomId ) ;
690
691
// 2. Try the bot client
691
692
if ( this . botClient . crypto ?. isReady ) {
692
693
try {
693
- return await decryptFn ( existingClient ) ;
694
- } catch ( ex ) {
694
+ return await decryptFn ( this . botClient ) ;
695
+ } catch ( error ) {
696
+ LogService . debug ( "Appservice" , `Failed to decrypt via bot client` , error ) ;
695
697
LogService . warn ( "Appservice" , `Bot client was not able to decrypt ${ roomId } ${ encrypted . eventId } - trying other intents` ) ;
696
698
}
697
699
}
@@ -709,19 +711,24 @@ export class Appservice extends EventEmitter {
709
711
continue ;
710
712
}
711
713
try {
712
- return await decryptFn ( existingClient ) ;
713
- } catch ( existingClientError ) {
714
+ return await decryptFn ( intent . underlyingClient ) ;
715
+ } catch ( error ) {
716
+ LogService . debug ( "Appservice" , `Failed to decrypt via ${ userId } ` , error ) ;
714
717
LogService . warn ( "Appservice" , `Existing encrypted client was not able to decrypt ${ roomId } ${ encrypted . eventId } - trying other intents` ) ;
715
718
}
716
719
}
717
720
718
721
// 4. Try to enable crypto on any client to decrypt it.
719
722
const userInRoom = this . intentsCache . find ( ( _intent , userId ) => userIdsInRoom . includes ( userId ) ) ;
723
+ if ( ! userInRoom ) {
724
+ throw Error ( 'No users in room, cannot decrypt' ) ;
725
+ }
720
726
await userInRoom . enableEncryption ( ) ;
721
727
try {
722
- return await decryptFn ( existingClient ) ;
723
- } catch ( existingClientError ) {
724
- throw new Error ( "Unable to decrypt event" , { cause : existingClient } ) ;
728
+ return await decryptFn ( userInRoom . underlyingClient ) ;
729
+ } catch ( error ) {
730
+ LogService . debug ( "Appservice" , `Failed to decrypt via random user ${ userInRoom . userId } ` , error ) ;
731
+ throw new Error ( "Unable to decrypt event" , { cause : error } ) ;
725
732
}
726
733
}
727
734
0 commit comments