From b68d244640c6b52b7359105fcaea5c23afc36af8 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Mon, 18 Sep 2023 21:35:56 +0200 Subject: [PATCH 1/2] fix(API): Reuse participant objects we already created Instead of querying the database yet again Signed-off-by: Joas Schilling --- lib/Manager.php | 17 +++++++++++++---- lib/Service/ParticipantService.php | 15 +++++++++++++++ 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/lib/Manager.php b/lib/Manager.php index c8c8734ed9b..11f3e925763 100644 --- a/lib/Manager.php +++ b/lib/Manager.php @@ -385,7 +385,9 @@ public function getRoomsForActor(string $actorType, string $actorId, array $sess $room = $this->createRoomObject($row); if ($actorType === Attendee::ACTOR_USERS && isset($row['actor_id'])) { - $room->setParticipant($row['actor_id'], $this->createParticipantObject($room, $row)); + $participant = $this->createParticipantObject($room, $row); + $this->participantService->cacheParticipant($room, $participant); + $room->setParticipant($row['actor_id'], $participant); } $rooms[] = $room; } @@ -572,7 +574,9 @@ public function getRoomForUser(int $roomId, ?string $userId): Room { $room = $this->createRoomObject($row); if ($userId !== null && isset($row['actor_id'])) { - $room->setParticipant($row['actor_id'], $this->createParticipantObject($room, $row)); + $participant = $this->createParticipantObject($room, $row); + $this->participantService->cacheParticipant($room, $participant); + $room->setParticipant($row['actor_id'], $participant); } if ($userId === null && $room->getType() !== Room::TYPE_PUBLIC) { @@ -643,7 +647,9 @@ public function getRoomForUserByToken(string $token, ?string $userId, ?string $s $room = $this->createRoomObject($row); if ($userId !== null && isset($row['actor_id'])) { - $room->setParticipant($row['actor_id'], $this->createParticipantObject($room, $row)); + $participant = $this->createParticipantObject($room, $row); + $this->participantService->cacheParticipant($room, $participant); + $room->setParticipant($row['actor_id'], $participant); } if ($isSIPBridgeRequest || $room->getType() === Room::TYPE_PUBLIC) { @@ -746,7 +752,9 @@ public function getRoomByActor(string $token, string $actorType, string $actorId $room = $this->createRoomObject($row); if ($actorType === Attendee::ACTOR_USERS && isset($row['actor_id'])) { - $room->setParticipant($row['actor_id'], $this->createParticipantObject($room, $row)); + $participant = $this->createParticipantObject($room, $row); + $this->participantService->cacheParticipant($room, $participant); + $room->setParticipant($row['actor_id'], $participant); } return $room; @@ -900,6 +908,7 @@ public function getRoomForSession(?string $userId, ?string $sessionId): Room { $room = $this->createRoomObject($row); $participant = $this->createParticipantObject($room, $row); + $this->participantService->cacheParticipant($room, $participant); $room->setParticipant($row['actor_id'], $participant); if ($room->getType() === Room::TYPE_PUBLIC || !in_array($participant->getAttendee()->getParticipantType(), [Participant::GUEST, Participant::GUEST_MODERATOR, Participant::USER_SELF_JOINED], true)) { diff --git a/lib/Service/ParticipantService.php b/lib/Service/ParticipantService.php index 62b8c9a029a..1c32f1e7061 100644 --- a/lib/Service/ParticipantService.php +++ b/lib/Service/ParticipantService.php @@ -1608,6 +1608,21 @@ public function hasActiveSessions(Room $room): bool { return (bool) $row; } + public function cacheParticipant(Room $room, Participant $participant): void { + $attendee = $participant->getAttendee(); + if ($attendee->getActorType() !== Attendee::ACTOR_USERS) { + return; + } + + $this->userCache[$room->getId()] ??= []; + $this->userCache[$room->getId()][$attendee->getActorId()] = $participant; + if ($participant->getSession()) { + $participantSessionId = $participant->getSession()->getSessionId(); + $this->sessionCache[$room->getId()] ??= []; + $this->sessionCache[$room->getId()][$participantSessionId] = $participant; + } + } + /** * @param Room $room * @return bool From b72bceffb45cd6e83adf3835f77b416d903def07 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Mon, 18 Sep 2023 22:35:58 +0200 Subject: [PATCH 2/2] fix(participants): Don't try to load hardcoded guests Signed-off-by: Joas Schilling --- lib/Chat/ChatManager.php | 6 ++++-- lib/Chat/MessageParser.php | 6 ++++++ lib/Chat/Parser/Changelog.php | 4 ++-- lib/Chat/Parser/SystemMessage.php | 4 ++++ lib/Chat/SystemMessage/Listener.php | 2 +- lib/Model/Attendee.php | 4 ++++ 6 files changed, 21 insertions(+), 5 deletions(-) diff --git a/lib/Chat/ChatManager.php b/lib/Chat/ChatManager.php index d438a98800d..c7ec3bc565d 100644 --- a/lib/Chat/ChatManager.php +++ b/lib/Chat/ChatManager.php @@ -230,7 +230,7 @@ public function addSystemMessage( * @return IComment */ public function addChangelogMessage(Room $chat, string $message): IComment { - $comment = $this->commentsManager->create(Attendee::ACTOR_GUESTS, 'changelog', 'chat', (string) $chat->getId()); + $comment = $this->commentsManager->create(Attendee::ACTOR_GUESTS, Attendee::ACTOR_ID_CHANGELOG, 'chat', (string) $chat->getId()); $comment->setMessage($message, self::MAX_CHAT_LENGTH); $comment->setCreationDateTime($this->timeFactory->getDateTime()); @@ -300,7 +300,9 @@ public function sendMessage(Room $chat, ?Participant $participant, string $actor } // Update last_message - if ($comment->getActorType() !== Attendee::ACTOR_BOTS || $comment->getActorId() === 'changelog' || str_starts_with($comment->getActorId(), Attendee::ACTOR_BOT_PREFIX)) { + if ($comment->getActorType() !== Attendee::ACTOR_BOTS + || $comment->getActorId() === Attendee::ACTOR_ID_CHANGELOG + || str_starts_with($comment->getActorId(), Attendee::ACTOR_BOT_PREFIX)) { $this->roomService->setLastMessage($chat, $comment); $this->unreadCountCache->clear($chat->getId() . '-'); } else { diff --git a/lib/Chat/MessageParser.php b/lib/Chat/MessageParser.php index c30e2c16cf7..48fd67cb8e8 100644 --- a/lib/Chat/MessageParser.php +++ b/lib/Chat/MessageParser.php @@ -91,6 +91,12 @@ protected function setActor(Message $message): void { } elseif ($comment->getActorType() === Attendee::ACTOR_BRIDGED) { $displayName = $comment->getActorId(); $actorId = MatterbridgeManager::BRIDGE_BOT_USERID; + } elseif ($comment->getActorType() === Attendee::ACTOR_GUESTS + && $comment->getActorId() === Attendee::ACTOR_ID_CLI) { + $actorId = Attendee::ACTOR_ID_CLI; + } elseif ($comment->getActorType() === Attendee::ACTOR_GUESTS + && $comment->getActorId() === Attendee::ACTOR_ID_CHANGELOG) { + $actorId = Attendee::ACTOR_ID_CHANGELOG; } elseif ($comment->getActorType() === Attendee::ACTOR_GUESTS) { if (isset($guestNames[$comment->getActorId()])) { $displayName = $this->guestNames[$comment->getActorId()]; diff --git a/lib/Chat/Parser/Changelog.php b/lib/Chat/Parser/Changelog.php index c0c24a90d8b..d1fd93d139e 100644 --- a/lib/Chat/Parser/Changelog.php +++ b/lib/Chat/Parser/Changelog.php @@ -33,11 +33,11 @@ class Changelog { */ public function parseMessage(Message $chatMessage): void { if ($chatMessage->getActorType() !== Attendee::ACTOR_GUESTS || - $chatMessage->getActorId() !== 'changelog') { + $chatMessage->getActorId() !== Attendee::ACTOR_ID_CHANGELOG) { throw new \OutOfBoundsException('Not a changelog'); } $l = $chatMessage->getL10n(); - $chatMessage->setActor('bots', 'changelog', $l->t('Talk updates ✅')); + $chatMessage->setActor(Attendee::ACTOR_BOTS, Attendee::ACTOR_ID_CHANGELOG, $l->t('Talk updates ✅')); } } diff --git a/lib/Chat/Parser/SystemMessage.php b/lib/Chat/Parser/SystemMessage.php index 8aa5364cc27..b94ab14bc27 100644 --- a/lib/Chat/Parser/SystemMessage.php +++ b/lib/Chat/Parser/SystemMessage.php @@ -843,6 +843,10 @@ protected function getGuest(Room $room, string $actorType, string $actorId): arr } protected function getGuestName(Room $room, string $actorType, string $actorId): string { + if ($actorId === Attendee::ACTOR_ID_CLI) { + return $this->l->t('Guest'); + } + try { $participant = $this->participantService->getParticipantByActor($room, $actorType, $actorId); $name = $participant->getAttendee()->getDisplayName(); diff --git a/lib/Chat/SystemMessage/Listener.php b/lib/Chat/SystemMessage/Listener.php index 4fb0eba8e62..22106d1577e 100644 --- a/lib/Chat/SystemMessage/Listener.php +++ b/lib/Chat/SystemMessage/Listener.php @@ -447,7 +447,7 @@ protected function sendSystemMessage(Room $room, string $message, array $paramet $actorId = $user->getUID(); } elseif (\OC::$CLI || $this->session->exists('talk-overwrite-actor-cli')) { $actorType = Attendee::ACTOR_GUESTS; - $actorId = 'cli'; + $actorId = Attendee::ACTOR_ID_CLI; } elseif ($this->session->exists('talk-overwrite-actor-type')) { $actorType = $this->session->get('talk-overwrite-actor-type'); $actorId = $this->session->get('talk-overwrite-actor-id'); diff --git a/lib/Model/Attendee.php b/lib/Model/Attendee.php index ffb84d9b57e..1487a3bcbf5 100644 --- a/lib/Model/Attendee.php +++ b/lib/Model/Attendee.php @@ -70,7 +70,11 @@ class Attendee extends Entity { public const ACTOR_BRIDGED = 'bridged'; public const ACTOR_BOTS = 'bots'; public const ACTOR_FEDERATED_USERS = 'federated_users'; + + // Special actor IDs public const ACTOR_BOT_PREFIX = 'bot-'; + public const ACTOR_ID_CLI = 'cli'; + public const ACTOR_ID_CHANGELOG = 'changelog'; public const PERMISSIONS_DEFAULT = 0; public const PERMISSIONS_CUSTOM = 1;