@@ -668,7 +668,7 @@ export class Appservice extends EventEmitter {
668
668
return providedToken === this . registration . hs_token ;
669
669
}
670
670
671
- private async decryptAppserivceEvent ( roomId : string , encrypted : EncryptedRoomEvent ) : ReturnType < Appservice [ "processEvent" ] > {
671
+ private async decryptAppserviceEvent ( roomId : string , encrypted : EncryptedRoomEvent ) : ReturnType < Appservice [ "processEvent" ] > {
672
672
const existingClient = this . cryptoClientForRoomId . get ( roomId ) ;
673
673
const decryptFn = async ( client : MatrixClient ) => {
674
674
// Also fetches state in order to decrypt room. We should throw if the client is confused.
@@ -723,12 +723,13 @@ export class Appservice extends EventEmitter {
723
723
}
724
724
725
725
// 4. Try to enable crypto on any client to decrypt it.
726
- const userInRoom = this . intentsCache . find ( ( _intent , userId ) => userIdsInRoom . includes ( userId ) ) ;
726
+ // We deliberately do not enable crypto on every client for performance reasons.
727
+ const userInRoom = this . intentsCache . find ( ( intent , userId ) => ! intent . underlyingClient . crypto ?. isReady && userIdsInRoom . includes ( userId ) ) ;
727
728
if ( ! userInRoom ) {
728
729
throw Error ( 'No users in room, cannot decrypt' ) ;
729
730
}
730
- await userInRoom . enableEncryption ( ) ;
731
731
try {
732
+ await userInRoom . enableEncryption ( ) ;
732
733
return await decryptFn ( userInRoom . underlyingClient ) ;
733
734
} catch ( error ) {
734
735
LogService . debug ( "Appservice" , `Failed to decrypt via random user ${ userInRoom . userId } ` , error ) ;
@@ -882,7 +883,7 @@ export class Appservice extends EventEmitter {
882
883
try {
883
884
const encrypted = new EncryptedRoomEvent ( event ) ;
884
885
const roomId = event [ 'room_id' ] ;
885
- event = await this . decryptAppserivceEvent ( roomId , encrypted ) ;
886
+ event = await this . decryptAppserviceEvent ( roomId , encrypted ) ;
886
887
this . emit ( "room.decrypted_event" , roomId , event ) ;
887
888
888
889
// For logging purposes: show that the event was decrypted
0 commit comments