From 9a599f12ce266fbac430be412c4e1bdb1a4cb87e Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Tue, 22 Sep 2020 11:07:37 +0200 Subject: Allow to preload a user when getting the room by token Signed-off-by: Joas Schilling --- lib/Manager.php | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) (limited to 'lib/Manager.php') diff --git a/lib/Manager.php b/lib/Manager.php index 944d1c333..d372f8fd8 100644 --- a/lib/Manager.php +++ b/lib/Manager.php @@ -446,14 +446,25 @@ class Manager { /** * @param string $token + * @param string|null $preloadParticipant Load this participants information if possible * @return Room * @throws RoomNotFoundException */ - public function getRoomByToken(string $token): Room { + public function getRoomByToken(string $token, ?string $preloadParticipant = null): Room { + $preloadParticipant = $preloadParticipant === '' ? null : $preloadParticipant; + $query = $this->db->getQueryBuilder(); - $query->select('*') - ->from('talk_rooms') - ->where($query->expr()->eq('token', $query->createNamedParameter($token))); + $query->select('r.*') + ->from('talk_rooms', 'r') + ->where($query->expr()->eq('r.token', $query->createNamedParameter($token))); + + if ($preloadParticipant !== null) { + $query->addSelect('p.*') + ->leftJoin('r', 'talk_participants', 'p', $query->expr()->andX( + $query->expr()->eq('p.user_id', $query->createNamedParameter($preloadParticipant)), + $query->expr()->eq('p.room_id', 'r.id') + )); + } $result = $query->execute(); $row = $result->fetch(); @@ -468,7 +479,12 @@ class Manager { throw new RoomNotFoundException(); } - return $this->createRoomObject($row); + $room = $this->createRoomObject($row); + if ($preloadParticipant !== null && isset($row['user_id'])) { + $room->setParticipant($row['user_id'], $this->createParticipantObject($room, $row)); + } + + return $room; } /** -- cgit v1.2.3