feat: Do well known by default in check homeserver

This commit is contained in:
Krille Fear 2021-01-20 13:21:51 +00:00
parent 1171720bf2
commit c701268d71
10 changed files with 63 additions and 25 deletions

View File

@ -73,6 +73,8 @@ class Client extends MatrixApi {
Set<String> roomPreviewLastEvents; Set<String> roomPreviewLastEvents;
Set<String> supportedLoginTypes;
int sendMessageTimeoutSeconds; int sendMessageTimeoutSeconds;
bool requestHistoryOnLimitedTimeline; bool requestHistoryOnLimitedTimeline;
@ -110,6 +112,8 @@ class Client extends MatrixApi {
/// If [formatLocalpart] is true, then the localpart of an mxid will /// If [formatLocalpart] is true, then the localpart of an mxid will
/// be formatted in the way, that all "_" characters are becomming white spaces and /// be formatted in the way, that all "_" characters are becomming white spaces and
/// the first character of each word becomes uppercase. /// 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( Client(
this.clientName, { this.clientName, {
this.databaseBuilder, this.databaseBuilder,
@ -121,8 +125,10 @@ class Client extends MatrixApi {
this.pinUnreadRooms = false, this.pinUnreadRooms = false,
this.sendMessageTimeoutSeconds = 60, this.sendMessageTimeoutSeconds = 60,
this.requestHistoryOnLimitedTimeline = true, this.requestHistoryOnLimitedTimeline = true,
this.supportedLoginTypes,
@deprecated bool debug, @deprecated bool debug,
}) { }) {
supportedLoginTypes ??= {AuthenticationTypes.password};
verificationMethods ??= <KeyVerificationMethod>{}; verificationMethods ??= <KeyVerificationMethod>{};
importantStateEvents ??= {}; importantStateEvents ??= {};
importantStateEvents.addAll([ importantStateEvents.addAll([
@ -287,7 +293,7 @@ class Client extends MatrixApi {
/// client and sets [homeserver] to [serverUrl] if it is. Supports the types [Uri] /// client and sets [homeserver] to [serverUrl] if it is. Supports the types [Uri]
/// and [String]. /// and [String].
Future<void> checkHomeserver(dynamic homeserverUrl, Future<void> checkHomeserver(dynamic homeserverUrl,
{Set<String> supportedLoginTypes = supportedLoginTypes}) async { {bool checkWellKnown = true}) async {
try { try {
if (homeserverUrl is Uri) { if (homeserverUrl is Uri) {
homeserver = homeserverUrl; homeserver = homeserverUrl;
@ -302,8 +308,19 @@ class Client extends MatrixApi {
} }
homeserver = Uri.parse(homeserverUrl); 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 if (!versions.versions
.any((version) => supportedVersions.contains(version))) { .any((version) => supportedVersions.contains(version))) {
throw Exception( throw Exception(
@ -363,7 +380,6 @@ class Client extends MatrixApi {
/// Handles the login and allows the client to call all APIs which require /// Handles the login and allows the client to call all APIs which require
/// authentication. Returns false if the login was not successful. Throws /// authentication. Returns false if the login was not successful. Throws
/// MatrixException if login was not successful. /// MatrixException if login was not successful.
/// You have to call [checkHomeserver] first to set a homeserver.
@override @override
Future<LoginResponse> login({ Future<LoginResponse> login({
String type = AuthenticationTypes.password, String type = AuthenticationTypes.password,
@ -377,6 +393,9 @@ class Client extends MatrixApi {
String initialDeviceDisplayName, String initialDeviceDisplayName,
AuthenticationData auth, AuthenticationData auth,
}) async { }) async {
if (homeserver == null && user.isValidMatrixId) {
await checkHomeserver(user.domain);
}
final loginResp = await super.login( final loginResp = await super.login(
type: type, type: type,
userIdentifierType: userIdentifierType, userIdentifierType: userIdentifierType,
@ -592,7 +611,6 @@ class Client extends MatrixApi {
: null; : null;
static const Set<String> supportedVersions = {'r0.5.0', 'r0.6.0'}; static const Set<String> supportedVersions = {'r0.5.0', 'r0.6.0'};
static const Set<String> supportedLoginTypes = {AuthenticationTypes.password};
static const String syncFilters = static const String syncFilters =
'{"room":{"state":{"lazy_load_members":true}}}'; '{"room":{"state":{"lazy_load_members":true}}}';
static const String messagesFilters = '{"lazy_load_members":true}'; static const String messagesFilters = '{"lazy_load_members":true}';

View File

@ -23,7 +23,7 @@ dependencies:
matrix_file_e2ee: ^1.0.5 matrix_file_e2ee: ^1.0.5
isolate: ^2.0.3 isolate: ^2.0.3
logger: ^0.9.4 logger: ^0.9.4
matrix_api_lite: ^0.1.5 matrix_api_lite: ^0.1.6
dev_dependencies: dev_dependencies:
test: ^1.15.7 test: ^1.15.7

View File

@ -87,7 +87,8 @@ void main() {
} on MatrixConnectionException catch (exception) { } on MatrixConnectionException catch (exception) {
expect(exception != null, true); 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'); expect(matrix.homeserver.toString(), 'https://fakeserver.notexisting');
final available = await matrix.usernameAvailable('testuser'); final available = await matrix.usernameAvailable('testuser');
@ -318,7 +319,8 @@ void main() {
roomUpdateListFuture = matrix.onRoomUpdate.stream.toList(); roomUpdateListFuture = matrix.onRoomUpdate.stream.toList();
eventUpdateListFuture = matrix.onEvent.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'); final loginResp = await matrix.login(user: 'test', password: '1234');

View File

@ -52,7 +52,8 @@ void main() {
test('setupClient', () async { test('setupClient', () async {
client = await getClient(); client = await getClient();
await otherClient.checkHomeserver('https://fakeServer.notExisting'); await otherClient.checkHomeserver('https://fakeserver.notexisting',
checkWellKnown: false);
otherClient.init( otherClient.init(
newToken: 'abc', newToken: 'abc',
newUserID: '@othertest:fakeServer.notExisting', newUserID: '@othertest:fakeServer.notExisting',

View File

@ -87,7 +87,8 @@ void main() {
client2 = Client('othertestclient', client2 = Client('othertestclient',
httpClient: FakeMatrixApi(), httpClient: FakeMatrixApi(),
databaseBuilder: (_) => client1.database); databaseBuilder: (_) => client1.database);
await client2.checkHomeserver('https://fakeServer.notExisting'); await client2.checkHomeserver('https://fakeserver.notexisting',
checkWellKnown: false);
client2.init( client2.init(
newToken: 'abc', newToken: 'abc',
newUserID: '@othertest:fakeServer.notExisting', newUserID: '@othertest:fakeServer.notExisting',

View File

@ -264,7 +264,8 @@ void main() {
test('sendAgain', () async { test('sendAgain', () async {
var matrix = Client('testclient', httpClient: FakeMatrixApi()); 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'); await matrix.login(user: 'test', password: '1234');
var event = Event.fromJson( var event = Event.fromJson(
@ -280,7 +281,8 @@ void main() {
test('requestKey', () async { test('requestKey', () async {
var matrix = Client('testclient', httpClient: FakeMatrixApi()); 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'); await matrix.login(user: 'test', password: '1234');
var event = Event.fromJson( var event = Event.fromJson(
@ -1051,7 +1053,8 @@ void main() {
THUMBNAIL_BUFF, THUMBNAIL_BUFF,
}[url]; }[url];
}; };
await client.checkHomeserver('https://fakeServer.notExisting'); await client.checkHomeserver('https://fakeserver.notexisting',
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({
'type': EventTypes.Message, 'type': EventTypes.Message,
@ -1234,7 +1237,8 @@ void main() {
FILE_BUFF, FILE_BUFF,
}[url]; }[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()); final room = Room(id: '!localpart:server.abc', client: await getClient());
var event = Event.fromJson({ var event = Event.fromJson({
'type': EventTypes.Message, 'type': EventTypes.Message,

View File

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

View File

@ -30,7 +30,8 @@ void main() {
Logs().level = Level.error; Logs().level = Level.error;
test('Formatting', () async { test('Formatting', () async {
var client = Client('testclient', httpClient: FakeMatrixApi()); 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 mxc = 'mxc://exampleserver.abc/abcdefghijklmn';
final content = Uri.parse(mxc); final content = Uri.parse(mxc);
expect(content.isScheme('mxc'), true); expect(content.isScheme('mxc'), true);

View File

@ -51,7 +51,8 @@ void main() {
}); });
test('Create', () async { test('Create', () async {
await client.checkHomeserver('https://fakeServer.notExisting'); await client.checkHomeserver('https://fakeserver.notexisting',
checkWellKnown: false);
client.onEvent.add(EventUpdate( client.onEvent.add(EventUpdate(
type: EventUpdateType.timeline, type: EventUpdateType.timeline,

View File

@ -86,28 +86,34 @@ void main() {
user3.calcDisplayname(mxidLocalPartFallback: false), 'Unknown user'); user3.calcDisplayname(mxidLocalPartFallback: false), 'Unknown user');
}); });
test('kick', () async { test('kick', () async {
await client.checkHomeserver('https://fakeserver.notexisting'); await client.checkHomeserver('https://fakeserver.notexisting',
checkWellKnown: false);
await user1.kick(); await user1.kick();
}); });
test('ban', () async { test('ban', () async {
await client.checkHomeserver('https://fakeserver.notexisting'); await client.checkHomeserver('https://fakeserver.notexisting',
checkWellKnown: false);
await user1.ban(); await user1.ban();
}); });
test('unban', () async { test('unban', () async {
await client.checkHomeserver('https://fakeserver.notexisting'); await client.checkHomeserver('https://fakeserver.notexisting',
checkWellKnown: false);
await user1.unban(); await user1.unban();
}); });
test('setPower', () async { test('setPower', () async {
await client.checkHomeserver('https://fakeserver.notexisting'); await client.checkHomeserver('https://fakeserver.notexisting',
checkWellKnown: false);
await user1.setPower(50); await user1.setPower(50);
}); });
test('startDirectChat', () async { 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 client.login(user: 'test', password: '1234');
await user1.startDirectChat(); await user1.startDirectChat();
}); });
test('getPresence', () async { test('getPresence', () async {
await client.checkHomeserver('https://fakeserver.notexisting'); await client.checkHomeserver('https://fakeserver.notexisting',
checkWellKnown: false);
await client.handleSync(SyncUpdate.fromJson({ await client.handleSync(SyncUpdate.fromJson({
'presence': { 'presence': {
'events': [ 'events': [
@ -122,15 +128,18 @@ void main() {
expect(user1.presence.presence.presence, PresenceType.online); expect(user1.presence.presence.presence, PresenceType.online);
}); });
test('canBan', () async { test('canBan', () async {
await client.checkHomeserver('https://fakeserver.notexisting'); await client.checkHomeserver('https://fakeserver.notexisting',
checkWellKnown: false);
expect(user1.canBan, false); expect(user1.canBan, false);
}); });
test('canKick', () async { test('canKick', () async {
await client.checkHomeserver('https://fakeserver.notexisting'); await client.checkHomeserver('https://fakeserver.notexisting',
checkWellKnown: false);
expect(user1.canKick, false); expect(user1.canKick, false);
}); });
test('canChangePowerLevel', () async { test('canChangePowerLevel', () async {
await client.checkHomeserver('https://fakeserver.notexisting'); await client.checkHomeserver('https://fakeserver.notexisting',
checkWellKnown: false);
expect(user1.canChangePowerLevel, false); expect(user1.canChangePowerLevel, false);
}); });
test('dispose client', () async { test('dispose client', () async {