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,107 @@
/*
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 { EventType, KNOWN_SAFE_ROOM_VERSION, MatrixClient } from "matrix-js-sdk/src/matrix";
import { KnownMembership } from "matrix-js-sdk/src/types";
import { canEncryptToAllUsers } from "../../../../src/createRoom";
import { LocalRoom, LOCAL_ROOM_ID_PREFIX } from "../../../../src/models/LocalRoom";
import { DirectoryMember, Member, ThreepidMember } from "../../../../src/utils/direct-messages";
import { createDmLocalRoom } from "../../../../src/utils/dm/createDmLocalRoom";
import { privateShouldBeEncrypted } from "../../../../src/utils/rooms";
import { createTestClient } from "../../../test-utils";
jest.mock("../../../../src/utils/rooms", () => ({
privateShouldBeEncrypted: jest.fn(),
}));
jest.mock("../../../../src/createRoom", () => ({
canEncryptToAllUsers: jest.fn(),
}));
function assertLocalRoom(room: LocalRoom, targets: Member[], encrypted: boolean) {
expect(room.roomId).toBe(LOCAL_ROOM_ID_PREFIX + "t1");
expect(room.name).toBe(targets.length ? targets[0].name : "Empty Room");
expect(room.encrypted).toBe(encrypted);
expect(room.targets).toEqual(targets);
expect(room.getMyMembership()).toBe(KnownMembership.Join);
const roomCreateEvent = room.currentState.getStateEvents(EventType.RoomCreate)[0];
expect(roomCreateEvent).toBeDefined();
expect(roomCreateEvent.getContent()["room_version"]).toBe(KNOWN_SAFE_ROOM_VERSION);
// check that the user and all targets are joined
expect(room.getMember("@userId:matrix.org")?.membership).toBe(KnownMembership.Join);
targets.forEach((target: Member) => {
expect(room.getMember(target.userId)?.membership).toBe(KnownMembership.Join);
});
if (encrypted) {
const encryptionEvent = room.currentState.getStateEvents(EventType.RoomEncryption)[0];
expect(encryptionEvent).toBeDefined();
}
}
describe("createDmLocalRoom", () => {
let mockClient: MatrixClient;
const userId1 = "@user1:example.com";
const member1 = new DirectoryMember({ user_id: userId1 });
const member2 = new ThreepidMember("user2");
beforeEach(() => {
mockClient = createTestClient();
});
describe("when rooms should be encrypted", () => {
beforeEach(() => {
mocked(privateShouldBeEncrypted).mockReturnValue(true);
});
it("should create an encrytped room for 3PID targets", async () => {
const room = await createDmLocalRoom(mockClient, [member2]);
expect(mockClient.store.storeRoom).toHaveBeenCalledWith(room);
assertLocalRoom(room, [member2], true);
});
describe("for MXID targets with encryption available", () => {
beforeEach(() => {
mocked(canEncryptToAllUsers).mockResolvedValue(true);
});
it("should create an encrypted room", async () => {
const room = await createDmLocalRoom(mockClient, [member1]);
expect(mockClient.store.storeRoom).toHaveBeenCalledWith(room);
assertLocalRoom(room, [member1], true);
});
});
describe("for MXID targets with encryption unavailable", () => {
beforeEach(() => {
mocked(canEncryptToAllUsers).mockResolvedValue(false);
});
it("should create an unencrypted room", async () => {
const room = await createDmLocalRoom(mockClient, [member1]);
expect(mockClient.store.storeRoom).toHaveBeenCalledWith(room);
assertLocalRoom(room, [member1], false);
});
});
});
describe("if rooms should not be encrypted", () => {
beforeEach(() => {
mocked(privateShouldBeEncrypted).mockReturnValue(false);
});
it("should create an unencrypted room", async () => {
const room = await createDmLocalRoom(mockClient, [member1]);
assertLocalRoom(room, [member1], false);
});
});
});

View file

@ -0,0 +1,69 @@
/*
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 { filterValidMDirect } from "../../../../src/utils/dm/filterValidMDirect";
const roomId1 = "!room1:example.com";
const roomId2 = "!room2:example.com";
const userId1 = "@user1:example.com";
const userId2 = "@user2:example.com";
const userId3 = "@user3:example.com";
describe("filterValidMDirect", () => {
it("should return an empty object as valid content", () => {
expect(filterValidMDirect({})).toEqual({
valid: true,
filteredContent: {},
});
});
it("should return valid content", () => {
expect(
filterValidMDirect({
[userId1]: [roomId1, roomId2],
[userId2]: [roomId1],
}),
).toEqual({
valid: true,
filteredContent: {
[userId1]: [roomId1, roomId2],
[userId2]: [roomId1],
},
});
});
it("should return an empy object for null", () => {
expect(filterValidMDirect(null)).toEqual({
valid: false,
filteredContent: {},
});
});
it("should return an empy object for a non-object", () => {
expect(filterValidMDirect(23)).toEqual({
valid: false,
filteredContent: {},
});
});
it("should only return valid content", () => {
const invalidContent = {
[userId1]: [23],
[userId2]: [roomId2],
[userId3]: "room1",
};
expect(filterValidMDirect(invalidContent)).toEqual({
valid: false,
filteredContent: {
[userId1]: [],
[userId2]: [roomId2],
},
});
});
});

View file

@ -0,0 +1,178 @@
/*
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 } from "matrix-js-sdk/src/matrix";
import { KnownMembership } from "matrix-js-sdk/src/types";
import DMRoomMap from "../../../../src/utils/DMRoomMap";
import { createTestClient, makeMembershipEvent, mkThirdPartyInviteEvent } from "../../../test-utils";
import { LocalRoom } from "../../../../src/models/LocalRoom";
import { findDMForUser } from "../../../../src/utils/dm/findDMForUser";
import { getFunctionalMembers } from "../../../../src/utils/room/getFunctionalMembers";
jest.mock("../../../../src/utils/room/getFunctionalMembers", () => ({
getFunctionalMembers: jest.fn(),
}));
describe("findDMForUser", () => {
const userId1 = "@user1:example.com";
const userId2 = "@user2:example.com";
const userId3 = "@user3:example.com";
const botId = "@bot:example.com";
const thirdPartyId = "party@example.com";
let room1: Room;
let room2: LocalRoom;
let room3: Room;
let room4: Room;
let room5: Room;
let room6: Room;
let room7: Room;
const unknownRoomId = "!unknown:example.com";
let dmRoomMap: DMRoomMap;
let mockClient: MatrixClient;
beforeEach(() => {
mockClient = createTestClient();
// always return the bot user as functional member
mocked(getFunctionalMembers).mockReturnValue([botId]);
room1 = new Room("!room1:example.com", mockClient, userId1);
room1.getMyMembership = () => KnownMembership.Join;
room1.currentState.setStateEvents([
makeMembershipEvent(room1.roomId, userId1, KnownMembership.Join),
makeMembershipEvent(room1.roomId, userId2, KnownMembership.Join),
]);
// this should not be a DM room because it is a local room
room2 = new LocalRoom("!room2:example.com", mockClient, userId1);
room2.getMyMembership = () => KnownMembership.Join;
room2.getLastActiveTimestamp = () => 100;
room3 = new Room("!room3:example.com", mockClient, userId1);
room3.getMyMembership = () => KnownMembership.Join;
room3.currentState.setStateEvents([
makeMembershipEvent(room3.roomId, userId1, KnownMembership.Join),
makeMembershipEvent(room3.roomId, userId2, KnownMembership.Join),
// Adding the bot user here. Should be excluded when determining if the room is a DM.
makeMembershipEvent(room3.roomId, botId, KnownMembership.Join),
]);
// this should not be a DM room because it has only one joined user
room4 = new Room("!room4:example.com", mockClient, userId1);
room4.getMyMembership = () => KnownMembership.Join;
room4.currentState.setStateEvents([
makeMembershipEvent(room4.roomId, userId1, KnownMembership.Invite),
makeMembershipEvent(room4.roomId, userId2, KnownMembership.Join),
]);
// this should not be a DM room because it has no users
room5 = new Room("!room5:example.com", mockClient, userId1);
room5.getLastActiveTimestamp = () => 100;
// room not correctly stored in userId → room map; should be found by the "all rooms" fallback
room6 = new Room("!room6:example.com", mockClient, userId1);
room6.getMyMembership = () => KnownMembership.Join;
room6.currentState.setStateEvents([
makeMembershipEvent(room6.roomId, userId1, KnownMembership.Join),
makeMembershipEvent(room6.roomId, userId3, KnownMembership.Join),
]);
// room with pending third-party invite
room7 = new Room("!room7:example.com", mockClient, userId1);
room7.getMyMembership = () => KnownMembership.Join;
room7.currentState.setStateEvents([
makeMembershipEvent(room7.roomId, userId1, KnownMembership.Join),
mkThirdPartyInviteEvent(thirdPartyId, "third-party", room7.roomId),
]);
mocked(mockClient.getRoom).mockImplementation((roomId: string) => {
return (
{
[room1.roomId]: room1,
[room2.roomId]: room2,
[room3.roomId]: room3,
[room4.roomId]: room4,
[room5.roomId]: room5,
[room6.roomId]: room6,
[room7.roomId]: room7,
}[roomId] || null
);
});
dmRoomMap = {
getDMRoomForIdentifiers: jest.fn(),
getDMRoomsForUserId: jest.fn(),
getRoomIds: jest.fn().mockReturnValue(
new Set([
room1.roomId,
room2.roomId,
room3.roomId,
room4.roomId,
room5.roomId,
room6.roomId,
room7.roomId,
unknownRoomId, // this room does not exist in client
]),
),
} as unknown as DMRoomMap;
jest.spyOn(DMRoomMap, "shared").mockReturnValue(dmRoomMap);
mocked(dmRoomMap.getDMRoomsForUserId).mockImplementation((userId: string) => {
if (userId === userId1) {
return [room1.roomId, room2.roomId, room3.roomId, room4.roomId, room5.roomId, unknownRoomId];
}
if (userId === thirdPartyId) {
return [room7.roomId];
}
return [];
});
});
describe("for an empty DM room list", () => {
beforeEach(() => {
mocked(dmRoomMap.getDMRoomsForUserId).mockReturnValue([]);
mocked(dmRoomMap.getRoomIds).mockReturnValue(new Set());
});
it("should return undefined", () => {
expect(findDMForUser(mockClient, userId1)).toBeUndefined();
});
});
it("should find a room ordered by last activity 1", () => {
room1.getLastActiveTimestamp = () => 2;
room3.getLastActiveTimestamp = () => 1;
expect(findDMForUser(mockClient, userId1)).toBe(room1);
});
it("should find a room ordered by last activity 2", () => {
room1.getLastActiveTimestamp = () => 1;
room3.getLastActiveTimestamp = () => 2;
expect(findDMForUser(mockClient, userId1)).toBe(room3);
});
it("should find a room by the 'all rooms' fallback", () => {
room1.getLastActiveTimestamp = () => 1;
room6.getLastActiveTimestamp = () => 2;
expect(findDMForUser(mockClient, userId3)).toBe(room6);
});
it("should find a room with a pending third-party invite", () => {
expect(findDMForUser(mockClient, thirdPartyId)).toBe(room7);
});
it("should not find a room for an unknown Id", () => {
expect(findDMForUser(mockClient, "@unknown:example.com")).toBe(undefined);
});
});

View file

@ -0,0 +1,62 @@
/*
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 } from "matrix-js-sdk/src/matrix";
import { MatrixClientPeg } from "../../../../src/MatrixClientPeg";
import { DirectoryMember, ThreepidMember } from "../../../../src/utils/direct-messages";
import { findDMForUser } from "../../../../src/utils/dm/findDMForUser";
import { findDMRoom } from "../../../../src/utils/dm/findDMRoom";
import DMRoomMap from "../../../../src/utils/DMRoomMap";
import { createTestClient } from "../../../test-utils";
jest.mock("../../../../src/utils/dm/findDMForUser", () => ({
findDMForUser: jest.fn(),
}));
describe("findDMRoom", () => {
const userId1 = "@user1:example.com";
const member1 = new DirectoryMember({ user_id: userId1 });
const member2 = new ThreepidMember("user2");
let mockClient: MatrixClient;
let room1: Room;
let dmRoomMap: DMRoomMap;
beforeEach(() => {
mockClient = createTestClient();
jest.spyOn(MatrixClientPeg, "get").mockReturnValue(mockClient);
room1 = new Room("!room1:example.com", mockClient, userId1);
dmRoomMap = {
getDMRoomForIdentifiers: jest.fn(),
getDMRoomsForUserId: jest.fn(),
} as unknown as DMRoomMap;
jest.spyOn(DMRoomMap, "shared").mockReturnValue(dmRoomMap);
});
it("should return the room for a single target with a room", () => {
mocked(findDMForUser).mockReturnValue(room1);
expect(findDMRoom(mockClient, [member1])).toBe(room1);
});
it("should return undefined for a single target without a room", () => {
mocked(findDMForUser).mockReturnValue(undefined);
expect(findDMRoom(mockClient, [member1])).toBeNull();
});
it("should return the room for 2 targets with a room", () => {
mocked(dmRoomMap.getDMRoomForIdentifiers).mockReturnValue(room1);
expect(findDMRoom(mockClient, [member1, member2])).toBe(room1);
});
it("should return null for 2 targets without a room", () => {
mocked(dmRoomMap.getDMRoomForIdentifiers).mockReturnValue(null);
expect(findDMRoom(mockClient, [member1, member2])).toBeNull();
});
});