Skip to content

Commit

Permalink
Merge pull request #11371 from nextcloud/feat/11206/edit-message-capt…
Browse files Browse the repository at this point in the history
…ions

fix(chat): Handle basic editing of captions
  • Loading branch information
nickvergessen authored Jan 12, 2024
2 parents 11f5314 + 3f27de5 commit 419adca
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 14 deletions.
19 changes: 17 additions & 2 deletions lib/Chat/ChatManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -483,10 +483,25 @@ public function deleteMessage(Room $chat, IComment $comment, Participant $partic
* @param \DateTime $editTime
* @param string $message
* @return IComment
* @throws \InvalidArgumentException When the message is empty or the shared object is not a file share with caption
*/
public function editMessage(Room $chat, IComment $comment, Participant $participant, \DateTime $editTime, string $message): IComment {
// FIXME "Caption" handling via $comment->getVerb() === self::VERB_OBJECT_SHARED
// TODO prevent editing other shares like polls, contacts, etc.
if (trim($message) === '') {
throw new \InvalidArgumentException('message');
}

if ($comment->getVerb() === ChatManager::VERB_OBJECT_SHARED) {
$messageData = json_decode($comment->getMessage(), true);
if (!isset($messageData['message']) || $messageData['message'] !== 'file_shared') {
// Not a file share
throw new \InvalidArgumentException('object_share');
}

$messageData['parameters'] ??= [];
$messageData['parameters']['metaData'] ??= [];
$messageData['parameters']['metaData']['caption'] = $message;
$message = json_encode($messageData);
}

$metaData = $comment->getMetaData() ?? [];
$metaData['last_edited_by_type'] = $participant->getAttendee()->getActorType();
Expand Down
27 changes: 17 additions & 10 deletions lib/Controller/ChatController.php
Original file line number Diff line number Diff line change
Expand Up @@ -753,11 +753,11 @@ public function deleteMessage(int $messageId): DataResponse {
* @param int $messageId ID of the message
* @param string $message the message to send
* @psalm-param non-negative-int $messageId
* @return DataResponse<Http::STATUS_OK|Http::STATUS_ACCEPTED, TalkChatMessageWithParent, array{X-Chat-Last-Common-Read?: numeric-string}>|DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_FORBIDDEN|Http::STATUS_NOT_FOUND|Http::STATUS_METHOD_NOT_ALLOWED, array<empty>, array{}>
* @return DataResponse<Http::STATUS_OK|Http::STATUS_ACCEPTED, TalkChatMessageWithParent, array{X-Chat-Last-Common-Read?: numeric-string}>|DataResponse<Http::STATUS_BAD_REQUEST, array{error: string}, array{}>|DataResponse<Http::STATUS_FORBIDDEN|Http::STATUS_NOT_FOUND|Http::STATUS_METHOD_NOT_ALLOWED, array<empty>, array{}>
*
* 200: Message edited successfully
* 202: Message edited successfully, but Matterbridge is configured, so the information can be replicated elsewhere
* 400: Editing message is not possible
* 400: Editing message is not possible, e.g. when the new message is empty or the message is too old
* 403: Missing permissions to edit message
* 404: Message not found
* 405: Editing this message type is not allowed
Expand Down Expand Up @@ -797,16 +797,23 @@ public function editMessage(int $messageId, string $message): DataResponse {
$maxAge->sub(new \DateInterval('P1D'));
if ($comment->getCreationDateTime() < $maxAge) {
// Message is too old
return new DataResponse([], Http::STATUS_BAD_REQUEST);
return new DataResponse(['error' => 'age'], Http::STATUS_BAD_REQUEST);
}

$systemMessageComment = $this->chatManager->editMessage(
$this->room,
$comment,
$this->participant,
$this->timeFactory->getDateTime(),
$message
);
try {
$systemMessageComment = $this->chatManager->editMessage(
$this->room,
$comment,
$this->participant,
$this->timeFactory->getDateTime(),
$message
);
} catch (\InvalidArgumentException $e) {
if ($e->getMessage() === 'object_share') {
return new DataResponse([], Http::STATUS_METHOD_NOT_ALLOWED);
}
return new DataResponse(['error' => $e->getMessage()], Http::STATUS_BAD_REQUEST);
}

$systemMessage = $this->messageParser->createMessage($this->room, $this->participant, $systemMessageComment, $this->l);
$this->messageParser->parseMessage($systemMessage);
Expand Down
14 changes: 12 additions & 2 deletions openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -6282,7 +6282,7 @@
}
},
"400": {
"description": "Editing message is not possible",
"description": "Editing message is not possible, e.g. when the new message is empty or the message is too old",
"content": {
"application/json": {
"schema": {
Expand All @@ -6301,7 +6301,17 @@
"meta": {
"$ref": "#/components/schemas/OCSMeta"
},
"data": {}
"data": {
"type": "object",
"required": [
"error"
],
"properties": {
"error": {
"type": "string"
}
}
}
}
}
}
Expand Down
22 changes: 22 additions & 0 deletions tests/integration/features/chat-1/edit-message.feature
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,25 @@ Feature: chat-1/edit-message
Then user "participant2" sees the following messages in room "room" with 200
| room | actorType | actorId | actorDisplayName | message | messageParameters | parentMessage |
| room | users | participant2 | participant2-displayname | Message 1 - Edit 2 | [] | |
And user "participant2" edits message "Message 1 - Edit 1" in room "room" to "" with 400
Then user "participant1" sees the following messages in room "room" with 200
| room | actorType | actorId | actorDisplayName | message | messageParameters | parentMessage |
| room | users | participant2 | participant2-displayname | Message 1 - Edit 2 | [] | |

Scenario: Editing a caption
Given user "participant1" creates room "room" (v4)
| roomType | 3 |
| roomName | room |
And user "participant1" shares "welcome.txt" with room "room"
| talkMetaData | {"caption":"Caption 1"} |
And user "participant1" sees the following messages in room "room" with 200
| room | actorType | actorId | actorDisplayName | message | messageParameters |
| room | users | participant1 | participant1-displayname | Caption 1 | "IGNORE" |
When user "participant1" edits message "Caption 1" in room "room" to "Caption 1 - Edit 1" with 200
Then user "participant1" sees the following messages in room "room" with 200
| room | actorType | actorId | actorDisplayName | message | messageParameters |
| room | users | participant1 | participant1-displayname | Caption 1 - Edit 1 | "IGNORE" |
When user "participant1" edits message "Caption 1" in room "room" to "" with 400
Then user "participant1" sees the following messages in room "room" with 200
| room | actorType | actorId | actorDisplayName | message | messageParameters |
| room | users | participant1 | participant1-displayname | Caption 1 - Edit 1 | "IGNORE" |

0 comments on commit 419adca

Please sign in to comment.