diff --git a/lib/fake_matrix_api.dart b/lib/fake_matrix_api.dart index 8a3d6a11..0cf2c8ec 100644 --- a/lib/fake_matrix_api.dart +++ b/lib/fake_matrix_api.dart @@ -2735,8 +2735,6 @@ class FakeMatrixApi extends BaseClient { (var req) => {}, '/client/v3/user/%40test%3AfakeServer.notExisting/rooms/!localpart%3Aserver.abc/account_data/m.marked_unread': (var req) => {}, - '/client/v3/user/%40test%3AfakeServer.notExisting/account_data/m.direct': - (var req) => {}, '/client/v3/user/%40othertest%3AfakeServer.notExisting/account_data/m.direct': (var req) => {}, '/client/v3/profile/%40alice%3Aexample.com/displayname': (var reqI) => {}, diff --git a/lib/src/client.dart b/lib/src/client.dart index cd88d55c..d221e1fe 100644 --- a/lib/src/client.dart +++ b/lib/src/client.dart @@ -439,8 +439,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 (the room with the latest event) /// which is a private chat with the user [userId]. diff --git a/lib/src/room.dart b/lib/src/room.dart index da2fd10e..dc32f842 100644 --- a/lib/src/room.dart +++ b/lib/src/room.dart @@ -349,7 +349,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; } } @@ -1519,21 +1519,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; } diff --git a/test/user_test.dart b/test/user_test.dart index f732013f..89065bfd 100644 --- a/test/user_test.dart +++ b/test/user_test.dart @@ -139,6 +139,7 @@ void main() async { await user1.setPower(50); }); test('startDirectChat', () async { + FakeMatrixApi.client = user1.room.client; await user1.startDirectChat(waitForSync: false); }); test('getPresence', () async {