Skip to content

Commit

Permalink
refactor(talk_app): Extract message action checks
Browse files Browse the repository at this point in the history
Signed-off-by: provokateurin <[email protected]>
  • Loading branch information
provokateurin committed Dec 8, 2024
1 parent f47620f commit 2f74c5b
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import 'package:intl/intl.dart';
import 'package:neon_framework/blocs.dart';
import 'package:neon_framework/utils.dart';
import 'package:neon_framework/widgets.dart';
import 'package:nextcloud/spreed.dart' as spreed;
import 'package:talk_app/src/blocs/room.dart';
import 'package:talk_app/src/theme.dart';
import 'package:talk_app/src/utils/helpers.dart';
Expand Down Expand Up @@ -176,10 +175,7 @@ class _TalkRoomPageState extends State<TalkRoomPage> {
),
);

if (room.readOnly == 0 &&
spreed.ParticipantPermission.values
.byBinary(room.permissions)
.contains(spreed.ParticipantPermission.canSendMessageAndShareAndReact)) {
if (canSendMessageAndShareAndReact(room)) {
body = Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.end,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,30 @@ bool hasFeature(BuildContext context, String feature) {

return capabilities.features.contains(feature);
}

/// Checks whether the user is allowed to send chat messages in a [room].
bool canSendMessageAndShareAndReact(spreed.Room room) {
return room.readOnly == 0 &&
spreed.ParticipantPermission.values
.byBinary(room.permissions)
.contains(spreed.ParticipantPermission.canSendMessageAndShareAndReact);
}

/// Checks whether the user is allowed to reply to a [chatMessage] in a [room].
bool canReplyToMessage(spreed.Room room, spreed.$ChatMessageInterface chatMessage) {
return canSendMessageAndShareAndReact(room) &&
chatMessage.messageType != spreed.MessageType.commentDeleted &&
chatMessage.isReplyable;
}

/// Checks whether the user is allowed to edit a [chatMessage] in a [room].
bool canEditMessage(BuildContext context, spreed.Room room, spreed.$ChatMessageInterface chatMessage) {
return chatMessage.messageType != spreed.MessageType.commentDeleted &&
chatMessage.actorId == room.actorId &&
hasFeature(context, 'edit-messages');
}

/// Checks whether the user is allowed to delete a [chatMessage] in a [room].
bool canDeleteMessage(spreed.Room room, spreed.$ChatMessageInterface chatMessage) {
return chatMessage.messageType != spreed.MessageType.commentDeleted && chatMessage.actorId == room.actorId;
}
Original file line number Diff line number Diff line change
Expand Up @@ -517,15 +517,8 @@ class _TalkCommentMessageState extends State<TalkCommentMessage> {
spreed.Room room,
spreed.$ChatMessageInterface chatMessage,
) {
final readOnly = room.readOnly == 1;
final permissions = spreed.ParticipantPermission.values.byBinary(room.permissions);
final hasChatPermission = permissions.contains(spreed.ParticipantPermission.canSendMessageAndShareAndReact);

return [
if (widget.chatMessage.messageType != spreed.MessageType.commentDeleted &&
chatMessage.isReplyable &&
!readOnly &&
hasChatPermission)
if (canReplyToMessage(room, chatMessage))
(
icon: const Icon(Icons.add_reaction_outlined),
child: Text(TalkLocalizations.of(context).roomMessageReaction),
Expand All @@ -546,18 +539,13 @@ class _TalkCommentMessageState extends State<TalkCommentMessage> {
NeonProvider.of<TalkRoomBloc>(context).addReaction(chatMessage, reaction);
},
),
if (widget.chatMessage.messageType != spreed.MessageType.commentDeleted &&
chatMessage.isReplyable &&
!readOnly &&
hasChatPermission)
if (canReplyToMessage(room, chatMessage))
(
icon: const Icon(Icons.reply),
child: Text(TalkLocalizations.of(context).roomMessageReply),
onPressed: () => NeonProvider.of<TalkRoomBloc>(context).setReplyChatMessage(chatMessage),
),
if (chatMessage.messageType != spreed.MessageType.commentDeleted &&
chatMessage.actorId == room.actorId &&
hasFeature(context, 'edit-messages'))
if (canEditMessage(context, room, chatMessage))
(
icon: const Icon(Icons.edit),
child: Text(TalkLocalizations.of(context).roomMessageEdit),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:neon_framework/widgets.dart';
import 'package:nextcloud/spreed.dart' as spreed;
import 'package:talk_app/l10n/localizations.dart';
import 'package:talk_app/src/blocs/room.dart';
import 'package:talk_app/src/utils/helpers.dart';
import 'package:talk_app/src/widgets/reactions_overview_dialog.dart';

/// Widget for displaying the current reactions on a chat message including the ability to add and remove reactions.
Expand All @@ -26,10 +27,7 @@ class TalkReactions extends StatelessWidget {
Widget build(BuildContext context) {
final bloc = NeonProvider.of<TalkRoomBloc>(context);

final canUpdateReactions = room.readOnly == 0 &&
spreed.ParticipantPermission.values
.byBinary(room.permissions)
.contains(spreed.ParticipantPermission.canSendMessageAndShareAndReact);
final canUpdateReactions = canSendMessageAndShareAndReact(room);

const shape = RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(50)),
Expand Down

0 comments on commit 2f74c5b

Please sign in to comment.