Skip to content

Commit

Permalink
feat: Prevent sending messages if other party has no encryption keys
Browse files Browse the repository at this point in the history
  • Loading branch information
krille-chan committed Jan 13, 2025
1 parent 8cb06d6 commit aa01076
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 1 deletion.
3 changes: 2 additions & 1 deletion assets/l10n/intl_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -2821,5 +2821,6 @@
"invalidUrl": "Invalid url",
"addLink": "Add link",
"unableToJoinChat": "Unable to join chat. Maybe the other party has already closed the conversation.",
"previous": "Previous"
"previous": "Previous",
"otherPartyNotLoggedIn": "The other party is currently not logged in and therefore cannot receive messages!"
}
18 changes: 18 additions & 0 deletions lib/pages/chat/chat.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import 'package:fluffychat/utils/file_selector.dart';
import 'package:fluffychat/utils/matrix_sdk_extensions/event_extension.dart';
import 'package:fluffychat/utils/matrix_sdk_extensions/filtered_timeline_extension.dart';
import 'package:fluffychat/utils/matrix_sdk_extensions/matrix_locals.dart';
import 'package:fluffychat/utils/other_party_can_receive.dart';
import 'package:fluffychat/utils/platform_infos.dart';
import 'package:fluffychat/utils/show_scaffold_dialog.dart';
import 'package:fluffychat/widgets/adaptive_dialogs/show_modal_action_popup.dart';
Expand Down Expand Up @@ -237,6 +238,23 @@ class ChatController extends State<ChatPageWithRoom>
void _shareItems([_]) {
final shareItems = widget.shareItems;
if (shareItems == null || shareItems.isEmpty) return;
if (!room.otherPartyCanReceiveMessages) {
final theme = Theme.of(context);
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
backgroundColor: theme.colorScheme.errorContainer,
closeIconColor: theme.colorScheme.onErrorContainer,
content: Text(
L10n.of(context).otherPartyNotLoggedIn,
style: TextStyle(
color: theme.colorScheme.onErrorContainer,
),
),
showCloseIcon: true,
),
);
return;
}
for (final item in shareItems) {
if (item is FileShareItem) continue;
if (item is TextShareItem) room.sendTextEvent(item.value);
Expand Down
15 changes: 15 additions & 0 deletions lib/pages/chat/chat_input_row.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:flutter_gen/gen_l10n/l10n.dart';
import 'package:matrix/matrix.dart';

import 'package:fluffychat/config/app_config.dart';
import 'package:fluffychat/utils/other_party_can_receive.dart';
import 'package:fluffychat/utils/platform_infos.dart';
import 'package:fluffychat/widgets/avatar.dart';
import 'package:fluffychat/widgets/matrix.dart';
Expand All @@ -25,6 +26,20 @@ class ChatInputRow extends StatelessWidget {
return const SizedBox.shrink();
}
const height = 48.0;

if (!controller.room.otherPartyCanReceiveMessages) {
return Center(
child: Padding(
padding: const EdgeInsets.all(12.0),
child: Text(
L10n.of(context).otherPartyNotLoggedIn,
style: theme.textTheme.bodySmall,
textAlign: TextAlign.center,
),
),
);
}

return Row(
crossAxisAlignment: CrossAxisAlignment.end,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
Expand Down
4 changes: 4 additions & 0 deletions lib/pages/chat/send_file_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import 'package:mime/mime.dart';
import 'package:fluffychat/config/app_config.dart';
import 'package:fluffychat/utils/localized_exception_extension.dart';
import 'package:fluffychat/utils/matrix_sdk_extensions/matrix_file_extension.dart';
import 'package:fluffychat/utils/other_party_can_receive.dart';
import 'package:fluffychat/utils/platform_infos.dart';
import 'package:fluffychat/utils/size_string.dart';
import 'package:fluffychat/widgets/adaptive_dialogs/adaptive_dialog_action.dart';
Expand Down Expand Up @@ -44,6 +45,9 @@ class SendFileDialogState extends State<SendFileDialog> {
final l10n = L10n.of(context);

try {
if (!widget.room.otherPartyCanReceiveMessages) {
throw OtherPartyCanNotReceiveMessages();
}
scaffoldMessenger.showLoadingSnackBar(l10n.prepareSendingAttachment);
Navigator.of(context, rootNavigator: false).pop();
final clientConfig = await widget.room.client.getConfig();
Expand Down
21 changes: 21 additions & 0 deletions lib/utils/other_party_can_receive.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import 'package:matrix/matrix.dart';

extension OtherPartyCanReceiveExtension on Room {
bool get otherPartyCanReceiveMessages {
if (!encrypted) return true;
final users = getParticipants()
.map((u) => u.id)
.where((userId) => userId != client.userID)
.toSet();
if (users.isEmpty) return true;

for (final userId in users) {
if (client.userDeviceKeys[userId]?.deviceKeys.values.isNotEmpty == true) {
return true;
}
}
return false;
}
}

class OtherPartyCanNotReceiveMessages implements Exception {}

0 comments on commit aa01076

Please sign in to comment.