From eefc40e2d0540095bb1c7b0b17cad566fac25c82 Mon Sep 17 00:00:00 2001 From: Christian Pauly Date: Thu, 21 Apr 2022 15:40:21 +0200 Subject: [PATCH] refactor: Get rid of dynamic input in checkHomeserver This also fixes the automatic homeserver detection in the login method. It no longer uses the deprecated user. --- lib/src/client.dart | 20 +++++++++++-------- test/client_test.dart | 10 ++++++---- .../encrypt_decrypt_to_device_test.dart | 3 ++- test/encryption/key_verification_test.dart | 2 +- test/event_test.dart | 8 ++++---- test/fake_client.dart | 2 +- test/mxc_uri_extension_test.dart | 6 +++--- test/timeline_test.dart | 2 +- test/user_test.dart | 2 +- test_driver/matrixsdk_test.dart | 6 +++--- 10 files changed, 34 insertions(+), 27 deletions(-) diff --git a/lib/src/client.dart b/lib/src/client.dart index a5df3035..7b108628 100644 --- a/lib/src/client.dart +++ b/lib/src/client.dart @@ -358,20 +358,17 @@ class Client extends MatrixApi { /// login types. Throws an exception if the server is not compatible with the /// client and sets [homeserver] to [homeserverUrl] if it is. Supports the /// types `Uri` and `String`. - Future checkHomeserver(dynamic homeserverUrl, + Future checkHomeserver(Uri homeserverUrl, {bool checkWellKnown = true}) async { try { - var homeserver = this.homeserver = - (homeserverUrl is Uri) ? homeserverUrl : Uri.parse(homeserverUrl); - homeserver = this.homeserver = homeserver.stripTrailingSlash(); + homeserver = homeserverUrl.stripTrailingSlash(); // Look up well known DiscoveryInformation? wellKnown; if (checkWellKnown) { try { wellKnown = await getWellknown(); - homeserver = this.homeserver = - wellKnown.mHomeserver.baseUrl.stripTrailingSlash(); + homeserver = wellKnown.mHomeserver.baseUrl.stripTrailingSlash(); } catch (e) { Logs().v('Found no well known information', e); } @@ -462,8 +459,15 @@ class Client extends MatrixApi { @Deprecated('Deprecated in favour of identifier.') String? medium, @Deprecated('Deprecated in favour of identifier.') String? address, }) async { - if (homeserver == null && user != null && user.isValidMatrixId) { - await checkHomeserver(user.domain); + if (homeserver == null) { + final domain = identifier is AuthenticationUserIdentifier + ? identifier.user.domain + : null; + if (domain != null) { + await checkHomeserver(Uri.https(domain, '')); + } else { + throw Exception('No homeserver specified!'); + } } final response = await super.login( type, diff --git a/test/client_test.dart b/test/client_test.dart index c210406f..4aebb752 100644 --- a/test/client_test.dart +++ b/test/client_test.dart @@ -81,11 +81,12 @@ void main() { expect(matrix.homeserver, null); try { - await matrix.checkHomeserver('https://fakeserver.wrongaddress'); + await matrix + .checkHomeserver(Uri.parse('https://fakeserver.wrongaddress')); } catch (exception) { expect(exception.toString().isNotEmpty, true); } - await matrix.checkHomeserver('https://fakeserver.notexisting', + await matrix.checkHomeserver(Uri.parse('https://fakeserver.notexisting'), checkWellKnown: false); expect(matrix.homeserver.toString(), 'https://fakeserver.notexisting'); @@ -306,7 +307,7 @@ void main() { eventUpdateListFuture = matrix.onEvent.stream.toList(); - await matrix.checkHomeserver('https://fakeserver.notexisting', + await matrix.checkHomeserver(Uri.parse('https://fakeserver.notexisting'), checkWellKnown: false); final loginResp = await matrix.login(LoginType.mLoginPassword, @@ -894,7 +895,8 @@ void main() { databaseBuilder: (_) => database, ); FakeMatrixApi.client = moorClient; - await moorClient.checkHomeserver('https://fakeServer.notExisting', + await moorClient.checkHomeserver( + Uri.parse('https://fakeServer.notExisting'), checkWellKnown: false); await moorClient.init( newToken: 'abcd', diff --git a/test/encryption/encrypt_decrypt_to_device_test.dart b/test/encryption/encrypt_decrypt_to_device_test.dart index 69828063..42fb4b2e 100644 --- a/test/encryption/encrypt_decrypt_to_device_test.dart +++ b/test/encryption/encrypt_decrypt_to_device_test.dart @@ -53,7 +53,8 @@ void main() { client = await getClient(); await client.abortSync(); - await otherClient.checkHomeserver('https://fakeserver.notexisting', + await otherClient.checkHomeserver( + Uri.parse('https://fakeserver.notexisting'), checkWellKnown: false); await otherClient.init( newToken: 'abc', diff --git a/test/encryption/key_verification_test.dart b/test/encryption/key_verification_test.dart index 770af7aa..d1304d0d 100644 --- a/test/encryption/key_verification_test.dart +++ b/test/encryption/key_verification_test.dart @@ -89,7 +89,7 @@ void main() { httpClient: FakeMatrixApi(), databaseBuilder: getDatabase, ); - await client2.checkHomeserver('https://fakeserver.notexisting', + await client2.checkHomeserver(Uri.parse('https://fakeserver.notexisting'), checkWellKnown: false); await client2.init( newToken: 'abc', diff --git a/test/event_test.dart b/test/event_test.dart index 25a3a736..b67e6a04 100644 --- a/test/event_test.dart +++ b/test/event_test.dart @@ -265,7 +265,7 @@ void main() { test('sendAgain', () async { final matrix = Client('testclient', httpClient: FakeMatrixApi()); - await matrix.checkHomeserver('https://fakeserver.notexisting', + await matrix.checkHomeserver(Uri.parse('https://fakeserver.notexisting'), checkWellKnown: false); await matrix.login(LoginType.mLoginPassword, identifier: AuthenticationUserIdentifier(user: 'test'), @@ -284,7 +284,7 @@ void main() { test('requestKey', () async { final matrix = Client('testclient', httpClient: FakeMatrixApi()); - await matrix.checkHomeserver('https://fakeserver.notexisting', + await matrix.checkHomeserver(Uri.parse('https://fakeserver.notexisting'), checkWellKnown: false); await matrix.login(LoginType.mLoginPassword, identifier: AuthenticationUserIdentifier(user: 'test'), @@ -1252,7 +1252,7 @@ void main() { '/_matrix/media/r0/download/example.org/thumb': THUMBNAIL_BUFF, }[uri.path]!; }; - await client.checkHomeserver('https://fakeserver.notexisting', + await client.checkHomeserver(Uri.parse('https://fakeserver.notexisting'), checkWellKnown: false); final room = Room(id: '!localpart:server.abc', client: client); var event = Event.fromJson({ @@ -1439,7 +1439,7 @@ void main() { '/_matrix/media/r0/download/example.org/newfile': FILE_BUFF, }[uri.path]!; }; - await client.checkHomeserver('https://fakeserver.notexisting', + await client.checkHomeserver(Uri.parse('https://fakeserver.notexisting'), checkWellKnown: false); final room = Room(id: '!localpart:server.abc', client: await getClient()); final event = Event.fromJson({ diff --git a/test/fake_client.dart b/test/fake_client.dart index 0a13c708..f7afd062 100644 --- a/test/fake_client.dart +++ b/test/fake_client.dart @@ -35,7 +35,7 @@ Future getClient() async { databaseBuilder: getDatabase, ); FakeMatrixApi.client = client; - await client.checkHomeserver('https://fakeServer.notExisting', + await client.checkHomeserver(Uri.parse('https://fakeServer.notExisting'), checkWellKnown: false); await client.init( newToken: 'abcd', diff --git a/test/mxc_uri_extension_test.dart b/test/mxc_uri_extension_test.dart index 21ea321e..a1d485a4 100644 --- a/test/mxc_uri_extension_test.dart +++ b/test/mxc_uri_extension_test.dart @@ -28,7 +28,7 @@ void main() { Logs().level = Level.error; test('Formatting', () async { final client = Client('testclient', httpClient: FakeMatrixApi()); - await client.checkHomeserver('https://fakeserver.notexisting', + await client.checkHomeserver(Uri.parse('https://fakeserver.notexisting'), checkWellKnown: false); final mxc = 'mxc://exampleserver.abc/abcdefghijklmn'; final content = Uri.parse(mxc); @@ -50,7 +50,7 @@ void main() { }); test('other port', () async { final client = Client('testclient', httpClient: FakeMatrixApi()); - await client.checkHomeserver('https://fakeserver.notexisting', + await client.checkHomeserver(Uri.parse('https://fakeserver.notexisting'), checkWellKnown: false); client.homeserver = Uri.parse('https://fakeserver.notexisting:1337'); final mxc = 'mxc://exampleserver.abc/abcdefghijklmn'; @@ -73,7 +73,7 @@ void main() { }); test('other remote port', () async { final client = Client('testclient', httpClient: FakeMatrixApi()); - await client.checkHomeserver('https://fakeserver.notexisting', + await client.checkHomeserver(Uri.parse('https://fakeserver.notexisting'), checkWellKnown: false); final mxc = 'mxc://exampleserver.abc:1234/abcdefghijklmn'; final content = Uri.parse(mxc); diff --git a/test/timeline_test.dart b/test/timeline_test.dart index 44fadf88..94ac442a 100644 --- a/test/timeline_test.dart +++ b/test/timeline_test.dart @@ -63,7 +63,7 @@ void main() { }); test('Create', () async { - await client.checkHomeserver('https://fakeserver.notexisting', + await client.checkHomeserver(Uri.parse('https://fakeserver.notexisting'), checkWellKnown: false); client.onEvent.add(EventUpdate( diff --git a/test/user_test.dart b/test/user_test.dart index e33b8e2d..2130d524 100644 --- a/test/user_test.dart +++ b/test/user_test.dart @@ -45,7 +45,7 @@ void main() { room.setState(user1); room.setState(user2); setUp(() async { - await client.checkHomeserver('https://fakeserver.notexisting', + await client.checkHomeserver(Uri.parse('https://fakeserver.notexisting'), checkWellKnown: false); await client.login(LoginType.mLoginPassword, identifier: AuthenticationUserIdentifier(user: 'test'), diff --git a/test_driver/matrixsdk_test.dart b/test_driver/matrixsdk_test.dart index 2e3294bf..0b1013d0 100644 --- a/test_driver/matrixsdk_test.dart +++ b/test_driver/matrixsdk_test.dart @@ -38,7 +38,7 @@ void test() async { Logs().i('++++ Login Alice at ++++'); testClientA = Client('TestClientA', databaseBuilder: getDatabase); - await testClientA.checkHomeserver(TestUser.homeserver); + await testClientA.checkHomeserver(Uri.parse(TestUser.homeserver)); await testClientA.login(LoginType.mLoginPassword, identifier: AuthenticationUserIdentifier(user: TestUser.username), password: TestUser.password); @@ -46,7 +46,7 @@ void test() async { Logs().i('++++ Login Bob ++++'); testClientB = Client('TestClientB', databaseBuilder: getDatabase); - await testClientB.checkHomeserver(TestUser.homeserver); + await testClientB.checkHomeserver(Uri.parse(TestUser.homeserver)); await testClientB.login(LoginType.mLoginPassword, identifier: AuthenticationUserIdentifier(user: TestUser.username2), password: TestUser.password); @@ -237,7 +237,7 @@ void test() async { Logs().i('++++ Login Bob in another client ++++'); final testClientC = Client('TestClientC', databaseBuilder: getDatabase); - await testClientC.checkHomeserver(TestUser.homeserver); + await testClientC.checkHomeserver(Uri.parse(TestUser.homeserver)); await testClientC.login(LoginType.mLoginPassword, identifier: AuthenticationUserIdentifier(user: TestUser.username2), password: TestUser.password);