diff --git a/lib/src/client.dart b/lib/src/client.dart index 3be0d26e8..92430625d 100644 --- a/lib/src/client.dart +++ b/lib/src/client.dart @@ -448,8 +448,13 @@ class Client extends MatrixApi { return null; } - Map get directChats => - _accountData['m.direct']?.content ?? {}; + Map> get directChats => + (_accountData['m.direct']?.content ?? {}).map( + (userId, list) => MapEntry( + userId, + (list is! List) ? [] : list.whereType().toList(), + ), + ); /// Returns the (first) room ID from the store which is a private chat with the user [userId]. /// Returns null if there is none. diff --git a/lib/src/room.dart b/lib/src/room.dart index 773383247..20f69b49e 100644 --- a/lib/src/room.dart +++ b/lib/src/room.dart @@ -346,7 +346,7 @@ class Room { final cache = _cachedDirectChatMatrixId; if (cache != null) { final roomIds = client.directChats[cache]; - if (roomIds is List && roomIds.contains(id)) { + if (roomIds != null && roomIds.contains(id)) { return cache; } } @@ -1381,21 +1381,21 @@ class Room { /// Sets this room as a direct chat for this user if not already. Future addToDirectChat(String userID) async { - final directChats = client.directChats; - if (directChats[userID] is List) { - if (!directChats[userID].contains(id)) { - directChats[userID].add(id); - } else { - return; - } // Is already in direct chats - } else { - directChats[userID] = [id]; + final dmRooms = List.from(client.directChats[userID] ?? []); + if (dmRooms.contains(id)) { + Logs().d('Already a direct chat.'); + return; } + dmRooms.add(id); + await client.setAccountData( client.userID!, 'm.direct', - directChats, + { + ...client.directChats, + userID: dmRooms, + }, ); return; }