Skip to content

Commit

Permalink
chore: ensure direct chats have only 2 members before sending verific…
Browse files Browse the repository at this point in the history
…ation requests
  • Loading branch information
coder-with-a-bushido committed Feb 25, 2025
1 parent 352b3fa commit 8808f4d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
20 changes: 19 additions & 1 deletion lib/src/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -783,12 +783,21 @@ class Client extends MatrixApi {
bool waitForSync = true,
Map<String, dynamic>? powerLevelContentOverride,
CreateRoomPreset? preset = CreateRoomPreset.trustedPrivateChat,

/// Throws an exception if the DM room has more than two members, if enabled
bool ensureIsOnlyTwoMembers = false,
}) async {
// Try to find an existing direct chat
final directChatRoomId = getDirectChatFromUserId(mxid);
if (directChatRoomId != null) {
final room = getRoomById(directChatRoomId);
if (room != null) {
if (ensureIsOnlyTwoMembers &&
(room.summary.mInvitedMemberCount ?? 0) +
(room.summary.mJoinedMemberCount ?? 1) >
2) {
throw Exception('Room $directChatRoomId has more than two members.');
}
if (room.membership == Membership.join) {
return directChatRoomId;
} else if (room.membership == Membership.invite) {
Expand Down Expand Up @@ -835,7 +844,9 @@ class Client extends MatrixApi {
powerLevelContentOverride: powerLevelContentOverride,
);

if (waitForSync) {
// If we are ensuring that the room has only two members, we need to wait
// for the room to be joined.
if (waitForSync || ensureIsOnlyTwoMembers) {
final room = getRoomById(roomId);
if (room == null || room.membership != Membership.join) {
// Wait for room actually appears in sync
Expand All @@ -845,6 +856,13 @@ class Client extends MatrixApi {

await Room(id: roomId, client: this).addToDirectChat(mxid);

if (ensureIsOnlyTwoMembers) {
final room = getRoomById(roomId);
if ((room?.summary.mJoinedMemberCount ?? 1) > 2) {
throw Exception('Room $roomId has more than two members.');
}
}

return roomId;
}

Expand Down
1 change: 1 addition & 0 deletions lib/src/utils/device_keys_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ class DeviceKeysList {
enableEncryption: newDirectChatEnableEncryption,
initialState: newDirectChatInitialState,
waitForSync: false,
ensureIsOnlyTwoMembers: true,
);

final room =
Expand Down

0 comments on commit 8808f4d

Please sign in to comment.