Keep direct rooms orthogonal (#183)
authorajbura <ajbura@gmail.com>
Sat, 5 Feb 2022 13:55:59 +0000 (19:25 +0530)
committerajbura <ajbura@gmail.com>
Sat, 5 Feb 2022 13:55:59 +0000 (19:25 +0530)
Signed-off-by: ajbura <ajbura@gmail.com>
src/app/organisms/invite-user/InviteUser.jsx
src/app/organisms/profile-viewer/ProfileViewer.jsx
src/util/matrixUtil.js

index a6ff24210b2f9f6513c93f1455af4f3875aac39e..e7d7f51069d37efa6f6ebc4aa8953e9c2c6690d3 100644 (file)
@@ -6,6 +6,7 @@ import initMatrix from '../../../client/initMatrix';
 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';
@@ -104,6 +105,13 @@ function InviteUser({
 
   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);
index 34530122ed869bde610c759c3c62bfd7c2c178f3..ca4a4ea96bc1a75feacaca7c6641a82898878374 100644 (file)
@@ -10,7 +10,9 @@ import navigation from '../../../client/state/navigation';
 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';
 
@@ -187,17 +189,12 @@ function ProfileFooter({ roomId, userId, onRequestClose }) {
   }, [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
index e86194e45558801805daba62e5717d2d915e9829..5c40fa91ba8903fbd325defc93100376ffa114c3 100644 (file)
@@ -72,7 +72,22 @@ function parseReply(rawBody) {
   };
 }
 
+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,
 };