From 9f1b8e0ddf3a9bfe761c7b55032361b1d030c52a Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Tue, 27 Feb 2024 18:24:33 +0100 Subject: [PATCH] feat(federation): Translate parameters of chat messages Signed-off-by: Joas Schilling --- .../TalkV1/Controller/ChatController.php | 11 ++-- lib/Federation/Proxy/TalkV1/UserConverter.php | 55 +++++++++++++++++++ 2 files changed, 59 insertions(+), 7 deletions(-) diff --git a/lib/Federation/Proxy/TalkV1/Controller/ChatController.php b/lib/Federation/Proxy/TalkV1/Controller/ChatController.php index 8e9e16ad31fb..730622b941c7 100644 --- a/lib/Federation/Proxy/TalkV1/Controller/ChatController.php +++ b/lib/Federation/Proxy/TalkV1/Controller/ChatController.php @@ -94,7 +94,7 @@ public function sendMessage(Room $room, Participant $participant, string $messag /** @var ?TalkChatMessageWithParent $data */ $data = $this->proxy->getOCSData($proxy, [Http::STATUS_CREATED]); if (!empty($data)) { - $data = $this->userConverter->convertAttendee($room, $data, 'actorType', 'actorId', 'actorDisplayName'); + $data = $this->userConverter->convertMessage($room, $data); } else { $data = null; } @@ -188,8 +188,7 @@ public function receiveMessages( /** @var TalkChatMessageWithParent[] $data */ $data = $this->proxy->getOCSData($proxy); /** @var TalkChatMessageWithParent[] $data */ - $data = $this->userConverter->convertAttendees($room, $data, 'actorType', 'actorId', 'actorDisplayName'); - $data = $this->userConverter->convertAttendees($room, $data, 'lastEditActorType', 'lastEditActorId', 'lastEditActorDisplayName'); + $data = $this->userConverter->convertMessages($room, $data); return new DataResponse($data, Http::STATUS_OK, $headers); } @@ -228,8 +227,7 @@ public function getMessageContext(Room $room, Participant $participant, int $mes /** @var TalkChatMessageWithParent[] $data */ $data = $this->proxy->getOCSData($proxy); /** @var TalkChatMessageWithParent[] $data */ - $data = $this->userConverter->convertAttendees($room, $data, 'actorType', 'actorId', 'actorDisplayName'); - $data = $this->userConverter->convertAttendees($room, $data, 'lastEditActorType', 'lastEditActorId', 'lastEditActorDisplayName'); + $data = $this->userConverter->convertMessages($room, $data); return new DataResponse($data, Http::STATUS_OK, $headers); } @@ -275,8 +273,7 @@ public function editMessage(Room $room, Participant $participant, int $messageId /** @var ?TalkChatMessageWithParent $data */ $data = $this->proxy->getOCSData($proxy, [Http::STATUS_OK, Http::STATUS_ACCEPTED]); if (!empty($data)) { - $data = $this->userConverter->convertAttendee($room, $data, 'actorType', 'actorId', 'actorDisplayName'); - $data = $this->userConverter->convertAttendee($room, $data, 'lastEditActorType', 'lastEditActorId', 'lastEditActorDisplayName'); + $data = $this->userConverter->convertMessage($room, $data); } else { $data = null; } diff --git a/lib/Federation/Proxy/TalkV1/UserConverter.php b/lib/Federation/Proxy/TalkV1/UserConverter.php index 37288d5782a7..7ae2ff7e2992 100644 --- a/lib/Federation/Proxy/TalkV1/UserConverter.php +++ b/lib/Federation/Proxy/TalkV1/UserConverter.php @@ -27,9 +27,13 @@ namespace OCA\Talk\Federation\Proxy\TalkV1; use OCA\Talk\Model\Attendee; +use OCA\Talk\ResponseDefinitions; use OCA\Talk\Room; use OCA\Talk\Service\ParticipantService; +/** + * @psalm-import-type TalkChatMessageWithParent from ResponseDefinitions + */ class UserConverter { /** * @var array> @@ -69,6 +73,57 @@ public function convertAttendees(Room $room, array $entries, string $typeField, ); } + protected function convertMessageParameter(Room $room, array $parameter): array { + if ($parameter['type'] === 'user') { // RichObjectDefinition, not Attendee::ACTOR_USERS + if (!isset($parameter['server'])) { + $parameter['server'] = $room->getRemoteServer(); + } elseif ($parameter['server']) { + $localParticipants = $this->getLocalParticipants($room); + $cloudId = $parameter['id'] . '@' . $parameter['server']; + if (isset($localParticipants[$cloudId])) { + unset($parameter['server']); + $parameter['name'] = $localParticipants[$cloudId]['displayName']; + } + } + } + return $parameter; + } + + protected function convertMessageParameters(Room $room, array $message): array { + $message['messageParameters'] = array_map( + fn (array $message): array => $this->convertMessageParameter($room, $message), + $message['messageParameters'] + ); + return $message; + } + + /** + * @param Room $room + * @param TalkChatMessageWithParent $message + * @return TalkChatMessageWithParent + */ + public function convertMessage(Room $room, array $message): array { + $message = $this->convertAttendee($room, $message, 'actorType', 'actorId', 'actorDisplayName'); + $message = $this->convertAttendee($room, $message, 'lastEditActorType', 'lastEditActorId', 'lastEditActorDisplayName'); + $message = $this->convertMessageParameters($room, $message); + if (isset($message['parent'])) { + $message['parent'] = $this->convertMessage($room, $message['parent']); + } + return $message; + } + + /** + * @param Room $room + * @param TalkChatMessageWithParent[] $messages + * @return TalkChatMessageWithParent[] + */ + public function convertMessages(Room $room, array $messages): array { + return array_map( + fn (array $message): array => $this->convertMessage($room, $message), + $messages + ); + } + /** * @return array */