Skip to content

Commit

Permalink
Review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
florianduros committed Oct 15, 2024
1 parent 7b2aa50 commit c7dabe1
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 44 deletions.
3 changes: 2 additions & 1 deletion src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,6 @@ import {
decodeRecoveryKey,
ImportRoomKeysOpts,
CryptoEvent,
CryptoEvents,
CryptoEventHandlerMap,
} from "./crypto-api/index.ts";
import { DeviceInfoMap } from "./crypto/DeviceList.ts";
Expand Down Expand Up @@ -960,6 +959,8 @@ type LegacyCryptoEvents =
| LegacyCryptoEvent.WillUpdateDevices
| LegacyCryptoEvent.LegacyCryptoStoreMigrationProgress;

type CryptoEvents = (typeof CryptoEvent)[keyof typeof CryptoEvent];

type MatrixEventEvents = MatrixEventEvent.Decrypted | MatrixEventEvent.Replaced | MatrixEventEvent.VisibilityChange;

type RoomMemberEvents =
Expand Down
27 changes: 17 additions & 10 deletions src/crypto-api/CryptoEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,34 @@
* limitations under the License.
*/

/**
* Events emitted by the {@link CryptoApi}
*/
export enum CryptoEvent {
/**
* Fires when the trust status of a user changes.
* @param {string} userId - the user whose trust status changed
* @param {UserVerificationStatus} userTrustLevel - the new trust level
* The payload is a pair (userId, userTrustLevel).
*/
UserTrustStatusChanged = "userTrustStatusChanged",

/**
* Fires when the key backup status changes.
* @param {boolean} status - true if the key backup is enabled, false otherwise.
* The payload is a boolean
*/
KeyBackupStatus = "crypto.keyBackupStatus",

/**
* Fires when we failed to back up the keys
* @param {string} errorCode - the error code of the error that occurred
* The payload is an errorCode
*/
KeyBackupFailed = "crypto.keyBackupFailed",

/**
* Fires when the number of sessions that can be backed up changes.
* @param {number} remaining - the remaining number of sessions that can be backed up.
* The payload is the remaining number of sessions that can be backed up.
*/
KeyBackupSessionsRemaining = "crypto.keyBackupSessionsRemaining",

/**
* Fires when a new valid backup decryption key is in cache.
* This will happen when a secret is received from another session, from secret storage,
Expand All @@ -46,19 +52,22 @@ export enum CryptoEvent {
* This event is only fired by the rust crypto backend.
*/
KeyBackupDecryptionKeyCached = "crypto.keyBackupDecryptionKeyCached",

/**
* Fires when a key verification request is received.
* @param {VerificationRequest} - the request that was received
* The payload is a VerificationRequest object.
*/
VerificationRequestReceived = "crypto.verificationRequestReceived",

/** @deprecated Use {@link DevicesUpdated} instead when using rust crypto */
WillUpdateDevices = "crypto.willUpdateDevices",

/**
* Fires whenever the stored devices for a user have been updated
* @param {string[]} userIds - A list of user IDs that were updated
* @param {boolean} initialFetch - If true, the store was empty (apart from our own device) and has been seeded.
* The payload is a pair (userIds, initialFetch).
*/
DevicesUpdated = "crypto.devicesUpdated",

/**
* Fires when the user's cross-signing keys have changed or cross-signing
* has been enabled/disabled. The client can use getStoredCrossSigningForUser
Expand All @@ -82,5 +91,3 @@ export enum CryptoEvent {
*/
LegacyCryptoStoreMigrationProgress = "crypto.legacyCryptoStoreMigrationProgress",
}

export type CryptoEvents = (typeof CryptoEvent)[keyof typeof CryptoEvent];
35 changes: 3 additions & 32 deletions src/crypto-api/CryptoEventHandlerMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,43 +19,14 @@ import { VerificationRequest } from "./verification.ts";
import { UserVerificationStatus } from "./index.ts";
import { RustBackupCryptoEventMap } from "../rust-crypto/backup.ts";

/**
* A map of the {@link CryptoEvent} fired by the {@link CryptoApi} and their payloads.
*/
export type CryptoEventHandlerMap = {
/**
* Fires when a key verification request is received.
*/
[CryptoEvent.VerificationRequestReceived]: (request: VerificationRequest) => void;

/**
* Fires when the trust status of a user changes.
*/
[CryptoEvent.UserTrustStatusChanged]: (userId: string, userTrustLevel: UserVerificationStatus) => void;

[CryptoEvent.KeyBackupDecryptionKeyCached]: (version: string) => void;
/**
* Fires when the user's cross-signing keys have changed or cross-signing
* has been enabled/disabled. The client can use getStoredCrossSigningForUser
* with the user ID of the logged in user to check if cross-signing is
* enabled on the account. If enabled, it can test whether the current key
* is trusted using with checkUserTrust with the user ID of the logged
* in user. The checkOwnCrossSigningTrust function may be used to reconcile
* the trust in the account key.
*
* The cross-signing API is currently UNSTABLE and may change without notice.
* @experimental
*/
[CryptoEvent.KeysChanged]: (data: {}) => void;
/**
* Fires whenever the stored devices for a user will be updated
* @param users - A list of user IDs that will be updated
* @param initialFetch - If true, the store is empty (apart
* from our own device) and is being seeded.
*/
[CryptoEvent.WillUpdateDevices]: (users: string[], initialFetch: boolean) => void;
/**
* Fires whenever the stored devices for a user have changed
* @param users - A list of user IDs that were updated
* @param initialFetch - If true, the store was empty (apart
* from our own device) and has been seeded.
*/
[CryptoEvent.DevicesUpdated]: (users: string[], initialFetch: boolean) => void;
} & RustBackupCryptoEventMap;
2 changes: 1 addition & 1 deletion src/rust-crypto/rust-crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ import {
AllDevicesIsolationMode,
DeviceIsolationModeKind,
CryptoEvent,
CryptoEvents,
CryptoEventHandlerMap,
} from "../crypto-api/index.ts";
import { deviceKeysToDeviceMap, rustDeviceToJsDevice } from "./device-converter.ts";
Expand Down Expand Up @@ -2079,4 +2078,5 @@ function rustEncryptionInfoToJsEncryptionInfo(
return { shieldColour, shieldReason };
}

type CryptoEvents = (typeof CryptoEvent)[keyof typeof CryptoEvent];
type RustCryptoEvents = Exclude<CryptoEvents, CryptoEvent.LegacyCryptoStoreMigrationProgress>;

0 comments on commit c7dabe1

Please sign in to comment.