Find DMs with pending third-paty invites (#10256)
parent
10a765472b
commit
ffa047be68
|
@ -38,7 +38,15 @@ function extractSuitableRoom(rooms: Room[], userId: string): Room | undefined {
|
||||||
(m) => !functionalUsers.includes(m.userId) && m.membership && isJoinedOrNearlyJoined(m.membership),
|
(m) => !functionalUsers.includes(m.userId) && m.membership && isJoinedOrNearlyJoined(m.membership),
|
||||||
);
|
);
|
||||||
const otherMember = joinedMembers.find((m) => m.userId === userId);
|
const otherMember = joinedMembers.find((m) => m.userId === userId);
|
||||||
return otherMember && joinedMembers.length === 2;
|
|
||||||
|
if (otherMember && joinedMembers.length === 2) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
const thirdPartyInvites = r.currentState.getStateEvents("m.room.third_party_invite") || [];
|
||||||
|
|
||||||
|
// match room with pending third-party invite
|
||||||
|
return joinedMembers.length === 1 && thirdPartyInvites.length === 1;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
})
|
})
|
||||||
|
|
|
@ -691,6 +691,19 @@ export const mkRoomMemberJoinEvent = (user: string, room: string): MatrixEvent =
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const mkThirdPartyInviteEvent = (user: string, displayName: string, room: string): MatrixEvent => {
|
||||||
|
return mkEvent({
|
||||||
|
event: true,
|
||||||
|
type: EventType.RoomThirdPartyInvite,
|
||||||
|
content: {
|
||||||
|
display_name: displayName,
|
||||||
|
},
|
||||||
|
skey: "test" + Math.random(),
|
||||||
|
user,
|
||||||
|
room,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
export const mkPusher = (extra: Partial<IPusher> = {}): IPusher => ({
|
export const mkPusher = (extra: Partial<IPusher> = {}): IPusher => ({
|
||||||
app_display_name: "app",
|
app_display_name: "app",
|
||||||
app_id: "123",
|
app_id: "123",
|
||||||
|
|
|
@ -18,7 +18,7 @@ import { mocked } from "jest-mock";
|
||||||
import { MatrixClient, Room } from "matrix-js-sdk/src/matrix";
|
import { MatrixClient, Room } from "matrix-js-sdk/src/matrix";
|
||||||
|
|
||||||
import DMRoomMap from "../../../src/utils/DMRoomMap";
|
import DMRoomMap from "../../../src/utils/DMRoomMap";
|
||||||
import { createTestClient, makeMembershipEvent } from "../../test-utils";
|
import { createTestClient, makeMembershipEvent, mkThirdPartyInviteEvent } from "../../test-utils";
|
||||||
import { LocalRoom } from "../../../src/models/LocalRoom";
|
import { LocalRoom } from "../../../src/models/LocalRoom";
|
||||||
import { findDMForUser } from "../../../src/utils/dm/findDMForUser";
|
import { findDMForUser } from "../../../src/utils/dm/findDMForUser";
|
||||||
import { getFunctionalMembers } from "../../../src/utils/room/getFunctionalMembers";
|
import { getFunctionalMembers } from "../../../src/utils/room/getFunctionalMembers";
|
||||||
|
@ -32,13 +32,15 @@ describe("findDMForUser", () => {
|
||||||
const userId2 = "@user2:example.com";
|
const userId2 = "@user2:example.com";
|
||||||
const userId3 = "@user3:example.com";
|
const userId3 = "@user3:example.com";
|
||||||
const botId = "@bot:example.com";
|
const botId = "@bot:example.com";
|
||||||
|
const thirdPartyId = "party@example.com";
|
||||||
let room1: Room;
|
let room1: Room;
|
||||||
let room2: LocalRoom;
|
let room2: LocalRoom;
|
||||||
let room3: Room;
|
let room3: Room;
|
||||||
let room4: Room;
|
let room4: Room;
|
||||||
let room5: Room;
|
let room5: Room;
|
||||||
let room6: Room;
|
let room6: Room;
|
||||||
const room7Id = "!room7:example.com";
|
let room7: Room;
|
||||||
|
const unknownRoomId = "!unknown:example.com";
|
||||||
let dmRoomMap: DMRoomMap;
|
let dmRoomMap: DMRoomMap;
|
||||||
let mockClient: MatrixClient;
|
let mockClient: MatrixClient;
|
||||||
|
|
||||||
|
@ -89,6 +91,14 @@ describe("findDMForUser", () => {
|
||||||
makeMembershipEvent(room6.roomId, userId3, "join"),
|
makeMembershipEvent(room6.roomId, userId3, "join"),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
// room with pending third-party invite
|
||||||
|
room7 = new Room("!room7:example.com", mockClient, userId1);
|
||||||
|
room7.getMyMembership = () => "join";
|
||||||
|
room7.currentState.setStateEvents([
|
||||||
|
makeMembershipEvent(room7.roomId, userId1, "join"),
|
||||||
|
mkThirdPartyInviteEvent(thirdPartyId, "third-party", room7.roomId),
|
||||||
|
]);
|
||||||
|
|
||||||
mocked(mockClient.getRoom).mockImplementation((roomId: string) => {
|
mocked(mockClient.getRoom).mockImplementation((roomId: string) => {
|
||||||
return (
|
return (
|
||||||
{
|
{
|
||||||
|
@ -98,6 +108,7 @@ describe("findDMForUser", () => {
|
||||||
[room4.roomId]: room4,
|
[room4.roomId]: room4,
|
||||||
[room5.roomId]: room5,
|
[room5.roomId]: room5,
|
||||||
[room6.roomId]: room6,
|
[room6.roomId]: room6,
|
||||||
|
[room7.roomId]: room7,
|
||||||
}[roomId] || null
|
}[roomId] || null
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
@ -113,14 +124,15 @@ describe("findDMForUser", () => {
|
||||||
room4.roomId,
|
room4.roomId,
|
||||||
room5.roomId,
|
room5.roomId,
|
||||||
room6.roomId,
|
room6.roomId,
|
||||||
room7Id, // this room does not exist in client
|
room7.roomId,
|
||||||
|
unknownRoomId, // this room does not exist in client
|
||||||
]),
|
]),
|
||||||
),
|
),
|
||||||
} as unknown as DMRoomMap;
|
} as unknown as DMRoomMap;
|
||||||
jest.spyOn(DMRoomMap, "shared").mockReturnValue(dmRoomMap);
|
jest.spyOn(DMRoomMap, "shared").mockReturnValue(dmRoomMap);
|
||||||
mocked(dmRoomMap.getDMRoomsForUserId).mockImplementation((userId: string) => {
|
mocked(dmRoomMap.getDMRoomsForUserId).mockImplementation((userId: string) => {
|
||||||
if (userId === userId1) {
|
if (userId === userId1) {
|
||||||
return [room1.roomId, room2.roomId, room3.roomId, room4.roomId, room5.roomId, room7Id];
|
return [room1.roomId, room2.roomId, room3.roomId, room4.roomId, room5.roomId, unknownRoomId];
|
||||||
}
|
}
|
||||||
|
|
||||||
return [];
|
return [];
|
||||||
|
@ -158,4 +170,8 @@ describe("findDMForUser", () => {
|
||||||
|
|
||||||
expect(findDMForUser(mockClient, userId3)).toBe(room6);
|
expect(findDMForUser(mockClient, userId3)).toBe(room6);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("should find a room with a pending third-party invite", () => {
|
||||||
|
expect(findDMForUser(mockClient, thirdPartyId)).toBe(room7);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue