diff options
author | Joas Schilling <coding@schilljs.com> | 2022-04-11 16:08:50 +0300 |
---|---|---|
committer | Joas Schilling <coding@schilljs.com> | 2022-04-12 09:48:41 +0300 |
commit | 7cde6b2cb1a970d2bbd61c7ca9b534bdc381e33f (patch) | |
tree | 82e5f2f57e72626956648b985f1d2912591b4585 /lib/Controller/ChatController.php | |
parent | 1d6a805c17876336b702e2c8b968773dfec876fd (diff) |
Move to a dedicated method
Signed-off-by: Joas Schilling <coding@schilljs.com>
Diffstat (limited to 'lib/Controller/ChatController.php')
-rw-r--r-- | lib/Controller/ChatController.php | 61 |
1 files changed, 34 insertions, 27 deletions
diff --git a/lib/Controller/ChatController.php b/lib/Controller/ChatController.php index 5c53d635a..c60885bc5 100644 --- a/lib/Controller/ChatController.php +++ b/lib/Controller/ChatController.php @@ -501,30 +501,58 @@ class ChatController extends AEnvironmentAwareController { ]; } - /** - * Gather information to expose $message['reactions']['self'] - */ + $messages = $this->loadSelfReactions($messages, $commentIdToIndex); + + $response = new DataResponse($messages, Http::STATUS_OK); + + $newLastKnown = end($comments); + if ($newLastKnown instanceof IComment) { + $response->addHeader('X-Chat-Last-Given', $newLastKnown->getId()); + /** + * This falsely set the read marker on new messages although you + * navigated away to a different chat already. So we removed this + * and instead update the read marker before your next waiting. + * So when you are still there, it will just have a wrong read + * marker for the time until your next request starts, while it will + * not update the value, when you actually left the chat already. + * if ($setReadMarker === 1 && $lookIntoFuture) { + * $this->participantService->updateLastReadMessage($this->participant, (int) $newLastKnown->getId()); + * } + */ + if ($this->participant->getAttendee()->getReadPrivacy() === Participant::PRIVACY_PUBLIC) { + $response->addHeader('X-Chat-Last-Common-Read', $this->chatManager->getLastCommonReadMessage($this->room)); + } + } + + return $response; + } + + protected function loadSelfReactions(array $messages, array $commentIdToIndex): array { + // Get message ids with reactions $messageIdsWithReactions = array_map( static fn (array $message) => $message['id'], array_filter($messages, static fn (array $message) => !empty($message['reactions'])) ); + // Get parents with reactions $parentsWithReactions = array_map( static fn (array $message) => ['parent' => $message['parent']['id'], 'message' => $message['id']], array_filter($messages, static fn (array $message) => !empty($message['parent']['reactions'])) ); + // Create a map, so we can translate the parent's $messageId to the correct child entries $parentMap = $parentIdsWithReactions = []; foreach ($parentsWithReactions as $entry) { - // Create a map, so we can translate the parent's $messageId to the correct child entries $parentMap[(int) $entry['parent']] ??= []; $parentMap[(int) $entry['parent']][] = (int) $entry['message']; $parentIdsWithReactions[] = (int) $entry['parent']; } + // Unique list for the query $idsWithReactions = array_unique(array_merge($messageIdsWithReactions, $parentIdsWithReactions)); + $reactionsById = $this->reactionManager->getReactionsByActorForMessages($this->participant, $idsWithReactions); - $reactionsById = $this->reactionManager->getReactionsForMessages($this->participant, $idsWithReactions); + // Inject the reactions self into the $messages array foreach ($reactionsById as $messageId => $reactions) { if (isset($messages[$commentIdToIndex[$messageId]])) { $messages[$commentIdToIndex[$messageId]]['reactions']['self'] = $reactions; @@ -540,28 +568,7 @@ class ChatController extends AEnvironmentAwareController { } } - $response = new DataResponse($messages, Http::STATUS_OK); - - $newLastKnown = end($comments); - if ($newLastKnown instanceof IComment) { - $response->addHeader('X-Chat-Last-Given', $newLastKnown->getId()); - /** - * This falsely set the read marker on new messages although you - * navigated away to a different chat already. So we removed this - * and instead update the read marker before your next waiting. - * So when you are still there, it will just have a wrong read - * marker for the time until your next request starts, while it will - * not update the value, when you actually left the chat already. - * if ($setReadMarker === 1 && $lookIntoFuture) { - * $this->participantService->updateLastReadMessage($this->participant, (int) $newLastKnown->getId()); - * } - */ - if ($this->participant->getAttendee()->getReadPrivacy() === Participant::PRIVACY_PUBLIC) { - $response->addHeader('X-Chat-Last-Common-Read', $this->chatManager->getLastCommonReadMessage($this->room)); - } - } - - return $response; + return $messages; } /** |