Skip to content

Commit

Permalink
Switch to stable /relations endpoint, stop using unspecced original_e…
Browse files Browse the repository at this point in the history
…vent field (#2911)

* Switch to stable /relations endpoint, stop using unspecced original_event field
* Adapt the tests to the changed endpoint
  • Loading branch information
justjanne authored Dec 2, 2022
1 parent 79ccd7c commit 43bfa0c
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 23 deletions.
39 changes: 32 additions & 7 deletions spec/integ/matrix-client-event-timeline.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,12 @@ describe("MatrixClient event timelines", function() {
return THREAD_ROOT;
});

httpBackend.when("GET", "/rooms/!foo%3Abar/relations/" +
httpBackend.when("GET", "/rooms/!foo%3Abar/event/" + encodeURIComponent(THREAD_ROOT.event_id!))
.respond(200, function() {
return THREAD_ROOT;
});

httpBackend.when("GET", "/_matrix/client/v1/rooms/!foo%3Abar/relations/" +
encodeURIComponent(THREAD_ROOT.event_id!) + "/" +
encodeURIComponent(THREAD_RELATION_TYPE.name) + "?dir=b&limit=1")
.respond(200, function() {
Expand Down Expand Up @@ -650,7 +655,6 @@ describe("MatrixClient event timelines", function() {
encodeURIComponent(THREAD_RELATION_TYPE.name) + "?dir=b&limit=1")
.respond(200, function() {
return {
original_event: THREAD_ROOT,
chunk: [THREAD_REPLY],
// no next batch as this is the oldest end of the timeline
};
Expand All @@ -660,11 +664,17 @@ describe("MatrixClient event timelines", function() {
await httpBackend.flushAllExpected();
const timelineSet = thread.timelineSet;

httpBackend.when("GET", "/rooms/!foo%3Abar/event/" + encodeURIComponent(THREAD_ROOT.event_id!))
.respond(200, function() {
return THREAD_ROOT;
});

const timelinePromise = client.getEventTimeline(timelineSet, THREAD_REPLY.event_id!);
const timeline = await timelinePromise;
const [timeline] = await Promise.all([timelinePromise, httpBackend.flushAllExpected()]);

expect(timeline!.getEvents().find(e => e.getId() === THREAD_ROOT.event_id!)).toBeTruthy();
expect(timeline!.getEvents().find(e => e.getId() === THREAD_REPLY.event_id!)).toBeTruthy();
const eventIds = timeline!.getEvents().map(it => it.getId());
expect(eventIds).toContain(THREAD_ROOT.event_id);
expect(eventIds).toContain(THREAD_REPLY.event_id);
});

it("should return relevant timeline from non-thread timelineSet when asking for the thread root", async () => {
Expand Down Expand Up @@ -1504,7 +1514,22 @@ describe("MatrixClient event timelines", function() {
state: [],
end: "end_token",
});
httpBackend.when("GET", "/rooms/!foo%3Abar/relations/" +
httpBackend.when("GET", "/rooms/!foo%3Abar/event/" +
encodeURIComponent(THREAD_ROOT.event_id!))
.respond(200, function() {
return THREAD_ROOT;
});
httpBackend.when("GET", "/rooms/!foo%3Abar/event/" +
encodeURIComponent(THREAD_ROOT.event_id!))
.respond(200, function() {
return THREAD_ROOT;
});
httpBackend.when("GET", "/rooms/!foo%3Abar/event/" +
encodeURIComponent(THREAD_ROOT.event_id!))
.respond(200, function() {
return THREAD_ROOT;
});
httpBackend.when("GET", "/_matrix/client/v1/rooms/!foo%3Abar/relations/" +
encodeURIComponent(THREAD_ROOT.event_id!) + "/" +
encodeURIComponent(THREAD_RELATION_TYPE.name) + buildParams(Direction.Backward, "start_token"))
.respond(200, function() {
Expand All @@ -1513,7 +1538,7 @@ describe("MatrixClient event timelines", function() {
chunk: [],
};
});
httpBackend.when("GET", "/rooms/!foo%3Abar/relations/" +
httpBackend.when("GET", "/_matrix/client/v1/rooms/!foo%3Abar/relations/" +
encodeURIComponent(THREAD_ROOT.event_id!) + "/" +
encodeURIComponent(THREAD_RELATION_TYPE.name) + buildParams(Direction.Forward, "end_token"))
.respond(200, function() {
Expand Down
20 changes: 16 additions & 4 deletions spec/integ/matrix-client-relations.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ describe("MatrixClient relations", () => {
const response = client!.relations(roomId, '$event-0', null, null);

httpBackend!
.when("GET", "/rooms/!room%3Ahere/relations/%24event-0?dir=b")
.when("GET", "/rooms/!room%3Ahere/event/%24event-0")
.respond(200, null);
httpBackend!
.when("GET", "/_matrix/client/v1/rooms/!room%3Ahere/relations/%24event-0?dir=b")
.respond(200, { chunk: [], next_batch: 'NEXT' });

await httpBackend!.flushAllExpected();
Expand All @@ -67,7 +70,10 @@ describe("MatrixClient relations", () => {
const response = client!.relations(roomId, '$event-0', 'm.reference', null);

httpBackend!
.when("GET", "/rooms/!room%3Ahere/relations/%24event-0/m.reference?dir=b")
.when("GET", "/rooms/!room%3Ahere/event/%24event-0")
.respond(200, null);
httpBackend!
.when("GET", "/_matrix/client/v1/rooms/!room%3Ahere/relations/%24event-0/m.reference?dir=b")
.respond(200, { chunk: [], next_batch: 'NEXT' });

await httpBackend!.flushAllExpected();
Expand All @@ -78,10 +84,13 @@ describe("MatrixClient relations", () => {
it("should read related events with relation type and event type", async () => {
const response = client!.relations(roomId, '$event-0', 'm.reference', 'm.room.message');

httpBackend!
.when("GET", "/rooms/!room%3Ahere/event/%24event-0")
.respond(200, null);
httpBackend!
.when(
"GET",
"/rooms/!room%3Ahere/relations/%24event-0/m.reference/m.room.message?dir=b",
"/_matrix/client/v1/rooms/!room%3Ahere/relations/%24event-0/m.reference/m.room.message?dir=b",
)
.respond(200, { chunk: [], next_batch: 'NEXT' });

Expand All @@ -98,10 +107,13 @@ describe("MatrixClient relations", () => {
to: 'TO',
});

httpBackend!
.when("GET", "/rooms/!room%3Ahere/event/%24event-0")
.respond(200, null);
httpBackend!
.when(
"GET",
"/rooms/!room%3Ahere/relations/%24event-0?dir=f&from=FROM&limit=10&to=TO",
"/_matrix/client/v1/rooms/!room%3Ahere/relations/%24event-0?dir=f&from=FROM&limit=10&to=TO",
)
.respond(200, { chunk: [], next_batch: 'NEXT' });

Expand Down
1 change: 0 additions & 1 deletion src/@types/requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ export interface IRelationsRequestOpts {
}

export interface IRelationsResponse {
original_event: IEvent;
chunk: IEvent[];
next_batch?: string;
prev_batch?: string;
Expand Down
23 changes: 12 additions & 11 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5469,7 +5469,8 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa

timelineSet.addEventsToTimeline(events, true, timeline, resNewer.next_batch);
if (!resOlder.next_batch) {
timelineSet.addEventsToTimeline([mapper(resOlder.original_event)], true, timeline, null);
const originalEvent = await this.fetchRoomEvent(timelineSet.room.roomId, thread.id);
timelineSet.addEventsToTimeline([mapper(originalEvent)], true, timeline, null);
}
timeline.setPaginationToken(resOlder.next_batch ?? null, Direction.Backward);
timeline.setPaginationToken(resNewer.next_batch ?? null, Direction.Forward);
Expand Down Expand Up @@ -5526,7 +5527,8 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa

timelineSet.addEventsToTimeline(events, true, timeline, null);
if (!resOlder.next_batch) {
timelineSet.addEventsToTimeline([mapper(resOlder.original_event)], true, timeline, null);
const originalEvent = await this.fetchRoomEvent(timelineSet.room.roomId, thread.id);
timelineSet.addEventsToTimeline([mapper(originalEvent)], true, timeline, null);
}
timeline.setPaginationToken(resOlder.next_batch ?? null, Direction.Backward);
timeline.setPaginationToken(null, Direction.Forward);
Expand Down Expand Up @@ -5878,7 +5880,8 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
const timelineSet = eventTimeline.getTimelineSet();
timelineSet.addEventsToTimeline(matrixEvents, backwards, eventTimeline, newToken ?? null);
if (!newToken && backwards) {
timelineSet.addEventsToTimeline([mapper(res.original_event)], true, eventTimeline, null);
const originalEvent = await this.fetchRoomEvent(eventTimeline.getRoomId() ?? "", thread.id);
timelineSet.addEventsToTimeline([mapper(originalEvent)], true, eventTimeline, null);
}
this.processBeaconEvents(timelineSet.room, matrixEvents);

Expand Down Expand Up @@ -7099,15 +7102,13 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
prevBatch?: string | null;
}> {
const fetchedEventType = eventType ? this.getEncryptedIfNeededEventType(roomId, eventType) : null;
const result = await this.fetchRelations(
roomId,
eventId,
relationType,
fetchedEventType,
opts);
const [eventResult, result] = await Promise.all([
this.fetchRoomEvent(roomId, eventId),
this.fetchRelations(roomId, eventId, relationType, fetchedEventType, opts),
]);
const mapper = this.getEventMapper();

const originalEvent = result.original_event ? mapper(result.original_event) : undefined;
const originalEvent = eventResult ? mapper(eventResult) : undefined;
let events = result.chunk.map(mapper);

if (fetchedEventType === EventType.RoomMessageEncrypted) {
Expand Down Expand Up @@ -7669,7 +7670,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
});
return this.http.authedRequest(
Method.Get, path, undefined, undefined, {
prefix: ClientPrefix.Unstable,
prefix: ClientPrefix.V1,
},
);
}
Expand Down

0 comments on commit 43bfa0c

Please sign in to comment.