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

fix(chat): Handle basic editing of captions #11371

Merged
merged 2 commits into from
Jan 12, 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
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" |
Loading