Examine all m.direct rooms to find a DM as fallback (#10127)
This commit is contained in:
parent
1c6b06bb58
commit
a6eee32c66
7 changed files with 147 additions and 32 deletions
|
@ -192,6 +192,16 @@ export default class DMRoomMap {
|
|||
.reduce((obj, r) => (obj[r.userId] = r.room) && obj, {});
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns all room Ids from m.direct
|
||||
*/
|
||||
public getRoomIds(): Set<string> {
|
||||
return Object.values(this.mDirectEvent).reduce((prevRoomIds: Set<string>, roomIds: string[]): Set<string> => {
|
||||
roomIds.forEach((roomId) => prevRoomIds.add(roomId));
|
||||
return prevRoomIds;
|
||||
}, new Set<string>());
|
||||
}
|
||||
|
||||
private getUserToRooms(): { [key: string]: string[] } {
|
||||
if (!this.userToRooms) {
|
||||
const userToRooms = this.mDirectEvent;
|
||||
|
|
|
@ -21,17 +21,8 @@ import { isLocalRoom } from "../localRoom/isLocalRoom";
|
|||
import { isJoinedOrNearlyJoined } from "../membership";
|
||||
import { getFunctionalMembers } from "../room/getFunctionalMembers";
|
||||
|
||||
/**
|
||||
* Tries to find a DM room with a specific user.
|
||||
*
|
||||
* @param {MatrixClient} client
|
||||
* @param {string} userId ID of the user to find the DM for
|
||||
* @returns {Room} Room if found
|
||||
*/
|
||||
export function findDMForUser(client: MatrixClient, userId: string): Room {
|
||||
const roomIds = DMRoomMap.shared().getDMRoomsForUserId(userId);
|
||||
const rooms = roomIds.map((id) => client.getRoom(id));
|
||||
const suitableDMRooms = rooms
|
||||
function extractSuitableRoom(rooms: Room[], userId: string): Room | undefined {
|
||||
const suitableRooms = rooms
|
||||
.filter((r) => {
|
||||
// Validate that we are joined and the other person is also joined. We'll also make sure
|
||||
// that the room also looks like a DM (until we have canonical DMs to tell us). For now,
|
||||
|
@ -44,7 +35,7 @@ export function findDMForUser(client: MatrixClient, userId: string): Room {
|
|||
const functionalUsers = getFunctionalMembers(r);
|
||||
const members = r.currentState.getMembers();
|
||||
const joinedMembers = members.filter(
|
||||
(m) => !functionalUsers.includes(m.userId) && isJoinedOrNearlyJoined(m.membership),
|
||||
(m) => !functionalUsers.includes(m.userId) && m.membership && isJoinedOrNearlyJoined(m.membership),
|
||||
);
|
||||
const otherMember = joinedMembers.find((m) => m.userId === userId);
|
||||
return otherMember && joinedMembers.length === 2;
|
||||
|
@ -54,7 +45,34 @@ export function findDMForUser(client: MatrixClient, userId: string): Room {
|
|||
.sort((r1, r2) => {
|
||||
return r2.getLastActiveTimestamp() - r1.getLastActiveTimestamp();
|
||||
});
|
||||
if (suitableDMRooms.length) {
|
||||
return suitableDMRooms[0];
|
||||
|
||||
if (suitableRooms.length) {
|
||||
return suitableRooms[0];
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tries to find a DM room with a specific user.
|
||||
*
|
||||
* @param {MatrixClient} client
|
||||
* @param {string} userId ID of the user to find the DM for
|
||||
* @returns {Room | undefined} Room if found
|
||||
*/
|
||||
export function findDMForUser(client: MatrixClient, userId: string): Room | undefined {
|
||||
const roomIdsForUserId = DMRoomMap.shared().getDMRoomsForUserId(userId);
|
||||
const roomsForUserId = roomIdsForUserId.map((id) => client.getRoom(id)).filter((r): r is Room => r !== null);
|
||||
const suitableRoomForUserId = extractSuitableRoom(roomsForUserId, userId);
|
||||
|
||||
if (suitableRoomForUserId) {
|
||||
return suitableRoomForUserId;
|
||||
}
|
||||
|
||||
// Try to find in all rooms as a fallback
|
||||
const allRoomIds = DMRoomMap.shared().getRoomIds();
|
||||
const allRooms = Array.from(allRoomIds)
|
||||
.map((id) => client.getRoom(id))
|
||||
.filter((r): r is Room => r !== null);
|
||||
return extractSuitableRoom(allRooms, userId);
|
||||
}
|
||||
|
|
|
@ -29,7 +29,7 @@ import { findDMForUser } from "./findDMForUser";
|
|||
*/
|
||||
export function findDMRoom(client: MatrixClient, targets: Member[]): Room | null {
|
||||
const targetIds = targets.map((t) => t.userId);
|
||||
let existingRoom: Room;
|
||||
let existingRoom: Room | undefined;
|
||||
if (targetIds.length === 1) {
|
||||
existingRoom = findDMForUser(client, targetIds[0]);
|
||||
} else {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue