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.
This commit is contained in:
Christian Pauly 2022-04-21 15:40:21 +02:00
parent f1358610c2
commit eefc40e2d0
10 changed files with 34 additions and 27 deletions

View File

@ -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<HomeserverSummary> checkHomeserver(dynamic homeserverUrl,
Future<HomeserverSummary> 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,

View File

@ -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',

View File

@ -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',

View File

@ -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',

View File

@ -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({

View File

@ -35,7 +35,7 @@ Future<Client> 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',

View File

@ -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);

View File

@ -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(

View File

@ -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'),

View File

@ -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);