FurryChat/lib/components/matrix.dart

229 lines
7 KiB
Dart
Raw Normal View History

2020-01-03 16:23:40 +00:00
import 'dart:async';
import 'dart:io';
2020-01-01 18:10:13 +00:00
import 'package:famedlysdk/famedlysdk.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-01-01 18:10:13 +00:00
import '../i18n/i18n.dart';
2020-02-22 07:27:08 +00:00
import '../utils/beautify_string_extension.dart';
import '../utils/famedlysdk_store.dart';
2020-04-08 15:43:07 +00:00
import 'avatar.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-04-12 08:35:45 +00:00
static const String defaultHomeserver = 'tchncs.de';
2020-04-08 15:43:07 +00:00
2020-01-01 18:10:13 +00: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;
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;
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-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;
final LocalStorage storage = LocalStorage('LocalStorage');
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 {
Future<LoginState> initLoginState = client.onLoginStateChanged.stream.first;
2020-01-26 11:17:54 +00:00
client.storeAPI = kIsWeb ? Store(client) : ExtendedStore(client);
debugPrint(
"[Store] Store is extended: ${client.storeAPI.extended.toString()}");
if (await initLoginState == LoginState.logged && !kIsWeb) {
2020-05-05 08:30:24 +00:00
await FirebaseController.setupFirebase(
client,
widget.clientName,
);
2020-01-08 13:19:15 +00:00
}
}
2020-02-19 15:23:13 +00: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 07:27:08 +00:00
StreamSubscription onRoomKeyRequestSub;
2020-04-08 15:43:07 +00: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 08:16:38 +00:00
title: Text(I18n.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-01-01 18:10:13 +00:00
@override
void initState() {
if (widget.client == null) {
2020-01-26 11:17:54 +00:00
debugPrint("[Matrix] Init matrix client");
client = Client(widget.clientName, debug: false);
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 {
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 11:17:54 +00:00
_initWithStore();
2020-01-01 18:10:13 +00:00
} else {
client = widget.client;
}
2020-04-03 18:24:25 +00:00
if (client.storeAPI != null) {
2020-04-08 15:43:07 +00:00
client.storeAPI
.getItem("chat.fluffy.jitsi_instance")
.then((final instance) => jitsiInstance = instance ?? jitsiInstance);
2020-04-03 18:24:25 +00:00
client.storeAPI.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-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-04-08 15:43:07 +00:00
onJitsiCallSub?.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) {
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;
}
}