diff --git a/lib/src/client.dart b/lib/src/client.dart index 2188ab27..ca2d6711 100644 --- a/lib/src/client.dart +++ b/lib/src/client.dart @@ -73,6 +73,8 @@ class Client extends MatrixApi { Set roomPreviewLastEvents; + Set supportedLoginTypes; + int sendMessageTimeoutSeconds; bool requestHistoryOnLimitedTimeline; @@ -110,6 +112,8 @@ class Client extends MatrixApi { /// If [formatLocalpart] is true, then the localpart of an mxid will /// be formatted in the way, that all "_" characters are becomming white spaces and /// the first character of each word becomes uppercase. + /// If your client supports more login types like login with token or SSO, then add this to + /// [supportedLoginTypes]. Client( this.clientName, { this.databaseBuilder, @@ -121,8 +125,10 @@ class Client extends MatrixApi { this.pinUnreadRooms = false, this.sendMessageTimeoutSeconds = 60, this.requestHistoryOnLimitedTimeline = true, + this.supportedLoginTypes, @deprecated bool debug, }) { + supportedLoginTypes ??= {AuthenticationTypes.password}; verificationMethods ??= {}; importantStateEvents ??= {}; importantStateEvents.addAll([ @@ -287,7 +293,7 @@ class Client extends MatrixApi { /// client and sets [homeserver] to [serverUrl] if it is. Supports the types [Uri] /// and [String]. Future checkHomeserver(dynamic homeserverUrl, - {Set supportedLoginTypes = supportedLoginTypes}) async { + {bool checkWellKnown = true}) async { try { if (homeserverUrl is Uri) { homeserver = homeserverUrl; @@ -302,8 +308,19 @@ class Client extends MatrixApi { } homeserver = Uri.parse(homeserverUrl); } - final versions = await requestSupportedVersions(); + // Look up well known + if (checkWellKnown) { + try { + final wellKnown = await requestWellKnownInformations(); + homeserver = Uri.parse(wellKnown.mHomeserver.baseUrl); + } catch (e) { + Logs().v('Found no well known information', e); + } + } + + // Check if server supports at least one supported version + final versions = await requestSupportedVersions(); if (!versions.versions .any((version) => supportedVersions.contains(version))) { throw Exception( @@ -363,7 +380,6 @@ class Client extends MatrixApi { /// Handles the login and allows the client to call all APIs which require /// authentication. Returns false if the login was not successful. Throws /// MatrixException if login was not successful. - /// You have to call [checkHomeserver] first to set a homeserver. @override Future login({ String type = AuthenticationTypes.password, @@ -377,6 +393,9 @@ class Client extends MatrixApi { String initialDeviceDisplayName, AuthenticationData auth, }) async { + if (homeserver == null && user.isValidMatrixId) { + await checkHomeserver(user.domain); + } final loginResp = await super.login( type: type, userIdentifierType: userIdentifierType, @@ -592,7 +611,6 @@ class Client extends MatrixApi { : null; static const Set supportedVersions = {'r0.5.0', 'r0.6.0'}; - static const Set supportedLoginTypes = {AuthenticationTypes.password}; static const String syncFilters = '{"room":{"state":{"lazy_load_members":true}}}'; static const String messagesFilters = '{"lazy_load_members":true}'; diff --git a/pubspec.yaml b/pubspec.yaml index 8cc68158..6e41462f 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -23,7 +23,7 @@ dependencies: matrix_file_e2ee: ^1.0.5 isolate: ^2.0.3 logger: ^0.9.4 - matrix_api_lite: ^0.1.5 + matrix_api_lite: ^0.1.6 dev_dependencies: test: ^1.15.7 diff --git a/test/client_test.dart b/test/client_test.dart index 7b215ef2..4eca7c27 100644 --- a/test/client_test.dart +++ b/test/client_test.dart @@ -87,7 +87,8 @@ void main() { } on MatrixConnectionException catch (exception) { expect(exception != null, true); } - await matrix.checkHomeserver('https://fakeserver.notexisting'); + await matrix.checkHomeserver('https://fakeserver.notexisting', + checkWellKnown: false); expect(matrix.homeserver.toString(), 'https://fakeserver.notexisting'); final available = await matrix.usernameAvailable('testuser'); @@ -318,7 +319,8 @@ void main() { roomUpdateListFuture = matrix.onRoomUpdate.stream.toList(); eventUpdateListFuture = matrix.onEvent.stream.toList(); - await matrix.checkHomeserver('https://fakeServer.notExisting'); + await matrix.checkHomeserver('https://fakeserver.notexisting', + checkWellKnown: false); final loginResp = await matrix.login(user: 'test', password: '1234'); diff --git a/test/encryption/encrypt_decrypt_to_device_test.dart b/test/encryption/encrypt_decrypt_to_device_test.dart index aa08ec75..cb270752 100644 --- a/test/encryption/encrypt_decrypt_to_device_test.dart +++ b/test/encryption/encrypt_decrypt_to_device_test.dart @@ -52,7 +52,8 @@ void main() { test('setupClient', () async { client = await getClient(); - await otherClient.checkHomeserver('https://fakeServer.notExisting'); + await otherClient.checkHomeserver('https://fakeserver.notexisting', + checkWellKnown: false); otherClient.init( newToken: 'abc', newUserID: '@othertest:fakeServer.notExisting', diff --git a/test/encryption/key_verification_test.dart b/test/encryption/key_verification_test.dart index d8429bd6..8bbf3e8e 100644 --- a/test/encryption/key_verification_test.dart +++ b/test/encryption/key_verification_test.dart @@ -87,7 +87,8 @@ void main() { client2 = Client('othertestclient', httpClient: FakeMatrixApi(), databaseBuilder: (_) => client1.database); - await client2.checkHomeserver('https://fakeServer.notExisting'); + await client2.checkHomeserver('https://fakeserver.notexisting', + checkWellKnown: false); client2.init( newToken: 'abc', newUserID: '@othertest:fakeServer.notExisting', diff --git a/test/event_test.dart b/test/event_test.dart index a0d3520a..8089d675 100644 --- a/test/event_test.dart +++ b/test/event_test.dart @@ -264,7 +264,8 @@ void main() { test('sendAgain', () async { var matrix = Client('testclient', httpClient: FakeMatrixApi()); - await matrix.checkHomeserver('https://fakeServer.notExisting'); + await matrix.checkHomeserver('https://fakeserver.notexisting', + checkWellKnown: false); await matrix.login(user: 'test', password: '1234'); var event = Event.fromJson( @@ -280,7 +281,8 @@ void main() { test('requestKey', () async { var matrix = Client('testclient', httpClient: FakeMatrixApi()); - await matrix.checkHomeserver('https://fakeServer.notExisting'); + await matrix.checkHomeserver('https://fakeserver.notexisting', + checkWellKnown: false); await matrix.login(user: 'test', password: '1234'); var event = Event.fromJson( @@ -1051,7 +1053,8 @@ void main() { THUMBNAIL_BUFF, }[url]; }; - await client.checkHomeserver('https://fakeServer.notExisting'); + await client.checkHomeserver('https://fakeserver.notexisting', + checkWellKnown: false); final room = Room(id: '!localpart:server.abc', client: client); var event = Event.fromJson({ 'type': EventTypes.Message, @@ -1234,7 +1237,8 @@ void main() { FILE_BUFF, }[url]; }; - await client.checkHomeserver('https://fakeServer.notExisting'); + await client.checkHomeserver('https://fakeserver.notexisting', + checkWellKnown: false); final room = Room(id: '!localpart:server.abc', client: await getClient()); var event = Event.fromJson({ 'type': EventTypes.Message, diff --git a/test/fake_client.dart b/test/fake_client.dart index 9020d23c..8166ff13 100644 --- a/test/fake_client.dart +++ b/test/fake_client.dart @@ -35,7 +35,8 @@ Future getClient() async { databaseBuilder: getDatabase, ); FakeMatrixApi.client = client; - await client.checkHomeserver('https://fakeServer.notExisting'); + await client.checkHomeserver('https://fakeServer.notExisting', + checkWellKnown: false); client.init( newToken: 'abcd', newUserID: '@test:fakeServer.notExisting', diff --git a/test/mxc_uri_extension_test.dart b/test/mxc_uri_extension_test.dart index b5c03a73..6d73e9b6 100644 --- a/test/mxc_uri_extension_test.dart +++ b/test/mxc_uri_extension_test.dart @@ -30,7 +30,8 @@ void main() { Logs().level = Level.error; test('Formatting', () async { var client = Client('testclient', httpClient: FakeMatrixApi()); - await client.checkHomeserver('https://fakeserver.notexisting'); + await client.checkHomeserver('https://fakeserver.notexisting', + checkWellKnown: false); final mxc = 'mxc://exampleserver.abc/abcdefghijklmn'; final content = Uri.parse(mxc); expect(content.isScheme('mxc'), true); diff --git a/test/timeline_test.dart b/test/timeline_test.dart index a57b96d7..8176d365 100644 --- a/test/timeline_test.dart +++ b/test/timeline_test.dart @@ -51,7 +51,8 @@ void main() { }); test('Create', () async { - await client.checkHomeserver('https://fakeServer.notExisting'); + await client.checkHomeserver('https://fakeserver.notexisting', + checkWellKnown: false); client.onEvent.add(EventUpdate( type: EventUpdateType.timeline, diff --git a/test/user_test.dart b/test/user_test.dart index 4867c46e..edb2691f 100644 --- a/test/user_test.dart +++ b/test/user_test.dart @@ -86,28 +86,34 @@ void main() { user3.calcDisplayname(mxidLocalPartFallback: false), 'Unknown user'); }); test('kick', () async { - await client.checkHomeserver('https://fakeserver.notexisting'); + await client.checkHomeserver('https://fakeserver.notexisting', + checkWellKnown: false); await user1.kick(); }); test('ban', () async { - await client.checkHomeserver('https://fakeserver.notexisting'); + await client.checkHomeserver('https://fakeserver.notexisting', + checkWellKnown: false); await user1.ban(); }); test('unban', () async { - await client.checkHomeserver('https://fakeserver.notexisting'); + await client.checkHomeserver('https://fakeserver.notexisting', + checkWellKnown: false); await user1.unban(); }); test('setPower', () async { - await client.checkHomeserver('https://fakeserver.notexisting'); + await client.checkHomeserver('https://fakeserver.notexisting', + checkWellKnown: false); await user1.setPower(50); }); test('startDirectChat', () async { - await client.checkHomeserver('https://fakeserver.notexisting'); + await client.checkHomeserver('https://fakeserver.notexisting', + checkWellKnown: false); await client.login(user: 'test', password: '1234'); await user1.startDirectChat(); }); test('getPresence', () async { - await client.checkHomeserver('https://fakeserver.notexisting'); + await client.checkHomeserver('https://fakeserver.notexisting', + checkWellKnown: false); await client.handleSync(SyncUpdate.fromJson({ 'presence': { 'events': [ @@ -122,15 +128,18 @@ void main() { expect(user1.presence.presence.presence, PresenceType.online); }); test('canBan', () async { - await client.checkHomeserver('https://fakeserver.notexisting'); + await client.checkHomeserver('https://fakeserver.notexisting', + checkWellKnown: false); expect(user1.canBan, false); }); test('canKick', () async { - await client.checkHomeserver('https://fakeserver.notexisting'); + await client.checkHomeserver('https://fakeserver.notexisting', + checkWellKnown: false); expect(user1.canKick, false); }); test('canChangePowerLevel', () async { - await client.checkHomeserver('https://fakeserver.notexisting'); + await client.checkHomeserver('https://fakeserver.notexisting', + checkWellKnown: false); expect(user1.canChangePowerLevel, false); }); test('dispose client', () async {