fluffychat/lib/views/chat_encryption_settings.dart

227 lines
8.7 KiB
Dart
Raw Normal View History

2020-11-14 10:08:13 +01:00
import 'package:adaptive_dialog/adaptive_dialog.dart';
2020-06-25 16:29:06 +02:00
import 'package:famedlysdk/encryption.dart';
import 'package:famedlysdk/famedlysdk.dart';
2020-02-23 09:31:44 +01:00
import 'package:fluffychat/components/avatar.dart';
2020-02-04 14:42:35 +01:00
import 'package:fluffychat/components/matrix.dart';
import 'package:fluffychat/utils/beautify_string_extension.dart';
import 'package:flushbar/flushbar_helper.dart';
2020-02-04 14:42:35 +01:00
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';
2020-11-22 22:48:10 +01:00
import '../components/dialogs/key_verification_dialog.dart';
2020-02-04 14:42:35 +01:00
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 16:29:06 +02: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);
}
};
2021-01-19 15:46:43 +01:00
await KeyVerificationDialog(
request: req,
l10n: L10n.of(context),
).show(context);
2020-06-25 16:29:06 +02:00
break;
case 'verify_manual':
2020-11-14 10:08:13 +01:00
if (await showOkCancelAlertDialog(
context: context,
title: L10n.of(context).isDeviceKeyCorrect,
message: key.ed25519Key.beautified,
2021-02-18 14:23:22 +01:00
okLabel: L10n.of(context).ok,
cancelLabel: L10n.of(context).cancel,
2020-11-14 10:08:13 +01:00
) ==
OkCancelResult.ok) {
2020-06-25 16:29:06 +02:00
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);
}
};
2021-01-19 15:46:43 +01:00
await KeyVerificationDialog(
request: req,
l10n: L10n.of(context),
).show(context);
2020-06-25 16:29:06 +02:00
break;
case 'block':
if (key.directVerified) {
await key.setVerified(false);
}
await key.setBlocked(true);
setState(() => null);
break;
case 'unblock':
await unblock();
setState(() => null);
break;
}
}
@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback(
(_) => FlushbarHelper.createInformation(
message: L10n.of(context).warningEncryptionInBeta)
.show(context),
);
}
2020-02-04 14:42:35 +01:00
@override
Widget build(BuildContext context) {
2020-05-09 07:17:55 +02:00
final room = Matrix.of(context).client.getRoomById(widget.id);
2020-02-04 14:42:35 +01:00
return Scaffold(
appBar: AppBar(
2021-01-16 14:24:52 +01:00
leading: BackButton(),
2020-05-07 07:52:40 +02:00
title: Text(L10n.of(context).participatingUserDevices),
2020-02-04 14:42:35 +01:00
),
2020-05-09 07:17:55 +02: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 15:58:59 +02:00
': ' +
2020-05-09 07:17:55 +02:00
snapshot.error.toString()),
);
}
if (!snapshot.hasData) {
return Center(child: CircularProgressIndicator());
}
2020-05-13 15:58:59 +02:00
final deviceKeys = snapshot.data;
2020-05-09 11:54:10 +02: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 16:29:06 +02:00
child: PopupMenuButton(
onSelected: (action) =>
onSelected(context, action, deviceKeys[i]),
itemBuilder: (c) {
var items = <PopupMenuEntry<String>>[];
if (room
2020-10-31 10:39:38 +01:00
.client
.userDeviceKeys[deviceKeys[i].userId]
.verified ==
UserVerifiedStatus.unknown) {
2020-06-25 16:29:06 +02:00
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 11:54:10 +02:00
.getUserByMXIDSync(deviceKeys[i].userId)
2020-06-25 16:29:06 +02:00
.calcDisplayname()),
subtitle: Text(deviceKeys[i].userId),
2020-02-23 09:31:44 +01:00
),
2020-05-09 07:17:55 +02:00
),
2020-05-09 11:54:10 +02:00
elevation: 2,
),
2020-06-25 16:29:06 +02: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',
));
} else {
items.add(PopupMenuItem(
child: Text(L10n.of(context).verifyUser),
value: 'verify_user',
));
2020-05-09 11:54:10 +02:00
}
items.add(PopupMenuItem(
child: Text(L10n.of(context).verifyManual),
value: 'verify_manual',
));
2020-05-09 11:54:10 +02:00
}
2020-06-25 16:29:06 +02: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 11:54:10 +02:00
},
2020-06-25 16:29:06 +02:00
child: ListTile(
title: Text(
2020-11-22 11:46:31 +01:00
'${deviceKeys[i].deviceDisplayName ?? L10n.of(context).unknownDevice}',
2020-06-25 16:29:06 +02:00
style: TextStyle(
color: deviceKeys[i].blocked
? Colors.red
: deviceKeys[i].verified
? Colors.green
: Colors.orange),
),
subtitle: Text(
2020-11-22 11:46:31 +01:00
'${L10n.of(context).deviceId}: ${deviceKeys[i].deviceId}',
2020-06-25 16:29:06 +02:00
),
),
2020-05-09 11:54:10 +02:00
),
],
2020-02-23 09:31:44 +01:00
),
2020-05-09 07:17:55 +02:00
);
},
);
}),
2020-02-04 14:42:35 +01:00
);
}
}