fluffychat/lib/components/matrix.dart

437 lines
14 KiB
Dart
Raw Normal View History

2020-01-03 17:23:40 +01:00
import 'dart:async';
2020-01-01 19:10:13 +01:00
import 'dart:convert';
2020-01-03 17:23:40 +01:00
import 'dart:io';
2020-01-01 19:10:13 +01:00
import 'package:famedlysdk/famedlysdk.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
2020-02-22 08:27:08 +01:00
import 'package:fluffychat/components/dialogs/simple_dialogs.dart';
2020-01-01 19:10:13 +01:00
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
2020-01-08 14:19:15 +01:00
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
2020-04-12 09:19:22 +02:00
import 'package:flutter_styled_toast/flutter_styled_toast.dart';
2020-01-01 19:10:13 +01:00
import 'package:localstorage/localstorage.dart';
2020-01-08 14:19:15 +01:00
import 'package:path_provider/path_provider.dart';
2020-04-08 17:43:07 +02:00
import 'package:url_launcher/url_launcher.dart';
2020-01-01 19:10:13 +01:00
import '../i18n/i18n.dart';
import '../utils/app_route.dart';
2020-02-22 08:27:08 +01:00
import '../utils/beautify_string_extension.dart';
import '../utils/event_extension.dart';
import '../utils/famedlysdk_store.dart';
import '../utils/room_extension.dart';
import '../views/chat.dart';
2020-04-08 17:43:07 +02:00
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-04-12 10:35:45 +02:00
static const String defaultHomeserver = 'tchncs.de';
2020-04-08 17:43:07 +02:00
2020-01-01 19:10:13 +01:00
final Widget child;
final String clientName;
final Client client;
Matrix({this.child, this.clientName, this.client, Key key}) : super(key: key);
@override
MatrixState createState() => MatrixState();
/// Returns the (nearest) Client instance of your application.
static MatrixState of(BuildContext context) {
MatrixState newState =
(context.dependOnInheritedWidgetOfExactType<_InheritedMatrix>()).data;
newState.context = context;
return newState;
}
}
class MatrixState extends State<Matrix> {
Client client;
BuildContext context;
2020-01-03 17:23:40 +01:00
FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
2020-01-08 14:19:15 +01:00
FlutterLocalNotificationsPlugin _flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
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-01-03 17:23:40 +01:00
2020-04-08 17:43:07 +02:00
String jitsiInstance = 'https://meet.jit.si/';
2020-01-01 19:10:13 +01:00
void clean() async {
if (!kIsWeb) return;
final LocalStorage storage = LocalStorage('LocalStorage');
await storage.ready;
2020-01-02 22:31:39 +01:00
await storage.deleteItem(widget.clientName);
2020-01-01 19:10:13 +01:00
}
2020-04-28 14:11:56 +02:00
Future<String> downloadAndSaveContent(Uri content,
2020-01-08 14:19:15 +01:00
{int width, int height, ThumbnailMethod method}) async {
final bool thumbnail = width == null && height == null ? false : true;
final String tempDirectory = (await getTemporaryDirectory()).path;
final String prefix = thumbnail ? "thumbnail" : "";
2020-04-28 14:11:56 +02:00
File file =
File('$tempDirectory/${prefix}_${content.toString().split("/").last}');
2020-01-08 14:19:15 +01:00
if (!file.existsSync()) {
final url = thumbnail
? content.getThumbnail(client,
width: width, height: height, method: method)
: content.getDownloadLink(client);
var request = await HttpClient().getUrl(Uri.parse(url));
var response = await request.close();
var bytes = await consolidateHttpClientResponseBytes(response);
await file.writeAsBytes(bytes);
}
return file.path;
}
2020-01-03 17:23:40 +01:00
2020-01-08 14:19:15 +01:00
Future<void> setupFirebase() async {
2020-01-03 17:23:40 +01:00
if (Platform.isIOS) iOS_Permission();
2020-04-12 09:19:22 +02:00
String token;
try {
token = await _firebaseMessaging.getToken();
} catch (_) {
token = null;
}
if (token?.isEmpty ?? true) {
2020-04-17 07:52:01 +02:00
showToast(
I18n.of(context).noGoogleServicesWarning,
duration: Duration(seconds: 15),
);
return;
}
await client.setPushers(
token,
"http",
"chat.fluffy.fluffychat",
widget.clientName,
client.deviceName,
"en",
"https://janian.de:7023/",
append: false,
format: "event_id_only",
);
2020-01-03 17:23:40 +01:00
2020-01-08 14:19:15 +01:00
Function goToRoom = (dynamic message) async {
try {
String roomId;
if (message is String) {
roomId = message;
} else if (message is Map) {
2020-04-08 11:38:13 +02:00
roomId = (message["data"] ?? message)["room_id"];
2020-01-08 14:19:15 +01:00
}
if (roomId?.isEmpty ?? true) throw ("Bad roomId");
await Navigator.of(context).pushAndRemoveUntil(
AppRoute.defaultRoute(
context,
2020-01-27 10:14:38 +01:00
ChatView(roomId),
2020-01-08 14:19:15 +01:00
),
(r) => r.isFirst);
} catch (_) {
2020-04-12 09:19:22 +02:00
showToast("Failed to open chat...");
2020-01-26 12:17:54 +01:00
debugPrint(_);
2020-01-08 14:19:15 +01:00
}
};
// initialise the plugin. app_icon needs to be a added as a drawable resource to the Android head project
var initializationSettingsAndroid =
AndroidInitializationSettings('notifications_icon');
var initializationSettingsIOS =
IOSInitializationSettings(onDidReceiveLocalNotification: (i, a, b, c) {
return null;
});
var initializationSettings = InitializationSettings(
initializationSettingsAndroid, initializationSettingsIOS);
await _flutterLocalNotificationsPlugin.initialize(initializationSettings,
onSelectNotification: goToRoom);
2020-01-03 17:23:40 +01:00
_firebaseMessaging.configure(
2020-01-08 14:19:15 +01:00
onMessage: (Map<String, dynamic> message) async {
try {
2020-04-08 11:38:13 +02:00
final data = message['data'] ?? message;
final String roomId = data["room_id"];
final String eventId = data["event_id"];
final int unread = json.decode(data["counts"])["unread"];
2020-01-08 14:19:15 +01:00
if ((roomId?.isEmpty ?? true) ||
(eventId?.isEmpty ?? true) ||
unread == 0) {
await _flutterLocalNotificationsPlugin.cancelAll();
return null;
}
if (activeRoomId == roomId) return null;
// Get the room
Room room = client.getRoomById(roomId);
if (room == null) {
await client.onRoomUpdate.stream
.where((u) => u.id == roomId)
.first
.timeout(Duration(seconds: 10));
room = client.getRoomById(roomId);
if (room == null) return null;
}
// Get the event
Event event = await client.store.getEventById(eventId, room);
if (event == null) {
final EventUpdate eventUpdate = await client.onEvent.stream
.where((u) => u.content["event_id"] == eventId)
.first
.timeout(Duration(seconds: 10));
event = Event.fromJson(eventUpdate.content, room);
if (room == null) return null;
}
// Count all unread events
int unreadEvents = 0;
client.rooms
.forEach((Room room) => unreadEvents += room.notificationCount);
// Calculate title
final String title = unread > 1
2020-01-20 13:46:39 +01:00
? I18n.of(context).unreadMessagesInChats(
unreadEvents.toString(), unread.toString())
: I18n.of(context).unreadMessages(unreadEvents.toString());
2020-01-08 14:19:15 +01:00
// Calculate the body
2020-01-19 15:07:42 +01:00
final String body = event.getLocalizedBody(context,
2020-02-20 20:45:38 +01:00
withSenderNamePrefix: true, hideReply: true);
2020-01-08 14:19:15 +01:00
// The person object for the android message style notification
final person = Person(
2020-01-19 15:07:42 +01:00
name: room.getLocalizedDisplayname(context),
2020-04-28 14:11:56 +02:00
icon: room.avatar == null
2020-01-08 14:19:15 +01:00
? null
: await downloadAndSaveContent(
room.avatar,
width: 126,
height: 126,
),
iconSource: IconSource.FilePath,
);
// Show notification
var androidPlatformChannelSpecifics = AndroidNotificationDetails(
'fluffychat_push',
'FluffyChat push channel',
'Push notifications for FluffyChat',
style: AndroidNotificationStyle.Messaging,
styleInformation: MessagingStyleInformation(
person,
conversationTitle: title,
messages: [
Message(
body,
event.time,
person,
)
],
),
importance: Importance.Max,
priority: Priority.High,
2020-01-20 13:46:39 +01:00
ticker: I18n.of(context).newMessageInFluffyChat);
2020-01-08 14:19:15 +01:00
var iOSPlatformChannelSpecifics = IOSNotificationDetails();
var platformChannelSpecifics = NotificationDetails(
androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
await _flutterLocalNotificationsPlugin.show(
2020-01-19 15:07:42 +01:00
0,
room.getLocalizedDisplayname(context),
body,
platformChannelSpecifics,
2020-01-08 14:19:15 +01:00
payload: roomId);
} catch (exception) {
2020-01-26 12:17:54 +01:00
debugPrint("[Push] Error while processing notification: " +
2020-01-08 14:19:15 +01:00
exception.toString());
}
return null;
2020-01-03 17:23:40 +01:00
},
2020-01-08 14:19:15 +01:00
onResume: goToRoom,
// Currently fires unexpectetly... https://github.com/FirebaseExtended/flutterfire/issues/1060
//onLaunch: goToRoom,
2020-01-03 17:23:40 +01:00
);
2020-01-26 12:17:54 +01:00
debugPrint("[Push] Firebase initialized");
2020-01-08 14:19:15 +01:00
return;
2020-01-03 17:23:40 +01:00
}
void iOS_Permission() {
_firebaseMessaging.requestNotificationPermissions(
IosNotificationSettings(sound: true, badge: true, alert: true));
_firebaseMessaging.onIosSettingsRegistered
.listen((IosNotificationSettings settings) {
2020-01-26 12:17:54 +01:00
debugPrint("Settings registered: $settings");
2020-01-03 17:23:40 +01:00
});
}
2020-01-08 14:19:15 +01:00
void _initWithStore() async {
Future<LoginState> initLoginState = client.onLoginStateChanged.stream.first;
2020-01-26 12:17:54 +01:00
client.storeAPI = kIsWeb ? Store(client) : ExtendedStore(client);
debugPrint(
"[Store] Store is extended: ${client.storeAPI.extended.toString()}");
if (await initLoginState == LoginState.logged && !kIsWeb) {
2020-01-08 14:19:15 +01:00
await setupFirebase();
}
}
2020-02-19 16:23:13 +01:00
Map<String, dynamic> getAuthByPassword(String password, String session) => {
"type": "m.login.password",
"identifier": {
"type": "m.id.user",
"user": client.userID,
},
"user": client.userID,
"password": password,
"session": session,
};
2020-02-22 08:27:08 +01:00
StreamSubscription onRoomKeyRequestSub;
2020-04-08 17:43:07 +02:00
StreamSubscription onJitsiCallSub;
void onJitsiCall(EventUpdate eventUpdate) {
final event = Event.fromJson(
eventUpdate.content, client.getRoomById(eventUpdate.roomID));
if (DateTime.now().millisecondsSinceEpoch -
event.time.millisecondsSinceEpoch >
1000 * 60 * 5) {
return;
}
final senderName = event.sender.calcDisplayname();
final senderAvatar = event.sender.avatarUrl;
showDialog(
context: context,
builder: (context) => AlertDialog(
2020-04-09 10:16:38 +02:00
title: Text(I18n.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,
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 08:27:08 +01:00
2020-01-01 19:10:13 +01:00
@override
void initState() {
if (widget.client == null) {
2020-01-26 12:17:54 +01:00
debugPrint("[Matrix] Init matrix client");
client = Client(widget.clientName, debug: false);
2020-04-08 17:43:07 +02:00
onJitsiCallSub ??= client.onEvent.stream
.where((e) =>
2020-04-09 10:21:13 +02:00
e.type == 'timeline' &&
2020-04-08 17:43:07 +02:00
e.eventType == 'm.room.message' &&
e.content['content']['msgtype'] == Matrix.callNamespace &&
e.content['sender'] != client.userID)
.listen(onJitsiCall);
2020-02-22 08:27:08 +01:00
onRoomKeyRequestSub ??=
client.onRoomKeyRequest.stream.listen((RoomKeyRequest request) async {
final Room room = request.room;
final User sender = room.getUserByMXIDSync(request.sender);
if (await SimpleDialogs(context).askConfirmation(
titleText: I18n.of(context).requestToReadOlderMessages,
contentText:
"${sender.id}\n\n${I18n.of(context).device}:\n${request.requestingDevice.deviceId}\n\n${I18n.of(context).identity}:\n${request.requestingDevice.curve25519Key.beautified}",
confirmText: I18n.of(context).verify,
cancelText: I18n.of(context).deny,
)) {
await request.forwardKey();
}
});
2020-01-26 12:17:54 +01:00
_initWithStore();
2020-01-01 19:10:13 +01:00
} else {
client = widget.client;
}
2020-04-03 20:24:25 +02:00
if (client.storeAPI != null) {
2020-04-08 17:43:07 +02:00
client.storeAPI
.getItem("chat.fluffy.jitsi_instance")
.then((final instance) => jitsiInstance = instance ?? jitsiInstance);
2020-04-03 20:24:25 +02:00
client.storeAPI.getItem("chat.fluffy.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;
}
});
}
2020-01-01 19:10:13 +01:00
super.initState();
}
2020-01-03 17:23:40 +01:00
@override
void dispose() {
2020-02-22 08:27:08 +01:00
onRoomKeyRequestSub?.cancel();
2020-04-08 17:43:07 +02:00
onJitsiCallSub?.cancel();
2020-01-03 17:23:40 +01:00
super.dispose();
}
2020-01-01 19:10:13 +01: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) {
bool update = old.data.client.accessToken != this.data.client.accessToken ||
old.data.client.userID != this.data.client.userID ||
old.data.client.matrixVersions != this.data.client.matrixVersions ||
old.data.client.deviceID != this.data.client.deviceID ||
old.data.client.deviceName != this.data.client.deviceName ||
old.data.client.homeserver != this.data.client.homeserver;
return update;
}
}