2022-11-18 19:05:00 +00:00
|
|
|
/*
|
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
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 { mocked } from "jest-mock";
|
2024-03-12 14:52:54 +00:00
|
|
|
import { EventType, IContent, KnownMembership, MatrixClient, MatrixEvent, Room } from "matrix-js-sdk/src/matrix";
|
2022-11-18 19:05:00 +00:00
|
|
|
|
|
|
|
import SdkConfig from "../../src/SdkConfig";
|
|
|
|
import SettingsStore from "../../src/settings/SettingsStore";
|
|
|
|
import { MemberListStore } from "../../src/stores/MemberListStore";
|
|
|
|
import { stubClient } from "../test-utils";
|
|
|
|
import { TestSdkContext } from "../TestSdkContext";
|
|
|
|
|
|
|
|
describe("MemberListStore", () => {
|
|
|
|
const alice = "@alice:bar";
|
|
|
|
const bob = "@bob:bar";
|
|
|
|
const charlie = "@charlie:bar";
|
|
|
|
const roomId = "!foo:bar";
|
|
|
|
let store: MemberListStore;
|
|
|
|
let client: MatrixClient;
|
|
|
|
let room: Room;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
const context = new TestSdkContext();
|
|
|
|
client = stubClient();
|
|
|
|
client.baseUrl = "https://invalid.base.url.here";
|
|
|
|
context.client = client;
|
|
|
|
store = new MemberListStore(context);
|
|
|
|
// alice is joined to the room.
|
|
|
|
room = new Room(roomId, client, client.getUserId()!);
|
|
|
|
room.currentState.setStateEvents([
|
|
|
|
new MatrixEvent({
|
|
|
|
type: EventType.RoomCreate,
|
|
|
|
state_key: "",
|
|
|
|
content: {
|
|
|
|
creator: alice,
|
|
|
|
},
|
|
|
|
sender: alice,
|
|
|
|
room_id: roomId,
|
|
|
|
event_id: "$1",
|
|
|
|
}),
|
|
|
|
new MatrixEvent({
|
|
|
|
type: EventType.RoomMember,
|
|
|
|
state_key: alice,
|
|
|
|
content: {
|
2024-03-12 14:52:54 +00:00
|
|
|
membership: KnownMembership.Join,
|
2022-11-18 19:05:00 +00:00
|
|
|
},
|
|
|
|
sender: alice,
|
|
|
|
room_id: roomId,
|
|
|
|
event_id: "$2",
|
|
|
|
}),
|
|
|
|
]);
|
|
|
|
room.recalculate();
|
|
|
|
mocked(client.getRoom).mockImplementation((r: string): Room | null => {
|
|
|
|
if (r === roomId) {
|
|
|
|
return room;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
SdkConfig.put({
|
|
|
|
enable_presence_by_hs_url: {
|
|
|
|
[client.baseUrl]: false,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("loads members in a room", async () => {
|
2024-03-12 14:52:54 +00:00
|
|
|
addMember(room, bob, KnownMembership.Invite);
|
|
|
|
addMember(room, charlie, KnownMembership.Leave);
|
2022-11-18 19:05:00 +00:00
|
|
|
|
|
|
|
const { invited, joined } = await store.loadMemberList(roomId);
|
|
|
|
expect(invited).toEqual([room.getMember(bob)]);
|
|
|
|
expect(joined).toEqual([room.getMember(alice)]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("fails gracefully for invalid rooms", async () => {
|
|
|
|
const { invited, joined } = await store.loadMemberList("!idontexist:bar");
|
|
|
|
expect(invited).toEqual([]);
|
|
|
|
expect(joined).toEqual([]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("sorts by power level", async () => {
|
2024-03-12 14:52:54 +00:00
|
|
|
addMember(room, bob, KnownMembership.Join);
|
|
|
|
addMember(room, charlie, KnownMembership.Join);
|
2022-11-18 19:05:00 +00:00
|
|
|
setPowerLevels(room, {
|
|
|
|
users: {
|
|
|
|
[alice]: 100,
|
|
|
|
[charlie]: 50,
|
|
|
|
},
|
|
|
|
users_default: 10,
|
|
|
|
});
|
|
|
|
|
|
|
|
const { invited, joined } = await store.loadMemberList(roomId);
|
|
|
|
expect(invited).toEqual([]);
|
|
|
|
expect(joined).toEqual([room.getMember(alice), room.getMember(charlie), room.getMember(bob)]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("sorts by name if power level is equal", async () => {
|
|
|
|
const doris = "@doris:bar";
|
2024-03-12 14:52:54 +00:00
|
|
|
addMember(room, bob, KnownMembership.Join);
|
|
|
|
addMember(room, charlie, KnownMembership.Join);
|
2022-11-18 19:05:00 +00:00
|
|
|
setPowerLevels(room, {
|
|
|
|
users_default: 10,
|
|
|
|
});
|
|
|
|
|
|
|
|
let { invited, joined } = await store.loadMemberList(roomId);
|
|
|
|
expect(invited).toEqual([]);
|
|
|
|
expect(joined).toEqual([room.getMember(alice), room.getMember(bob), room.getMember(charlie)]);
|
|
|
|
|
|
|
|
// Ensure it sorts by display name if they are set
|
2024-03-12 14:52:54 +00:00
|
|
|
addMember(room, doris, KnownMembership.Join, "AAAAA");
|
2022-11-18 19:05:00 +00:00
|
|
|
({ invited, joined } = await store.loadMemberList(roomId));
|
|
|
|
expect(invited).toEqual([]);
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(joined).toEqual([
|
|
|
|
room.getMember(doris),
|
|
|
|
room.getMember(alice),
|
|
|
|
room.getMember(bob),
|
|
|
|
room.getMember(charlie),
|
|
|
|
]);
|
2022-11-18 19:05:00 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("filters based on a search query", async () => {
|
|
|
|
const mice = "@mice:bar";
|
|
|
|
const zorro = "@zorro:bar";
|
2024-03-12 14:52:54 +00:00
|
|
|
addMember(room, bob, KnownMembership.Join);
|
|
|
|
addMember(room, mice, KnownMembership.Join);
|
2022-11-18 19:05:00 +00:00
|
|
|
|
|
|
|
let { invited, joined } = await store.loadMemberList(roomId, "ice");
|
|
|
|
expect(invited).toEqual([]);
|
|
|
|
expect(joined).toEqual([room.getMember(alice), room.getMember(mice)]);
|
|
|
|
|
|
|
|
// Ensure it filters by display name if they are set
|
2024-03-12 14:52:54 +00:00
|
|
|
addMember(room, zorro, KnownMembership.Join, "ice ice baby");
|
2022-11-18 19:05:00 +00:00
|
|
|
({ invited, joined } = await store.loadMemberList(roomId, "ice"));
|
|
|
|
expect(invited).toEqual([]);
|
|
|
|
expect(joined).toEqual([room.getMember(alice), room.getMember(zorro), room.getMember(mice)]);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("lazy loading", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
mocked(client.hasLazyLoadMembersEnabled).mockReturnValue(true);
|
|
|
|
room.loadMembersIfNeeded = jest.fn();
|
|
|
|
mocked(room.loadMembersIfNeeded).mockResolvedValue(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("calls Room.loadMembersIfNeeded once when enabled", async () => {
|
|
|
|
let { joined } = await store.loadMemberList(roomId);
|
|
|
|
expect(joined).toEqual([room.getMember(alice)]);
|
|
|
|
expect(room.loadMembersIfNeeded).toHaveBeenCalledTimes(1);
|
|
|
|
({ joined } = await store.loadMemberList(roomId));
|
|
|
|
expect(joined).toEqual([room.getMember(alice)]);
|
|
|
|
expect(room.loadMembersIfNeeded).toHaveBeenCalledTimes(1);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("sliding sync", () => {
|
|
|
|
beforeEach(() => {
|
2022-12-12 11:24:14 +00:00
|
|
|
jest.spyOn(SettingsStore, "getValue").mockImplementation((settingName, roomId, value) => {
|
2022-11-18 19:05:00 +00:00
|
|
|
return settingName === "feature_sliding_sync"; // this is enabled, everything else is disabled.
|
|
|
|
});
|
|
|
|
client.members = jest.fn();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("calls /members when lazy loading", async () => {
|
|
|
|
mocked(client.members).mockResolvedValue({
|
|
|
|
chunk: [
|
|
|
|
{
|
|
|
|
type: EventType.RoomMember,
|
|
|
|
state_key: bob,
|
|
|
|
content: {
|
2024-03-12 14:52:54 +00:00
|
|
|
membership: KnownMembership.Join,
|
2022-11-18 19:05:00 +00:00
|
|
|
displayname: "Bob",
|
|
|
|
},
|
|
|
|
sender: bob,
|
|
|
|
room_id: room.roomId,
|
|
|
|
event_id: "$" + Math.random(),
|
|
|
|
origin_server_ts: 2,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
const { joined } = await store.loadMemberList(roomId);
|
|
|
|
expect(joined).toEqual([room.getMember(alice), room.getMember(bob)]);
|
|
|
|
expect(client.members).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("does not use lazy loading on encrypted rooms", async () => {
|
|
|
|
client.isRoomEncrypted = jest.fn();
|
|
|
|
mocked(client.isRoomEncrypted).mockReturnValue(true);
|
|
|
|
|
|
|
|
const { joined } = await store.loadMemberList(roomId);
|
|
|
|
expect(joined).toEqual([room.getMember(alice)]);
|
|
|
|
expect(client.members).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
function addEventToRoom(room: Room, ev: MatrixEvent) {
|
|
|
|
room.getLiveTimeline().addEvent(ev, {
|
|
|
|
toStartOfTimeline: false,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function setPowerLevels(room: Room, pl: IContent) {
|
2022-12-12 11:24:14 +00:00
|
|
|
addEventToRoom(
|
|
|
|
room,
|
|
|
|
new MatrixEvent({
|
|
|
|
type: EventType.RoomPowerLevels,
|
|
|
|
state_key: "",
|
|
|
|
content: pl,
|
|
|
|
sender: room.getCreator()!,
|
|
|
|
room_id: room.roomId,
|
|
|
|
event_id: "$" + Math.random(),
|
|
|
|
}),
|
|
|
|
);
|
2022-11-18 19:05:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function addMember(room: Room, userId: string, membership: string, displayName?: string) {
|
2022-12-12 11:24:14 +00:00
|
|
|
addEventToRoom(
|
|
|
|
room,
|
|
|
|
new MatrixEvent({
|
|
|
|
type: EventType.RoomMember,
|
|
|
|
state_key: userId,
|
|
|
|
content: {
|
|
|
|
membership: membership,
|
|
|
|
displayname: displayName,
|
|
|
|
},
|
|
|
|
sender: userId,
|
|
|
|
room_id: room.roomId,
|
|
|
|
event_id: "$" + Math.random(),
|
|
|
|
}),
|
|
|
|
);
|
2022-11-18 19:05:00 +00:00
|
|
|
}
|