Merge matrix-react-sdk into element-web

Merge remote-tracking branch 'repomerge/t3chguy/repomerge' into t3chguy/repo-merge

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
This commit is contained in:
Michael Telatynski 2024-10-15 14:57:26 +01:00
commit f0ee7f7905
No known key found for this signature in database
GPG key ID: A2B008A5F49F5D0D
3265 changed files with 484599 additions and 699 deletions

View file

@ -0,0 +1,98 @@
/*
Copyright 2024 New Vector Ltd.
Copyright 2022 The Matrix.org Foundation C.I.C.
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
Please see LICENSE files in the repository root for full details.
*/
import { mocked } from "jest-mock";
import { JoinRule, Room } from "matrix-js-sdk/src/matrix";
import { KnownMembership } from "matrix-js-sdk/src/types";
import { shouldShowComponent } from "../../../../src/customisations/helpers/UIComponents";
import { UIComponent } from "../../../../src/settings/UIFeature";
import { canInviteTo } from "../../../../src/utils/room/canInviteTo";
import { getMockClientWithEventEmitter, mockClientMethodsUser } from "../../../test-utils";
jest.mock("../../../../src/customisations/helpers/UIComponents", () => ({
shouldShowComponent: jest.fn(),
}));
describe("canInviteTo()", () => {
afterEach(() => {
jest.restoreAllMocks();
});
const userId = "@alice:server.org";
const roomId = "!room:server.org";
const makeRoom = (): Room => {
const client = getMockClientWithEventEmitter({
...mockClientMethodsUser(userId),
});
const room = new Room(roomId, client, userId);
jest.spyOn(room, "getMyMembership").mockReturnValue(KnownMembership.Join);
jest.spyOn(room, "getJoinRule").mockReturnValue(JoinRule.Public);
jest.spyOn(room, "canInvite").mockReturnValue(true);
return room;
};
beforeEach(() => {
mocked(shouldShowComponent).mockReturnValue(true);
});
describe("when user has permissions to issue an invite for this room", () => {
// aka when Room.canInvite is true
it("should return false when current user membership is not joined", () => {
const room = makeRoom();
jest.spyOn(room, "getMyMembership").mockReturnValue(KnownMembership.Invite);
expect(canInviteTo(room)).toEqual(false);
});
it("should return false when UIComponent.InviteUsers customisation hides invite", () => {
const room = makeRoom();
mocked(shouldShowComponent).mockReturnValue(false);
expect(canInviteTo(room)).toEqual(false);
expect(shouldShowComponent).toHaveBeenCalledWith(UIComponent.InviteUsers);
});
it("should return true when user can invite and is a room member", () => {
const room = makeRoom();
expect(canInviteTo(room)).toEqual(true);
});
});
describe("when user does not have permissions to issue an invite for this room", () => {
// aka when Room.canInvite is false
it("should return false when room is a private space", () => {
const room = makeRoom();
jest.spyOn(room, "getJoinRule").mockReturnValue(JoinRule.Invite);
jest.spyOn(room, "isSpaceRoom").mockReturnValue(true);
jest.spyOn(room, "canInvite").mockReturnValue(false);
expect(canInviteTo(room)).toEqual(false);
});
it("should return false when room is just a room", () => {
const room = makeRoom();
jest.spyOn(room, "canInvite").mockReturnValue(false);
expect(canInviteTo(room)).toEqual(false);
});
it("should return true when room is a public space", () => {
const room = makeRoom();
// default join rule is public
jest.spyOn(room, "isSpaceRoom").mockReturnValue(true);
jest.spyOn(room, "canInvite").mockReturnValue(false);
expect(canInviteTo(room)).toEqual(true);
});
});
});

View file

@ -0,0 +1,79 @@
/*
Copyright 2024 New Vector Ltd.
Copyright 2022 The Matrix.org Foundation C.I.C.
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
Please see LICENSE files in the repository root for full details.
*/
import { mocked } from "jest-mock";
import { MatrixClient, Room, RoomMember } from "matrix-js-sdk/src/matrix";
import { getFunctionalMembers } from "../../../../src/utils/room/getFunctionalMembers";
import { getJoinedNonFunctionalMembers } from "../../../../src/utils/room/getJoinedNonFunctionalMembers";
jest.mock("../../../../src/utils/room/getFunctionalMembers", () => ({
getFunctionalMembers: jest.fn(),
}));
describe("getJoinedNonFunctionalMembers", () => {
let room: Room;
let roomMember1: RoomMember;
let roomMember2: RoomMember;
beforeEach(() => {
room = new Room("!room:example.com", {} as unknown as MatrixClient, "@user:example.com");
room.getJoinedMembers = jest.fn();
roomMember1 = new RoomMember(room.roomId, "@user1:example.com");
roomMember2 = new RoomMember(room.roomId, "@user2:example.com");
});
describe("if there are no members", () => {
beforeEach(() => {
mocked(room.getJoinedMembers).mockReturnValue([]);
mocked(getFunctionalMembers).mockReturnValue([]);
});
it("should return an empty list", () => {
expect(getJoinedNonFunctionalMembers(room)).toHaveLength(0);
});
});
describe("if there are only regular room members", () => {
beforeEach(() => {
mocked(room.getJoinedMembers).mockReturnValue([roomMember1, roomMember2]);
mocked(getFunctionalMembers).mockReturnValue([]);
});
it("should return the room members", () => {
const members = getJoinedNonFunctionalMembers(room);
expect(members).toContain(roomMember1);
expect(members).toContain(roomMember2);
});
});
describe("if there are only functional room members", () => {
beforeEach(() => {
mocked(room.getJoinedMembers).mockReturnValue([]);
mocked(getFunctionalMembers).mockReturnValue(["@functional:example.com"]);
});
it("should return an empty list", () => {
expect(getJoinedNonFunctionalMembers(room)).toHaveLength(0);
});
});
describe("if there are some functional room members", () => {
beforeEach(() => {
mocked(room.getJoinedMembers).mockReturnValue([roomMember1, roomMember2]);
mocked(getFunctionalMembers).mockReturnValue([roomMember1.userId]);
});
it("should only return the non-functional members", () => {
const members = getJoinedNonFunctionalMembers(room);
expect(members).not.toContain(roomMember1);
expect(members).toContain(roomMember2);
});
});
});

View file

@ -0,0 +1,49 @@
/*
Copyright 2024 New Vector Ltd.
Copyright 2022 The Matrix.org Foundation C.I.C.
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
Please see LICENSE files in the repository root for full details.
*/
import { Room, UNSTABLE_ELEMENT_FUNCTIONAL_USERS } from "matrix-js-sdk/src/matrix";
import { getFunctionalMembers } from "../../../../src/utils/room/getFunctionalMembers";
import { createTestClient, mkEvent } from "../../../test-utils";
describe("getRoomFunctionalMembers", () => {
const client = createTestClient();
const room = new Room("!room:example.com", client, client.getUserId()!);
it("should return an empty array if no functional members state event exists", () => {
expect(getFunctionalMembers(room)).toHaveLength(0);
});
it("should return an empty array if functional members state event does not have a service_members field", () => {
room.currentState.setStateEvents([
mkEvent({
event: true,
type: UNSTABLE_ELEMENT_FUNCTIONAL_USERS.name,
user: "@user:example.com)",
room: room.roomId,
skey: "",
content: {},
}),
]);
expect(getFunctionalMembers(room)).toHaveLength(0);
});
it("should return service_members field of the functional users state event", () => {
room.currentState.setStateEvents([
mkEvent({
event: true,
type: UNSTABLE_ELEMENT_FUNCTIONAL_USERS.name,
user: "@user:example.com)",
room: room.roomId,
skey: "",
content: { service_members: ["@user:example.com"] },
}),
]);
expect(getFunctionalMembers(room)).toEqual(["@user:example.com"]);
});
});

View file

@ -0,0 +1,57 @@
/*
Copyright 2024 New Vector Ltd.
Copyright 2022 The Matrix.org Foundation C.I.C.
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
Please see LICENSE files in the repository root for full details.
*/
import { Room } from "matrix-js-sdk/src/matrix";
import defaultDispatcher from "../../../../src/dispatcher/dispatcher";
import { inviteToRoom } from "../../../../src/utils/room/inviteToRoom";
import { getMockClientWithEventEmitter } from "../../../test-utils";
describe("inviteToRoom()", () => {
const userId = "@alice:server.org";
const roomId = "!room:server.org";
const makeRoom = (): Room => {
const client = getMockClientWithEventEmitter({
isGuest: jest.fn(),
});
const room = new Room(roomId, client, userId);
return room;
};
beforeEach(() => {
// stub
jest.spyOn(defaultDispatcher, "dispatch").mockImplementation(() => {});
});
afterEach(() => {
jest.restoreAllMocks();
});
it("requires registration when a guest tries to invite to a room", () => {
const room = makeRoom();
jest.spyOn(room.client, "isGuest").mockReturnValue(true);
inviteToRoom(room);
expect(defaultDispatcher.dispatch).toHaveBeenCalledTimes(1);
expect(defaultDispatcher.dispatch).toHaveBeenCalledWith({ action: "require_registration" });
});
it("opens the room inviter", () => {
const room = makeRoom();
jest.spyOn(room.client, "isGuest").mockReturnValue(false);
inviteToRoom(room);
expect(defaultDispatcher.dispatch).toHaveBeenCalledTimes(1);
expect(defaultDispatcher.dispatch).toHaveBeenCalledWith({ action: "view_invite", roomId });
});
});

View file

@ -0,0 +1,102 @@
/*
Copyright 2024 New Vector Ltd.
Copyright 2023 The Matrix.org Foundation C.I.C.
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
Please see LICENSE files in the repository root for full details.
*/
import { mocked } from "jest-mock";
import { MatrixClient, MatrixEvent, Room } from "matrix-js-sdk/src/matrix";
import DMRoomMap from "../../../../src/utils/DMRoomMap";
import { shouldEncryptRoomWithSingle3rdPartyInvite } from "../../../../src/utils/room/shouldEncryptRoomWithSingle3rdPartyInvite";
import { privateShouldBeEncrypted } from "../../../../src/utils/rooms";
import { mkRoomMemberJoinEvent, mkThirdPartyInviteEvent, stubClient } from "../../../test-utils";
jest.mock("../../../../src/utils/rooms", () => ({
privateShouldBeEncrypted: jest.fn(),
}));
describe("shouldEncryptRoomWithSingle3rdPartyInvite", () => {
let client: MatrixClient;
let thirdPartyInviteEvent: MatrixEvent;
let roomWithOneThirdPartyInvite: Room;
beforeAll(() => {
client = stubClient();
DMRoomMap.makeShared(client);
});
beforeEach(() => {
roomWithOneThirdPartyInvite = new Room("!room1:example.com", client, client.getSafeUserId());
thirdPartyInviteEvent = mkThirdPartyInviteEvent(
client.getSafeUserId(),
"user@example.com",
roomWithOneThirdPartyInvite.roomId,
);
roomWithOneThirdPartyInvite.currentState.setStateEvents([
mkRoomMemberJoinEvent(client.getSafeUserId(), roomWithOneThirdPartyInvite.roomId),
thirdPartyInviteEvent,
]);
jest.spyOn(DMRoomMap.shared(), "getRoomIds").mockReturnValue(new Set([roomWithOneThirdPartyInvite.roomId]));
});
describe("when well-known promotes encryption", () => {
beforeEach(() => {
mocked(privateShouldBeEncrypted).mockReturnValue(true);
});
it("should return true + invite event for a DM room with one third-party invite", () => {
expect(shouldEncryptRoomWithSingle3rdPartyInvite(roomWithOneThirdPartyInvite)).toEqual({
shouldEncrypt: true,
inviteEvent: thirdPartyInviteEvent,
});
});
it("should return false for a non-DM room with one third-party invite", () => {
mocked(DMRoomMap.shared().getRoomIds).mockReturnValue(new Set());
expect(shouldEncryptRoomWithSingle3rdPartyInvite(roomWithOneThirdPartyInvite)).toEqual({
shouldEncrypt: false,
});
});
it("should return false for a DM room with two members", () => {
roomWithOneThirdPartyInvite.currentState.setStateEvents([
mkRoomMemberJoinEvent("@user2:example.com", roomWithOneThirdPartyInvite.roomId),
]);
expect(shouldEncryptRoomWithSingle3rdPartyInvite(roomWithOneThirdPartyInvite)).toEqual({
shouldEncrypt: false,
});
});
it("should return false for a DM room with two third-party invites", () => {
roomWithOneThirdPartyInvite.currentState.setStateEvents([
mkThirdPartyInviteEvent(
client.getSafeUserId(),
"user2@example.com",
roomWithOneThirdPartyInvite.roomId,
),
]);
expect(shouldEncryptRoomWithSingle3rdPartyInvite(roomWithOneThirdPartyInvite)).toEqual({
shouldEncrypt: false,
});
});
});
describe("when well-known does not promote encryption", () => {
beforeEach(() => {
mocked(privateShouldBeEncrypted).mockReturnValue(false);
});
it("should return false for a DM room with one third-party invite", () => {
expect(shouldEncryptRoomWithSingle3rdPartyInvite(roomWithOneThirdPartyInvite)).toEqual({
shouldEncrypt: false,
});
});
});
});

View file

@ -0,0 +1,146 @@
/*
Copyright 2024 New Vector Ltd.
Copyright 2022 The Matrix.org Foundation C.I.C.
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
Please see LICENSE files in the repository root for full details.
*/
import { Room } from "matrix-js-sdk/src/matrix";
import RoomListActions from "../../../../src/actions/RoomListActions";
import defaultDispatcher from "../../../../src/dispatcher/dispatcher";
import { DefaultTagID, TagID } from "../../../../src/stores/room-list/models";
import RoomListStore from "../../../../src/stores/room-list/RoomListStore";
import { tagRoom } from "../../../../src/utils/room/tagRoom";
import { getMockClientWithEventEmitter } from "../../../test-utils";
describe("tagRoom()", () => {
const userId = "@alice:server.org";
const roomId = "!room:server.org";
const makeRoom = (tags: TagID[] = []): Room => {
const client = getMockClientWithEventEmitter({
isGuest: jest.fn(),
});
const room = new Room(roomId, client, userId);
jest.spyOn(RoomListStore.instance, "getTagsForRoom").mockReturnValue(tags);
return room;
};
beforeEach(() => {
// stub
jest.spyOn(defaultDispatcher, "dispatch").mockImplementation(() => {});
jest.spyOn(RoomListActions, "tagRoom").mockReturnValue({ action: "mocked_tag_room_action", fn: () => {} });
});
afterEach(() => {
jest.restoreAllMocks();
});
it("does nothing when room tag is not allowed", () => {
const room = makeRoom();
tagRoom(room, DefaultTagID.ServerNotice);
expect(defaultDispatcher.dispatch).not.toHaveBeenCalled();
expect(RoomListActions.tagRoom).not.toHaveBeenCalled();
});
describe("when a room has no tags", () => {
it("should tag a room as favourite", () => {
const room = makeRoom();
tagRoom(room, DefaultTagID.Favourite);
expect(defaultDispatcher.dispatch).toHaveBeenCalled();
expect(RoomListActions.tagRoom).toHaveBeenCalledWith(
room.client,
room,
DefaultTagID.LowPriority, // remove
DefaultTagID.Favourite, // add
0,
);
});
it("should tag a room low priority", () => {
const room = makeRoom();
tagRoom(room, DefaultTagID.LowPriority);
expect(defaultDispatcher.dispatch).toHaveBeenCalled();
expect(RoomListActions.tagRoom).toHaveBeenCalledWith(
room.client,
room,
DefaultTagID.Favourite, // remove
DefaultTagID.LowPriority, // add
0,
);
});
});
describe("when a room is tagged as favourite", () => {
it("should unfavourite a room", () => {
const room = makeRoom([DefaultTagID.Favourite]);
tagRoom(room, DefaultTagID.Favourite);
expect(defaultDispatcher.dispatch).toHaveBeenCalled();
expect(RoomListActions.tagRoom).toHaveBeenCalledWith(
room.client,
room,
DefaultTagID.Favourite, // remove
null, // add
0,
);
});
it("should tag a room low priority", () => {
const room = makeRoom([DefaultTagID.Favourite]);
tagRoom(room, DefaultTagID.LowPriority);
expect(defaultDispatcher.dispatch).toHaveBeenCalled();
expect(RoomListActions.tagRoom).toHaveBeenCalledWith(
room.client,
room,
DefaultTagID.Favourite, // remove
DefaultTagID.LowPriority, // add
0,
);
});
});
describe("when a room is tagged as low priority", () => {
it("should favourite a room", () => {
const room = makeRoom([DefaultTagID.LowPriority]);
tagRoom(room, DefaultTagID.Favourite);
expect(defaultDispatcher.dispatch).toHaveBeenCalled();
expect(RoomListActions.tagRoom).toHaveBeenCalledWith(
room.client,
room,
DefaultTagID.LowPriority, // remove
DefaultTagID.Favourite, // add
0,
);
});
it("should untag a room low priority", () => {
const room = makeRoom([DefaultTagID.LowPriority]);
tagRoom(room, DefaultTagID.LowPriority);
expect(defaultDispatcher.dispatch).toHaveBeenCalled();
expect(RoomListActions.tagRoom).toHaveBeenCalledWith(
room.client,
room,
DefaultTagID.LowPriority, // remove
null, // add
0,
);
});
});
});