2021-06-06 17:07:19 +02:00
|
|
|
import 'dart:async';
|
|
|
|
|
2022-04-29 07:26:01 +02:00
|
|
|
import 'package:flutter/foundation.dart';
|
2020-04-12 10:35:45 +02:00
|
|
|
import 'package:flutter/material.dart';
|
2021-10-26 18:50:34 +02:00
|
|
|
|
2022-04-29 07:26:01 +02:00
|
|
|
import 'package:adaptive_dialog/adaptive_dialog.dart';
|
|
|
|
import 'package:file_picker_cross/file_picker_cross.dart';
|
|
|
|
import 'package:flutter_gen/gen_l10n/l10n.dart';
|
|
|
|
import 'package:future_loading_dialog/future_loading_dialog.dart';
|
|
|
|
import 'package:hive_flutter/hive_flutter.dart';
|
2021-10-26 18:50:34 +02:00
|
|
|
import 'package:matrix/matrix.dart';
|
2022-04-15 14:31:57 +02:00
|
|
|
import 'package:matrix_homeserver_recommendations/matrix_homeserver_recommendations.dart';
|
2021-10-26 18:50:34 +02:00
|
|
|
import 'package:vrouter/vrouter.dart';
|
2021-06-06 17:07:19 +02:00
|
|
|
|
2021-10-26 18:50:34 +02:00
|
|
|
import 'package:fluffychat/config/app_config.dart';
|
2022-04-15 14:31:57 +02:00
|
|
|
import 'package:fluffychat/pages/homeserver_picker/homeserver_bottom_sheet.dart';
|
2021-11-09 21:32:16 +01:00
|
|
|
import 'package:fluffychat/pages/homeserver_picker/homeserver_picker_view.dart';
|
2023-01-07 10:29:34 +01:00
|
|
|
import 'package:fluffychat/utils/adaptive_bottom_sheet.dart';
|
2021-10-26 18:50:34 +02:00
|
|
|
import 'package:fluffychat/widgets/matrix.dart';
|
2021-11-09 21:32:16 +01:00
|
|
|
import '../../utils/localized_exception_extension.dart';
|
2020-04-12 10:35:45 +02:00
|
|
|
|
2022-04-29 07:26:01 +02:00
|
|
|
import 'package:fluffychat/utils/tor_stub.dart'
|
|
|
|
if (dart.library.html) 'package:tor_detector_web/tor_detector_web.dart';
|
|
|
|
|
2021-01-17 15:33:31 +01:00
|
|
|
class HomeserverPicker extends StatefulWidget {
|
2022-01-29 12:35:03 +01:00
|
|
|
const HomeserverPicker({Key? key}) : super(key: key);
|
2021-10-14 18:09:30 +02:00
|
|
|
|
2021-01-17 15:33:31 +01:00
|
|
|
@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;
|
2022-04-15 14:31:57 +02:00
|
|
|
final TextEditingController homeserverController = TextEditingController(
|
|
|
|
text: AppConfig.defaultHomeserver,
|
|
|
|
);
|
|
|
|
final FocusNode homeserverFocusNode = FocusNode();
|
2022-01-29 12:35:03 +01:00
|
|
|
String? error;
|
2022-04-15 14:31:57 +02:00
|
|
|
List<HomeserverBenchmarkResult>? benchmarkResults;
|
|
|
|
bool displayServerList = false;
|
2022-05-25 15:53:49 +02:00
|
|
|
|
2022-04-15 14:31:57 +02:00
|
|
|
bool get loadingHomeservers =>
|
|
|
|
AppConfig.allowOtherHomeservers && benchmarkResults == null;
|
|
|
|
String searchTerm = '';
|
|
|
|
|
2022-04-29 07:26:01 +02:00
|
|
|
bool isTorBrowser = false;
|
|
|
|
|
|
|
|
Future<void> _checkTorBrowser() async {
|
|
|
|
if (!kIsWeb) return;
|
|
|
|
|
|
|
|
Hive.openBox('test').then((value) => null).catchError(
|
|
|
|
(e, s) async {
|
|
|
|
await showOkAlertDialog(
|
|
|
|
context: context,
|
|
|
|
title: L10n.of(context)!.indexedDbErrorTitle,
|
|
|
|
message: L10n.of(context)!.indexedDbErrorLong,
|
|
|
|
onWillPop: () async => false);
|
|
|
|
_checkTorBrowser();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
final isTor = await TorBrowserDetector.isTorBrowser;
|
2022-11-13 18:13:01 +01:00
|
|
|
isTorBrowser = isTor;
|
2022-04-29 07:26:01 +02:00
|
|
|
}
|
|
|
|
|
2022-04-15 14:31:57 +02:00
|
|
|
void _updateFocus() {
|
|
|
|
if (benchmarkResults == null) _loadHomeserverList();
|
2022-04-23 10:57:12 +02:00
|
|
|
if (homeserverFocusNode.hasFocus) {
|
|
|
|
setState(() {
|
|
|
|
displayServerList = true;
|
|
|
|
});
|
|
|
|
}
|
2022-04-15 14:31:57 +02:00
|
|
|
}
|
|
|
|
|
2023-01-07 10:29:34 +01:00
|
|
|
void showServerInfo(HomeserverBenchmarkResult server) =>
|
|
|
|
showAdaptiveBottomSheet(
|
2022-04-15 14:31:57 +02:00
|
|
|
context: context,
|
|
|
|
builder: (_) => HomeserverBottomSheet(
|
|
|
|
homeserver: server,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
void onChanged(String text) => setState(() {
|
|
|
|
searchTerm = text;
|
|
|
|
});
|
|
|
|
|
|
|
|
List<HomeserverBenchmarkResult> get filteredHomeservers => benchmarkResults!
|
|
|
|
.where((element) =>
|
|
|
|
element.homeserver.baseUrl.host.contains(searchTerm) ||
|
|
|
|
(element.homeserver.description?.contains(searchTerm) ?? false))
|
|
|
|
.toList();
|
|
|
|
|
|
|
|
void _loadHomeserverList() async {
|
|
|
|
try {
|
2022-08-10 21:15:31 +02:00
|
|
|
final homeserverList =
|
|
|
|
await const JoinmatrixOrgParser().fetchHomeservers();
|
2022-04-15 14:31:57 +02:00
|
|
|
final benchmark = await HomeserverListProvider.benchmarkHomeserver(
|
|
|
|
homeserverList,
|
|
|
|
timeout: const Duration(seconds: 10),
|
|
|
|
);
|
2022-11-21 17:53:01 +01:00
|
|
|
if (!mounted) return;
|
2022-04-15 14:31:57 +02:00
|
|
|
setState(() {
|
|
|
|
benchmarkResults = benchmark;
|
|
|
|
});
|
|
|
|
} catch (e, s) {
|
|
|
|
Logs().e('Homeserver benchmark failed', e, s);
|
|
|
|
benchmarkResults = [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void setServer(String server) => setState(() {
|
|
|
|
homeserverController.text = server;
|
|
|
|
searchTerm = '';
|
|
|
|
homeserverFocusNode.unfocus();
|
2022-04-23 10:57:12 +02:00
|
|
|
displayServerList = false;
|
2022-04-15 14:31:57 +02:00
|
|
|
});
|
2021-11-16 10:23:29 +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
|
2021-06-11 10:08:04 +02:00
|
|
|
/// login type.
|
2021-10-16 09:59:38 +02:00
|
|
|
Future<void> checkHomeserverAction() async {
|
2021-11-16 10:23:29 +01:00
|
|
|
setState(() {
|
2022-04-15 14:31:57 +02:00
|
|
|
homeserverFocusNode.unfocus();
|
2022-04-15 11:42:59 +02:00
|
|
|
error = null;
|
2021-11-16 10:23:29 +01:00
|
|
|
isLoading = true;
|
2022-04-15 14:31:57 +02:00
|
|
|
searchTerm = '';
|
|
|
|
displayServerList = false;
|
2021-11-16 10:23:29 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
2022-04-15 11:42:59 +02:00
|
|
|
homeserverController.text =
|
|
|
|
homeserverController.text.trim().toLowerCase().replaceAll(' ', '-');
|
|
|
|
var homeserver = Uri.parse(homeserverController.text);
|
|
|
|
if (homeserver.scheme.isEmpty) {
|
|
|
|
homeserver = Uri.https(homeserverController.text, '');
|
|
|
|
}
|
|
|
|
final matrix = Matrix.of(context);
|
|
|
|
matrix.loginHomeserverSummary =
|
|
|
|
await matrix.getLoginClient().checkHomeserver(homeserver);
|
|
|
|
final ssoSupported = matrix.loginHomeserverSummary!.loginFlows
|
|
|
|
.any((flow) => flow.type == 'm.login.sso');
|
2021-11-15 12:14:00 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
await Matrix.of(context).getLoginClient().register();
|
2022-04-15 11:42:59 +02:00
|
|
|
matrix.loginRegistrationSupported = true;
|
2021-11-15 12:14:00 +01:00
|
|
|
} on MatrixException catch (e) {
|
2022-04-15 11:42:59 +02:00
|
|
|
matrix.loginRegistrationSupported = e.requireAdditionalAuthentication;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ssoSupported && matrix.loginRegistrationSupported == false) {
|
|
|
|
// Server does not support SSO or registration. We can skip to login page:
|
|
|
|
VRouter.of(context).to('login');
|
|
|
|
} else {
|
|
|
|
VRouter.of(context).to('connect');
|
2021-11-15 12:14:00 +01:00
|
|
|
}
|
2021-01-17 15:33:31 +01:00
|
|
|
} catch (e) {
|
2022-01-29 12:35:03 +01:00
|
|
|
setState(() => error = (e).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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-15 14:31:57 +02:00
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
homeserverFocusNode.removeListener(_updateFocus);
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
homeserverFocusNode.addListener(_updateFocus);
|
2022-04-29 07:26:01 +02:00
|
|
|
_checkTorBrowser();
|
2022-04-15 14:31:57 +02:00
|
|
|
super.initState();
|
|
|
|
}
|
|
|
|
|
2020-04-12 10:35:45 +02:00
|
|
|
@override
|
2021-05-23 13:28:55 +02:00
|
|
|
Widget build(BuildContext context) {
|
|
|
|
Matrix.of(context).navigatorContext = context;
|
|
|
|
return HomeserverPickerView(this);
|
|
|
|
}
|
2022-04-29 07:26:01 +02:00
|
|
|
|
|
|
|
Future<void> restoreBackup() async {
|
2022-11-15 12:51:14 +01:00
|
|
|
final file = await FilePickerCross.importFromStorage();
|
2022-10-16 12:37:38 +02:00
|
|
|
if (file.fileName == null) return;
|
2022-04-29 07:26:01 +02:00
|
|
|
await showFutureLoadingDialog(
|
|
|
|
context: context,
|
|
|
|
future: () async {
|
|
|
|
try {
|
|
|
|
final client = Matrix.of(context).getLoginClient();
|
|
|
|
await client.importDump(file.toString());
|
|
|
|
Matrix.of(context).initMatrix();
|
|
|
|
} catch (e, s) {
|
|
|
|
Logs().e('Future error:', e, s);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2020-04-12 10:35:45 +02:00
|
|
|
}
|