Skylar-Tech / node-red-contrib-matrix-chat

Matrix chat server support for Node-RED
GNU General Public License v3.0
31 stars 10 forks source link

Return if message came from DM room #29

Closed skylord123 closed 2 years ago

skylord123 commented 2 years ago

Would be nice to return in the msg object whether it came from a direct message room or not.

Here is some code someone posted in the matrix-dev room to do just this:

const isDmRoom = (room) => {
  // Find out if this is a direct message room.
  let isDM = room.getDMInviter() ? true : false;
  const allMembers = room.currentState.getMembers();
  if (!isDM && allMembers.length <= 2) {
    // if not a DM, but there are 2 users only
    // double check DM (needed because getDMInviter works only if you were invited, not if you invite)
    // hence why we check for each member
    if (allMembers.some((m) => m.getDMInviter())) {
      return true;
    }
  }
  return allMembers.length <= 2 && isDM;
};

Performance will need to be tested.