1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-11-25 05:23:13 +03:00

Clean up Event.clearEvent handling

This commit is contained in:
Brad Murray
2021-07-23 15:56:24 -04:00
parent 830a4a741e
commit 5aa60b8056

View File

@@ -152,7 +152,7 @@ export class MatrixEvent extends EventEmitter {
private _replacingEvent: MatrixEvent = null;
private _localRedactionEvent: MatrixEvent = null;
private _isCancelled = false;
private clearEvent: Partial<IClearEvent> = {};
private clearEvent?: IClearEvent;
/* curve25519 key which we believe belongs to the sender of the event. See
* getSenderKey()
@@ -285,7 +285,7 @@ export class MatrixEvent extends EventEmitter {
* @return {string} The event type, e.g. <code>m.room.message</code>
*/
public getType(): EventType | string {
return this.clearEvent.type || this.event.type;
return this.clearEvent?.type || this.event.type;
}
/**
@@ -334,7 +334,7 @@ export class MatrixEvent extends EventEmitter {
if (this._localRedactionEvent) {
return {} as T;
}
return (this.clearEvent.content || this.event.content || {}) as T;
return (this.clearEvent?.content || this.event.content || {}) as T;
}
/**
@@ -486,7 +486,7 @@ export class MatrixEvent extends EventEmitter {
}
public shouldAttemptDecryption() {
return this.isEncrypted() && !this.isBeingDecrypted() && this.getClearContent() === null;
return this.isEncrypted() && !this.isBeingDecrypted() && !this.clearEvent;
}
/**
@@ -518,10 +518,7 @@ export class MatrixEvent extends EventEmitter {
throw new Error("Attempt to decrypt event which isn't encrypted");
}
if (
this.clearEvent && this.clearEvent.content &&
this.clearEvent.content.msgtype !== "m.bad.encrypted"
) {
if (this.clearEvent?.content?.msgtype !== "m.bad.encrypted") {
// we may want to just ignore this? let's start with rejecting it.
throw new Error(
"Attempt to decrypt event which has already been decrypted",
@@ -729,8 +726,7 @@ export class MatrixEvent extends EventEmitter {
* @returns {Object} The cleartext (decrypted) content for the event
*/
public getClearContent(): IContent | null {
const ev = this.clearEvent;
return ev && ev.content ? ev.content : null;
return this.clearEvent?.content || null;
}
/**
@@ -921,8 +917,8 @@ export class MatrixEvent extends EventEmitter {
public getRedactionEvent(): object | null {
if (!this.isRedacted()) return null;
if (this.clearEvent.unsigned) {
return this.clearEvent.unsigned.redacted_because;
if (this.clearEvent?.unsigned) {
return this.clearEvent?.unsigned.redacted_because;
} else if (this.event.unsigned.redacted_because) {
return this.event.unsigned.redacted_because;
} else {