Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(neon_talk): Enable editing of chat messages #2277

Merged
merged 5 commits into from
Jul 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions packages/neon/neon_talk/lib/l10n/en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@
"roomMessageReply": "Reply",
"roomMessageReaction": "Add reaction",
"roomMessageDelete": "Delete",
"roomMessageEdit": "Edit",
"roomMessageEdited": "edited",
"roomMessageLastEdited": "Last edited by {name} at {time}",
"@roomMessageLastEdited": {
"placeholders": {
"name": {
"type": "String"
},
"time": {
"type": "String"
}
}
},
provokateurin marked this conversation as resolved.
Show resolved Hide resolved
"reactionsAddNew": "Add a new reaction",
"reactionsLoading": "Loading reactions",
"roomsCreateNew": "Create new room"
Expand Down
18 changes: 18 additions & 0 deletions packages/neon/neon_talk/lib/l10n/localizations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,24 @@ abstract class TalkLocalizations {
/// **'Delete'**
String get roomMessageDelete;

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

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

/// No description provided for @roomMessageLastEdited.
///
/// In en, this message translates to:
/// **'Last edited by {name} at {time}'**
String roomMessageLastEdited(String name, String time);

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

@override
String get roomMessageEdit => 'Edit';

@override
String get roomMessageEdited => 'edited';

@override
String roomMessageLastEdited(String name, String time) {
return 'Last edited by $name at $time';
}

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

Expand Down
78 changes: 61 additions & 17 deletions packages/neon/neon_talk/lib/src/blocs/room.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,15 @@ abstract class TalkRoomBloc implements InteractiveBloc {
/// Sets a [chatMessage] as the message to [replyTo].
void setReplyChatMessage(spreed.$ChatMessageInterface chatMessage);

/// Sets a [chatMessage] as the message to [editing].
void setEditChatMessage(spreed.$ChatMessageInterface chatMessage);

/// Removes the current [replyTo] chat message.
void removeReplyChatMessage();

/// Removes the current [editing] chat message.
void removeEditChatMessage();

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

Expand All @@ -61,6 +67,9 @@ abstract class TalkRoomBloc implements InteractiveBloc {

/// Current chat message to reply to.
BehaviorSubject<spreed.$ChatMessageInterface?> get replyTo;

/// Current chat message that is edited.
BehaviorSubject<spreed.$ChatMessageInterface?> get editing;
}

class _TalkRoomBloc extends InteractiveBloc implements TalkRoomBloc {
Expand Down Expand Up @@ -182,6 +191,9 @@ class _TalkRoomBloc extends InteractiveBloc implements TalkRoomBloc {
@override
final replyTo = BehaviorSubject.seeded(null);

@override
final editing = BehaviorSubject.seeded(null);

@override
void dispose() {
pollLoop = false;
Expand All @@ -192,6 +204,7 @@ class _TalkRoomBloc extends InteractiveBloc implements TalkRoomBloc {
unawaited(lastCommonRead.close());
unawaited(reactions.close());
unawaited(replyTo.close());
unawaited(editing.close());
super.dispose();
}

Expand Down Expand Up @@ -234,29 +247,48 @@ class _TalkRoomBloc extends InteractiveBloc implements TalkRoomBloc {

@override
Future<void> sendMessage(String message) async {
final replyToId = replyTo.value?.id;
replyTo.add(null);

await wrapAction(
() async {
final response = await account.client.spreed.chat.sendMessage(
token: token,
$body: spreed.ChatSendMessageRequestApplicationJson(
(b) {
b.message = message;
if (replyToId != null) {
b.replyTo = replyToId;
}
},
),
);
late spreed.ChatMessageWithParent? m;
late String? lastCommonRead;

updateLastCommonRead(response.headers.xChatLastCommonRead);
final editingId = editing.value?.id;
if (editingId != null) {
editing.add(null);

final m = response.body.ocs.data;
final response = await account.client.spreed.chat.editMessage(
token: token,
messageId: editingId,
$body: spreed.ChatEditMessageRequestApplicationJson(
(b) => b.message = message,
),
);

m = response.body.ocs.data;
lastCommonRead = response.headers.xChatLastCommonRead;
} else {
final replyToId = replyTo.value?.id;
replyTo.add(null);

final response = await account.client.spreed.chat.sendMessage(
token: token,
$body: spreed.ChatSendMessageRequestApplicationJson(
(b) {
b.message = message;
if (replyToId != null) {
b.replyTo = replyToId;
}
},
),
);

m = response.body.ocs.data;
lastCommonRead = response.headers.xChatLastCommonRead;
}

updateLastCommonRead(lastCommonRead);
if (m != null) {
updateLastKnownMessageId(m.id);

prependMessages([m]);
}
},
Expand Down Expand Up @@ -330,6 +362,7 @@ class _TalkRoomBloc extends InteractiveBloc implements TalkRoomBloc {

@override
void setReplyChatMessage(spreed.$ChatMessageInterface chatMessage) {
editing.add(null);
replyTo.add(chatMessage);
}

Expand All @@ -338,6 +371,17 @@ class _TalkRoomBloc extends InteractiveBloc implements TalkRoomBloc {
replyTo.add(null);
}

@override
void setEditChatMessage(spreed.$ChatMessageInterface chatMessage) {
replyTo.add(null);
editing.add(chatMessage);
}

@override
void removeEditChatMessage() {
editing.add(null);
}

@override
Future<void> deleteMessage(spreed.$ChatMessageInterface chatMessage) async {
await wrapAction(
Expand Down
16 changes: 16 additions & 0 deletions packages/neon/neon_talk/lib/src/utils/helpers.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import 'package:flutter/material.dart';
import 'package:neon_framework/blocs.dart';
import 'package:neon_framework/utils.dart';
import 'package:nextcloud/spreed.dart' as spreed;
import 'package:nextcloud/utils.dart';
import 'package:timezone/timezone.dart' as tz;
Expand Down Expand Up @@ -47,3 +50,16 @@ extension $ReactionInterfaceHelpers on spreed.$ReactionInterface {
/// Parsed equivalent of [timestamp].
tz.TZDateTime get parsedTimestamp => DateTimeUtils.fromSecondsSinceEpoch(tz.local, timestamp);
}

/// Returns if the Talk [feature] is supported on the instance.
bool hasFeature(BuildContext context, String feature) {
final capabilitiesBloc = NeonProvider.of<CapabilitiesBloc>(context);
final capabilities = capabilitiesBloc
.capabilities.valueOrNull?.data?.capabilities.spreedPublicCapabilities?.spreedPublicCapabilities0?.spreed;

if (capabilities == null) {
return false;
}

return capabilities.features.contains(feature);
}
43 changes: 39 additions & 4 deletions packages/neon/neon_talk/lib/src/widgets/message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -459,19 +459,40 @@ class _TalkCommentMessageState extends State<TalkCommentMessage> {
final separateMessages = widget.chatMessage.actorId != widget.previousChatMessage?.actorId ||
widget.previousChatMessage?.messageType == spreed.MessageType.system ||
previousDate == null ||
date.difference(previousDate) > const Duration(minutes: 3);
date.difference(previousDate) > const Duration(minutes: 3) ||
widget.chatMessage.lastEditTimestamp != null;

Widget? displayName;
Widget? label;
Widget? avatar;
Widget? time;
if (separateMessages) {
displayName = Text(
label = Text(
getActorDisplayName(TalkLocalizations.of(context), widget.chatMessage),
style: textTheme.labelLarge!.copyWith(
color: labelColor,
),
);

if (widget.chatMessage.lastEditTimestamp != null && widget.chatMessage.lastEditActorDisplayName != null) {
label = Row(
children: [
label,
Tooltip(
message: TalkLocalizations.of(context).roomMessageLastEdited(
widget.chatMessage.lastEditActorDisplayName!,
DateFormat.yMd().add_jm().format(widget.chatMessage.parsedLastEditTimestamp!),
),
child: Text(
' (${TalkLocalizations.of(context).roomMessageEdited})',
provokateurin marked this conversation as resolved.
Show resolved Hide resolved
style: textTheme.labelLarge!.copyWith(
color: labelColor,
),
),
),
],
);
}

if (!widget.isParent) {
avatar = TalkActorAvatar(
actorId: widget.chatMessage.actorId,
Expand Down Expand Up @@ -562,7 +583,7 @@ class _TalkCommentMessageState extends State<TalkCommentMessage> {
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
if (displayName != null) displayName,
if (label != null) label,
if (time != null) time,
],
),
Expand Down Expand Up @@ -694,6 +715,20 @@ class _TalkCommentMessageState extends State<TalkCommentMessage> {
NeonProvider.of<TalkRoomBloc>(context).setReplyChatMessage(chatMessage);
},
),
if (chatMessage.messageType != spreed.MessageType.commentDeleted &&
chatMessage.actorId == room.actorId &&
hasFeature(context, 'edit-messages'))
MenuItemButton(
leadingIcon: const Icon(Icons.edit),
child: Text(TalkLocalizations.of(context).roomMessageEdit),
onPressed: () {
setState(() {
menuOpen = false;
});

NeonProvider.of<TalkRoomBloc>(context).setEditChatMessage(chatMessage);
},
),
if (chatMessage.messageType != spreed.MessageType.commentDeleted && chatMessage.actorId == room.actorId)
MenuItemButton(
leadingIcon: const Icon(Icons.delete_forever),
Expand Down
Loading