Skip to content

Commit

Permalink
Grab bag of Element-R cleanups (#3773)
Browse files Browse the repository at this point in the history
* `RustBackupManager.getActiveBackupVersion`: check that backup is enabled

The previous check on `isBackupEnabled` was a no-op

* Fix log spam on shieldless events

* Reduce log spam about tracking users

* Reduce log spam about decrypting events

Logging the entire event is excessive
  • Loading branch information
richvdh authored Oct 4, 2023
1 parent 6e8d15e commit 10b6c24
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 13 deletions.
4 changes: 2 additions & 2 deletions spec/unit/rust-crypto/rust-crypto.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ describe("RustCrypto", () => {
[RustSdkCryptoJs.ShieldColor.Red, EventShieldColour.RED],
])("gets the right shield color (%i)", async (rustShield, expectedShield) => {
const mockEncryptionInfo = {
shieldState: jest.fn().mockReturnValue({ color: rustShield, message: null }),
shieldState: jest.fn().mockReturnValue({ color: rustShield, message: undefined }),
} as unknown as RustSdkCryptoJs.EncryptionInfo;
olmMachine.getRoomEventEncryptionInfo.mockResolvedValue(mockEncryptionInfo);

Expand All @@ -509,7 +509,7 @@ describe("RustCrypto", () => {
});

it.each([
[null, null],
[undefined, null],
["Encrypted by an unverified user.", EventShieldReason.UNVERIFIED_IDENTITY],
["Encrypted by a device not verified by its owner.", EventShieldReason.UNSIGNED_DEVICE],
[
Expand Down
7 changes: 3 additions & 4 deletions src/rust-crypto/RoomEncryptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,14 @@ export class RoomEncryptor {
* @param member - new membership state
*/
public onRoomMembership(member: RoomMember): void {
this.prefixedLogger.debug(`${member.membership} event for ${member.userId}`);

if (
member.membership == "join" ||
(member.membership == "invite" && this.room.shouldEncryptForInvitedMembers())
) {
// make sure we are tracking the deviceList for this user
this.prefixedLogger.debug(`starting to track devices for: ${member.userId}`);
this.olmMachine.updateTrackedUsers([new UserId(member.userId)]);
this.olmMachine.updateTrackedUsers([new UserId(member.userId)]).catch((e) => {
this.prefixedLogger.error("Unable to update tracked users", e);
});
}

// TODO: handle leaves (including our own)
Expand Down
2 changes: 1 addition & 1 deletion src/rust-crypto/backup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export class RustBackupManager extends TypedEventEmitter<RustBackupCryptoEvents,
* Get the backup version we are currently backing up to, if any
*/
public async getActiveBackupVersion(): Promise<string | null> {
if (!this.olmMachine.isBackupEnabled()) return null;
if (!(await this.olmMachine.isBackupEnabled())) return null;
return this.activeBackupVersion;
}

Expand Down
11 changes: 5 additions & 6 deletions src/rust-crypto/rust-crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1223,10 +1223,6 @@ export class RustCrypto extends TypedEventEmitter<RustCryptoEvents, RustCryptoEv

// start tracking devices for any users already known to be in this room.
const members = await room.getEncryptionTargetMembers();
logger.debug(
`[${room.roomId} encryption] starting to track devices for: `,
members.map((u) => `${u.userId} (${u.membership})`),
);
await this.olmMachine.updateTrackedUsers(members.map((u) => new RustSdkCryptoJs.UserId(u.userId)));
}

Expand Down Expand Up @@ -1518,7 +1514,10 @@ class EventDecryptor {
public constructor(private readonly olmMachine: RustSdkCryptoJs.OlmMachine, private readonly crypto: RustCrypto) {}

public async attemptEventDecryption(event: MatrixEvent): Promise<IEventDecryptionResult> {
logger.info("Attempting decryption of event", event);
logger.info(
`Attempting decryption of event ${event.getId()} in ${event.getRoomId()} from ${event.getSender()}`,
);

// add the event to the pending list *before* attempting to decrypt.
// then, if the key turns up while decryption is in progress (and
// decryption fails), we will schedule a retry.
Expand Down Expand Up @@ -1691,7 +1690,7 @@ function rustEncryptionInfoToJsEncryptionInfo(
}

let shieldReason: EventShieldReason | null;
if (shieldState.message === null) {
if (shieldState.message === undefined) {
shieldReason = null;
} else if (shieldState.message === "Encrypted by an unverified user.") {
// this case isn't actually used with lax shield semantics.
Expand Down

0 comments on commit 10b6c24

Please sign in to comment.