2020-01-03 16:23:40 +00:00
|
|
|
import 'dart:async';
|
|
|
|
import 'dart:io';
|
2020-02-16 14:57:50 +00:00
|
|
|
|
2020-01-01 18:10:13 +00:00
|
|
|
import 'package:famedlysdk/famedlysdk.dart';
|
2020-06-10 08:07:01 +00:00
|
|
|
import 'package:famedlysdk/encryption.dart';
|
2020-02-22 07:27:08 +00:00
|
|
|
import 'package:fluffychat/components/dialogs/simple_dialogs.dart';
|
2020-05-05 08:30:24 +00:00
|
|
|
import 'package:fluffychat/utils/firebase_controller.dart';
|
2020-01-01 18:10:13 +00:00
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:localstorage/localstorage.dart';
|
2020-04-08 15:43:07 +00:00
|
|
|
import 'package:url_launcher/url_launcher.dart';
|
2020-06-27 08:15:37 +00:00
|
|
|
import 'package:universal_html/prefer_universal/html.dart' as html;
|
2020-05-07 05:52:40 +00:00
|
|
|
import '../l10n/l10n.dart';
|
2020-02-22 07:27:08 +00:00
|
|
|
import '../utils/beautify_string_extension.dart';
|
2020-02-16 14:57:50 +00:00
|
|
|
import '../utils/famedlysdk_store.dart';
|
2020-04-08 15:43:07 +00:00
|
|
|
import 'avatar.dart';
|
2020-06-25 14:29:06 +00:00
|
|
|
import '../views/key_verification.dart';
|
|
|
|
import '../utils/app_route.dart';
|
2020-02-16 14:57:50 +00:00
|
|
|
|
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;
|
|
|
|
|
|
|
|
final String clientName;
|
|
|
|
|
|
|
|
final Client client;
|
|
|
|
|
2020-05-13 13:58:59 +00:00
|
|
|
final Store store;
|
|
|
|
|
|
|
|
Matrix({this.child, this.clientName, this.client, this.store, 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.
|
|
|
|
static MatrixState of(BuildContext context) {
|
2020-05-13 13:58:59 +00:00
|
|
|
var newState =
|
2020-01-01 18:10:13 +00:00
|
|
|
(context.dependOnInheritedWidgetOfExactType<_InheritedMatrix>()).data;
|
2020-05-05 08:30:24 +00:00
|
|
|
newState.context = FirebaseController.context = context;
|
2020-01-01 18:10:13 +00:00
|
|
|
return newState;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class MatrixState extends State<Matrix> {
|
|
|
|
Client client;
|
2020-05-13 13:58:59 +00:00
|
|
|
Store store;
|
|
|
|
@override
|
2020-01-01 18:10:13 +00:00
|
|
|
BuildContext context;
|
|
|
|
|
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
|
|
|
|
|
|
|
String activeRoomId;
|
2020-04-03 18:24:25 +00:00
|
|
|
File wallpaper;
|
2020-05-09 11:36:41 +00:00
|
|
|
bool renderHtml = false;
|
2020-01-03 16:23:40 +00:00
|
|
|
|
2020-04-08 15:43:07 +00:00
|
|
|
String jitsiInstance = 'https://meet.jit.si/';
|
|
|
|
|
2020-01-01 18:10:13 +00:00
|
|
|
void clean() async {
|
|
|
|
if (!kIsWeb) return;
|
|
|
|
|
2020-05-13 13:58:59 +00:00
|
|
|
final storage = LocalStorage('LocalStorage');
|
2020-01-01 18:10:13 +00:00
|
|
|
await storage.ready;
|
2020-01-02 21:31:39 +00:00
|
|
|
await storage.deleteItem(widget.clientName);
|
2020-01-01 18:10:13 +00:00
|
|
|
}
|
|
|
|
|
2020-01-08 13:19:15 +00:00
|
|
|
void _initWithStore() async {
|
2020-05-13 13:58:59 +00:00
|
|
|
var initLoginState = client.onLoginStateChanged.stream.first;
|
2020-05-20 07:10:13 +00:00
|
|
|
client.database = await getDatabase(client);
|
2020-05-13 13:58:59 +00:00
|
|
|
client.connect();
|
2020-02-16 14:57:50 +00:00
|
|
|
if (await initLoginState == LoginState.logged && !kIsWeb) {
|
2020-05-05 08:30:24 +00:00
|
|
|
await FirebaseController.setupFirebase(
|
2020-07-21 08:56:26 +00:00
|
|
|
this,
|
2020-05-05 08:30:24 +00:00
|
|
|
widget.clientName,
|
|
|
|
);
|
2020-01-08 13:19:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-10 08:07:01 +00:00
|
|
|
Map<String, dynamic> getAuthByPassword(String password, [String session]) => {
|
2020-05-13 13:58:59 +00:00
|
|
|
'type': 'm.login.password',
|
|
|
|
'identifier': {
|
|
|
|
'type': 'm.id.user',
|
|
|
|
'user': client.userID,
|
2020-02-19 15:23:13 +00:00
|
|
|
},
|
2020-05-13 13:58:59 +00:00
|
|
|
'user': client.userID,
|
|
|
|
'password': password,
|
2020-06-10 08:07:01 +00:00
|
|
|
if (session != null) 'session': session,
|
2020-02-19 15:23:13 +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;
|
2020-08-22 13:20:07 +00:00
|
|
|
StreamSubscription<html.Event> onFocusSub;
|
|
|
|
StreamSubscription<html.Event> onBlurSub;
|
2020-04-08 15:43:07 +00:00
|
|
|
|
|
|
|
void onJitsiCall(EventUpdate eventUpdate) {
|
|
|
|
final event = Event.fromJson(
|
|
|
|
eventUpdate.content, client.getRoomById(eventUpdate.roomID));
|
|
|
|
if (DateTime.now().millisecondsSinceEpoch -
|
2020-06-10 08:07:01 +00:00
|
|
|
event.originServerTs.millisecondsSinceEpoch >
|
2020-04-08 15:43:07 +00:00
|
|
|
1000 * 60 * 5) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
final senderName = event.sender.calcDisplayname();
|
|
|
|
final senderAvatar = event.sender.avatarUrl;
|
|
|
|
showDialog(
|
|
|
|
context: context,
|
|
|
|
builder: (context) => AlertDialog(
|
2020-05-07 05:52:40 +00:00
|
|
|
title: Text(L10n.of(context).videoCall),
|
2020-04-08 15:43:07 +00:00
|
|
|
content: Column(
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
children: <Widget>[
|
2020-04-09 08:16:38 +00: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 15:43:07 +00:00
|
|
|
Divider(),
|
|
|
|
Row(
|
|
|
|
children: <Widget>[
|
|
|
|
Spacer(),
|
|
|
|
FloatingActionButton(
|
|
|
|
backgroundColor: Colors.red,
|
|
|
|
child: Icon(Icons.phone_missed),
|
|
|
|
onPressed: () => Navigator.of(context).pop(),
|
|
|
|
),
|
|
|
|
Spacer(),
|
|
|
|
FloatingActionButton(
|
|
|
|
backgroundColor: Colors.green,
|
|
|
|
child: Icon(Icons.phone),
|
|
|
|
onPressed: () {
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
launch(event.body);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
Spacer(),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
2020-02-22 07:27:08 +00:00
|
|
|
|
2020-08-22 13:20:07 +00:00
|
|
|
bool webHasFocus = true;
|
|
|
|
|
2020-06-27 08:15:37 +00:00
|
|
|
void _showWebNotification(EventUpdate eventUpdate) async {
|
2020-08-22 13:20:07 +00:00
|
|
|
if (webHasFocus && activeRoomId == eventUpdate.roomID) return;
|
2020-06-27 08:15:37 +00:00
|
|
|
final room = client.getRoomById(eventUpdate.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 body = event.getLocalizedBody(
|
|
|
|
L10n.of(context),
|
|
|
|
withSenderNamePrefix:
|
|
|
|
!room.isDirectChat || room.lastEvent.senderId == client.userID,
|
|
|
|
);
|
|
|
|
html.AudioElement()
|
|
|
|
..src = 'assets/assets/sounds/notification.wav'
|
|
|
|
..autoplay = true
|
|
|
|
..load();
|
|
|
|
html.Notification(
|
|
|
|
room.getLocalizedDisplayname(L10n.of(context)),
|
|
|
|
body: body,
|
|
|
|
icon: event.sender.avatarUrl?.getThumbnail(client,
|
|
|
|
width: 64, height: 64, method: ThumbnailMethod.crop) ??
|
|
|
|
room.avatar?.getThumbnail(client,
|
|
|
|
width: 64, height: 64, method: ThumbnailMethod.crop),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-01-01 18:10:13 +00:00
|
|
|
@override
|
|
|
|
void initState() {
|
2020-05-13 13:58:59 +00:00
|
|
|
store = widget.store ?? Store();
|
2020-01-01 18:10:13 +00:00
|
|
|
if (widget.client == null) {
|
2020-05-13 13:58:59 +00:00
|
|
|
debugPrint('[Matrix] Init matrix client');
|
2020-06-25 14:29:06 +00:00
|
|
|
final Set verificationMethods = <KeyVerificationMethod>{
|
|
|
|
KeyVerificationMethod.numbers
|
|
|
|
};
|
|
|
|
if (!kIsWeb) {
|
|
|
|
// emojis don't show in web somehow
|
|
|
|
verificationMethods.add(KeyVerificationMethod.emoji);
|
|
|
|
}
|
|
|
|
client = Client(widget.clientName,
|
|
|
|
enableE2eeRecovery: true,
|
2020-07-02 09:30:59 +00:00
|
|
|
verificationMethods: verificationMethods,
|
|
|
|
importantStateEvents: <String>{
|
|
|
|
'im.ponies.room_emotes', // we want emotes to work properly
|
|
|
|
});
|
2020-04-08 15:43:07 +00:00
|
|
|
onJitsiCallSub ??= client.onEvent.stream
|
|
|
|
.where((e) =>
|
2020-04-09 08:21:13 +00:00
|
|
|
e.type == 'timeline' &&
|
2020-04-08 15:43:07 +00:00
|
|
|
e.eventType == 'm.room.message' &&
|
|
|
|
e.content['content']['msgtype'] == Matrix.callNamespace &&
|
|
|
|
e.content['sender'] != client.userID)
|
|
|
|
.listen(onJitsiCall);
|
2020-02-22 07:27:08 +00:00
|
|
|
onRoomKeyRequestSub ??=
|
|
|
|
client.onRoomKeyRequest.stream.listen((RoomKeyRequest request) async {
|
2020-05-13 13:58:59 +00:00
|
|
|
final room = request.room;
|
2020-06-24 19:10:54 +00:00
|
|
|
if (request.sender != room.client.userID) {
|
|
|
|
return; // ignore share requests by others
|
|
|
|
}
|
2020-05-13 13:58:59 +00:00
|
|
|
final sender = room.getUserByMXIDSync(request.sender);
|
2020-02-22 07:27:08 +00:00
|
|
|
if (await SimpleDialogs(context).askConfirmation(
|
2020-05-07 05:52:40 +00:00
|
|
|
titleText: L10n.of(context).requestToReadOlderMessages,
|
2020-02-22 07:27:08 +00:00
|
|
|
contentText:
|
2020-05-13 13:58:59 +00:00
|
|
|
'${sender.id}\n\n${L10n.of(context).device}:\n${request.requestingDevice.deviceId}\n\n${L10n.of(context).identity}:\n${request.requestingDevice.curve25519Key.beautified}',
|
2020-05-07 05:52:40 +00:00
|
|
|
confirmText: L10n.of(context).verify,
|
|
|
|
cancelText: L10n.of(context).deny,
|
2020-02-22 07:27:08 +00:00
|
|
|
)) {
|
|
|
|
await request.forwardKey();
|
|
|
|
}
|
|
|
|
});
|
2020-06-25 14:29:06 +00:00
|
|
|
onKeyVerificationRequestSub ??= client.onKeyVerificationRequest.stream
|
|
|
|
.listen((KeyVerification request) async {
|
|
|
|
if (await SimpleDialogs(context).askConfirmation(
|
|
|
|
titleText: L10n.of(context).newVerificationRequest,
|
|
|
|
contentText: L10n.of(context).askVerificationRequest(request.userId),
|
|
|
|
)) {
|
|
|
|
await request.acceptVerification();
|
|
|
|
await Navigator.of(context).push(
|
|
|
|
AppRoute.defaultRoute(
|
|
|
|
context,
|
|
|
|
KeyVerificationView(request: request),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
await request.rejectVerification();
|
|
|
|
}
|
|
|
|
});
|
2020-01-26 11:17:54 +00:00
|
|
|
_initWithStore();
|
2020-01-01 18:10:13 +00:00
|
|
|
} else {
|
|
|
|
client = widget.client;
|
2020-05-13 13:58:59 +00:00
|
|
|
client.connect();
|
2020-01-01 18:10:13 +00:00
|
|
|
}
|
2020-05-13 13:58:59 +00:00
|
|
|
if (store != null) {
|
|
|
|
store
|
|
|
|
.getItem('chat.fluffy.jitsi_instance')
|
2020-04-08 15:43:07 +00:00
|
|
|
.then((final instance) => jitsiInstance = instance ?? jitsiInstance);
|
2020-05-13 13:58:59 +00:00
|
|
|
store.getItem('chat.fluffy.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;
|
|
|
|
}
|
|
|
|
});
|
2020-05-13 13:58:59 +00:00
|
|
|
store.getItem('chat.fluffy.renderHtml').then((final render) async {
|
|
|
|
renderHtml = render == '1';
|
2020-05-09 11:36:41 +00:00
|
|
|
});
|
2020-04-03 18:24:25 +00:00
|
|
|
}
|
2020-06-27 08:15:37 +00:00
|
|
|
if (kIsWeb) {
|
2020-08-22 13:20:07 +00:00
|
|
|
onFocusSub = html.window.onFocus.listen((_) => webHasFocus = true);
|
|
|
|
onBlurSub = html.window.onBlur.listen((_) => webHasFocus = false);
|
|
|
|
|
2020-06-27 08:15:37 +00:00
|
|
|
client.onSync.stream.first.then((s) {
|
|
|
|
html.Notification.requestPermission();
|
|
|
|
onNotification ??= client.onEvent.stream
|
|
|
|
.where((e) =>
|
|
|
|
e.type == 'timeline' &&
|
|
|
|
[EventTypes.Message, EventTypes.Sticker, EventTypes.Encrypted]
|
|
|
|
.contains(e.eventType) &&
|
|
|
|
e.content['sender'] != client.userID)
|
|
|
|
.listen(_showWebNotification);
|
|
|
|
});
|
|
|
|
}
|
2020-01-01 18:10:13 +00:00
|
|
|
super.initState();
|
|
|
|
}
|
|
|
|
|
2020-01-03 16:23:40 +00:00
|
|
|
@override
|
|
|
|
void dispose() {
|
2020-02-22 07:27:08 +00:00
|
|
|
onRoomKeyRequestSub?.cancel();
|
2020-06-25 14:29:06 +00:00
|
|
|
onKeyVerificationRequestSub?.cancel();
|
2020-04-08 15:43:07 +00:00
|
|
|
onJitsiCallSub?.cancel();
|
2020-06-27 08:15:37 +00:00
|
|
|
onNotification?.cancel();
|
2020-08-22 13:20:07 +00:00
|
|
|
onFocusSub?.cancel();
|
|
|
|
onBlurSub?.cancel();
|
2020-01-03 16:23:40 +00:00
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
2020-01-01 18:10:13 +00:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return _InheritedMatrix(
|
|
|
|
data: this,
|
|
|
|
child: widget.child,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class _InheritedMatrix extends InheritedWidget {
|
|
|
|
final MatrixState data;
|
|
|
|
|
|
|
|
_InheritedMatrix({Key key, this.data, Widget child})
|
|
|
|
: super(key: key, child: child);
|
|
|
|
|
|
|
|
@override
|
|
|
|
bool updateShouldNotify(_InheritedMatrix old) {
|
2020-08-16 10:54:43 +00:00
|
|
|
var update = old.data.client.accessToken != data.client.accessToken ||
|
|
|
|
old.data.client.userID != data.client.userID ||
|
|
|
|
old.data.client.deviceID != data.client.deviceID ||
|
|
|
|
old.data.client.deviceName != data.client.deviceName ||
|
|
|
|
old.data.client.homeserver != data.client.homeserver;
|
2020-01-01 18:10:13 +00:00
|
|
|
return update;
|
|
|
|
}
|
|
|
|
}
|