Skip to content

Commit 9e51abc

Browse files
committed
Use WITHHELD_MESSAGES instead of hardcoded string
1 parent 76d0c1a commit 9e51abc

File tree

3 files changed

+16
-13
lines changed

3 files changed

+16
-13
lines changed

spec/unit/models/event.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ describe("MatrixEvent", () => {
126126
expect(encryptedEvent.isEncrypted()).toBeTruthy();
127127
expect(encryptedEvent.isBeingDecrypted()).toBeFalsy();
128128
expect(encryptedEvent.isDecryptionFailure()).toBeTruthy();
129-
expect(encryptedEvent.isEncryptedDisabledForUnverifiedDevices).toBeFalsy();
129+
expect(encryptedEvent.isEncryptedDisabledForUnverifiedDevices()).toBeFalsy();
130130
expect(encryptedEvent.getContent()).toEqual({
131131
msgtype: "m.bad.encrypted",
132132
body: "** Unable to decrypt: Error: test error **",
@@ -144,7 +144,7 @@ describe("MatrixEvent", () => {
144144
expect(encryptedEvent.isEncrypted()).toBeTruthy();
145145
expect(encryptedEvent.isBeingDecrypted()).toBeFalsy();
146146
expect(encryptedEvent.isDecryptionFailure()).toBeTruthy();
147-
expect(encryptedEvent.isEncryptedDisabledForUnverifiedDevices).toBeTruthy();
147+
expect(encryptedEvent.isEncryptedDisabledForUnverifiedDevices()).toBeTruthy();
148148
expect(encryptedEvent.getContent()).toEqual({
149149
msgtype: "m.bad.encrypted",
150150
body: "** Unable to decrypt: DecryptionError: The sender has disabled encrypting to unverified devices. **",

src/@types/crypto.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export interface IEventDecryptionResult {
4747
/**
4848
* The sender doesn't authorize the unverified devices to decrypt his messages
4949
*/
50-
isEncryptedDisabledForUnverifiedDevices?: boolean;
50+
encryptedDisabledForUnverifiedDevices?: boolean;
5151
}
5252

5353
interface Extensible {

src/models/event.ts

+13-10
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import { TypedEventEmitter } from "./typed-event-emitter";
3636
import { EventStatus } from "./event-status";
3737
import { DecryptionError } from "../crypto/algorithms";
3838
import { CryptoBackend } from "../common-crypto/CryptoBackend";
39+
import { WITHHELD_MESSAGES } from "../crypto/OlmDevice";
3940

4041
export { EventStatus } from "./event-status";
4142

@@ -272,6 +273,12 @@ export class MatrixEvent extends TypedEventEmitter<MatrixEventEmittedEvents, Mat
272273
private thread?: Thread;
273274
private threadId?: string;
274275

276+
/*
277+
* True if this event is an encrypted event which we failed to decrypt, the receiver's device is unverified and
278+
* the sender has disabled encrypting to unverified devices.
279+
*/
280+
private encryptedDisabledForUnverifiedDevices = false;
281+
275282
/* Set an approximate timestamp for the event relative the local clock.
276283
* This will inherently be approximate because it doesn't take into account
277284
* the time between the server putting the 'age' field on the event as it sent
@@ -324,12 +331,6 @@ export class MatrixEvent extends TypedEventEmitter<MatrixEventEmittedEvents, Mat
324331
*/
325332
public verificationRequest?: VerificationRequest;
326333

327-
/*
328-
* True if this event is an encrypted event which we failed to decrypt, the receiver's device is unverified and
329-
* the sender has disabled encrypting to unverified devices.
330-
*/
331-
public isEncryptedDisabledForUnverifiedDevices = false;
332-
333334
private readonly reEmitter: TypedReEmitter<MatrixEventEmittedEvents, MatrixEventHandlerMap>;
334335

335336
/**
@@ -710,6 +711,10 @@ export class MatrixEvent extends TypedEventEmitter<MatrixEventEmittedEvents, Mat
710711
return this.clearEvent?.content?.msgtype === "m.bad.encrypted";
711712
}
712713

714+
public isEncryptedDisabledForUnverifiedDevices(): boolean {
715+
return this.isDecryptionFailure() && this.encryptedDisabledForUnverifiedDevices;
716+
}
717+
713718
public shouldAttemptDecryption(): boolean {
714719
if (this.isRedacted()) return false;
715720
if (this.isBeingDecrypted()) return false;
@@ -905,8 +910,7 @@ export class MatrixEvent extends TypedEventEmitter<MatrixEventEmittedEvents, Mat
905910
body: "** Unable to decrypt: " + reason + " **",
906911
},
907912
},
908-
isEncryptedDisabledForUnverifiedDevices:
909-
reason === "DecryptionError: The sender has disabled encrypting to unverified devices.",
913+
encryptedDisabledForUnverifiedDevices: reason === `DecryptionError: ${WITHHELD_MESSAGES["m.unverified"]}`,
910914
};
911915
}
912916

@@ -928,8 +932,7 @@ export class MatrixEvent extends TypedEventEmitter<MatrixEventEmittedEvents, Mat
928932
this.claimedEd25519Key = decryptionResult.claimedEd25519Key ?? null;
929933
this.forwardingCurve25519KeyChain = decryptionResult.forwardingCurve25519KeyChain || [];
930934
this.untrusted = decryptionResult.untrusted || false;
931-
this.isEncryptedDisabledForUnverifiedDevices =
932-
decryptionResult.isEncryptedDisabledForUnverifiedDevices || false;
935+
this.encryptedDisabledForUnverifiedDevices = decryptionResult.encryptedDisabledForUnverifiedDevices || false;
933936
this.invalidateExtensibleEvent();
934937
}
935938

0 commit comments

Comments
 (0)