Skip to content

Commit a75e5b7

Browse files
committed
Correct the dir parameter of MSC3715
Signed-off-by: Dominik Henneke <[email protected]>
1 parent fe2c350 commit a75e5b7

File tree

4 files changed

+118
-6
lines changed

4 files changed

+118
-6
lines changed
+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
Copyright 2022 Dominik Henneke
3+
Copyright 2022 Nordeck IT + Consulting GmbH.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
*/
17+
18+
import HttpBackend from "matrix-mock-request";
19+
20+
import { Direction, MatrixClient, MatrixScheduler } from "../../src/matrix";
21+
import { TestClient } from "../TestClient";
22+
23+
describe("MatrixClient relations", () => {
24+
const userId = "@alice:localhost";
25+
const accessToken = "aseukfgwef";
26+
const roomId = "!room:here";
27+
let client: MatrixClient | undefined;
28+
let httpBackend: HttpBackend | undefined;
29+
30+
const setupTests = (): [MatrixClient, HttpBackend] => {
31+
const scheduler = new MatrixScheduler();
32+
const testClient = new TestClient(
33+
userId,
34+
"DEVICE",
35+
accessToken,
36+
undefined,
37+
{ scheduler },
38+
);
39+
const httpBackend = testClient.httpBackend;
40+
const client = testClient.client;
41+
42+
return [client, httpBackend];
43+
};
44+
45+
beforeEach(() => {
46+
[client, httpBackend] = setupTests();
47+
});
48+
49+
afterEach(() => {
50+
httpBackend!.verifyNoOutstandingExpectation();
51+
return httpBackend!.stop();
52+
});
53+
54+
it("should read related events with the default options", async () => {
55+
const response = client!.relations(roomId, '$event-0', null, null);
56+
57+
httpBackend!
58+
.when("GET", "/rooms/!room%3Ahere/relations/%24event-0?dir=b")
59+
.respond(200, { chunk: [], next_batch: 'NEXT' });
60+
61+
await httpBackend!.flushAllExpected();
62+
63+
expect(await response).toEqual({ "events": [], "nextBatch": "NEXT" });
64+
});
65+
66+
it("should read related events with relation type", async () => {
67+
const response = client!.relations(roomId, '$event-0', 'm.reference', null);
68+
69+
httpBackend!
70+
.when("GET", "/rooms/!room%3Ahere/relations/%24event-0/m.reference?dir=b")
71+
.respond(200, { chunk: [], next_batch: 'NEXT' });
72+
73+
await httpBackend!.flushAllExpected();
74+
75+
expect(await response).toEqual({ "events": [], "nextBatch": "NEXT" });
76+
});
77+
78+
it("should read related events with relation type and event type", async () => {
79+
const response = client!.relations(roomId, '$event-0', 'm.reference', 'm.room.message');
80+
81+
httpBackend!
82+
.when(
83+
"GET",
84+
"/rooms/!room%3Ahere/relations/%24event-0/m.reference/m.room.message?dir=b",
85+
)
86+
.respond(200, { chunk: [], next_batch: 'NEXT' });
87+
88+
await httpBackend!.flushAllExpected();
89+
90+
expect(await response).toEqual({ "events": [], "nextBatch": "NEXT" });
91+
});
92+
93+
it("should read related events with custom options", async () => {
94+
const response = client!.relations(roomId, '$event-0', null, null, {
95+
dir: Direction.Forward,
96+
from: 'FROM',
97+
limit: 10,
98+
to: 'TO',
99+
});
100+
101+
httpBackend!
102+
.when(
103+
"GET",
104+
"/rooms/!room%3Ahere/relations/%24event-0?dir=f&from=FROM&limit=10&to=TO",
105+
)
106+
.respond(200, { chunk: [], next_batch: 'NEXT' });
107+
108+
await httpBackend!.flushAllExpected();
109+
110+
expect(await response).toEqual({ "events": [], "nextBatch": "NEXT" });
111+
});
112+
});

src/@types/requests.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ export interface IRelationsRequestOpts {
160160
from?: string;
161161
to?: string;
162162
limit?: number;
163-
direction?: Direction;
163+
dir?: Direction;
164164
}
165165

166166
export interface IRelationsResponse {

src/client.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -5399,7 +5399,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
53995399
if (Thread.hasServerSideSupport && timelineSet.thread) {
54005400
const thread = timelineSet.thread;
54015401
const opts: IRelationsRequestOpts = {
5402-
direction: Direction.Backward,
5402+
dir: Direction.Backward,
54035403
limit: 50,
54045404
};
54055405

@@ -6920,7 +6920,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
69206920
eventId: string,
69216921
relationType?: RelationType | string | null,
69226922
eventType?: EventType | string | null,
6923-
opts: IRelationsRequestOpts = { direction: Direction.Backward },
6923+
opts: IRelationsRequestOpts = { dir: Direction.Backward },
69246924
): Promise<{
69256925
originalEvent: MatrixEvent;
69266926
events: MatrixEvent[];
@@ -7498,7 +7498,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
74987498
eventId: string,
74997499
relationType?: RelationType | string | null,
75007500
eventType?: EventType | string | null,
7501-
opts: IRelationsRequestOpts = { direction: Direction.Backward },
7501+
opts: IRelationsRequestOpts = { dir: Direction.Backward },
75027502
): Promise<IRelationsResponse> {
75037503
const queryString = utils.encodeParams(opts as Record<string, string | number>);
75047504

src/models/thread.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ export class Thread extends ReadReceipt<EmittedEvents, EventHandlerMap> {
406406
return this.timelineSet.getLiveTimeline();
407407
}
408408

409-
public async fetchEvents(opts: IRelationsRequestOpts = { limit: 20, direction: Direction.Backward }): Promise<{
409+
public async fetchEvents(opts: IRelationsRequestOpts = { limit: 20, dir: Direction.Backward }): Promise<{
410410
originalEvent: MatrixEvent;
411411
events: MatrixEvent[];
412412
nextBatch?: string | null;
@@ -438,7 +438,7 @@ export class Thread extends ReadReceipt<EmittedEvents, EventHandlerMap> {
438438
return this.client.decryptEventIfNeeded(event);
439439
}));
440440

441-
const prependEvents = (opts.direction ?? Direction.Backward) === Direction.Backward;
441+
const prependEvents = (opts.dir ?? Direction.Backward) === Direction.Backward;
442442

443443
this.timelineSet.addEventsToTimeline(
444444
events,

0 commit comments

Comments
 (0)