Skip to content

Commit e578d84

Browse files
author
Kerry Archibald
committed
allow alt event types in relations model
1 parent 984e5eb commit e578d84

File tree

2 files changed

+100
-2
lines changed

2 files changed

+100
-2
lines changed

spec/unit/relations.spec.ts

+94
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,21 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17+
import { M_POLL_START } from "matrix-events-sdk";
18+
1719
import { EventTimelineSet } from "../../src/models/event-timeline-set";
1820
import { MatrixEvent, MatrixEventEvent } from "../../src/models/event";
1921
import { Room } from "../../src/models/room";
2022
import { Relations } from "../../src/models/relations";
2123
import { TestClient } from "../TestClient";
24+
import { RelationType } from "../../src";
25+
import { logger } from "../../src/logger";
2226

2327
describe("Relations", function () {
28+
afterEach(() => {
29+
jest.spyOn(logger, "error").mockRestore();
30+
});
31+
2432
it("should deduplicate annotations", function () {
2533
const room = new Room("room123", null!, null!);
2634
const relations = new Relations("m.annotation", "m.reaction", room);
@@ -75,6 +83,92 @@ describe("Relations", function () {
7583
}
7684
});
7785

86+
describe("addEvent()", () => {
87+
const relationType = RelationType.Reference;
88+
const eventType = M_POLL_START.stable!;
89+
const altEventTypes = [M_POLL_START.unstable!];
90+
const room = new Room("room123", null!, null!);
91+
92+
it("should not add events without a relation", async () => {
93+
// dont pollute console
94+
const logSpy = jest.spyOn(logger, "error").mockImplementation(() => {});
95+
const relations = new Relations(relationType, eventType, room);
96+
const emitSpy = jest.spyOn(relations, "emit");
97+
const event = new MatrixEvent({ type: eventType });
98+
99+
await relations.addEvent(event);
100+
expect(logSpy).toHaveBeenCalledWith("Event must have relation info");
101+
// event not added
102+
expect(relations.getRelations().length).toBe(0);
103+
expect(emitSpy).not.toHaveBeenCalled();
104+
});
105+
106+
it("should not add events of incorrect event type", async () => {
107+
// dont pollute console
108+
const logSpy = jest.spyOn(logger, "error").mockImplementation(() => {});
109+
const relations = new Relations(relationType, eventType, room);
110+
const emitSpy = jest.spyOn(relations, "emit");
111+
const event = new MatrixEvent({
112+
type: "different-event-type",
113+
content: {
114+
"m.relates_to": {
115+
event_id: "$2s4yYpEkVQrPglSCSqB_m6E8vDhWsg0yFNyOJdVIb_o",
116+
rel_type: relationType,
117+
},
118+
},
119+
});
120+
121+
await relations.addEvent(event);
122+
123+
expect(logSpy).toHaveBeenCalledWith(`Event relation info doesn't match this container`);
124+
// event not added
125+
expect(relations.getRelations().length).toBe(0);
126+
expect(emitSpy).not.toHaveBeenCalled();
127+
});
128+
129+
it("adds events that match alt event types", async () => {
130+
const relations = new Relations(relationType, eventType, room, altEventTypes);
131+
const emitSpy = jest.spyOn(relations, "emit");
132+
const event = new MatrixEvent({
133+
type: M_POLL_START.unstable!,
134+
content: {
135+
"m.relates_to": {
136+
event_id: "$2s4yYpEkVQrPglSCSqB_m6E8vDhWsg0yFNyOJdVIb_o",
137+
rel_type: relationType,
138+
},
139+
},
140+
});
141+
142+
await relations.addEvent(event);
143+
144+
// event added
145+
expect(relations.getRelations()).toEqual([event]);
146+
expect(emitSpy).toHaveBeenCalled();
147+
});
148+
149+
it("should not add events of incorrect relation type", async () => {
150+
const logSpy = jest.spyOn(logger, "error").mockImplementation(() => {});
151+
const relations = new Relations(relationType, eventType, room);
152+
const event = new MatrixEvent({
153+
type: eventType,
154+
content: {
155+
"m.relates_to": {
156+
event_id: "$2s4yYpEkVQrPglSCSqB_m6E8vDhWsg0yFNyOJdVIb_o",
157+
rel_type: "m.annotation",
158+
},
159+
},
160+
});
161+
162+
await relations.addEvent(event);
163+
const emitSpy = jest.spyOn(relations, "emit");
164+
165+
expect(logSpy).toHaveBeenCalledWith(`Event relation info doesn't match this container`);
166+
// event not added
167+
expect(relations.getRelations().length).toBe(0);
168+
expect(emitSpy).not.toHaveBeenCalled();
169+
});
170+
});
171+
78172
it("should emit created regardless of ordering", async function () {
79173
const targetEvent = new MatrixEvent({
80174
sender: "@bob:example.com",

src/models/relations.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ export type EventHandlerMap = {
3333
[RelationsEvent.Redaction]: (event: MatrixEvent) => void;
3434
};
3535

36+
const matchesEventType = (eventType: string, targetEventType: string, altTargetEventTypes: string[] = []): boolean =>
37+
[targetEventType, ...altTargetEventTypes].includes(eventType);
38+
3639
/**
3740
* A container for relation events that supports easy access to common ways of
3841
* aggregating such events. Each instance holds events that of a single relation
@@ -60,6 +63,7 @@ export class Relations extends TypedEventEmitter<RelationsEvent, EventHandlerMap
6063
public readonly relationType: RelationType | string,
6164
public readonly eventType: string,
6265
client: MatrixClient | Room,
66+
public readonly altEventTypes?: string[],
6367
) {
6468
super();
6569
this.client = client instanceof Room ? client.client : client;
@@ -84,7 +88,7 @@ export class Relations extends TypedEventEmitter<RelationsEvent, EventHandlerMap
8488
const relationType = relation.rel_type;
8589
const eventType = event.getType();
8690

87-
if (this.relationType !== relationType || this.eventType !== eventType) {
91+
if (this.relationType !== relationType || !matchesEventType(eventType, this.eventType, this.altEventTypes)) {
8892
logger.error("Event relation info doesn't match this container");
8993
return;
9094
}
@@ -131,7 +135,7 @@ export class Relations extends TypedEventEmitter<RelationsEvent, EventHandlerMap
131135
const relationType = relation.rel_type;
132136
const eventType = event.getType();
133137

134-
if (this.relationType !== relationType || this.eventType !== eventType) {
138+
if (this.relationType !== relationType || !matchesEventType(eventType, this.eventType, this.altEventTypes)) {
135139
logger.error("Event relation info doesn't match this container");
136140
return;
137141
}

0 commit comments

Comments
 (0)