fluffychat/lib/components/matrix.dart

455 lines
15 KiB
Dart
Raw Normal View History

2020-01-03 17:23:40 +01:00
import 'dart:async';
import 'dart:io';
2020-12-18 11:43:13 +01:00
import 'dart:convert';
2020-11-14 10:08:13 +01:00
import 'package:adaptive_dialog/adaptive_dialog.dart';
2021-01-16 12:46:38 +01:00
import 'package:adaptive_page_layout/adaptive_page_layout.dart';
2020-06-10 10:07:01 +02:00
import 'package:famedlysdk/encryption.dart';
import 'package:famedlysdk/famedlysdk.dart';
2020-05-05 10:30:24 +02:00
import 'package:fluffychat/utils/firebase_controller.dart';
import 'package:fluffychat/utils/matrix_locals.dart';
2020-09-26 20:27:15 +02:00
import 'package:fluffychat/utils/platform_infos.dart';
2020-10-28 10:56:24 +01:00
import 'package:fluffychat/utils/sentry_controller.dart';
import 'package:flushbar/flushbar.dart';
2020-01-01 19:10:13 +01:00
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';
2021-01-16 15:35:21 +01:00
import 'package:future_loading_dialog/future_loading_dialog.dart';
2021-01-15 19:59:30 +01:00
import 'package:provider/provider.dart';
2020-06-27 10:15:37 +02:00
import 'package:universal_html/prefer_universal/html.dart' as html;
import 'package:url_launcher/url_launcher.dart';
2020-12-18 11:43:13 +01:00
import 'package:path_provider/path_provider.dart';
import 'package:http/http.dart' as http;
/*import 'package:fluffychat/views/chat.dart';
2020-12-11 14:14:33 +01:00
import 'package:fluffychat/app_config.dart';
import 'package:dbus/dbus.dart';
import 'package:desktop_notifications/desktop_notifications.dart';*/
2020-02-22 08:27:08 +01:00
import '../utils/beautify_string_extension.dart';
2021-01-16 15:35:21 +01:00
import '../utils/localized_exception_extension.dart';
import '../utils/famedlysdk_store.dart';
2020-11-22 22:48:10 +01:00
import 'dialogs/key_verification_dialog.dart';
2020-10-04 19:19:35 +02:00
import '../utils/platform_infos.dart';
2020-12-11 14:14:33 +01:00
import '../app_config.dart';
import '../config/setting_keys.dart';
import 'avatar.dart';
2020-01-01 19:10:13 +01:00
class Matrix extends StatefulWidget {
2020-04-08 17:43:07 +02:00
static const String callNamespace = 'chat.fluffy.jitsi_call';
2020-01-01 19:10:13 +01:00
final Widget child;
2021-01-16 12:46:38 +01:00
final GlobalKey<AdaptivePageLayoutState> apl;
final BuildContext context;
Matrix({
this.child,
@required this.apl,
@required this.context,
Key key,
}) : super(key: key);
2020-01-01 19:10:13 +01:00
@override
MatrixState createState() => MatrixState();
/// Returns the (nearest) Client instance of your application.
2021-01-15 19:59:30 +01:00
static MatrixState of(BuildContext context) =>
Provider.of<MatrixState>(context, listen: false);
2020-01-01 19:10:13 +01:00
}
class MatrixState extends State<Matrix> {
Client client;
2020-12-11 14:14:33 +01:00
Store store = Store();
2020-05-13 15:58:59 +02:00
@override
2021-01-16 12:46:38 +01:00
BuildContext get context => widget.context;
2020-01-01 19:10:13 +01:00
2020-04-09 09:51:52 +02: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 14:19:15 +01:00
String activeRoomId;
2020-04-03 20:24:25 +02:00
File wallpaper;
2020-12-11 14:14:33 +01:00
String clientName;
2020-04-08 17:43:07 +02:00
2020-01-01 19:10:13 +01:00
void clean() async {
if (!kIsWeb) return;
2020-12-11 14:14:33 +01:00
await store.deleteItem(clientName);
2020-01-01 19:10:13 +01:00
}
2020-01-08 14:19:15 +01:00
void _initWithStore() async {
2020-10-04 11:52:06 +02:00
try {
2020-11-21 09:22:35 +01:00
client.init();
2020-11-24 17:53:35 +01:00
final storeItem = await store.getItem(SettingKeys.showNoPid);
final configOptionMissing = storeItem == null || storeItem.isEmpty;
if (configOptionMissing || (!configOptionMissing && storeItem == '1')) {
if (configOptionMissing) {
await store.setItem(SettingKeys.showNoPid, '0');
}
2021-01-16 12:46:38 +01:00
await client.requestThirdPartyIdentifiers().then((l) {
if (l.isEmpty) {
Flushbar(
title: L10n.of(context).warning,
message: L10n.of(context).noPasswordRecoveryDescription,
mainButton: RaisedButton(
elevation: 7,
color: Theme.of(context).scaffoldBackgroundColor,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(6),
),
2020-11-24 17:53:35 +01:00
child: Text(L10n.of(context).edit),
2021-01-16 12:46:38 +01:00
onPressed: () =>
AdaptivePageLayout.of(context).pushNamed('/settings/3pid'),
),
flushbarStyle: FlushbarStyle.FLOATING,
).show(context);
}
}).catchError((_) => null);
}
2020-10-04 11:52:06 +02:00
} catch (e, s) {
client.onLoginStateChanged.sink.addError(e, s);
2020-10-28 10:56:24 +01:00
SentryController.captureException(e, s);
2020-10-04 12:32:29 +02:00
rethrow;
2020-01-08 14:19:15 +01:00
}
}
2020-02-22 08:27:08 +01:00
StreamSubscription onRoomKeyRequestSub;
2020-06-25 16:29:06 +02:00
StreamSubscription onKeyVerificationRequestSub;
2020-04-08 17:43:07 +02:00
StreamSubscription onJitsiCallSub;
2020-06-27 10:15:37 +02:00
StreamSubscription onNotification;
2021-01-16 12:46:38 +01:00
StreamSubscription<LoginState> onLoginStateChanged;
StreamSubscription<UiaRequest> onUiaRequest;
2020-08-22 15:20:07 +02:00
StreamSubscription<html.Event> onFocusSub;
StreamSubscription<html.Event> onBlurSub;
2020-04-08 17:43:07 +02:00
void _onUiaRequest(UiaRequest uiaRequest) async {
2020-12-11 10:27:38 +01:00
uiaRequest.onUpdate = (_) => _onUiaRequest(uiaRequest);
if (uiaRequest.state != UiaRequestState.waitForUser ||
uiaRequest.nextStages.isEmpty) return;
final stage = uiaRequest.nextStages.first;
switch (stage) {
2020-12-11 10:27:38 +01:00
case AuthenticationTypes.password:
final input = await showTextInputDialog(context: context, textFields: [
DialogTextField(
minLines: 1,
maxLines: 1,
obscureText: true,
)
]);
if (input?.isEmpty ?? true) return;
return uiaRequest.completeStage(
2020-12-11 10:27:38 +01:00
AuthenticationPassword(
session: uiaRequest.session,
user: client.userID,
password: input.single,
identifier: AuthenticationUserIdentifier(user: client.userID),
),
);
default:
2020-12-19 13:06:31 +01:00
Logs().w('Warning! Cannot handle the stage "$stage"');
return;
}
}
2020-04-08 17:43:07 +02:00
void onJitsiCall(EventUpdate eventUpdate) {
final event = Event.fromJson(
eventUpdate.content, client.getRoomById(eventUpdate.roomID));
if (DateTime.now().millisecondsSinceEpoch -
2020-06-10 10:07:01 +02:00
event.originServerTs.millisecondsSinceEpoch >
2020-04-08 17:43:07 +02:00
1000 * 60 * 5) {
return;
}
final senderName = event.sender.calcDisplayname();
final senderAvatar = event.sender.avatarUrl;
showDialog(
context: context,
builder: (context) => AlertDialog(
2020-05-07 07:52:40 +02:00
title: Text(L10n.of(context).videoCall),
2020-04-08 17:43:07 +02:00
content: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
2020-04-09 10:16:38 +02:00
ListTile(
contentPadding: EdgeInsets.all(0),
leading: Avatar(senderAvatar, senderName),
title: Text(
senderName,
style: TextStyle(fontSize: 18),
),
subtitle:
event.room.isDirectChat ? null : Text(event.room.displayname),
),
2020-04-08 17:43:07 +02:00
Divider(),
Row(
children: <Widget>[
Spacer(),
FloatingActionButton(
backgroundColor: Colors.red,
2020-12-06 10:31:35 +01:00
child: Icon(Icons.phone_missed_outlined),
2020-04-08 17:43:07 +02:00
onPressed: () => Navigator.of(context).pop(),
),
Spacer(),
FloatingActionButton(
backgroundColor: Colors.green,
2020-12-06 10:31:35 +01:00
child: Icon(Icons.phone_outlined),
2020-04-08 17:43:07 +02:00
onPressed: () {
Navigator.of(context).pop();
launch(event.body);
},
),
Spacer(),
],
),
],
),
),
);
return;
}
2020-02-22 08:27:08 +01:00
2020-08-22 15:20:07 +02:00
bool webHasFocus = true;
void _showLocalNotification(EventUpdate eventUpdate) async {
final roomId = eventUpdate.roomID;
if (webHasFocus && activeRoomId == roomId) return;
final room = client.getRoomById(roomId);
2020-06-27 11:08:05 +02:00
if (room.notificationCount == 0) return;
2020-06-27 10:15:37 +02:00
final event = Event.fromJson(eventUpdate.content, room);
final body = event.getLocalizedBody(
MatrixLocals(L10n.of(context)),
2020-06-27 10:15:37 +02: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(
room.getLocalizedDisplayname(MatrixLocals(L10n.of(context))),
body: body,
icon: icon,
);
} else if (Platform.isLinux) {
/*var sessionBus = DBusClient.session();
var client = NotificationClient(sessionBus);
_linuxNotificationIds[roomId] = await client.notify(
room.getLocalizedDisplayname(MatrixLocals(L10n.of(context))),
body: body,
replacesID: _linuxNotificationIds[roomId] ?? -1,
appName: AppConfig.applicationName,
actionCallback: (_) => Navigator.of(context).pushAndRemoveUntil(
AppRoute.defaultRoute(
context,
ChatView(roomId),
),
(r) => r.isFirst),
);
await sessionBus.close();*/
}
2020-06-27 10:15:37 +02:00
}
//final Map<String, int> _linuxNotificationIds = {};
2020-01-01 19:10:13 +01:00
@override
void initState() {
2020-11-08 20:42:35 +01:00
super.initState();
initMatrix();
2020-12-18 11:43:13 +01:00
initConfig().then((_) => initSettings());
}
Future<void> initConfig() async {
if (PlatformInfos.isMobile) {
return;
}
try {
var configJsonString = '';
if (PlatformInfos.isWeb) {
configJsonString =
utf8.decode((await http.get('config.json')).bodyBytes);
} else if (PlatformInfos.isBetaDesktop) {
final appDocDir = await getApplicationSupportDirectory();
configJsonString =
await File('${appDocDir.path}/config.json').readAsString();
} else {
final appDocDir = await getApplicationDocumentsDirectory();
configJsonString =
await File('${appDocDir.path}/config.json').readAsString();
}
final configJson = json.decode(configJsonString);
AppConfig.loadFromJson(configJson);
2020-12-19 13:06:31 +01:00
} catch (e, s) {
Logs().w('[ConfigLoader] Failed to load config.json', e, s);
2020-12-18 11:43:13 +01:00
}
2020-11-08 20:42:35 +01:00
}
2021-01-16 12:46:38 +01:00
LoginState loginState;
2020-11-08 20:42:35 +01:00
void initMatrix() {
2020-12-11 14:14:33 +01:00
clientName =
'${AppConfig.applicationName} ${kIsWeb ? 'Web' : Platform.operatingSystem}';
2020-11-21 09:22:35 +01:00
final Set verificationMethods = <KeyVerificationMethod>{
KeyVerificationMethod.numbers
};
if (PlatformInfos.isMobile) {
// emojis don't show in web somehow
verificationMethods.add(KeyVerificationMethod.emoji);
}
client = Client(
2020-12-11 14:14:33 +01:00
clientName,
2020-11-21 09:22:35 +01:00
enableE2eeRecovery: true,
verificationMethods: verificationMethods,
importantStateEvents: <String>{
'im.ponies.room_emotes', // we want emotes to work properly
},
databaseBuilder: getDatabase,
);
2021-01-16 15:35:21 +01:00
LoadingDialog.defaultTitle = L10n.of(context).loadingPleaseWait;
LoadingDialog.defaultBackLabel = L10n.of(context).close;
LoadingDialog.defaultOnError = (Object e) => e.toLocalizedString(context);
2020-11-21 09:22:35 +01:00
onJitsiCallSub ??= client.onEvent.stream
.where((e) =>
e.type == EventUpdateType.timeline &&
e.eventType == 'm.room.message' &&
e.content['content']['msgtype'] == Matrix.callNamespace &&
e.content['sender'] != client.userID)
.listen(onJitsiCall);
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 16:29:06 +02:00
}
2020-11-21 09:22:35 +01:00
final sender = room.getUserByMXIDSync(request.sender);
if (await showOkCancelAlertDialog(
context: context,
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 09:22:35 +01: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 16:29:06 +02:00
}
2020-11-21 09:22:35 +01:00
hidPopup = true;
};
if (await showOkCancelAlertDialog(
context: context,
title: L10n.of(context).newVerificationRequest,
message: L10n.of(context).askVerificationRequest(request.userId),
) ==
OkCancelResult.ok) {
request.onUpdate = null;
hidPopup = true;
await request.acceptVerification();
2020-11-22 22:48:10 +01:00
await KeyVerificationDialog(request: request).show(context);
2020-11-21 09:22:35 +01:00
} else {
request.onUpdate = null;
hidPopup = true;
await request.rejectVerification();
}
});
_initWithStore();
2020-11-08 20:42:35 +01:00
if (kIsWeb) {
onFocusSub = html.window.onFocus.listen((_) => webHasFocus = true);
onBlurSub = html.window.onBlur.listen((_) => webHasFocus = false);
}
2021-01-16 12:46:38 +01:00
onLoginStateChanged ??= client.onLoginStateChanged.stream.listen((state) {
if (loginState != state) {
loginState = state;
widget.apl.currentState.pushNamedAndRemoveAllOthers('/');
if (loginState == LoginState.logged) {
FirebaseController.context = context;
FirebaseController.setupFirebase(
this,
clientName,
).catchError(SentryController.captureException);
}
}
});
onUiaRequest ??= client.onUiaRequest.stream.listen(_onUiaRequest);
2020-11-08 20:42:35 +01:00
if (kIsWeb || Platform.isLinux) {
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.eventType) &&
e.content['sender'] != client.userID)
.listen(_showLocalNotification);
});
}
}
void initSettings() {
2020-05-13 15:58:59 +02:00
if (store != null) {
2020-12-11 14:14:33 +01:00
store.getItem(SettingKeys.jitsiInstance).then((final instance) =>
AppConfig.jitsiInstance = instance ?? AppConfig.jitsiInstance);
store.getItem(SettingKeys.wallpaper).then((final path) async {
2020-04-08 10:54:17 +02:00
if (path == null) return;
2020-04-03 20:24:25 +02:00
final file = File(path);
if (await file.exists()) {
wallpaper = file;
}
});
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 20:24:25 +02:00
}
2020-01-01 19:10:13 +01:00
}
2020-01-03 17:23:40 +01:00
@override
void dispose() {
2020-02-22 08:27:08 +01:00
onRoomKeyRequestSub?.cancel();
2020-06-25 16:29:06 +02:00
onKeyVerificationRequestSub?.cancel();
2020-04-08 17:43:07 +02:00
onJitsiCallSub?.cancel();
2020-06-27 10:15:37 +02:00
onNotification?.cancel();
2020-08-22 15:20:07 +02:00
onFocusSub?.cancel();
onBlurSub?.cancel();
2020-01-03 17:23:40 +01:00
super.dispose();
}
2020-01-01 19:10:13 +01:00
@override
Widget build(BuildContext context) {
2021-01-15 19:59:30 +01:00
return Provider(
create: (_) => this,
2020-12-19 16:37:32 +01:00
child: widget.child,
2020-01-01 19:10:13 +01:00
);
}
}