FurryChat/lib/views/chat_encryption_settings.dart

243 lines
9.2 KiB
Dart
Raw Normal View History

2020-06-25 14:29:06 +00:00
import 'package:famedlysdk/encryption.dart';
import 'package:famedlysdk/famedlysdk.dart';
2020-02-04 13:42:35 +00:00
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';
2020-10-15 13:51:24 +00:00
import '../components/adaptive_page_layout.dart';
import '../components/avatar.dart';
2020-06-25 14:29:06 +00:00
import '../components/dialogs/simple_dialogs.dart';
2020-10-15 13:51:24 +00:00
import '../components/matrix.dart';
import '../utils/app_route.dart';
2020-10-15 13:51:24 +00:00
import '../utils/beautify_string_extension.dart';
import 'chat_list.dart';
import 'key_verification.dart';
2020-02-04 13:42:35 +00:00
class ChatEncryptionSettingsView extends StatelessWidget {
final String id;
const ChatEncryptionSettingsView(this.id, {Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return AdaptivePageLayout(
firstScaffold: ChatList(
activeChat: id,
),
secondScaffold: ChatEncryptionSettings(id),
primaryPage: FocusPage.SECOND,
);
}
}
class ChatEncryptionSettings extends StatefulWidget {
final String id;
const ChatEncryptionSettings(this.id, {Key key}) : super(key: key);
@override
_ChatEncryptionSettingsState createState() => _ChatEncryptionSettingsState();
}
class _ChatEncryptionSettingsState extends State<ChatEncryptionSettings> {
2020-06-25 14:29:06 +00:00
Future<void> onSelected(
BuildContext context, String action, DeviceKeys key) async {
final room = Matrix.of(context).client.getRoomById(widget.id);
final unblock = () async {
if (key.blocked) {
await key.setBlocked(false);
}
};
switch (action) {
case 'verify':
await unblock();
final req = key.startVerification();
req.onUpdate = () {
if (req.state == KeyVerificationState.done) {
setState(() => null);
}
};
await Navigator.of(context).push(
AppRoute.defaultRoute(
context,
KeyVerificationView(request: req),
),
);
break;
case 'verify_manual':
if (await SimpleDialogs(context).askConfirmation(
titleText: L10n.of(context).isDeviceKeyCorrect,
contentText: key.ed25519Key.beautified,
)) {
await unblock();
await key.setVerified(true);
setState(() => null);
}
break;
case 'verify_user':
await unblock();
final req =
await room.client.userDeviceKeys[key.userId].startVerification();
req.onUpdate = () {
if (req.state == KeyVerificationState.done) {
setState(() => null);
}
};
await Navigator.of(context).push(
AppRoute.defaultRoute(
context,
KeyVerificationView(request: req),
),
);
break;
case 'block':
if (key.directVerified) {
await key.setVerified(false);
}
await key.setBlocked(true);
setState(() => null);
break;
case 'unblock':
await unblock();
setState(() => null);
break;
}
}
2020-02-04 13:42:35 +00:00
@override
Widget build(BuildContext context) {
2020-05-09 05:17:55 +00:00
final room = Matrix.of(context).client.getRoomById(widget.id);
2020-02-04 13:42:35 +00:00
return Scaffold(
appBar: AppBar(
2020-05-07 05:52:40 +00:00
title: Text(L10n.of(context).participatingUserDevices),
2020-02-04 13:42:35 +00:00
),
2020-05-09 05:17:55 +00:00
body: StreamBuilder(
stream: room.onUpdate.stream,
builder: (context, snapshot) {
return FutureBuilder<List<DeviceKeys>>(
future: room.getUserDeviceKeys(),
builder: (BuildContext context, snapshot) {
if (snapshot.hasError) {
return Center(
child: Text(L10n.of(context).oopsSomethingWentWrong +
2020-05-13 13:58:59 +00:00
': ' +
2020-05-09 05:17:55 +00:00
snapshot.error.toString()),
);
}
if (!snapshot.hasData) {
return Center(child: CircularProgressIndicator());
}
2020-05-13 13:58:59 +00:00
final deviceKeys = snapshot.data;
2020-05-09 09:54:10 +00:00
return ListView.separated(
separatorBuilder: (BuildContext context, int i) =>
Divider(height: 1),
itemCount: deviceKeys.length,
itemBuilder: (BuildContext context, int i) => Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
if (i == 0 ||
deviceKeys[i].userId != deviceKeys[i - 1].userId)
Material(
2020-06-25 14:29:06 +00:00
child: PopupMenuButton(
onSelected: (action) =>
onSelected(context, action, deviceKeys[i]),
itemBuilder: (c) {
var items = <PopupMenuEntry<String>>[];
if (room
.client
.userDeviceKeys[deviceKeys[i].userId]
.verified ==
UserVerifiedStatus.unknown &&
deviceKeys[i].userId != room.client.userID) {
items.add(PopupMenuItem(
child: Text(L10n.of(context).verifyUser),
value: 'verify_user',
));
}
return items;
},
child: ListTile(
leading: Avatar(
room
.getUserByMXIDSync(deviceKeys[i].userId)
.avatarUrl,
room
.getUserByMXIDSync(deviceKeys[i].userId)
.calcDisplayname(),
),
title: Text(room
2020-05-09 09:54:10 +00:00
.getUserByMXIDSync(deviceKeys[i].userId)
2020-06-25 14:29:06 +00:00
.calcDisplayname()),
subtitle: Text(deviceKeys[i].userId),
2020-02-23 08:31:44 +00:00
),
2020-05-09 05:17:55 +00:00
),
2020-05-09 09:54:10 +00:00
elevation: 2,
),
2020-06-25 14:29:06 +00:00
PopupMenuButton(
onSelected: (action) =>
onSelected(context, action, deviceKeys[i]),
itemBuilder: (c) {
var items = <PopupMenuEntry<String>>[];
if (deviceKeys[i].blocked ||
!deviceKeys[i].verified) {
if (deviceKeys[i].userId == room.client.userID) {
items.add(PopupMenuItem(
child: Text(L10n.of(context).verifyStart),
value: 'verify',
));
items.add(PopupMenuItem(
child: Text(L10n.of(context).verifyManual),
value: 'verify_manual',
));
} else {
items.add(PopupMenuItem(
child: Text(L10n.of(context).verifyUser),
value: 'verify_user',
));
2020-05-09 09:54:10 +00:00
}
}
2020-06-25 14:29:06 +00:00
if (deviceKeys[i].blocked) {
items.add(PopupMenuItem(
child: Text(L10n.of(context).unblockDevice),
value: 'unblock',
));
}
if (!deviceKeys[i].blocked) {
items.add(PopupMenuItem(
child: Text(L10n.of(context).blockDevice),
value: 'block',
));
}
return items;
2020-05-09 09:54:10 +00:00
},
2020-06-25 14:29:06 +00:00
child: ListTile(
title: Text(
"${deviceKeys[i].unsigned["device_display_name"] ?? L10n.of(context).unknownDevice} - ${deviceKeys[i].deviceId}",
style: TextStyle(
color: deviceKeys[i].blocked
? Colors.red
: deviceKeys[i].verified
? Colors.green
: Colors.orange),
),
subtitle: Text(
deviceKeys[i]
.keys['ed25519:${deviceKeys[i].deviceId}']
.beautified,
style: TextStyle(
color: Theme.of(context)
.textTheme
.bodyText2
.color),
),
),
2020-05-09 09:54:10 +00:00
),
],
2020-02-23 08:31:44 +00:00
),
2020-05-09 05:17:55 +00:00
);
},
);
}),
2020-02-04 13:42:35 +00:00
);
}
}