Skip to content

Commit 1870b79

Browse files
committed
Fix chat endpoints with empty reactions in XML format
Signed-off-by: Joas Schilling <[email protected]>
1 parent 81dd257 commit 1870b79

File tree

4 files changed

+34
-12
lines changed

4 files changed

+34
-12
lines changed

lib/Controller/AEnvironmentAwareController.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
use OCA\Talk\Participant;
3131
use OCA\Talk\Room;
3232
use OCP\AppFramework\OCSController;
33+
use OCP\IRequest;
3334

3435
abstract class AEnvironmentAwareController extends OCSController {
3536
protected int $apiVersion = 1;
@@ -59,4 +60,24 @@ public function setParticipant(Participant $participant): void {
5960
public function getParticipant(): ?Participant {
6061
return $this->participant;
6162
}
63+
64+
/**
65+
* @return string Either 'json' or 'xml'
66+
*/
67+
public function getResponseFormat(): string {
68+
// get format from the url format or request format parameter
69+
$format = $this->request->getParam('format');
70+
71+
// if none is given try the first Accept header
72+
if ($format === null) {
73+
$headers = $this->request->getHeader('Accept');
74+
$format = $this->getResponderByHTTPHeader($headers, null);
75+
}
76+
77+
/**
78+
* Default value of
79+
* @see OCSController::buildResponse()
80+
*/
81+
return $format ?? 'xml';
82+
}
6283
}

lib/Controller/ChatController.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,9 @@ public function parseCommentToResponse(IComment $comment, Message $parentMessage
168168

169169
$this->participantService->updateLastReadMessage($this->participant, (int) $comment->getId());
170170

171-
$data = $chatMessage->toArray();
171+
$data = $chatMessage->toArray($this->getResponseFormat());
172172
if ($parentMessage instanceof Message) {
173-
$data['parent'] = $parentMessage->toArray();
173+
$data['parent'] = $parentMessage->toArray($this->getResponseFormat());
174174
}
175175

176176
$response = new DataResponse($data, Http::STATUS_CREATED);
@@ -481,7 +481,7 @@ public function receiveMessages(int $lookIntoFuture,
481481
$parentIds[$id] = $comment->getParentId();
482482
}
483483

484-
$messages[] = $message->toArray();
484+
$messages[] = $message->toArray($this->getResponseFormat());
485485
$commentIdToIndex[$id] = $i;
486486
$i++;
487487
}
@@ -517,7 +517,7 @@ public function receiveMessages(int $lookIntoFuture,
517517
$this->messageParser->parseMessage($message);
518518

519519
if ($message->getVisibility()) {
520-
$loadedParents[$parentId] = $message->toArray();
520+
$loadedParents[$parentId] = $message->toArray($this->getResponseFormat());
521521
$messages[$commentKey]['parent'] = $loadedParents[$parentId];
522522
continue;
523523
}
@@ -667,8 +667,8 @@ public function deleteMessage(int $messageId): DataResponse {
667667
$message = $this->messageParser->createMessage($this->room, $this->participant, $comment, $this->l);
668668
$this->messageParser->parseMessage($message);
669669

670-
$data = $systemMessage->toArray();
671-
$data['parent'] = $message->toArray();
670+
$data = $systemMessage->toArray($this->getResponseFormat());
671+
$data['parent'] = $message->toArray($this->getResponseFormat());
672672

673673
$bridge = $this->matterbridgeManager->getBridgeOfRoom($this->room);
674674

@@ -704,7 +704,7 @@ public function clearHistory(): DataResponse {
704704
$this->messageParser->parseMessage($systemMessage);
705705

706706

707-
$data = $systemMessage->toArray();
707+
$data = $systemMessage->toArray($this->getResponseFormat());
708708

709709
$bridge = $this->matterbridgeManager->getBridgeOfRoom($this->room);
710710

@@ -797,7 +797,7 @@ public function getObjectsSharedInRoomOverview(int $limit = 7): DataResponse {
797797
continue;
798798
}
799799

800-
$messages[(int) $comment->getId()] = $message->toArray();
800+
$messages[(int) $comment->getId()] = $message->toArray($this->getResponseFormat());
801801
}
802802

803803
$messagesByType = [];
@@ -853,7 +853,7 @@ protected function getMessagesForRoom(Room $room, array $messageIds): array {
853853
continue;
854854
}
855855

856-
$messages[(int) $comment->getId()] = $message->toArray();
856+
$messages[(int) $comment->getId()] = $message->toArray($this->getResponseFormat());
857857
}
858858

859859
return $messages;

lib/Controller/RoomController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,7 @@ protected function formatLastMessage(Room $room, Participant $participant, IComm
624624
return [];
625625
}
626626

627-
return $message->toArray();
627+
return $message->toArray($this->getResponseFormat());
628628
}
629629

630630
/**

lib/Model/Message.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use OCA\Talk\Room;
2929
use OCP\Comments\IComment;
3030
use OCP\IL10N;
31+
use OCP\IRequest;
3132

3233
class Message {
3334
/** @var Room */
@@ -168,11 +169,11 @@ public function isReplyable(): bool {
168169
\in_array($this->getActorType(), [Attendee::ACTOR_USERS, Attendee::ACTOR_GUESTS]);
169170
}
170171

171-
public function toArray(): array {
172+
public function toArray(string $format): array {
172173
$expireDate = $this->getComment()->getExpireDate();
173174

174175
$reactions = $this->getComment()->getReactions();
175-
if (empty($reactions)) {
176+
if ($format === 'json' && empty($reactions)) {
176177
// Cheating here to make sure the reactions array is always a
177178
// JSON object on the API, even when there is no reaction at all.
178179
$reactions = new \StdClass();

0 commit comments

Comments
 (0)