Skip to content

Commit

Permalink
feat(federation): Translate parameters of chat messages
Browse files Browse the repository at this point in the history
Signed-off-by: Joas Schilling <coding@schilljs.com>
  • Loading branch information
nickvergessen committed Feb 28, 2024
1 parent a916baf commit a0b5479
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 7 deletions.
11 changes: 4 additions & 7 deletions lib/Federation/Proxy/TalkV1/Controller/ChatController.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,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;
}
Expand Down Expand Up @@ -164,8 +164,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);
}
Expand Down Expand Up @@ -204,8 +203,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);
}
Expand Down Expand Up @@ -251,8 +249,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;
}
Expand Down
55 changes: 55 additions & 0 deletions lib/Federation/Proxy/TalkV1/UserConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, array<string, array{userId: string, displayName: string}>>
Expand Down Expand Up @@ -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<string, array{userId: string, displayName: string}>
*/
Expand Down

0 comments on commit a0b5479

Please sign in to comment.