Fix local room encryption status always not enabled (#30461)
* Fix local room encryption status always not enabled * refactor: put back the e2e test after merge * fix: look at e2eStatus in composer of local room * doc: add docs to `LocalRoom.isEncryptionEnabled` * test(e2e): check composer doesn't display unencrypted state * test: update existing tests * test(e2e): update existing tests * refactor: move room encryption check in a dedicated function * refactor: make `isEncryptionEnabled` cleaner * test: add tests for `LocalRoom.isEncrypted` * doc: fix `useIsEncrypted` comment --------- Co-authored-by: Florian Duros <florian.duros@ormaz.fr>
This commit is contained in:
co-authored by
Florian Duros
parent
8a1fc65beb
commit
cd7f1a0638
@@ -7,9 +7,29 @@ Please see LICENSE files in the repository root for full details.
|
||||
*/
|
||||
|
||||
import { type MatrixClient, type MatrixEvent, type Room, EventType } from "matrix-js-sdk/src/matrix";
|
||||
import { type CryptoApi } from "matrix-js-sdk/src/crypto-api";
|
||||
|
||||
import { useRoomState } from "./useRoomState.ts";
|
||||
import { useAsyncMemo } from "./useAsyncMemo.ts";
|
||||
import { LocalRoom } from "../models/LocalRoom.ts";
|
||||
|
||||
/**
|
||||
* Check if a room is encrypted.
|
||||
* If the room is a LocalRoom, check the state directly.
|
||||
* Otherwise, use the crypto API to check if encryption is enabled in the room.
|
||||
*
|
||||
* @param room - The room to check.
|
||||
* @param cryptoApi - The crypto API from the Matrix client.
|
||||
*/
|
||||
export async function isRoomEncrypted(room: Room, cryptoApi: CryptoApi): Promise<boolean> {
|
||||
if (room instanceof LocalRoom) {
|
||||
// For local room check the state.
|
||||
// The crypto check fails because the room ID is not valid (it is a local id)
|
||||
return (room as LocalRoom).isEncryptionEnabled();
|
||||
}
|
||||
|
||||
return await cryptoApi.isEncryptionEnabledInRoom(room.roomId);
|
||||
}
|
||||
|
||||
// Hook to simplify watching whether a Matrix room is encrypted, returns null if room is undefined or the state is loading
|
||||
export function useIsEncrypted(cli: MatrixClient, room?: Room): boolean | null {
|
||||
@@ -22,7 +42,7 @@ export function useIsEncrypted(cli: MatrixClient, room?: Room): boolean | null {
|
||||
const crypto = cli.getCrypto();
|
||||
if (!room || !crypto) return null;
|
||||
|
||||
return crypto.isEncryptionEnabledInRoom(room.roomId);
|
||||
return isRoomEncrypted(room, crypto);
|
||||
},
|
||||
[room, encryptionStateEvent],
|
||||
null,
|
||||
|
||||
Reference in New Issue
Block a user