diff --git a/lib/src/room.dart b/lib/src/room.dart index f2227c19..07b112cf 100644 --- a/lib/src/room.dart +++ b/lib/src/room.dart @@ -1235,17 +1235,21 @@ class Room { /// Removes this room from all direct chat tags. Future removeFromDirectChat() async { - final directChats = client.directChats; - if (directChats[directChatMatrixID] is List && - directChats[directChatMatrixID].contains(id)) { - directChats[directChatMatrixID].remove(id); - } else { - return; - } // Nothing to do here + final directChats = client.directChats.copy(); + for (final k in directChats.keys) { + if (directChats[k] is List && directChats[k].contains(id)) { + directChats[k].remove(id); + } + } - await client.setAccountDataPerRoom( + directChats.removeWhere((_, v) => v is List && v.isEmpty); + + if (directChats == client.directChats) { + return; + } + + await client.setAccountData( client.userID!, - id, 'm.direct', directChats, ); diff --git a/lib/src/utils/commands_extension.dart b/lib/src/utils/commands_extension.dart index 6a4ca5e7..7e50c20f 100644 --- a/lib/src/utils/commands_extension.dart +++ b/lib/src/utils/commands_extension.dart @@ -219,6 +219,14 @@ extension CommandsClientExtension on Client { await clearCache(); return ''; }); + addCommand('markasdm', (CommandArgs args) async { + await args.room.addToDirectChat(args.msg); + return; + }); + addCommand('markasgroup', (CommandArgs args) async { + await args.room.removeFromDirectChat(); + return; + }); } } diff --git a/test/commands_test.dart b/test/commands_test.dart index aaeac122..e8c7ab53 100644 --- a/test/commands_test.dart +++ b/test/commands_test.dart @@ -312,7 +312,68 @@ void main() { } }); - test('create', () async { + test('markasdm', () async { + FakeMatrixApi.calledEndpoints.clear(); + await room.sendTextEvent('/markasdm @fakealice:example.com'); + expect( + json.decode(FakeMatrixApi + .calledEndpoints[ + '/client/v3/user/%40test%3AfakeServer.notExisting/account_data/m.direct'] + ?.first)?['@alice:example.com'], + ['!1234:fakeServer.notExisting']); + expect( + json.decode(FakeMatrixApi + .calledEndpoints[ + '/client/v3/user/%40test%3AfakeServer.notExisting/account_data/m.direct'] + ?.first)?['@fakealice:example.com'], + ['!1234:fakeServer.notExisting']); + expect( + json + .decode(FakeMatrixApi + .calledEndpoints[ + '/client/v3/user/%40test%3AfakeServer.notExisting/account_data/m.direct'] + ?.first) + .entries + .any((e) => + e.key != '@fakealice:example.com' && + e.key != '@alice:example.com' && + e.value.contains('!1234:fakeServer.notExisting')), + false); + + FakeMatrixApi.calledEndpoints.clear(); + await room.sendTextEvent('/markasdm'); + expect( + json.decode(FakeMatrixApi + .calledEndpoints[ + '/client/v3/user/%40test%3AfakeServer.notExisting/account_data/m.direct'] + ?.first)?[''], + ['!1234:fakeServer.notExisting']); + }); + + test('markasgroup', () async { + FakeMatrixApi.calledEndpoints.clear(); + await room.sendTextEvent('/markasgroup'); + expect( + json + .decode(FakeMatrixApi + .calledEndpoints[ + '/client/v3/user/%40test%3AfakeServer.notExisting/account_data/m.direct'] + ?.first) + ?.containsKey('@alice:example.com'), + false); + expect( + json + .decode(FakeMatrixApi + .calledEndpoints[ + '/client/v3/user/%40test%3AfakeServer.notExisting/account_data/m.direct'] + ?.first) + .entries + .any((e) => (e.value as List) + .contains('!1234:fakeServer.notExisting')), + false); + }); + + test('clearcache', () async { await room.sendTextEvent('/clearcache'); expect(room.client.prevBatch, null); });