From 31d4d5d08ef77f1af0ad511904efa580116dd68f Mon Sep 17 00:00:00 2001 From: Christian Pauly Date: Thu, 26 Jan 2023 09:15:36 +0100 Subject: [PATCH] fix: Check if argument is valid mxid in /maskasdm command --- lib/src/utils/commands_extension.dart | 7 +++++++ test/commands_test.dart | 13 +++---------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/src/utils/commands_extension.dart b/lib/src/utils/commands_extension.dart index 27ac03ac..470821bc 100644 --- a/lib/src/utils/commands_extension.dart +++ b/lib/src/utils/commands_extension.dart @@ -220,6 +220,13 @@ extension CommandsClientExtension on Client { return ''; }); addCommand('markasdm', (CommandArgs args) async { + final mxid = args.msg; + if (!mxid.isValidMatrixId) { + throw Exception('You must enter a valid mxid when using /maskasdm'); + } + if (await args.room.requestUser(mxid, requestProfile: false) == null) { + throw Exception('User $mxid is not in this room'); + } await args.room.addToDirectChat(args.msg); return; }); diff --git a/test/commands_test.dart b/test/commands_test.dart index aecdcb6a..f6575ec6 100644 --- a/test/commands_test.dart +++ b/test/commands_test.dart @@ -314,7 +314,7 @@ void main() { test('markasdm', () async { FakeMatrixApi.calledEndpoints.clear(); - await room.sendTextEvent('/markasdm @fakealice:example.com'); + await room.sendTextEvent('/markasdm @test:fakeServer.notExisting'); expect( json.decode(FakeMatrixApi .calledEndpoints[ @@ -325,7 +325,7 @@ void main() { json.decode(FakeMatrixApi .calledEndpoints[ '/client/v3/user/%40test%3AfakeServer.notExisting/account_data/m.direct'] - ?.first)?['@fakealice:example.com'], + ?.first)?['@test:fakeServer.notExisting'], ['!1234:fakeServer.notExisting']); expect( json @@ -335,19 +335,12 @@ void main() { ?.first) .entries .any((e) => - e.key != '@fakealice:example.com' && + e.key != '@test:fakeServer.notExisting' && 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 {