From 028b12e584633c4379d7b5de2360dc6587b1c4d2 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Mon, 1 Oct 2018 13:43:20 +0200 Subject: [PATCH] Always return the full message object on create Signed-off-by: Joas Schilling --- docs/api-v1.md | 3 +++ lib/Controller/ChatController.php | 23 +++++++++++++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/docs/api-v1.md b/docs/api-v1.md index 43567f174..ae394dbb9 100644 --- a/docs/api-v1.md +++ b/docs/api-v1.md @@ -548,6 +548,9 @@ Base endpoint is: `/ocs/v2.php/apps/spreed/api/v1` + `404 Not Found` When the room could not be found for the participant + `413 Payload Too Large` When the message was longer than the allowed limit of 1000 characters + - Data: + The full message array of the new message, as defined in [Receive chat messages of a room](#receive-chat-messages-of-a-room) + ### Get mention autocomplete suggestions * Method: `GET` diff --git a/lib/Controller/ChatController.php b/lib/Controller/ChatController.php index c069fd6f0..281403588 100644 --- a/lib/Controller/ChatController.php +++ b/lib/Controller/ChatController.php @@ -184,6 +184,8 @@ class ChatController extends OCSController { return new DataResponse([], Http::STATUS_NOT_FOUND); } + $currentUser = null; + $displayName = ''; if ($this->userId === null) { $actorType = 'guests'; $sessionId = $this->session->getSessionForRoom($token); @@ -196,10 +198,15 @@ class ChatController extends OCSController { if ($sessionId && $actorDisplayName) { $this->guestManager->updateName($room, $sessionId, $actorDisplayName); + $displayName = $actorDisplayName; + } else if ($sessionId) { + $displayName = $this->guestManager->getNameBySessionHash($actorId); } } else { $actorType = 'users'; $actorId = $this->userId; + $currentUser = $this->userManager->get($this->userId); + $displayName = $currentUser instanceof IUser ? $currentUser->getDisplayName() : ''; } if (!$actorId) { @@ -209,14 +216,26 @@ class ChatController extends OCSController { $creationDateTime = new \DateTime('now', new \DateTimeZone('UTC')); try { - $this->chatManager->sendMessage($room, $actorType, $actorId, $message, $creationDateTime); + $comment = $this->chatManager->sendMessage($room, $actorType, $actorId, $message, $creationDateTime); } catch (MessageTooLongException $e) { return new DataResponse([], Http::STATUS_REQUEST_ENTITY_TOO_LARGE); } catch (\Exception $e) { return new DataResponse([], Http::STATUS_BAD_REQUEST); } - return new DataResponse([], Http::STATUS_CREATED); + list($message, $messageParameters) = $this->messageParser->parseMessage($comment, $this->l, $currentUser); + + return new DataResponse([ + 'id' => (int) $comment->getId(), + 'token' => $token, + 'actorType' => $comment->getActorType(), + 'actorId' => $comment->getActorId(), + 'actorDisplayName' => $displayName, + 'timestamp' => $comment->getCreationDateTime()->getTimestamp(), + 'message' => $message, + 'messageParameters' => $messageParameters, + 'systemMessage' => $comment->getVerb() === 'system' ? $comment->getMessage() : '', + ], Http::STATUS_CREATED); } /**