diff --git a/lib/Controller/AEnvironmentAwareController.php b/lib/Controller/AEnvironmentAwareController.php index 9b7ce742558..d5fc73287e4 100644 --- a/lib/Controller/AEnvironmentAwareController.php +++ b/lib/Controller/AEnvironmentAwareController.php @@ -27,6 +27,7 @@ namespace OCA\Talk\Controller; +use OC\AppFramework\Http\Dispatcher; use OCA\Talk\Participant; use OCA\Talk\Room; use OCP\AppFramework\OCSController; @@ -59,4 +60,25 @@ public function setParticipant(Participant $participant): void { public function getParticipant(): ?Participant { return $this->participant; } + + /** + * Following the logic of {@see Dispatcher::executeController} + * @return string Either 'json' or 'xml' + */ + public function getResponseFormat(): string { + // get format from the url format or request format parameter + $format = $this->request->getParam('format'); + + // if none is given try the first Accept header + if ($format === null) { + $headers = $this->request->getHeader('Accept'); + /** + * Default value of + * @see OCSController::buildResponse() + */ + $format = $this->getResponderByHTTPHeader($headers, 'xml'); + } + + return $format; + } } diff --git a/lib/Controller/ChatController.php b/lib/Controller/ChatController.php index 611c41fe1cf..3608f5959a9 100644 --- a/lib/Controller/ChatController.php +++ b/lib/Controller/ChatController.php @@ -168,9 +168,9 @@ public function parseCommentToResponse(IComment $comment, Message $parentMessage $this->participantService->updateLastReadMessage($this->participant, (int) $comment->getId()); - $data = $chatMessage->toArray(); + $data = $chatMessage->toArray($this->getResponseFormat()); if ($parentMessage instanceof Message) { - $data['parent'] = $parentMessage->toArray(); + $data['parent'] = $parentMessage->toArray($this->getResponseFormat()); } $response = new DataResponse($data, Http::STATUS_CREATED); @@ -481,7 +481,7 @@ public function receiveMessages(int $lookIntoFuture, $parentIds[$id] = $comment->getParentId(); } - $messages[] = $message->toArray(); + $messages[] = $message->toArray($this->getResponseFormat()); $commentIdToIndex[$id] = $i; $i++; } @@ -517,7 +517,7 @@ public function receiveMessages(int $lookIntoFuture, $this->messageParser->parseMessage($message); if ($message->getVisibility()) { - $loadedParents[$parentId] = $message->toArray(); + $loadedParents[$parentId] = $message->toArray($this->getResponseFormat()); $messages[$commentKey]['parent'] = $loadedParents[$parentId]; continue; } @@ -667,8 +667,8 @@ public function deleteMessage(int $messageId): DataResponse { $message = $this->messageParser->createMessage($this->room, $this->participant, $comment, $this->l); $this->messageParser->parseMessage($message); - $data = $systemMessage->toArray(); - $data['parent'] = $message->toArray(); + $data = $systemMessage->toArray($this->getResponseFormat()); + $data['parent'] = $message->toArray($this->getResponseFormat()); $bridge = $this->matterbridgeManager->getBridgeOfRoom($this->room); @@ -704,7 +704,7 @@ public function clearHistory(): DataResponse { $this->messageParser->parseMessage($systemMessage); - $data = $systemMessage->toArray(); + $data = $systemMessage->toArray($this->getResponseFormat()); $bridge = $this->matterbridgeManager->getBridgeOfRoom($this->room); @@ -797,7 +797,7 @@ public function getObjectsSharedInRoomOverview(int $limit = 7): DataResponse { continue; } - $messages[(int) $comment->getId()] = $message->toArray(); + $messages[(int) $comment->getId()] = $message->toArray($this->getResponseFormat()); } $messagesByType = []; @@ -853,7 +853,7 @@ protected function getMessagesForRoom(Room $room, array $messageIds): array { continue; } - $messages[(int) $comment->getId()] = $message->toArray(); + $messages[(int) $comment->getId()] = $message->toArray($this->getResponseFormat()); } return $messages; diff --git a/lib/Controller/RoomController.php b/lib/Controller/RoomController.php index 79b06b63231..7568e59af7e 100644 --- a/lib/Controller/RoomController.php +++ b/lib/Controller/RoomController.php @@ -624,7 +624,7 @@ protected function formatLastMessage(Room $room, Participant $participant, IComm return []; } - return $message->toArray(); + return $message->toArray($this->getResponseFormat()); } /** diff --git a/lib/Model/Message.php b/lib/Model/Message.php index d888d013346..3d5e16fbc2b 100644 --- a/lib/Model/Message.php +++ b/lib/Model/Message.php @@ -168,11 +168,11 @@ public function isReplyable(): bool { \in_array($this->getActorType(), [Attendee::ACTOR_USERS, Attendee::ACTOR_GUESTS]); } - public function toArray(): array { + public function toArray(string $format): array { $expireDate = $this->getComment()->getExpireDate(); $reactions = $this->getComment()->getReactions(); - if (empty($reactions)) { + if ($format === 'json' && empty($reactions)) { // Cheating here to make sure the reactions array is always a // JSON object on the API, even when there is no reaction at all. $reactions = new \StdClass();