From 15a9a55767d228421a9dfe16353cac682ff8aeb0 Mon Sep 17 00:00:00 2001 From: Inex Code Date: Sat, 13 Jun 2020 22:39:18 +0300 Subject: [PATCH] Two-step well-known check --- lib/src/client.dart | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/lib/src/client.dart b/lib/src/client.dart index ef6b2a4..0000da0 100644 --- a/lib/src/client.dart +++ b/lib/src/client.dart @@ -192,10 +192,29 @@ class Client { ) async { final response = await http .get('https://${MatrixIdOrDomain.domain}/.well-known/matrix/client'); - final rawJson = json.decode(response.body); - return WellKnownInformations.fromJson(rawJson); + WellKnownInformations wellKnown = WellKnownInformations.fromJson(json.decode(response.body)); + if (Uri.parse(wellKnown.mHomeserver.baseUrl).host != MatrixIdOrDomain.domain) { + final response = await http + .get('https://${Uri.parse(wellKnown.mHomeserver.baseUrl).host}/.well-known/matrix/client'); + wellKnown = WellKnownInformations.fromJson(json.decode(response.body)); + } + return wellKnown; } + Future getWellKnownInformationsByDomain(dynamic serverUrl) async { + Uri homeserver = (serverUrl is Uri) ? serverUrl : Uri.parse(serverUrl); + final response = await http + .get('https://${homeserver.host}/.well-known/matrix/client'); + WellKnownInformations wellKnown = WellKnownInformations.fromJson(json.decode(response.body)); + if (Uri.parse(wellKnown.mHomeserver.baseUrl).host != homeserver.host) { + final response = await http + .get('https://${Uri.parse(wellKnown.mHomeserver.baseUrl).host}/.well-known/matrix/client'); + wellKnown = WellKnownInformations.fromJson(json.decode(response.body)); + } + return wellKnown; + } + + /// Checks the supported versions of the Matrix protocol and the supported /// login types. Returns false if the server is not compatible with the /// client.