Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore thread relations on state events for consistency with edits #3540

Merged
merged 2 commits into from
Jul 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

implicit cast to bool

([RelationType.Replace, RelationType.Thread] as string[]).includes(relation.rel_type)
) {
// State events cannot be m.replace relations
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... or some other type of relation? ;)

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
Loading