From a75e5b7d19ca56e963c15e4a2a6f8c6f1ba11fe2 Mon Sep 17 00:00:00 2001 From: Dominik Henneke Date: Mon, 10 Oct 2022 15:39:26 +0200 Subject: [PATCH 1/2] Correct the dir parameter of MSC3715 Signed-off-by: Dominik Henneke --- spec/integ/matrix-client-relations.spec.ts | 112 +++++++++++++++++++++ src/@types/requests.ts | 2 +- src/client.ts | 6 +- src/models/thread.ts | 4 +- 4 files changed, 118 insertions(+), 6 deletions(-) create mode 100644 spec/integ/matrix-client-relations.spec.ts diff --git a/spec/integ/matrix-client-relations.spec.ts b/spec/integ/matrix-client-relations.spec.ts new file mode 100644 index 00000000000..7766ebc528e --- /dev/null +++ b/spec/integ/matrix-client-relations.spec.ts @@ -0,0 +1,112 @@ +/* +Copyright 2022 Dominik Henneke +Copyright 2022 Nordeck IT + Consulting GmbH. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +import HttpBackend from "matrix-mock-request"; + +import { Direction, MatrixClient, MatrixScheduler } from "../../src/matrix"; +import { TestClient } from "../TestClient"; + +describe("MatrixClient relations", () => { + const userId = "@alice:localhost"; + const accessToken = "aseukfgwef"; + const roomId = "!room:here"; + let client: MatrixClient | undefined; + let httpBackend: HttpBackend | undefined; + + const setupTests = (): [MatrixClient, HttpBackend] => { + const scheduler = new MatrixScheduler(); + const testClient = new TestClient( + userId, + "DEVICE", + accessToken, + undefined, + { scheduler }, + ); + const httpBackend = testClient.httpBackend; + const client = testClient.client; + + return [client, httpBackend]; + }; + + beforeEach(() => { + [client, httpBackend] = setupTests(); + }); + + afterEach(() => { + httpBackend!.verifyNoOutstandingExpectation(); + return httpBackend!.stop(); + }); + + it("should read related events with the default options", async () => { + const response = client!.relations(roomId, '$event-0', null, null); + + httpBackend! + .when("GET", "/rooms/!room%3Ahere/relations/%24event-0?dir=b") + .respond(200, { chunk: [], next_batch: 'NEXT' }); + + await httpBackend!.flushAllExpected(); + + expect(await response).toEqual({ "events": [], "nextBatch": "NEXT" }); + }); + + it("should read related events with relation type", async () => { + const response = client!.relations(roomId, '$event-0', 'm.reference', null); + + httpBackend! + .when("GET", "/rooms/!room%3Ahere/relations/%24event-0/m.reference?dir=b") + .respond(200, { chunk: [], next_batch: 'NEXT' }); + + await httpBackend!.flushAllExpected(); + + expect(await response).toEqual({ "events": [], "nextBatch": "NEXT" }); + }); + + 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/relations/%24event-0/m.reference/m.room.message?dir=b", + ) + .respond(200, { chunk: [], next_batch: 'NEXT' }); + + await httpBackend!.flushAllExpected(); + + expect(await response).toEqual({ "events": [], "nextBatch": "NEXT" }); + }); + + it("should read related events with custom options", async () => { + const response = client!.relations(roomId, '$event-0', null, null, { + dir: Direction.Forward, + from: 'FROM', + limit: 10, + to: 'TO', + }); + + httpBackend! + .when( + "GET", + "/rooms/!room%3Ahere/relations/%24event-0?dir=f&from=FROM&limit=10&to=TO", + ) + .respond(200, { chunk: [], next_batch: 'NEXT' }); + + await httpBackend!.flushAllExpected(); + + expect(await response).toEqual({ "events": [], "nextBatch": "NEXT" }); + }); +}); diff --git a/src/@types/requests.ts b/src/@types/requests.ts index 4246cbab6f8..cb292bf2cf0 100644 --- a/src/@types/requests.ts +++ b/src/@types/requests.ts @@ -160,7 +160,7 @@ export interface IRelationsRequestOpts { from?: string; to?: string; limit?: number; - direction?: Direction; + dir?: Direction; } export interface IRelationsResponse { diff --git a/src/client.ts b/src/client.ts index 2858cab7d1f..a8857abbbe1 100644 --- a/src/client.ts +++ b/src/client.ts @@ -5399,7 +5399,7 @@ export class MatrixClient extends TypedEventEmitter { const queryString = utils.encodeParams(opts as Record); diff --git a/src/models/thread.ts b/src/models/thread.ts index 60757f9c316..dc433a24fa8 100644 --- a/src/models/thread.ts +++ b/src/models/thread.ts @@ -406,7 +406,7 @@ export class Thread extends ReadReceipt { return this.timelineSet.getLiveTimeline(); } - public async fetchEvents(opts: IRelationsRequestOpts = { limit: 20, direction: Direction.Backward }): Promise<{ + public async fetchEvents(opts: IRelationsRequestOpts = { limit: 20, dir: Direction.Backward }): Promise<{ originalEvent: MatrixEvent; events: MatrixEvent[]; nextBatch?: string | null; @@ -438,7 +438,7 @@ export class Thread extends ReadReceipt { return this.client.decryptEventIfNeeded(event); })); - const prependEvents = (opts.direction ?? Direction.Backward) === Direction.Backward; + const prependEvents = (opts.dir ?? Direction.Backward) === Direction.Backward; this.timelineSet.addEventsToTimeline( events, From 694c59740e4ab2d7aa029ea4b77d59f66af85d55 Mon Sep 17 00:00:00 2001 From: Dominik Henneke Date: Wed, 12 Oct 2022 09:05:45 +0200 Subject: [PATCH 2/2] Add test for fetchRelations to improve the coverage Signed-off-by: Dominik Henneke --- spec/integ/matrix-client-relations.spec.ts | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/spec/integ/matrix-client-relations.spec.ts b/spec/integ/matrix-client-relations.spec.ts index 7766ebc528e..3a8a99fbff3 100644 --- a/spec/integ/matrix-client-relations.spec.ts +++ b/spec/integ/matrix-client-relations.spec.ts @@ -109,4 +109,19 @@ describe("MatrixClient relations", () => { expect(await response).toEqual({ "events": [], "nextBatch": "NEXT" }); }); + + it('should use default direction in the fetchRelations endpoint', async () => { + const response = client!.fetchRelations(roomId, '$event-0', null, null); + + httpBackend! + .when( + "GET", + "/rooms/!room%3Ahere/relations/%24event-0?dir=b", + ) + .respond(200, { chunk: [], next_batch: 'NEXT' }); + + await httpBackend!.flushAllExpected(); + + expect(await response).toEqual({ "chunk": [], "next_batch": "NEXT" }); + }); });