From 694187d5f1609e14d896df55d6c09339fcfe6766 Mon Sep 17 00:00:00 2001 From: Christian Pauly Date: Mon, 8 Aug 2022 08:47:07 +0200 Subject: [PATCH 1/2] feat: Add markasdm and markasgroup commands This makes it possible to mark a room as dm or group. As most of the apps using the SDK do not have a GUI to do this, this makes it now finally possible to repair broken DM rooms. --- lib/src/utils/commands_extension.dart | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/src/utils/commands_extension.dart b/lib/src/utils/commands_extension.dart index 038ab4e4..ebc8ad35 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; + }); } } From 064b84305d5a6f7323ace7fbca1ce9f2a36bfc84 Mon Sep 17 00:00:00 2001 From: Nicolas Werner Date: Tue, 9 Aug 2022 15:33:59 +0200 Subject: [PATCH 2/2] chore: Add markasdm/group tests --- lib/src/room.dart | 22 ++++++++------ test/commands_test.dart | 63 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 75 insertions(+), 10 deletions(-) diff --git a/lib/src/room.dart b/lib/src/room.dart index 4718e652..5f67a3dc 100644 --- a/lib/src/room.dart +++ b/lib/src/room.dart @@ -1233,17 +1233,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/test/commands_test.dart b/test/commands_test.dart index 053afc5d..0627e812 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); });