Skip to content

Commit

Permalink
Ignore thread relations on state events for consistency with edits (#…
Browse files Browse the repository at this point in the history
…3540)

* Ignore thread relations on state events for consistency with edits

* Add test
  • Loading branch information
t3chguy authored Jul 4, 2023
1 parent 5be4548 commit 89cabc4
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
21 changes: 21 additions & 0 deletions spec/unit/models/event.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,4 +308,25 @@ describe("MatrixEvent", () => {
});
});
});

it("should ignore thread relation on state events", async () => {
const stateEvent = new MatrixEvent({
event_id: "$event_id",
type: "some_state_event",
content: {
"foo": "bar",
"m.relates_to": {
"event_id": "$thread_id",
"m.in_reply_to": {
event_id: "$thread_id",
},
"rel_type": "m.thread",
},
},
state_key: "",
});

expect(stateEvent.isState()).toBeTruthy();
expect(stateEvent.threadRootId).toBeUndefined();
});
});
21 changes: 19 additions & 2 deletions src/models/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import {
import { Crypto } from "../crypto";
import { deepSortedObjectEntries, internaliseString } from "../utils";
import { RoomMember } from "./room-member";
import { Thread, ThreadEvent, ThreadEventHandlerMap, THREAD_RELATION_TYPE } from "./thread";
import { Thread, THREAD_RELATION_TYPE, ThreadEvent, ThreadEventHandlerMap } from "./thread";
import { IActionsObject } from "../pushprocessor";
import { TypedReEmitter } from "../ReEmitter";
import { MatrixError } from "../http-api";
Expand Down Expand Up @@ -576,6 +576,10 @@ export class MatrixEvent extends TypedEventEmitter<MatrixEventEmittedEvents, Mat
* Get the event ID of the thread head
*/
public get threadRootId(): string | undefined {
// don't allow state events to be threaded as per the spec
if (this.isState()) {
return undefined;
}
const relatesTo = this.getWireContent()?.["m.relates_to"];
if (relatesTo?.rel_type === THREAD_RELATION_TYPE.name) {
return relatesTo.event_id;
Expand All @@ -597,6 +601,11 @@ export class MatrixEvent extends TypedEventEmitter<MatrixEventEmittedEvents, Mat
* A helper to check if an event is a thread's head or not
*/
public get isThreadRoot(): boolean {
// don't allow state events to be threaded as per the spec
if (this.isState()) {
return false;
}

const threadDetails = this.getServerAggregatedRelation<IThreadBundledRelationship>(THREAD_RELATION_TYPE.name);

// Bundled relationships only returned when the sync response is limited
Expand Down Expand Up @@ -1365,7 +1374,11 @@ export class MatrixEvent extends TypedEventEmitter<MatrixEventEmittedEvents, Mat
// Relation info is lifted out of the encrypted content when sent to
// encrypted rooms, so we have to check `getWireContent` for this.
const relation = this.getWireContent()?.["m.relates_to"];
if (this.isState() && relation?.rel_type === RelationType.Replace) {
if (
this.isState() &&
relation?.rel_type &&
([RelationType.Replace, RelationType.Thread] as string[]).includes(relation.rel_type)
) {
// State events cannot be m.replace relations
return false;
}
Expand Down Expand Up @@ -1618,6 +1631,10 @@ export class MatrixEvent extends TypedEventEmitter<MatrixEventEmittedEvents, Mat
* @param thread - the thread
*/
public setThread(thread?: Thread): void {
// don't allow state events to be threaded as per the spec
if (this.isState()) {
return;
}
if (this.thread) {
this.reEmitter.stopReEmitting(this.thread, [ThreadEvent.Update]);
}
Expand Down

0 comments on commit 89cabc4

Please sign in to comment.