Merge pull request #3005 from krille-chan/krille/permission-dialog-for-encryption
feat: Replace encryption button with unverified devices warning and p…pull/3007/merge
commit
af0ad13fc6
@ -0,0 +1,107 @@
|
||||
// SPDX-FileCopyrightText: 2019-Present Christian Kußowski
|
||||
// SPDX-FileCopyrightText: 2019-Present Contributors to FluffyChat
|
||||
//
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import 'package:fluffychat/config/app_config.dart';
|
||||
import 'package:fluffychat/config/setting_keys.dart';
|
||||
import 'package:fluffychat/config/themes.dart';
|
||||
import 'package:fluffychat/l10n/l10n.dart';
|
||||
import 'package:flutter/gestures.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:matrix/matrix.dart';
|
||||
|
||||
class EncryptionInfo extends StatelessWidget {
|
||||
final Room room;
|
||||
|
||||
const EncryptionInfo({super.key, required this.room});
|
||||
|
||||
Future<int> _unverifiedDevices() async {
|
||||
if (!room.encrypted) return 0;
|
||||
final users = await room.requestParticipants();
|
||||
final devicesKeysLists = users
|
||||
.map((user) => room.client.userDeviceKeys[user.id])
|
||||
.nonNulls;
|
||||
final devices = devicesKeysLists.fold<List<DeviceKeys>>(
|
||||
[],
|
||||
(devices, devicesKeysList) => [
|
||||
...devices,
|
||||
...devicesKeysList.deviceKeys.values,
|
||||
],
|
||||
);
|
||||
return devices
|
||||
.where(
|
||||
(device) =>
|
||||
!device.verified && !device.blocked && device.encryptToDevice,
|
||||
)
|
||||
.length;
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final theme = Theme.of(context);
|
||||
return AnimatedSize(
|
||||
duration: FluffyThemes.animationDuration,
|
||||
curve: FluffyThemes.animationCurve,
|
||||
child: FutureBuilder(
|
||||
future: _unverifiedDevices(),
|
||||
builder: (context, asyncSnapshot) {
|
||||
final unverifiedDevices = asyncSnapshot.data ?? 0;
|
||||
if (unverifiedDevices == 0) return const SizedBox.shrink();
|
||||
return Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8.0),
|
||||
child: Center(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(4),
|
||||
child: Material(
|
||||
color: theme.colorScheme.surface.withAlpha(128),
|
||||
borderRadius: BorderRadius.circular(
|
||||
AppConfig.borderRadius / 3,
|
||||
),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: 8.0,
|
||||
vertical: 4.0,
|
||||
),
|
||||
child: Text.rich(
|
||||
TextSpan(
|
||||
children: [
|
||||
WidgetSpan(
|
||||
child: Icon(Icons.lock_person_outlined, size: 13),
|
||||
),
|
||||
TextSpan(text: ' '),
|
||||
TextSpan(
|
||||
text: L10n.of(
|
||||
context,
|
||||
).countUnverifiedDevices(unverifiedDevices),
|
||||
),
|
||||
TextSpan(text: ' '),
|
||||
TextSpan(
|
||||
style: TextStyle(
|
||||
color: theme.colorScheme.primary,
|
||||
decoration: TextDecoration.underline,
|
||||
decorationColor: theme.colorScheme.primary,
|
||||
),
|
||||
recognizer: TapGestureRecognizer()
|
||||
..onTap = () =>
|
||||
context.go('/rooms/${room.id}/encryption'),
|
||||
text: 'Check',
|
||||
),
|
||||
],
|
||||
),
|
||||
textAlign: TextAlign.center,
|
||||
style: TextStyle(
|
||||
fontSize: 11 * AppSettings.fontSizeFactor.value,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -1,67 +0,0 @@
|
||||
// SPDX-FileCopyrightText: 2019-Present Christian Kußowski
|
||||
// SPDX-FileCopyrightText: 2019-Present Contributors to FluffyChat
|
||||
//
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import 'package:badges/badges.dart' as b;
|
||||
import 'package:fluffychat/l10n/l10n.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:matrix/matrix.dart';
|
||||
|
||||
import '../../widgets/matrix.dart';
|
||||
|
||||
class EncryptionButton extends StatelessWidget {
|
||||
final Room room;
|
||||
const EncryptionButton(this.room, {super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final theme = Theme.of(context);
|
||||
return StreamBuilder<SyncUpdate>(
|
||||
stream: Matrix.of(
|
||||
context,
|
||||
).client.onSync.stream.where((s) => s.deviceLists != null),
|
||||
builder: (context, snapshot) {
|
||||
final shouldBeEncrypted = room.joinRules != JoinRules.public;
|
||||
return FutureBuilder<EncryptionHealthState>(
|
||||
future: room.encrypted
|
||||
? room.calcEncryptionHealthState()
|
||||
: Future.value(EncryptionHealthState.allVerified),
|
||||
builder: (BuildContext context, snapshot) => IconButton(
|
||||
tooltip: room.encrypted
|
||||
? L10n.of(context).encrypted
|
||||
: L10n.of(context).encryptionNotEnabled,
|
||||
icon: b.Badge(
|
||||
badgeAnimation: const b.BadgeAnimation.fade(),
|
||||
showBadge:
|
||||
snapshot.data == EncryptionHealthState.unverifiedDevices,
|
||||
badgeStyle: b.BadgeStyle(
|
||||
badgeColor: theme.colorScheme.error,
|
||||
elevation: 4,
|
||||
),
|
||||
badgeContent: Text(
|
||||
'!',
|
||||
style: TextStyle(
|
||||
fontSize: 9,
|
||||
color: theme.colorScheme.onError,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
child: Icon(
|
||||
room.encrypted
|
||||
? Icons.lock_outlined
|
||||
: Icons.no_encryption_outlined,
|
||||
size: 20,
|
||||
color: (shouldBeEncrypted && !room.encrypted)
|
||||
? theme.colorScheme.error
|
||||
: theme.colorScheme.onSurface,
|
||||
),
|
||||
),
|
||||
onPressed: () => context.go('/rooms/${room.id}/encryption'),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,131 @@
|
||||
// SPDX-FileCopyrightText: 2019-Present Christian Kußowski
|
||||
// SPDX-FileCopyrightText: 2019-Present Contributors to FluffyChat
|
||||
//
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import 'package:fluffychat/l10n/l10n.dart';
|
||||
import 'package:fluffychat/utils/beautify_string_extension.dart';
|
||||
import 'package:fluffychat/widgets/adaptive_dialogs/adaptive_dialog_action.dart';
|
||||
import 'package:fluffychat/widgets/adaptive_dialogs/dialog_text_field.dart';
|
||||
import 'package:fluffychat/widgets/avatar.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:matrix/matrix.dart';
|
||||
|
||||
Future<bool> showTrustUserInRoomDialog(BuildContext context, Room room) async {
|
||||
if (!room.encrypted) return true;
|
||||
|
||||
final users = await room.requestParticipants();
|
||||
if (!context.mounted) return false;
|
||||
|
||||
users.removeWhere((user) {
|
||||
if (user.id == room.client.userID) return true;
|
||||
final keys = room.client.userDeviceKeys[user.id];
|
||||
final masterKey = keys?.masterKey;
|
||||
|
||||
if (keys == null || masterKey == null || masterKey.verified) return true;
|
||||
return false;
|
||||
});
|
||||
|
||||
if (users.isEmpty) return true;
|
||||
|
||||
final l10n = L10n.of(context);
|
||||
final theme = Theme.of(context);
|
||||
|
||||
final allow = await showAdaptiveDialog<bool>(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog.adaptive(
|
||||
title: Center(
|
||||
child: Icon(
|
||||
Icons.lock_outlined,
|
||||
size: 32,
|
||||
color: theme.colorScheme.primary,
|
||||
),
|
||||
),
|
||||
content: ConstrainedBox(
|
||||
constraints: BoxConstraints(maxWidth: 128),
|
||||
child: SelectionArea(
|
||||
child: Column(
|
||||
crossAxisAlignment: .stretch,
|
||||
mainAxisSize: .min,
|
||||
children: [
|
||||
Center(
|
||||
child: Text(
|
||||
users.length == 1
|
||||
? l10n.allowEncryptedCommunicationWith(
|
||||
users.single.calcDisplayname(),
|
||||
)
|
||||
: 'Allow encrypted communication with ${users.length} users?',
|
||||
style: TextStyle(fontSize: 16),
|
||||
textAlign: .center,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
for (final user in users) ...[
|
||||
Row(
|
||||
children: [
|
||||
Avatar(
|
||||
name: user.calcDisplayname(),
|
||||
mxContent: user.avatarUrl,
|
||||
size: 14,
|
||||
),
|
||||
const SizedBox(width: 4),
|
||||
Expanded(
|
||||
child: Text(
|
||||
user.calcDisplayname(),
|
||||
style: theme.textTheme.labelSmall,
|
||||
maxLines: 1,
|
||||
textAlign: TextAlign.start,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
DialogTextField(
|
||||
controller: TextEditingController(
|
||||
text: l10n.publicKey(
|
||||
room
|
||||
.client
|
||||
.userDeviceKeys[user.id]
|
||||
?.masterKey
|
||||
?.publicKey
|
||||
?.beautifiedOneLine ??
|
||||
'???',
|
||||
),
|
||||
),
|
||||
textStyle: theme.textTheme.labelSmall,
|
||||
readOnly: true,
|
||||
maxLines: 2,
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
actions: [
|
||||
AdaptiveDialogAction(
|
||||
bigButtons: true,
|
||||
onPressed: () {
|
||||
for (final user in users) {
|
||||
room.client.userDeviceKeys[user.id]?.masterKey?.setVerified(true);
|
||||
}
|
||||
Navigator.of(context).pop(true);
|
||||
},
|
||||
child: Text(L10n.of(context).allow),
|
||||
),
|
||||
AdaptiveDialogAction(
|
||||
bigButtons: true,
|
||||
onPressed: () => Navigator.of(context).pop(true),
|
||||
child: Text(L10n.of(context).onlyThisTime),
|
||||
),
|
||||
AdaptiveDialogAction(
|
||||
bigButtons: true,
|
||||
onPressed: () => Navigator.of(context).pop(false),
|
||||
child: Text(l10n.cancel),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
|
||||
if (allow != true) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
Loading…
Reference in New Issue