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> 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 ??= <KeyVerificationMethod>{};
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<void> checkHomeserver(dynamic homeserverUrl,
{Set<String> 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<LoginResponse> 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<String> supportedVersions = {'r0.5.0', 'r0.6.0'};
static const Set<String> supportedLoginTypes = {AuthenticationTypes.password};
static const String syncFilters =
'{"room":{"state":{"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
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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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