import cons from '../../../client/state/cons';
import * as roomActions from '../../../client/action/room';
import { selectRoom } from '../../../client/action/navigation';
+import { hasDMWith } from '../../../util/matrixUtil';
import Text from '../../atoms/text/Text';
import Button from '../../atoms/button/Button';
async function createDM(userId) {
if (mx.getUserId() === userId) return;
+ const dmRoomId = hasDMWith(userId);
+ if (dmRoomId) {
+ selectRoom(dmRoomId);
+ onRequestClose();
+ return;
+ }
+
try {
addUserToProc(userId);
procUserError.delete(userId);
import { selectRoom, openReusableContextMenu } from '../../../client/action/navigation';
import * as roomActions from '../../../client/action/room';
-import { getUsername, getUsernameOfRoomMember, getPowerLabel } from '../../../util/matrixUtil';
+import {
+ getUsername, getUsernameOfRoomMember, getPowerLabel, hasDMWith
+} from '../../../util/matrixUtil';
import { getEventCords } from '../../../util/common';
import colorMXID from '../../../util/colorMXID';
}, [userId]);
const openDM = async () => {
- const directIds = [...initMatrix.roomList.directs];
-
// Check and open if user already have a DM with userId.
- for (let i = 0; i < directIds.length; i += 1) {
- const dRoom = mx.getRoom(directIds[i]);
- const roomMembers = dRoom.getMembers();
- if (roomMembers.length <= 2 && dRoom.getMember(userId)) {
- selectRoom(directIds[i]);
- onRequestClose();
- return;
- }
+ const dmRoomId = hasDMWith(userId);
+ if (dmRoomId) {
+ selectRoom(dmRoomId);
+ onRequestClose();
+ return;
}
// Create new DM
};
}
+function hasDMWith(userId) {
+ const mx = initMatrix.matrixClient;
+ const directIds = [...initMatrix.roomList.directs];
+
+ return directIds.find((roomId) => {
+ const dRoom = mx.getRoom(roomId);
+ const roomMembers = dRoom.getMembers();
+ if (roomMembers.length <= 2 && dRoom.getMember(userId)) {
+ return true;
+ }
+ return false;
+ });
+}
+
export {
getBaseUrl, getUsername, getUsernameOfRoomMember,
isRoomAliasAvailable, getPowerLabel, parseReply,
+ hasDMWith,
};