Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 6141cca

Browse files
authored
Support dynamic room predecessors for RoomProvider (#10346)
1 parent 2e064e5 commit 6141cca

File tree

2 files changed

+133
-2
lines changed

2 files changed

+133
-2
lines changed

src/autocomplete/RoomProvider.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
Copyright 2016 Aviral Dasgupta
33
Copyright 2018 Michael Telatynski <[email protected]>
4-
Copyright 2017, 2018, 2021 The Matrix.org Foundation C.I.C.
4+
Copyright 2017-2023 The Matrix.org Foundation C.I.C.
55
66
Licensed under the Apache License, Version 2.0 (the "License");
77
you may not use this file except in compliance with the License.
@@ -29,6 +29,7 @@ import { makeRoomPermalink } from "../utils/permalinks/Permalinks";
2929
import { ICompletion, ISelectionRange } from "./Autocompleter";
3030
import RoomAvatar from "../components/views/avatars/RoomAvatar";
3131
import { TimelineRenderingType } from "../contexts/RoomContext";
32+
import SettingsStore from "../settings/SettingsStore";
3233

3334
const ROOM_REGEX = /\B#\S*/g;
3435

@@ -67,7 +68,9 @@ export default class RoomProvider extends AutocompleteProvider {
6768
const cli = MatrixClientPeg.get();
6869

6970
// filter out spaces here as they get their own autocomplete provider
70-
return cli.getVisibleRooms().filter((r) => !r.isSpaceRoom());
71+
return cli
72+
.getVisibleRooms(SettingsStore.getValue("feature_dynamic_room_predecessors"))
73+
.filter((r) => !r.isSpaceRoom());
7174
}
7275

7376
public async getCompletions(
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/*
2+
Copyright 2023 The Matrix.org Foundation C.I.C.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
import { mocked } from "jest-mock";
18+
import { MatrixClient, Room } from "matrix-js-sdk/src/matrix";
19+
20+
import RoomProvider from "../../src/autocomplete/RoomProvider";
21+
import SettingsStore from "../../src/settings/SettingsStore";
22+
import { mkRoom, stubClient } from "../test-utils";
23+
24+
describe("RoomProvider", () => {
25+
it("suggests a room whose alias matches a prefix", async () => {
26+
// Given a room
27+
const client = stubClient();
28+
const room = makeRoom(client, "room:e.com");
29+
mocked(client.getVisibleRooms).mockReturnValue([room]);
30+
31+
// When we search for rooms starting with its prefix
32+
const roomProvider = new RoomProvider(room);
33+
const completions = await roomProvider.getCompletions("#ro", { beginning: true, start: 0, end: 3 });
34+
35+
// Then we find it
36+
expect(completions).toStrictEqual([
37+
{
38+
type: "room",
39+
completion: room.getCanonicalAlias(),
40+
completionId: room.roomId,
41+
component: expect.anything(),
42+
href: "https://matrix.to/#/#room:e.com",
43+
range: { start: 0, end: 3 },
44+
suffix: " ",
45+
},
46+
]);
47+
});
48+
49+
it("suggests only rooms matching a prefix", async () => {
50+
// Given some rooms with different names
51+
const client = stubClient();
52+
const room1 = makeRoom(client, "room1:e.com");
53+
const room2 = makeRoom(client, "room2:e.com");
54+
const other = makeRoom(client, "other:e.com");
55+
mocked(client.getVisibleRooms).mockReturnValue([room1, room2, other]);
56+
57+
// When we search for rooms starting with a prefix
58+
const roomProvider = new RoomProvider(room1);
59+
const completions = await roomProvider.getCompletions("#ro", { beginning: true, start: 0, end: 3 });
60+
61+
// Then we find the two rooms with that prefix, but not the other one
62+
expect(completions).toStrictEqual([
63+
{
64+
type: "room",
65+
completion: room1.getCanonicalAlias(),
66+
completionId: room1.roomId,
67+
component: expect.anything(),
68+
href: "https://matrix.to/#/#room1:e.com",
69+
range: { start: 0, end: 3 },
70+
suffix: " ",
71+
},
72+
{
73+
type: "room",
74+
completion: room2.getCanonicalAlias(),
75+
completionId: room2.roomId,
76+
component: expect.anything(),
77+
href: "https://matrix.to/#/#room2:e.com",
78+
range: { start: 0, end: 3 },
79+
suffix: " ",
80+
},
81+
]);
82+
});
83+
84+
describe("If the feature_dynamic_room_predecessors is not enabled", () => {
85+
beforeEach(() => {
86+
jest.spyOn(SettingsStore, "getValue").mockReturnValue(false);
87+
});
88+
89+
it("Passes through the dynamic predecessor setting", async () => {
90+
const client = stubClient();
91+
const room = makeRoom(client, "room:e.com");
92+
mocked(client.getVisibleRooms).mockReturnValue([room]);
93+
mocked(client.getVisibleRooms).mockClear();
94+
95+
const roomProvider = new RoomProvider(room);
96+
await roomProvider.getCompletions("#ro", { beginning: true, start: 0, end: 3 });
97+
98+
expect(client.getVisibleRooms).toHaveBeenCalledWith(false);
99+
});
100+
});
101+
102+
describe("If the feature_dynamic_room_predecessors is enabled", () => {
103+
beforeEach(() => {
104+
// Turn on feature_dynamic_room_predecessors setting
105+
jest.spyOn(SettingsStore, "getValue").mockImplementation(
106+
(settingName) => settingName === "feature_dynamic_room_predecessors",
107+
);
108+
});
109+
110+
it("Passes through the dynamic predecessor setting", async () => {
111+
const client = stubClient();
112+
const room = makeRoom(client, "room:e.com");
113+
mocked(client.getVisibleRooms).mockReturnValue([room]);
114+
mocked(client.getVisibleRooms).mockClear();
115+
116+
const roomProvider = new RoomProvider(room);
117+
await roomProvider.getCompletions("#ro", { beginning: true, start: 0, end: 3 });
118+
119+
expect(client.getVisibleRooms).toHaveBeenCalledWith(true);
120+
});
121+
});
122+
});
123+
124+
function makeRoom(client: MatrixClient, name: string): Room {
125+
const room = mkRoom(client, `!${name}`);
126+
room.getCanonicalAlias.mockReturnValue(`#${name}`);
127+
return room;
128+
}

0 commit comments

Comments
 (0)