fluffychat/lib/widgets/matrix.dart

521 lines
16 KiB
Dart
Raw Permalink Normal View History

2020-01-03 16:23:40 +00:00
import 'dart:async';
2020-12-18 10:43:13 +00:00
import 'dart:convert';
2020-01-01 18:10:13 +00:00
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
2021-10-26 16:50:34 +00:00
2022-11-04 12:49:23 +00:00
import 'package:adaptive_dialog/adaptive_dialog.dart';
2022-01-29 11:35:03 +00:00
import 'package:collection/collection.dart';
2021-10-26 16:50:34 +00:00
import 'package:desktop_notifications/desktop_notifications.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';
2022-08-25 16:31:30 +00:00
import 'package:future_loading_dialog/future_loading_dialog.dart';
2021-10-26 16:50:34 +00:00
import 'package:http/http.dart' as http;
2022-04-15 09:42:59 +00:00
import 'package:image_picker/image_picker.dart';
2024-02-22 17:59:38 +00:00
import 'package:intl/intl.dart';
2021-10-26 16:50:34 +00:00
import 'package:matrix/encryption.dart';
import 'package:matrix/matrix.dart';
2021-01-15 18:59:30 +00:00
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
2021-04-21 12:19:54 +00:00
import 'package:universal_html/html.dart' as html;
2023-01-26 08:47:30 +00:00
import 'package:url_launcher/url_launcher_string.dart';
2021-10-26 16:50:34 +00:00
import 'package:fluffychat/utils/client_manager.dart';
import 'package:fluffychat/utils/init_with_restore.dart';
2022-08-25 16:31:30 +00:00
import 'package:fluffychat/utils/localized_exception_extension.dart';
2024-02-22 17:59:38 +00:00
import 'package:fluffychat/utils/matrix_sdk_extensions/matrix_file_extension.dart';
2021-10-26 16:50:34 +00:00
import 'package:fluffychat/utils/platform_infos.dart';
import 'package:fluffychat/utils/uia_request_manager.dart';
import 'package:fluffychat/utils/voip_plugin.dart';
2023-08-13 11:41:01 +00:00
import 'package:fluffychat/widgets/fluffy_chat_app.dart';
2021-05-22 06:53:52 +00:00
import '../config/app_config.dart';
import '../config/setting_keys.dart';
2021-11-09 20:32:16 +00:00
import '../pages/key_verification/key_verification_dialog.dart';
import '../utils/account_bundles.dart';
2021-05-22 06:53:52 +00:00
import '../utils/background_push.dart';
import 'local_notifications_extension.dart';
// import 'package:flutter_secure_storage/flutter_secure_storage.dart';
2020-01-01 18:10:13 +00:00
class Matrix extends StatefulWidget {
2022-01-29 11:35:03 +00:00
final Widget? child;
2020-01-01 18:10:13 +00:00
final List<Client> clients;
2021-04-12 15:31:53 +00:00
2022-01-29 11:35:03 +00:00
final Map<String, String>? queryParameters;
2021-07-08 16:42:46 +00:00
final SharedPreferences store;
2021-10-14 16:09:30 +00:00
const Matrix({
2021-01-16 11:46:38 +00:00
this.child,
2022-01-29 11:35:03 +00:00
required this.clients,
required this.store,
2021-07-08 16:42:46 +00:00
this.queryParameters,
super.key,
});
2020-01-01 18:10:13 +00:00
@override
MatrixState createState() => MatrixState();
/// Returns the (nearest) Client instance of your application.
2021-01-15 18:59:30 +00:00
static MatrixState of(BuildContext context) =>
Provider.of<MatrixState>(context, listen: false);
2020-01-01 18:10:13 +00:00
}
2021-02-07 16:18:38 +00:00
class MatrixState extends State<Matrix> with WidgetsBindingObserver {
2021-11-24 17:39:40 +00:00
int _activeClient = -1;
2022-01-29 11:35:03 +00:00
String? activeBundle;
SharedPreferences get store => widget.store;
2020-01-01 18:10:13 +00:00
2022-04-15 09:42:59 +00:00
XFile? loginAvatar;
String? loginUsername;
bool? loginRegistrationSupported;
2022-12-05 08:11:52 +00:00
BackgroundPush? backgroundPush;
2021-02-07 16:18:38 +00:00
2021-11-24 17:39:40 +00:00
Client get client {
if (widget.clients.isEmpty) {
widget.clients.add(getLoginClient());
}
if (_activeClient < 0 || _activeClient >= widget.clients.length) {
2022-01-29 11:35:03 +00:00
return currentBundle!.first!;
2021-11-24 17:39:40 +00:00
}
return widget.clients[_activeClient];
}
VoipPlugin? voipPlugin;
bool get isMultiAccount => widget.clients.length > 1;
int getClientIndexByMatrixId(String matrixId) =>
widget.clients.indexWhere((client) => client.userID == matrixId);
2022-01-29 11:35:03 +00:00
late String currentClientSecret;
RequestTokenResponse? currentThreepidCreds;
2021-10-30 12:06:10 +00:00
2022-01-29 11:35:03 +00:00
void setActiveClient(Client? cl) {
final i = widget.clients.indexWhere((c) => c == cl);
2022-01-29 11:35:03 +00:00
if (i != -1) {
2021-11-24 17:39:40 +00:00
_activeClient = i;
// TODO: Multi-client VoiP support
createVoipPlugin();
} else {
2022-01-29 11:35:03 +00:00
Logs().w('Tried to set an unknown client ${cl!.userID} as active');
}
}
2022-01-29 11:35:03 +00:00
List<Client?>? get currentBundle {
if (!hasComplexBundles) {
return List.from(widget.clients);
}
final bundles = accountBundles;
if (bundles.containsKey(activeBundle)) {
return bundles[activeBundle];
}
return bundles.values.first;
}
2022-01-29 11:35:03 +00:00
Map<String?, List<Client?>> get accountBundles {
final resBundles = <String?, List<_AccountBundleWithClient>>{};
for (var i = 0; i < widget.clients.length; i++) {
final bundles = widget.clients[i].accountBundles;
for (final bundle in bundles) {
if (bundle.name == null) {
continue;
}
resBundles[bundle.name] ??= [];
resBundles[bundle.name]!.add(
_AccountBundleWithClient(
client: widget.clients[i],
bundle: bundle,
),
);
}
}
for (final b in resBundles.values) {
b.sort(
(a, b) => a.bundle!.priority == null
? 1
: b.bundle!.priority == null
? -1
: a.bundle!.priority!.compareTo(b.bundle!.priority!),
);
}
return resBundles
.map((k, v) => MapEntry(k, v.map((vv) => vv.client).toList()));
}
bool get hasComplexBundles => accountBundles.values.any((v) => v.length > 1);
2022-01-29 11:35:03 +00:00
Client? _loginClientCandidate;
Client getLoginClient() {
2021-10-27 09:14:27 +00:00
if (widget.clients.isNotEmpty && !client.isLogged()) {
return client;
}
2022-01-29 11:35:03 +00:00
final candidate = _loginClientCandidate ??= ClientManager.createClient(
'${AppConfig.applicationName}-${DateTime.now().millisecondsSinceEpoch}',
)..onLoginStateChanged
.stream
.where((l) => l == LoginState.loggedIn)
.first
.then((_) {
2021-10-27 09:14:27 +00:00
if (!widget.clients.contains(_loginClientCandidate)) {
2022-01-29 11:35:03 +00:00
widget.clients.add(_loginClientCandidate!);
2021-10-27 09:14:27 +00:00
}
ClientManager.addClientNameToStore(
_loginClientCandidate!.clientName,
store,
);
2022-01-29 11:35:03 +00:00
_registerSubs(_loginClientCandidate!.clientName);
2021-09-19 12:44:09 +00:00
_loginClientCandidate = null;
2023-08-13 11:41:01 +00:00
FluffyChatApp.router.go('/rooms');
});
2022-01-29 11:35:03 +00:00
return candidate;
}
2022-01-29 11:35:03 +00:00
Client? getClientByName(String name) =>
widget.clients.firstWhereOrNull((c) => c.clientName == name);
2022-01-29 11:35:03 +00:00
Map<String, dynamic>? get shareContent => _shareContent;
2022-01-29 11:35:03 +00:00
set shareContent(Map<String, dynamic>? content) {
2020-04-09 07:51:52 +00:00
_shareContent = content;
onShareContentChanged.add(_shareContent);
}
2022-01-29 11:35:03 +00:00
Map<String, dynamic>? _shareContent;
2020-04-09 07:51:52 +00:00
2022-01-29 11:35:03 +00:00
final StreamController<Map<String, dynamic>?> onShareContentChanged =
2020-04-09 07:51:52 +00:00
StreamController.broadcast();
2020-01-08 13:19:15 +00:00
final onRoomKeyRequestSub = <String, StreamSubscription>{};
final onKeyVerificationRequestSub = <String, StreamSubscription>{};
final onNotification = <String, StreamSubscription>{};
final onLoginStateChanged = <String, StreamSubscription<LoginState>>{};
final onUiaRequest = <String, StreamSubscription<UiaRequest>>{};
2022-01-29 11:35:03 +00:00
StreamSubscription<html.Event>? onFocusSub;
StreamSubscription<html.Event>? onBlurSub;
2020-04-08 15:43:07 +00:00
2022-01-29 11:35:03 +00:00
String? _cachedPassword;
Timer? _cachedPasswordClearTimer;
2022-01-29 11:35:03 +00:00
String? get cachedPassword => _cachedPassword;
2022-01-29 11:35:03 +00:00
set cachedPassword(String? p) {
2021-10-27 09:14:27 +00:00
Logs().d('Password cached');
_cachedPasswordClearTimer?.cancel();
_cachedPassword = p;
_cachedPasswordClearTimer = Timer(const Duration(minutes: 10), () {
_cachedPassword = null;
2021-10-27 09:14:27 +00:00
Logs().d('Cached Password cleared');
});
}
2020-08-22 13:20:07 +00:00
bool webHasFocus = true;
2023-08-13 11:41:01 +00:00
String? get activeRoomId {
2023-08-18 05:24:31 +00:00
final route = FluffyChatApp.router.routeInformationProvider.value.uri.path;
if (!route.startsWith('/rooms/')) return null;
2023-08-13 11:41:01 +00:00
return route.split('/')[2];
}
2021-05-01 13:42:23 +00:00
final linuxNotifications =
PlatformInfos.isLinux ? NotificationsClient() : null;
final Map<String, int> linuxNotificationIds = {};
2020-01-01 18:10:13 +00:00
@override
void initState() {
2020-11-08 19:42:35 +00:00
super.initState();
2022-05-12 09:25:34 +00:00
WidgetsBinding.instance.addObserver(this);
2020-11-08 19:42:35 +00:00
initMatrix();
2021-01-19 15:58:30 +00:00
if (PlatformInfos.isWeb) {
initConfig().then((_) => initSettings());
} else {
initSettings();
}
2022-08-25 16:31:30 +00:00
initLoadingDialog();
}
void initLoadingDialog() {
WidgetsBinding.instance.addPostFrameCallback((_) {
LoadingDialog.defaultTitle = L10n.of(context)!.loadingPleaseWait;
LoadingDialog.defaultBackLabel = L10n.of(context)!.close;
LoadingDialog.defaultOnError =
(e) => (e as Object?)!.toLocalizedString(context);
});
2020-12-18 10:43:13 +00:00
}
Future<void> initConfig() async {
try {
2021-04-14 08:37:15 +00:00
final configJsonString =
2021-04-21 12:19:54 +00:00
utf8.decode((await http.get(Uri.parse('config.json'))).bodyBytes);
2020-12-18 10:43:13 +00:00
final configJson = json.decode(configJsonString);
AppConfig.loadFromJson(configJson);
2021-11-23 10:37:25 +00:00
} on FormatException catch (_) {
Logs().v('[ConfigLoader] config.json not found');
} catch (e) {
2021-06-10 08:20:00 +00:00
Logs().v('[ConfigLoader] config.json not found', e);
2020-12-18 10:43:13 +00:00
}
2020-11-08 19:42:35 +00:00
}
void _registerSubs(String name) {
final c = getClientByName(name);
if (c == null) {
Logs().w(
'Attempted to register subscriptions for non-existing client $name',
);
return;
2021-01-18 16:31:27 +00:00
}
2021-11-24 17:39:40 +00:00
onRoomKeyRequestSub[name] ??=
c.onRoomKeyRequest.stream.listen((RoomKeyRequest request) async {
if (widget.clients.any(
((cl) =>
cl.userID == request.requestingDevice.userId &&
cl.identityKey == request.requestingDevice.curve25519Key),
)) {
2021-11-24 17:39:40 +00:00
Logs().i(
'[Key Request] Request is from one of our own clients, forwarding the key...',
);
2021-11-24 17:39:40 +00:00
await request.forwardKey();
}
});
onKeyVerificationRequestSub[name] ??= c.onKeyVerificationRequest.stream
2020-11-21 08:22:35 +00:00
.listen((KeyVerification request) async {
var hidPopup = false;
request.onUpdate = () {
if (!hidPopup &&
{KeyVerificationState.done, KeyVerificationState.error}
.contains(request.state)) {
2023-08-13 11:41:01 +00:00
Navigator.of(context).pop('dialog');
2020-06-25 14:29:06 +00:00
}
2020-11-21 08:22:35 +00:00
hidPopup = true;
};
2021-10-10 10:11:39 +00:00
request.onUpdate = null;
hidPopup = true;
await KeyVerificationDialog(request: request).show(context);
2020-11-21 08:22:35 +00:00
});
onLoginStateChanged[name] ??= c.onLoginStateChanged.stream.listen((state) {
final loggedInWithMultipleClients = widget.clients.length > 1;
if (state == LoginState.loggedOut) {
InitWithRestoreExtension.deleteSessionBackup(name);
}
if (loggedInWithMultipleClients && state != LoginState.loggedIn) {
_cancelSubs(c.clientName);
widget.clients.remove(c);
ClientManager.removeClientNameFromStore(c.clientName, store);
ScaffoldMessenger.of(context).showSnackBar(
2021-09-19 12:25:18 +00:00
SnackBar(
2022-01-29 11:35:03 +00:00
content: Text(L10n.of(context)!.oneClientLoggedOut),
2021-09-19 12:25:18 +00:00
),
);
2021-09-19 12:25:18 +00:00
if (state != LoginState.loggedIn) {
2023-08-13 11:41:01 +00:00
FluffyChatApp.router.go('/rooms');
2021-06-10 08:20:00 +00:00
}
} else {
2023-08-13 11:41:01 +00:00
FluffyChatApp.router
.go(state == LoginState.loggedIn ? '/rooms' : '/home');
2021-01-16 11:46:38 +00:00
}
});
2021-10-26 18:01:53 +00:00
onUiaRequest[name] ??= c.onUiaRequest.stream.listen(uiaRequestHandler);
2021-02-07 16:18:38 +00:00
if (PlatformInfos.isWeb || PlatformInfos.isLinux) {
c.onSync.stream.first.then((s) {
2020-11-08 19:42:35 +00:00
html.Notification.requestPermission();
onNotification[name] ??= c.onEvent.stream
.where(
(e) =>
e.type == EventUpdateType.timeline &&
[EventTypes.Message, EventTypes.Sticker, EventTypes.Encrypted]
.contains(e.content['type']) &&
e.content['sender'] != c.userID,
)
.listen(showLocalNotification);
2020-11-08 19:42:35 +00:00
});
}
}
void _cancelSubs(String name) {
onRoomKeyRequestSub[name]?.cancel();
onRoomKeyRequestSub.remove(name);
onKeyVerificationRequestSub[name]?.cancel();
onKeyVerificationRequestSub.remove(name);
onLoginStateChanged[name]?.cancel();
onLoginStateChanged.remove(name);
onNotification[name]?.cancel();
onNotification.remove(name);
}
void initMatrix() {
for (final c in widget.clients) {
_registerSubs(c.clientName);
}
if (kIsWeb) {
onFocusSub = html.window.onFocus.listen((_) => webHasFocus = true);
onBlurSub = html.window.onBlur.listen((_) => webHasFocus = false);
}
2021-02-07 16:18:38 +00:00
if (PlatformInfos.isMobile) {
2022-12-05 08:11:52 +00:00
backgroundPush = BackgroundPush(
2023-08-07 16:40:02 +00:00
this,
2022-11-04 12:49:23 +00:00
onFcmError: (errorMsg, {Uri? link}) async {
final result = await showOkCancelAlertDialog(
barrierDismissible: true,
context: context,
2023-10-28 09:30:02 +00:00
title: L10n.of(context)!.pushNotificationsNotAvailable,
2022-11-04 12:49:23 +00:00
message: errorMsg,
2023-10-28 09:30:02 +00:00
fullyCapitalizedForMaterial: false,
okLabel: link == null
? L10n.of(context)!.ok
: L10n.of(context)!.learnMore,
2022-11-04 12:49:23 +00:00
cancelLabel: L10n.of(context)!.doNotShowAgain,
);
if (result == OkCancelResult.ok && link != null) {
2023-10-28 09:30:02 +00:00
launchUrlString(
link.toString(),
mode: LaunchMode.externalApplication,
);
2022-11-04 12:49:23 +00:00
}
if (result == OkCancelResult.cancel) {
await store.setBool(SettingKeys.showNoGoogle, true);
2022-11-04 12:49:23 +00:00
}
},
2021-05-28 18:32:52 +00:00
);
2021-02-07 16:18:38 +00:00
}
createVoipPlugin();
}
2022-02-19 10:58:21 +00:00
void createVoipPlugin() async {
if (store.getBool(SettingKeys.experimentalVoip) == false) {
2022-02-19 10:58:21 +00:00
voipPlugin = null;
return;
}
2023-08-13 16:21:55 +00:00
voipPlugin = VoipPlugin(this);
2021-02-07 16:18:38 +00:00
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
Logs().v('AppLifecycleState = $state');
2023-12-27 13:47:53 +00:00
final foreground = state != AppLifecycleState.inactive &&
state != AppLifecycleState.paused;
2023-12-22 19:18:51 +00:00
client.syncPresence =
state == AppLifecycleState.resumed ? null : PresenceType.unavailable;
2023-12-22 18:27:19 +00:00
if (PlatformInfos.isMobile) {
client.backgroundSync = foreground;
client.requestHistoryOnLimitedTimeline = !foreground;
2023-12-26 12:57:40 +00:00
Logs().v('Set background sync to', foreground);
2023-12-22 18:27:19 +00:00
}
2020-11-08 19:42:35 +00:00
}
void initSettings() {
AppConfig.fontSizeFactor =
double.tryParse(store.getString(SettingKeys.fontSizeFactor) ?? '') ??
AppConfig.fontSizeFactor;
AppConfig.renderHtml =
store.getBool(SettingKeys.renderHtml) ?? AppConfig.renderHtml;
AppConfig.swipeRightToLeftToReply =
store.getBool(SettingKeys.swipeRightToLeftToReply) ??
AppConfig.swipeRightToLeftToReply;
AppConfig.hideRedactedEvents =
store.getBool(SettingKeys.hideRedactedEvents) ??
AppConfig.hideRedactedEvents;
AppConfig.hideUnknownEvents =
store.getBool(SettingKeys.hideUnknownEvents) ??
AppConfig.hideUnknownEvents;
AppConfig.hideUnimportantStateEvents =
store.getBool(SettingKeys.hideUnimportantStateEvents) ??
AppConfig.hideUnimportantStateEvents;
2024-07-22 17:42:27 +00:00
AppConfig.separateChatTypes =
store.getBool(SettingKeys.separateChatTypes) ??
AppConfig.separateChatTypes;
AppConfig.autoplayImages =
store.getBool(SettingKeys.autoplayImages) ?? AppConfig.autoplayImages;
AppConfig.sendTypingNotifications =
store.getBool(SettingKeys.sendTypingNotifications) ??
AppConfig.sendTypingNotifications;
2024-01-20 08:13:53 +00:00
AppConfig.sendPublicReadReceipts =
store.getBool(SettingKeys.sendPublicReadReceipts) ??
AppConfig.sendPublicReadReceipts;
AppConfig.sendOnEnter =
store.getBool(SettingKeys.sendOnEnter) ?? AppConfig.sendOnEnter;
AppConfig.experimentalVoip = store.getBool(SettingKeys.experimentalVoip) ??
AppConfig.experimentalVoip;
AppConfig.showPresences =
store.getBool(SettingKeys.showPresences) ?? AppConfig.showPresences;
2020-01-01 18:10:13 +00:00
}
2020-01-03 16:23:40 +00:00
@override
void dispose() {
2022-05-12 09:25:34 +00:00
WidgetsBinding.instance.removeObserver(this);
2021-02-07 16:18:38 +00:00
onRoomKeyRequestSub.values.map((s) => s.cancel());
onKeyVerificationRequestSub.values.map((s) => s.cancel());
onLoginStateChanged.values.map((s) => s.cancel());
onNotification.values.map((s) => s.cancel());
2022-06-17 20:17:41 +00:00
client.httpClient.close();
2020-08-22 13:20:07 +00:00
onFocusSub?.cancel();
onBlurSub?.cancel();
2021-02-07 16:18:38 +00:00
linuxNotifications?.close();
2020-01-03 16:23:40 +00:00
super.dispose();
}
2020-01-01 18:10:13 +00:00
@override
Widget build(BuildContext context) {
2021-01-15 18:59:30 +00:00
return Provider(
create: (_) => this,
2020-12-19 15:37:32 +00:00
child: widget.child,
2020-01-01 18:10:13 +00:00
);
}
2024-02-22 17:59:38 +00:00
Future<void> dehydrateAction() async {
final response = await showOkCancelAlertDialog(
context: context,
isDestructiveAction: true,
title: L10n.of(context)!.dehydrate,
message: L10n.of(context)!.dehydrateWarning,
);
if (response != OkCancelResult.ok) {
return;
}
final result = await showFutureLoadingDialog(
context: context,
future: client.exportDump,
);
final export = result.result;
if (export == null) return;
final exportBytes = Uint8List.fromList(
const Utf8Codec().encode(export),
);
final exportFileName =
'fluffychat-export-${DateFormat(DateFormat.YEAR_MONTH_DAY).format(DateTime.now())}.fluffybackup';
final file = MatrixFile(bytes: exportBytes, name: exportFileName);
file.save(context);
}
2020-01-01 18:10:13 +00:00
}
class _AccountBundleWithClient {
2022-01-29 11:35:03 +00:00
final Client? client;
final AccountBundle? bundle;
_AccountBundleWithClient({this.client, this.bundle});
}