Merge branch 'krille/checkhomeserver-fix' into 'main'

refactor: Get rid of dynamic input in checkHomeserver

See merge request famedly/company/frontend/famedlysdk!1014
This commit is contained in:
Krille Fear 2022-04-22 10:41:29 +00:00
commit 4d6b108438
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 /// login types. Throws an exception if the server is not compatible with the
/// client and sets [homeserver] to [homeserverUrl] if it is. Supports the /// client and sets [homeserver] to [homeserverUrl] if it is. Supports the
/// types `Uri` and `String`. /// types `Uri` and `String`.
Future<HomeserverSummary> checkHomeserver(dynamic homeserverUrl, Future<HomeserverSummary> checkHomeserver(Uri homeserverUrl,
{bool checkWellKnown = true}) async { {bool checkWellKnown = true}) async {
try { try {
var homeserver = this.homeserver = homeserver = homeserverUrl.stripTrailingSlash();
(homeserverUrl is Uri) ? homeserverUrl : Uri.parse(homeserverUrl);
homeserver = this.homeserver = homeserver.stripTrailingSlash();
// Look up well known // Look up well known
DiscoveryInformation? wellKnown; DiscoveryInformation? wellKnown;
if (checkWellKnown) { if (checkWellKnown) {
try { try {
wellKnown = await getWellknown(); wellKnown = await getWellknown();
homeserver = this.homeserver = homeserver = wellKnown.mHomeserver.baseUrl.stripTrailingSlash();
wellKnown.mHomeserver.baseUrl.stripTrailingSlash();
} catch (e) { } catch (e) {
Logs().v('Found no well known information', 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? medium,
@Deprecated('Deprecated in favour of identifier.') String? address, @Deprecated('Deprecated in favour of identifier.') String? address,
}) async { }) async {
if (homeserver == null && user != null && user.isValidMatrixId) { if (homeserver == null) {
await checkHomeserver(user.domain); 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( final response = await super.login(
type, type,

View File

@ -81,11 +81,12 @@ void main() {
expect(matrix.homeserver, null); expect(matrix.homeserver, null);
try { try {
await matrix.checkHomeserver('https://fakeserver.wrongaddress'); await matrix
.checkHomeserver(Uri.parse('https://fakeserver.wrongaddress'));
} catch (exception) { } catch (exception) {
expect(exception.toString().isNotEmpty, true); expect(exception.toString().isNotEmpty, true);
} }
await matrix.checkHomeserver('https://fakeserver.notexisting', await matrix.checkHomeserver(Uri.parse('https://fakeserver.notexisting'),
checkWellKnown: false); checkWellKnown: false);
expect(matrix.homeserver.toString(), 'https://fakeserver.notexisting'); expect(matrix.homeserver.toString(), 'https://fakeserver.notexisting');
@ -306,7 +307,7 @@ void main() {
eventUpdateListFuture = matrix.onEvent.stream.toList(); eventUpdateListFuture = matrix.onEvent.stream.toList();
await matrix.checkHomeserver('https://fakeserver.notexisting', await matrix.checkHomeserver(Uri.parse('https://fakeserver.notexisting'),
checkWellKnown: false); checkWellKnown: false);
final loginResp = await matrix.login(LoginType.mLoginPassword, final loginResp = await matrix.login(LoginType.mLoginPassword,
@ -894,7 +895,8 @@ void main() {
databaseBuilder: (_) => database, databaseBuilder: (_) => database,
); );
FakeMatrixApi.client = moorClient; FakeMatrixApi.client = moorClient;
await moorClient.checkHomeserver('https://fakeServer.notExisting', await moorClient.checkHomeserver(
Uri.parse('https://fakeServer.notExisting'),
checkWellKnown: false); checkWellKnown: false);
await moorClient.init( await moorClient.init(
newToken: 'abcd', newToken: 'abcd',

View File

@ -53,7 +53,8 @@ void main() {
client = await getClient(); client = await getClient();
await client.abortSync(); await client.abortSync();
await otherClient.checkHomeserver('https://fakeserver.notexisting', await otherClient.checkHomeserver(
Uri.parse('https://fakeserver.notexisting'),
checkWellKnown: false); checkWellKnown: false);
await otherClient.init( await otherClient.init(
newToken: 'abc', newToken: 'abc',

View File

@ -89,7 +89,7 @@ void main() {
httpClient: FakeMatrixApi(), httpClient: FakeMatrixApi(),
databaseBuilder: getDatabase, databaseBuilder: getDatabase,
); );
await client2.checkHomeserver('https://fakeserver.notexisting', await client2.checkHomeserver(Uri.parse('https://fakeserver.notexisting'),
checkWellKnown: false); checkWellKnown: false);
await client2.init( await client2.init(
newToken: 'abc', newToken: 'abc',

View File

@ -265,7 +265,7 @@ void main() {
test('sendAgain', () async { test('sendAgain', () async {
final matrix = Client('testclient', httpClient: FakeMatrixApi()); final matrix = Client('testclient', httpClient: FakeMatrixApi());
await matrix.checkHomeserver('https://fakeserver.notexisting', await matrix.checkHomeserver(Uri.parse('https://fakeserver.notexisting'),
checkWellKnown: false); checkWellKnown: false);
await matrix.login(LoginType.mLoginPassword, await matrix.login(LoginType.mLoginPassword,
identifier: AuthenticationUserIdentifier(user: 'test'), identifier: AuthenticationUserIdentifier(user: 'test'),
@ -284,7 +284,7 @@ void main() {
test('requestKey', () async { test('requestKey', () async {
final matrix = Client('testclient', httpClient: FakeMatrixApi()); final matrix = Client('testclient', httpClient: FakeMatrixApi());
await matrix.checkHomeserver('https://fakeserver.notexisting', await matrix.checkHomeserver(Uri.parse('https://fakeserver.notexisting'),
checkWellKnown: false); checkWellKnown: false);
await matrix.login(LoginType.mLoginPassword, await matrix.login(LoginType.mLoginPassword,
identifier: AuthenticationUserIdentifier(user: 'test'), identifier: AuthenticationUserIdentifier(user: 'test'),
@ -1252,7 +1252,7 @@ void main() {
'/_matrix/media/r0/download/example.org/thumb': THUMBNAIL_BUFF, '/_matrix/media/r0/download/example.org/thumb': THUMBNAIL_BUFF,
}[uri.path]!; }[uri.path]!;
}; };
await client.checkHomeserver('https://fakeserver.notexisting', await client.checkHomeserver(Uri.parse('https://fakeserver.notexisting'),
checkWellKnown: false); checkWellKnown: false);
final room = Room(id: '!localpart:server.abc', client: client); final room = Room(id: '!localpart:server.abc', client: client);
var event = Event.fromJson({ var event = Event.fromJson({
@ -1439,7 +1439,7 @@ void main() {
'/_matrix/media/r0/download/example.org/newfile': FILE_BUFF, '/_matrix/media/r0/download/example.org/newfile': FILE_BUFF,
}[uri.path]!; }[uri.path]!;
}; };
await client.checkHomeserver('https://fakeserver.notexisting', await client.checkHomeserver(Uri.parse('https://fakeserver.notexisting'),
checkWellKnown: false); checkWellKnown: false);
final room = Room(id: '!localpart:server.abc', client: await getClient()); final room = Room(id: '!localpart:server.abc', client: await getClient());
final event = Event.fromJson({ final event = Event.fromJson({

View File

@ -35,7 +35,7 @@ Future<Client> getClient() async {
databaseBuilder: getDatabase, databaseBuilder: getDatabase,
); );
FakeMatrixApi.client = client; FakeMatrixApi.client = client;
await client.checkHomeserver('https://fakeServer.notExisting', await client.checkHomeserver(Uri.parse('https://fakeServer.notExisting'),
checkWellKnown: false); checkWellKnown: false);
await client.init( await client.init(
newToken: 'abcd', newToken: 'abcd',

View File

@ -28,7 +28,7 @@ void main() {
Logs().level = Level.error; Logs().level = Level.error;
test('Formatting', () async { test('Formatting', () async {
final client = Client('testclient', httpClient: FakeMatrixApi()); final client = Client('testclient', httpClient: FakeMatrixApi());
await client.checkHomeserver('https://fakeserver.notexisting', await client.checkHomeserver(Uri.parse('https://fakeserver.notexisting'),
checkWellKnown: false); checkWellKnown: false);
final mxc = 'mxc://exampleserver.abc/abcdefghijklmn'; final mxc = 'mxc://exampleserver.abc/abcdefghijklmn';
final content = Uri.parse(mxc); final content = Uri.parse(mxc);
@ -50,7 +50,7 @@ void main() {
}); });
test('other port', () async { test('other port', () async {
final client = Client('testclient', httpClient: FakeMatrixApi()); final client = Client('testclient', httpClient: FakeMatrixApi());
await client.checkHomeserver('https://fakeserver.notexisting', await client.checkHomeserver(Uri.parse('https://fakeserver.notexisting'),
checkWellKnown: false); checkWellKnown: false);
client.homeserver = Uri.parse('https://fakeserver.notexisting:1337'); client.homeserver = Uri.parse('https://fakeserver.notexisting:1337');
final mxc = 'mxc://exampleserver.abc/abcdefghijklmn'; final mxc = 'mxc://exampleserver.abc/abcdefghijklmn';
@ -73,7 +73,7 @@ void main() {
}); });
test('other remote port', () async { test('other remote port', () async {
final client = Client('testclient', httpClient: FakeMatrixApi()); final client = Client('testclient', httpClient: FakeMatrixApi());
await client.checkHomeserver('https://fakeserver.notexisting', await client.checkHomeserver(Uri.parse('https://fakeserver.notexisting'),
checkWellKnown: false); checkWellKnown: false);
final mxc = 'mxc://exampleserver.abc:1234/abcdefghijklmn'; final mxc = 'mxc://exampleserver.abc:1234/abcdefghijklmn';
final content = Uri.parse(mxc); final content = Uri.parse(mxc);

View File

@ -63,7 +63,7 @@ void main() {
}); });
test('Create', () async { test('Create', () async {
await client.checkHomeserver('https://fakeserver.notexisting', await client.checkHomeserver(Uri.parse('https://fakeserver.notexisting'),
checkWellKnown: false); checkWellKnown: false);
client.onEvent.add(EventUpdate( client.onEvent.add(EventUpdate(

View File

@ -45,7 +45,7 @@ void main() {
room.setState(user1); room.setState(user1);
room.setState(user2); room.setState(user2);
setUp(() async { setUp(() async {
await client.checkHomeserver('https://fakeserver.notexisting', await client.checkHomeserver(Uri.parse('https://fakeserver.notexisting'),
checkWellKnown: false); checkWellKnown: false);
await client.login(LoginType.mLoginPassword, await client.login(LoginType.mLoginPassword,
identifier: AuthenticationUserIdentifier(user: 'test'), identifier: AuthenticationUserIdentifier(user: 'test'),

View File

@ -38,7 +38,7 @@ void test() async {
Logs().i('++++ Login Alice at ++++'); Logs().i('++++ Login Alice at ++++');
testClientA = Client('TestClientA', databaseBuilder: getDatabase); testClientA = Client('TestClientA', databaseBuilder: getDatabase);
await testClientA.checkHomeserver(TestUser.homeserver); await testClientA.checkHomeserver(Uri.parse(TestUser.homeserver));
await testClientA.login(LoginType.mLoginPassword, await testClientA.login(LoginType.mLoginPassword,
identifier: AuthenticationUserIdentifier(user: TestUser.username), identifier: AuthenticationUserIdentifier(user: TestUser.username),
password: TestUser.password); password: TestUser.password);
@ -46,7 +46,7 @@ void test() async {
Logs().i('++++ Login Bob ++++'); Logs().i('++++ Login Bob ++++');
testClientB = Client('TestClientB', databaseBuilder: getDatabase); testClientB = Client('TestClientB', databaseBuilder: getDatabase);
await testClientB.checkHomeserver(TestUser.homeserver); await testClientB.checkHomeserver(Uri.parse(TestUser.homeserver));
await testClientB.login(LoginType.mLoginPassword, await testClientB.login(LoginType.mLoginPassword,
identifier: AuthenticationUserIdentifier(user: TestUser.username2), identifier: AuthenticationUserIdentifier(user: TestUser.username2),
password: TestUser.password); password: TestUser.password);
@ -237,7 +237,7 @@ void test() async {
Logs().i('++++ Login Bob in another client ++++'); Logs().i('++++ Login Bob in another client ++++');
final testClientC = Client('TestClientC', databaseBuilder: getDatabase); final testClientC = Client('TestClientC', databaseBuilder: getDatabase);
await testClientC.checkHomeserver(TestUser.homeserver); await testClientC.checkHomeserver(Uri.parse(TestUser.homeserver));
await testClientC.login(LoginType.mLoginPassword, await testClientC.login(LoginType.mLoginPassword,
identifier: AuthenticationUserIdentifier(user: TestUser.username2), identifier: AuthenticationUserIdentifier(user: TestUser.username2),
password: TestUser.password); password: TestUser.password);