2021-02-01 12:12:28 +01:00
|
|
|
import 'dart:async';
|
2020-04-12 10:35:45 +02:00
|
|
|
|
2021-01-16 12:46:38 +01:00
|
|
|
import 'package:adaptive_page_layout/adaptive_page_layout.dart';
|
2021-01-18 22:59:02 +01:00
|
|
|
import 'package:famedlysdk/famedlysdk.dart';
|
2021-04-15 13:03:14 +02:00
|
|
|
import 'package:fluffychat/views/ui/homeserver_picker_ui.dart';
|
2021-04-09 16:15:03 +02:00
|
|
|
import 'package:fluffychat/views/widgets/matrix.dart';
|
2021-04-09 16:29:48 +02:00
|
|
|
import 'package:fluffychat/config/app_config.dart';
|
2021-02-13 09:56:16 +01:00
|
|
|
import 'package:fluffychat/config/setting_keys.dart';
|
2021-01-17 15:43:38 +01:00
|
|
|
import 'package:fluffychat/utils/platform_infos.dart';
|
2021-05-16 16:38:52 +02:00
|
|
|
import 'package:uni_links/uni_links.dart';
|
2021-04-03 13:09:20 +02:00
|
|
|
|
2021-02-01 12:12:28 +01:00
|
|
|
import 'package:flutter/foundation.dart';
|
2020-10-03 11:11:28 +02:00
|
|
|
import 'package:flutter_gen/gen_l10n/l10n.dart';
|
2020-04-12 10:35:45 +02:00
|
|
|
import 'package:flutter/material.dart';
|
2021-02-01 12:12:28 +01:00
|
|
|
import 'package:future_loading_dialog/future_loading_dialog.dart';
|
|
|
|
import 'package:url_launcher/url_launcher.dart';
|
2021-05-16 16:38:52 +02:00
|
|
|
import '../main.dart';
|
2021-01-17 15:33:31 +01:00
|
|
|
import '../utils/localized_exception_extension.dart';
|
2021-04-21 14:19:54 +02:00
|
|
|
import 'package:universal_html/html.dart' as html;
|
2020-04-12 10:35:45 +02:00
|
|
|
|
2021-01-17 15:33:31 +01:00
|
|
|
class HomeserverPicker extends StatefulWidget {
|
|
|
|
@override
|
2021-04-09 16:28:26 +02:00
|
|
|
HomeserverPickerController createState() => HomeserverPickerController();
|
2021-01-17 15:33:31 +01:00
|
|
|
}
|
2020-04-12 10:35:45 +02:00
|
|
|
|
2021-04-09 16:28:26 +02:00
|
|
|
class HomeserverPickerController extends State<HomeserverPicker> {
|
|
|
|
bool isLoading = false;
|
|
|
|
String domain = AppConfig.defaultHomeserver;
|
|
|
|
final TextEditingController homeserverController =
|
2021-01-22 21:39:37 +01:00
|
|
|
TextEditingController(text: AppConfig.defaultHomeserver);
|
2021-02-01 12:12:28 +01:00
|
|
|
StreamSubscription _intentDataStreamSubscription;
|
|
|
|
|
|
|
|
void _loginWithToken(String token) {
|
|
|
|
if (token?.isEmpty ?? true) return;
|
|
|
|
showFutureLoadingDialog(
|
|
|
|
context: context,
|
|
|
|
future: () => Matrix.of(context).client.login(
|
|
|
|
type: AuthenticationTypes.token,
|
|
|
|
token: token,
|
2021-02-07 17:18:38 +01:00
|
|
|
initialDeviceDisplayName: PlatformInfos.clientName,
|
2021-02-01 12:12:28 +01:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-05-16 16:38:52 +02:00
|
|
|
void _processIncomingUris(String text) async {
|
2021-02-01 12:12:28 +01:00
|
|
|
if (text == null || !text.startsWith(AppConfig.appOpenUrlScheme)) return;
|
|
|
|
AdaptivePageLayout.of(context).popUntilIsFirst();
|
|
|
|
final token = Uri.parse(text).queryParameters['loginToken'];
|
2021-05-16 16:38:52 +02:00
|
|
|
if (token != null) _loginWithToken(token);
|
2021-02-01 12:12:28 +01:00
|
|
|
}
|
|
|
|
|
2021-05-16 16:38:52 +02:00
|
|
|
void _initReceiveUri() {
|
2021-02-01 12:12:28 +01:00
|
|
|
if (!PlatformInfos.isMobile) return;
|
2021-05-16 16:38:52 +02:00
|
|
|
// For receiving shared Uris
|
|
|
|
_intentDataStreamSubscription = linkStream.listen(_processIncomingUris);
|
|
|
|
if (FluffyChatApp.gotInitialLink == false) {
|
|
|
|
FluffyChatApp.gotInitialLink = true;
|
|
|
|
getInitialLink().then(_processIncomingUris);
|
|
|
|
}
|
2021-02-01 12:12:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
2021-05-16 16:38:52 +02:00
|
|
|
_initReceiveUri();
|
2021-04-09 16:28:26 +02:00
|
|
|
if (kIsWeb) {
|
|
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
|
|
final token =
|
|
|
|
Uri.parse(html.window.location.href).queryParameters['loginToken'];
|
|
|
|
_loginWithToken(token);
|
|
|
|
});
|
|
|
|
}
|
2021-02-01 12:12:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
super.dispose();
|
|
|
|
_intentDataStreamSubscription?.cancel();
|
|
|
|
}
|
2021-01-17 15:33:31 +01:00
|
|
|
|
2021-04-09 16:28:26 +02:00
|
|
|
/// Starts an analysis of the given homeserver. It uses the current domain and
|
|
|
|
/// makes sure that it is prefixed with https. Then it searches for the
|
|
|
|
/// well-known information and forwards to the login page depending on the
|
|
|
|
/// login type. For SSO login only the app opens the page and otherwise it
|
|
|
|
/// forwards to the route `/signup`.
|
|
|
|
void checkHomeserverAction() async {
|
2021-01-22 21:39:37 +01:00
|
|
|
try {
|
2021-04-09 16:28:26 +02:00
|
|
|
if (domain.isEmpty) throw L10n.of(context).changeTheHomeserver;
|
|
|
|
var homeserver = domain;
|
2020-07-02 11:10:03 +02:00
|
|
|
|
2021-01-22 21:39:37 +01:00
|
|
|
if (!homeserver.startsWith('https://')) {
|
|
|
|
homeserver = 'https://$homeserver';
|
|
|
|
}
|
2021-01-18 22:59:02 +01:00
|
|
|
|
2021-04-09 16:28:26 +02:00
|
|
|
setState(() => isLoading = true);
|
2021-02-13 09:56:16 +01:00
|
|
|
final wellKnown =
|
|
|
|
await Matrix.of(context).client.checkHomeserver(homeserver);
|
|
|
|
|
|
|
|
var jitsi = wellKnown?.content
|
|
|
|
?.tryGet<Map<String, dynamic>>('im.vector.riot.jitsi')
|
|
|
|
?.tryGet<String>('preferredDomain');
|
|
|
|
if (jitsi != null) {
|
|
|
|
if (!jitsi.endsWith('/')) {
|
|
|
|
jitsi += '/';
|
|
|
|
}
|
|
|
|
Logs().v('Found custom jitsi instance $jitsi');
|
|
|
|
await Matrix.of(context)
|
|
|
|
.store
|
|
|
|
.setItem(SettingKeys.jitsiInstance, jitsi);
|
|
|
|
AppConfig.jitsiInstance = jitsi;
|
|
|
|
}
|
|
|
|
|
2021-05-20 13:59:55 +02:00
|
|
|
final loginTypes = await Matrix.of(context).client.getLoginFlows();
|
2021-01-18 22:59:02 +01:00
|
|
|
if (loginTypes.flows
|
|
|
|
.any((flow) => flow.type == AuthenticationTypes.password)) {
|
|
|
|
await AdaptivePageLayout.of(context)
|
|
|
|
.pushNamed(AppConfig.enableRegistration ? '/signup' : '/login');
|
|
|
|
} else if (loginTypes.flows
|
|
|
|
.any((flow) => flow.type == AuthenticationTypes.sso)) {
|
2021-02-01 12:12:28 +01:00
|
|
|
final redirectUrl = kIsWeb
|
|
|
|
? html.window.location.href
|
2021-03-28 10:43:16 +02:00
|
|
|
: AppConfig.appOpenUrlScheme.toLowerCase() + '://sso';
|
2021-02-01 12:12:28 +01:00
|
|
|
await launch(
|
2021-03-28 10:43:16 +02:00
|
|
|
'${Matrix.of(context).client.homeserver?.toString()}/_matrix/client/r0/login/sso/redirect?redirectUrl=${Uri.encodeQueryComponent(redirectUrl)}');
|
2021-01-18 22:59:02 +01:00
|
|
|
}
|
2021-01-17 15:33:31 +01:00
|
|
|
} catch (e) {
|
2021-04-03 13:09:20 +02:00
|
|
|
AdaptivePageLayout.of(context).showSnackBar(
|
|
|
|
SnackBar(content: Text((e as Object).toLocalizedString(context))));
|
2021-01-17 15:33:31 +01:00
|
|
|
} finally {
|
|
|
|
if (mounted) {
|
2021-04-09 16:28:26 +02:00
|
|
|
setState(() => isLoading = false);
|
2021-01-17 15:33:31 +01:00
|
|
|
}
|
2020-04-12 10:35:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2021-04-15 13:03:14 +02:00
|
|
|
Widget build(BuildContext context) => HomeserverPickerUI(this);
|
2020-04-12 10:35:45 +02:00
|
|
|
}
|