Skip to content

Commit

Permalink
feat(neon_talk): Enable message deletion
Browse files Browse the repository at this point in the history
Signed-off-by: provokateurin <kate@provokateurin.de>
  • Loading branch information
provokateurin committed Jun 21, 2024
1 parent d78319b commit aeb9814
Show file tree
Hide file tree
Showing 11 changed files with 227 additions and 12 deletions.
1 change: 1 addition & 0 deletions packages/neon/neon_talk/lib/l10n/en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"roomMessageSend": "Send message",
"roomMessageReply": "Reply",
"roomMessageReaction": "Add reaction",
"roomMessageDelete": "Delete",
"reactionsAddNew": "Add a new reaction",
"reactionsLoading": "Loading reactions",
"roomsCreateNew": "Create new room"
Expand Down
6 changes: 6 additions & 0 deletions packages/neon/neon_talk/lib/l10n/localizations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,12 @@ abstract class TalkLocalizations {
/// **'Add reaction'**
String get roomMessageReaction;

/// No description provided for @roomMessageDelete.
///
/// In en, this message translates to:
/// **'Delete'**
String get roomMessageDelete;

/// No description provided for @reactionsAddNew.
///
/// In en, this message translates to:
Expand Down
3 changes: 3 additions & 0 deletions packages/neon/neon_talk/lib/l10n/localizations_en.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ class TalkLocalizationsEn extends TalkLocalizations {
@override
String get roomMessageReaction => 'Add reaction';

@override
String get roomMessageDelete => 'Delete';

@override
String get reactionsAddNew => 'Add a new reaction';

Expand Down
32 changes: 25 additions & 7 deletions packages/neon/neon_talk/lib/src/blocs/room.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ abstract class TalkRoomBloc implements InteractiveBloc {
/// Removes the current [replyTo] chat message.
void removeReplyChatMessage();

/// Deletes a chat messages.
void deleteMessage(spreed.$ChatMessageInterface chatMessage);

/// The current room data.
BehaviorSubject<Result<spreed.Room>> get room;

Expand Down Expand Up @@ -321,6 +324,26 @@ class _TalkRoomBloc extends InteractiveBloc implements TalkRoomBloc {
replyTo.add(null);
}

@override
Future<void> deleteMessage(spreed.$ChatMessageInterface chatMessage) async {
await wrapAction(
() async {
final response = await account.client.spreed.chat.deleteMessage(
token: token,
messageId: chatMessage.id,
);

updateLastCommonRead(response.headers.xChatLastCommonRead);

final m = response.body.ocs.data;
updateLastKnownMessageId(m.id);

prependMessages([m]);
},
refresh: () async {},
);
}

void updateLastCommonRead(String? header) {
if (header != null) {
final id = int.parse(header);
Expand All @@ -342,13 +365,8 @@ class _TalkRoomBloc extends InteractiveBloc implements TalkRoomBloc {
if (messages.hasValue) {
final result = messages.value;
if (result.hasData) {
final lastMessageID = newMessages.lastOrNull?.id;

if (lastMessageID == null) {
builder.addAll(result.requireData);
} else {
builder.addAll(result.requireData.where((message) => message.id < lastMessageID));
}
final lastMessageID = newMessages.last.id;
builder.addAll(result.requireData.where((message) => message.id < lastMessageID));
}

// Skip messages without parents as we can't know which message should be updated
Expand Down
1 change: 1 addition & 0 deletions packages/neon/neon_talk/lib/src/pages/room.dart
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ class _TalkRoomPageState extends State<TalkRoomPage> {
messagesResult.requireData.length > index + 1 ? messagesResult.requireData[index + 1] : null;

Widget child = TalkMessage(
actorId: room.actorId,
chatMessage: message,
lastCommonRead: lastCommonReadSnapshot.data,
previousChatMessage: previousMessage,
Expand Down
35 changes: 32 additions & 3 deletions packages/neon/neon_talk/lib/src/widgets/message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ class TalkMessagePreview extends StatelessWidget {
super.key,
});

/// ID of the current actor.
/// {@macro TalkMessage.actorId}
final String actorId;

/// Type of the room
Expand Down Expand Up @@ -220,13 +220,19 @@ class TalkMessagePreview extends StatelessWidget {
class TalkMessage extends StatelessWidget {
/// Creates a new Talk message.
const TalkMessage({
required this.actorId,
required this.chatMessage,
required this.lastCommonRead,
this.previousChatMessage,
this.isParent = false,
super.key,
});

/// {@template TalkMessage.actorId}
/// ID of the current actor.
/// {@endtemplate}
final String actorId;

/// {@template TalkMessage.chatMessage}
/// The chat message to display.
/// {@endtemplate}
Expand Down Expand Up @@ -255,6 +261,7 @@ class TalkMessage extends StatelessWidget {
}

return TalkCommentMessage(
actorId: actorId,
chatMessage: chatMessage,
lastCommonRead: lastCommonRead,
previousChatMessage: previousChatMessage,
Expand Down Expand Up @@ -300,11 +307,15 @@ class TalkSystemMessage extends StatelessWidget {
class TalkParentMessage extends StatelessWidget {
/// Creates a new Talk parent message.
const TalkParentMessage({
required this.actorId,
required this.parentChatMessage,
required this.lastCommonRead,
super.key,
});

/// {@macro TalkMessage.actorId}
final String actorId;

/// The parent chat message.
///
/// Do not pass the child chat message.
Expand All @@ -327,6 +338,7 @@ class TalkParentMessage extends StatelessWidget {
),
padding: const EdgeInsets.symmetric(horizontal: 10),
child: TalkMessage(
actorId: actorId,
chatMessage: parentChatMessage,
lastCommonRead: lastCommonRead,
isParent: true,
Expand All @@ -339,13 +351,17 @@ class TalkParentMessage extends StatelessWidget {
class TalkCommentMessage extends StatefulWidget {
/// Creates a new Talk comment message.
const TalkCommentMessage({
required this.actorId,
required this.chatMessage,
required this.lastCommonRead,
this.previousChatMessage,
this.isParent = false,
super.key,
});

/// {@macro TalkMessage.actorId}
final String actorId;

/// {@macro TalkMessage.chatMessage}
final spreed.$ChatMessageInterface chatMessage;

Expand Down Expand Up @@ -419,6 +435,7 @@ class _TalkCommentMessageState extends State<TalkCommentMessage> {
messageType: != spreed.MessageType.commentDeleted,
) when p != null && !widget.isParent) {
parent = TalkParentMessage(
actorId: widget.actorId,
parentChatMessage: p,
lastCommonRead: widget.lastCommonRead,
);
Expand Down Expand Up @@ -540,7 +557,7 @@ class _TalkCommentMessageState extends State<TalkCommentMessage> {
SizedBox.square(
dimension: 32,
child: widget.chatMessage.messageType != spreed.MessageType.commentDeleted && (hoverState || menuOpen)
? _buildPopupMenuButton(widget.chatMessage)
? _buildPopupMenuButton(widget.actorId, widget.chatMessage)
: null,
),
],
Expand All @@ -558,7 +575,7 @@ class _TalkCommentMessageState extends State<TalkCommentMessage> {
);
}

Widget? _buildPopupMenuButton(spreed.$ChatMessageInterface chatMessage) {
Widget? _buildPopupMenuButton(String actorId, spreed.$ChatMessageInterface chatMessage) {
final children = [
if (chatMessage.messageType != spreed.MessageType.commentDeleted)
MenuItemButton(
Expand Down Expand Up @@ -593,6 +610,18 @@ class _TalkCommentMessageState extends State<TalkCommentMessage> {
NeonProvider.of<TalkRoomBloc>(context).setReplyChatMessage(chatMessage);
},
),
if (chatMessage.messageType != spreed.MessageType.commentDeleted && chatMessage.actorId == actorId)
MenuItemButton(
leadingIcon: const Icon(Icons.delete_forever),
child: Text(TalkLocalizations.of(context).roomMessageDelete),
onPressed: () {
setState(() {
menuOpen = false;
});

NeonProvider.of<TalkRoomBloc>(context).deleteMessage(chatMessage);
},
),
];

if (children.isEmpty) {
Expand Down
1 change: 1 addition & 0 deletions packages/neon/neon_talk/lib/src/widgets/message_input.dart
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ class _TalkMessageInputState extends State<TalkMessageInput> {
),
Expanded(
child: TalkParentMessage(
actorId: bloc.room.value.requireData.actorId,
parentChatMessage: replyToSnapshot.requireData!,
lastCommonRead: null,
),
Expand Down
Loading

0 comments on commit aeb9814

Please sign in to comment.