Skip to content

Commit

Permalink
Merge pull request #1791 from famedly/krille/cache-direct-chat-matrix-id
Browse files Browse the repository at this point in the history
refactor: Cache direct chat matrix ID
  • Loading branch information
krille-chan authored May 10, 2024
2 parents 188a4e2 + 42f44de commit 9cc7c0e
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions lib/src/room.dart
Original file line number Diff line number Diff line change
Expand Up @@ -356,15 +356,27 @@ class Room {
});
}

String? _cachedDirectChatMatrixId;

/// If this room is a direct chat, this is the matrix ID of the user.
/// Returns null otherwise.
String? get directChatMatrixID {
// Calculating the directChatMatrixId can be expensive. We cache it and
// validate the cache instead every time.
final cache = _cachedDirectChatMatrixId;
if (cache != null) {
final roomIds = client.directChats[cache];
if (roomIds is List && roomIds.contains(id)) {
return cache;
}
}

if (membership == Membership.invite) {
final userID = client.userID;
if (userID == null) return null;
final invitation = getState(EventTypes.RoomMember, userID);
if (invitation != null && invitation.content['is_direct'] == true) {
return invitation.senderId;
return _cachedDirectChatMatrixId = invitation.senderId;
}
}

Expand All @@ -373,8 +385,8 @@ class Room {
final roomIds = e.value;
return roomIds is List<dynamic> && roomIds.contains(id);
})?.key;
if (mxId?.isValidMatrixId == true) return mxId;
return null;
if (mxId?.isValidMatrixId == true) return _cachedDirectChatMatrixId = mxId;
return _cachedDirectChatMatrixId = null;
}

/// Wheither this is a direct chat or not
Expand Down

0 comments on commit 9cc7c0e

Please sign in to comment.