fluffychat/lib/views/widgets/matrix.dart

422 lines
14 KiB
Dart
Raw Normal View History

2020-01-03 16:23:40 +00:00
import 'dart:async';
import 'dart:io';
2020-12-18 10:43:13 +00:00
import 'dart:convert';
2020-11-14 09:08:13 +00:00
import 'package:adaptive_dialog/adaptive_dialog.dart';
2021-01-16 11:46:38 +00:00
import 'package:adaptive_page_layout/adaptive_page_layout.dart';
2020-06-10 08:07:01 +00:00
import 'package:famedlysdk/encryption.dart';
import 'package:famedlysdk/famedlysdk.dart';
import 'package:fluffychat/utils/matrix_locals.dart';
2020-09-26 18:27:15 +00:00
import 'package:fluffychat/utils/platform_infos.dart';
2020-10-28 09:56:24 +00:00
import 'package:fluffychat/utils/sentry_controller.dart';
2020-01-01 18:10:13 +00:00
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
2021-01-18 16:31:27 +00:00
import 'package:flutter_app_lock/flutter_app_lock.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';
2021-01-18 16:31:27 +00:00
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
2021-01-16 14:35:21 +00:00
import 'package:future_loading_dialog/future_loading_dialog.dart';
2021-01-15 18:59:30 +00:00
import 'package:provider/provider.dart';
2021-04-21 12:19:54 +00:00
import 'package:universal_html/html.dart' as html;
2020-12-18 10:43:13 +00:00
import 'package:http/http.dart' as http;
import 'package:url_launcher/url_launcher.dart';
import 'package:desktop_notifications/desktop_notifications.dart';
2021-04-09 14:15:03 +00:00
import '../../utils/beautify_string_extension.dart';
import '../../utils/localized_exception_extension.dart';
import '../../utils/famedlysdk_store.dart';
2021-04-24 07:15:59 +00:00
import '../key_verification_dialog.dart';
2021-04-09 14:15:03 +00:00
import '../../utils/platform_infos.dart';
2021-04-09 14:29:48 +00:00
import '../../config/app_config.dart';
2021-04-09 14:15:03 +00:00
import '../../config/setting_keys.dart';
import '../../utils/fluffy_client.dart';
import '../../utils/background_push.dart';
2020-01-01 18:10:13 +00:00
class Matrix extends StatefulWidget {
2020-04-08 15:43:07 +00:00
static const String callNamespace = 'chat.fluffy.jitsi_call';
2020-01-01 18:10:13 +00:00
final Widget child;
2021-01-16 11:46:38 +00:00
final GlobalKey<AdaptivePageLayoutState> apl;
final BuildContext context;
2021-04-12 15:31:53 +00:00
final Client testClient;
2021-01-16 11:46:38 +00:00
Matrix({
this.child,
@required this.apl,
@required this.context,
2021-04-12 15:31:53 +00:00
this.testClient,
2021-01-16 11:46:38 +00:00
Key key,
}) : super(key: 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 {
FluffyClient client;
2020-12-11 13:14:33 +00:00
Store store = Store();
2020-05-13 13:58:59 +00:00
@override
2021-01-16 11:46:38 +00:00
BuildContext get context => widget.context;
2020-01-01 18:10:13 +00:00
2021-02-07 16:18:38 +00:00
BackgroundPush _backgroundPush;
2021-04-12 15:31:53 +00:00
bool get testMode => widget.testClient != null;
2020-04-09 07:51:52 +00:00
Map<String, dynamic> get shareContent => _shareContent;
set shareContent(Map<String, dynamic> content) {
_shareContent = content;
onShareContentChanged.add(_shareContent);
}
Map<String, dynamic> _shareContent;
final StreamController<Map<String, dynamic>> onShareContentChanged =
StreamController.broadcast();
2020-01-08 13:19:15 +00:00
2020-04-03 18:24:25 +00:00
File wallpaper;
2020-01-01 18:10:13 +00:00
2021-03-12 08:30:10 +00:00
void _initWithStore() async {
2020-10-04 09:52:06 +00:00
try {
2021-04-12 16:33:43 +00:00
if (!testMode) await client.init();
2021-03-12 08:30:10 +00:00
if (client.isLogged()) {
final statusMsg = await store.getItem(SettingKeys.ownStatusMessage);
if (statusMsg?.isNotEmpty ?? false) {
Logs().v('Send cached status message: "$statusMsg"');
await client.sendPresence(
client.userID,
PresenceType.online,
statusMsg: statusMsg,
);
}
}
2020-10-04 09:52:06 +00:00
} catch (e, s) {
client.onLoginStateChanged.sink.addError(e, s);
2020-10-28 09:56:24 +00:00
SentryController.captureException(e, s);
2020-10-04 10:32:29 +00:00
rethrow;
2020-01-08 13:19:15 +00:00
}
}
2020-02-22 07:27:08 +00:00
StreamSubscription onRoomKeyRequestSub;
2020-06-25 14:29:06 +00:00
StreamSubscription onKeyVerificationRequestSub;
2020-04-08 15:43:07 +00:00
StreamSubscription onJitsiCallSub;
2020-06-27 08:15:37 +00:00
StreamSubscription onNotification;
2021-01-16 11:46:38 +00:00
StreamSubscription<LoginState> onLoginStateChanged;
StreamSubscription<UiaRequest> onUiaRequest;
2020-08-22 13:20:07 +00:00
StreamSubscription<html.Event> onFocusSub;
StreamSubscription<html.Event> onBlurSub;
2021-02-27 13:25:55 +00:00
StreamSubscription<Presence> onOwnPresence;
2020-04-08 15:43:07 +00:00
2021-02-27 08:10:08 +00:00
String _cachedPassword;
String get cachedPassword {
final tmp = _cachedPassword;
_cachedPassword = null;
return tmp;
}
set cachedPassword(String p) => _cachedPassword = p;
void _onUiaRequest(UiaRequest uiaRequest) async {
2020-12-11 09:27:38 +00:00
if (uiaRequest.state != UiaRequestState.waitForUser ||
uiaRequest.nextStages.isEmpty) return;
final stage = uiaRequest.nextStages.first;
switch (stage) {
2020-12-11 09:27:38 +00:00
case AuthenticationTypes.password:
2021-02-27 08:10:08 +00:00
final input = cachedPassword ??
(await showTextInputDialog(
context: context,
title: L10n.of(context).pleaseEnterYourPassword,
okLabel: L10n.of(context).ok,
cancelLabel: L10n.of(context).cancel,
useRootNavigator: false,
textFields: [
DialogTextField(
minLines: 1,
maxLines: 1,
obscureText: true,
hintText: '******',
)
],
))
?.single;
if (input?.isEmpty ?? true) return;
return uiaRequest.completeStage(
2020-12-11 09:27:38 +00:00
AuthenticationPassword(
session: uiaRequest.session,
user: client.userID,
2021-02-27 08:10:08 +00:00
password: input,
2020-12-11 09:27:38 +00:00
identifier: AuthenticationUserIdentifier(user: client.userID),
),
);
default:
await launch(
Matrix.of(context).client.homeserver.toString() +
'/_matrix/client/r0/auth/$stage/fallback/web?session=${uiaRequest.session}',
2021-01-18 21:59:02 +00:00
);
if (OkCancelResult.ok ==
await showOkCancelAlertDialog(
message: L10n.of(context).pleaseFollowInstructionsOnWeb,
context: context,
2021-02-24 11:17:23 +00:00
useRootNavigator: false,
okLabel: L10n.of(context).next,
cancelLabel: L10n.of(context).cancel,
)) {
return uiaRequest.completeStage(
AuthenticationData(session: uiaRequest.session),
);
}
}
}
2020-08-22 13:20:07 +00:00
bool webHasFocus = true;
void _showLocalNotification(EventUpdate eventUpdate) async {
final roomId = eventUpdate.roomID;
2021-02-07 16:18:38 +00:00
if (webHasFocus && client.activeRoomId == roomId) return;
final room = client.getRoomById(roomId);
2020-06-27 09:08:05 +00:00
if (room.notificationCount == 0) return;
2020-06-27 08:15:37 +00:00
final event = Event.fromJson(eventUpdate.content, room);
final title = room.getLocalizedDisplayname(MatrixLocals(L10n.of(context)));
2020-06-27 08:15:37 +00:00
final body = event.getLocalizedBody(
MatrixLocals(L10n.of(context)),
2020-06-27 08:15:37 +00:00
withSenderNamePrefix:
!room.isDirectChat || room.lastEvent.senderId == client.userID,
);
final icon = event.sender.avatarUrl?.getThumbnail(client,
width: 64, height: 64, method: ThumbnailMethod.crop) ??
room.avatar?.getThumbnail(client,
width: 64, height: 64, method: ThumbnailMethod.crop);
if (kIsWeb) {
html.AudioElement()
..src = 'assets/assets/sounds/notification.wav'
..autoplay = true
..load();
html.Notification(
title,
body: body,
2021-04-21 12:19:54 +00:00
icon: icon.toString(),
);
} else if (Platform.isLinux) {
await linuxNotifications.notify(
title,
body: body,
replacesId: _linuxNotificationIds[roomId] ?? -1,
appName: AppConfig.applicationName,
);
}
2020-06-27 08:15:37 +00:00
}
final linuxNotifications = NotificationsClient();
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();
2021-02-07 16:18:38 +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();
}
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);
2020-12-19 12:06:31 +00:00
} catch (e, s) {
2021-01-18 18:07:11 +00:00
Logs().v('[ConfigLoader] Failed to load config.json', e, s);
2020-12-18 10:43:13 +00:00
}
2020-11-08 19:42:35 +00:00
}
2021-01-16 11:46:38 +00:00
LoginState loginState;
2020-11-08 19:42:35 +00:00
void initMatrix() {
2021-01-18 16:31:27 +00:00
// Display the app lock
if (PlatformInfos.isMobile) {
WidgetsBinding.instance.addPostFrameCallback((_) {
FlutterSecureStorage().read(key: SettingKeys.appLockKey).then((lock) {
if (lock?.isNotEmpty ?? false) {
AppLock.of(context).enable();
AppLock.of(context).showLockScreen();
}
});
});
}
2021-04-12 16:33:43 +00:00
client = FluffyClient();
2021-01-16 14:35:21 +00:00
LoadingDialog.defaultTitle = L10n.of(context).loadingPleaseWait;
LoadingDialog.defaultBackLabel = L10n.of(context).close;
LoadingDialog.defaultOnError = (Object e) => e.toLocalizedString(context);
2020-11-21 08:22:35 +00:00
onRoomKeyRequestSub ??=
client.onRoomKeyRequest.stream.listen((RoomKeyRequest request) async {
final room = request.room;
if (request.sender != room.client.userID) {
return; // ignore share requests by others
2020-06-25 14:29:06 +00:00
}
2020-11-21 08:22:35 +00:00
final sender = room.getUserByMXIDSync(request.sender);
if (await showOkCancelAlertDialog(
context: context,
2021-02-24 11:17:23 +00:00
useRootNavigator: false,
2020-11-21 08:22:35 +00:00
title: L10n.of(context).requestToReadOlderMessages,
message:
'${sender.id}\n\n${L10n.of(context).device}:\n${request.requestingDevice.deviceId}\n\n${L10n.of(context).publicKey}:\n${request.requestingDevice.ed25519Key.beautified}',
2020-11-21 08:22:35 +00:00
okLabel: L10n.of(context).verify,
cancelLabel: L10n.of(context).deny,
) ==
OkCancelResult.ok) {
await request.forwardKey();
}
});
onKeyVerificationRequestSub ??= client.onKeyVerificationRequest.stream
.listen((KeyVerification request) async {
var hidPopup = false;
request.onUpdate = () {
if (!hidPopup &&
{KeyVerificationState.done, KeyVerificationState.error}
.contains(request.state)) {
Navigator.of(context, rootNavigator: true).pop('dialog');
2020-06-25 14:29:06 +00:00
}
2020-11-21 08:22:35 +00:00
hidPopup = true;
};
if (await showOkCancelAlertDialog(
context: context,
2021-02-24 11:17:23 +00:00
useRootNavigator: false,
2020-11-21 08:22:35 +00:00
title: L10n.of(context).newVerificationRequest,
message: L10n.of(context).askVerificationRequest(request.userId),
2021-02-18 13:23:22 +00:00
okLabel: L10n.of(context).ok,
cancelLabel: L10n.of(context).cancel,
2020-11-21 08:22:35 +00:00
) ==
OkCancelResult.ok) {
request.onUpdate = null;
hidPopup = true;
await request.acceptVerification();
2021-02-24 11:17:23 +00:00
await KeyVerificationDialog(request: request).show(context);
2020-11-21 08:22:35 +00:00
} else {
request.onUpdate = null;
hidPopup = true;
await request.rejectVerification();
}
});
_initWithStore();
2020-11-08 19:42:35 +00:00
if (kIsWeb) {
onFocusSub = html.window.onFocus.listen((_) => webHasFocus = true);
onBlurSub = html.window.onBlur.listen((_) => webHasFocus = false);
}
2021-01-16 11:46:38 +00:00
onLoginStateChanged ??= client.onLoginStateChanged.stream.listen((state) {
if (loginState != state) {
loginState = state;
widget.apl.currentState.pushNamedAndRemoveAllOthers('/');
}
});
2021-02-27 13:25:55 +00:00
// Cache and resend status message
onOwnPresence ??= client.onPresence.stream.listen((presence) {
if (client.isLogged() &&
client.userID == presence.senderId &&
presence.presence?.statusMsg != null) {
Logs().v('Update status message: "${presence.presence.statusMsg}"');
store.setItem(
SettingKeys.ownStatusMessage, presence.presence.statusMsg);
}
});
onUiaRequest ??= client.onUiaRequest.stream.listen(_onUiaRequest);
2021-02-07 16:18:38 +00:00
if (PlatformInfos.isWeb || PlatformInfos.isLinux) {
2020-11-08 19:42:35 +00:00
client.onSync.stream.first.then((s) {
html.Notification.requestPermission();
onNotification ??= client.onEvent.stream
.where((e) =>
e.type == EventUpdateType.timeline &&
[EventTypes.Message, EventTypes.Sticker, EventTypes.Encrypted]
.contains(e.content['type']) &&
2020-11-08 19:42:35 +00:00
e.content['sender'] != client.userID)
.listen(_showLocalNotification);
});
}
2021-02-07 16:18:38 +00:00
if (PlatformInfos.isMobile) {
_backgroundPush = BackgroundPush(client, context, widget.apl);
}
}
bool _firstStartup = true;
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
Logs().v('AppLifecycleState = $state');
final foreground = state != AppLifecycleState.detached &&
state != AppLifecycleState.paused;
client.backgroundSync = foreground;
client.syncPresence = foreground ? null : PresenceType.unavailable;
client.requestHistoryOnLimitedTimeline = !foreground;
if (_firstStartup) {
_firstStartup = false;
_backgroundPush?.setupPush();
}
2020-11-08 19:42:35 +00:00
}
void initSettings() {
2020-05-13 13:58:59 +00:00
if (store != null) {
2020-12-11 13:14:33 +00:00
store.getItem(SettingKeys.jitsiInstance).then((final instance) =>
AppConfig.jitsiInstance = instance ?? AppConfig.jitsiInstance);
store.getItem(SettingKeys.wallpaper).then((final path) async {
2020-04-08 08:54:17 +00:00
if (path == null) return;
2020-04-03 18:24:25 +00:00
final file = File(path);
if (await file.exists()) {
wallpaper = file;
}
});
store.getItem(SettingKeys.fontSizeFactor).then((value) =>
AppConfig.fontSizeFactor =
double.tryParse(value ?? '') ?? AppConfig.fontSizeFactor);
store
.getItemBool(SettingKeys.renderHtml, AppConfig.renderHtml)
.then((value) => AppConfig.renderHtml = value);
store
.getItemBool(
SettingKeys.hideRedactedEvents, AppConfig.hideRedactedEvents)
.then((value) => AppConfig.hideRedactedEvents = value);
store
.getItemBool(
SettingKeys.hideUnknownEvents, AppConfig.hideUnknownEvents)
.then((value) => AppConfig.hideUnknownEvents = value);
2020-04-03 18:24:25 +00:00
}
2020-01-01 18:10:13 +00:00
}
2020-01-03 16:23:40 +00:00
@override
void dispose() {
2021-02-07 16:18:38 +00:00
WidgetsBinding.instance.removeObserver(this);
2020-02-22 07:27:08 +00:00
onRoomKeyRequestSub?.cancel();
2020-06-25 14:29:06 +00:00
onKeyVerificationRequestSub?.cancel();
2021-01-16 15:39:07 +00:00
onLoginStateChanged?.cancel();
2021-02-27 13:25:55 +00:00
onOwnPresence?.cancel();
2020-06-27 08:15:37 +00:00
onNotification?.cancel();
2020-08-22 13:20:07 +00:00
onFocusSub?.cancel();
onBlurSub?.cancel();
2021-02-07 16:18:38 +00:00
_backgroundPush?.onLogin?.cancel();
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
);
}
}