From 021dc333dc73e168633eef6c260c1e77e11dde56 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 9 Nov 2018 14:55:49 +0100 Subject: [PATCH] Use Participant objects were possible Signed-off-by: Joas Schilling --- lib/Controller/CallController.php | 27 ++------ lib/Controller/RoomController.php | 90 ++++++++++++-------------- lib/Controller/SignalingController.php | 25 ++----- lib/Room.php | 32 ++++++++- lib/Signaling/BackendNotifier.php | 4 +- 5 files changed, 84 insertions(+), 94 deletions(-) diff --git a/lib/Controller/CallController.php b/lib/Controller/CallController.php index a46eae640c..82ef44a986 100644 --- a/lib/Controller/CallController.php +++ b/lib/Controller/CallController.php @@ -93,34 +93,19 @@ class CallController extends OCSController { return new DataResponse([], Http::STATUS_NOT_FOUND); } - /** @var array[] $participants */ - $participants = $room->getParticipants(time() - 30); $result = []; - foreach ($participants['users'] as $participant => $data) { - if ($data['sessionId'] === '0' || !$data['inCall']) { - // User is not active in call - continue; - } - - $result[] = [ - 'userId' => (string) $participant, - 'token' => $token, - 'lastPing' => $data['lastPing'], - 'sessionId' => $data['sessionId'], - ]; - } - - foreach ($participants['guests'] as $data) { - if (!$data['inCall']) { + $participants = $room->getParticipants(time() - 30); + foreach ($participants as $participant) { + if ($participant->getSessionId() === '0' || $participant->getInCallFlags() === Participant::FLAG_DISCONNECTED) { // User is not active in call continue; } $result[] = [ - 'userId' => '', + 'userId' => $participant->getUser(), 'token' => $token, - 'lastPing' => $data['lastPing'], - 'sessionId' => $data['sessionId'], + 'lastPing' => $participant->getLastPing(), + 'sessionId' => $participant->getSessionId(), ]; } diff --git a/lib/Controller/RoomController.php b/lib/Controller/RoomController.php index b8277877a9..ed465c7b9b 100644 --- a/lib/Controller/RoomController.php +++ b/lib/Controller/RoomController.php @@ -48,7 +48,6 @@ use OCP\IUserManager; use OCP\IGroup; use OCP\IGroupManager; use OCP\Mail\IMailer; -use OCP\Share; class RoomController extends OCSController { /** @var string */ @@ -166,16 +165,16 @@ class RoomController extends OCSController { /** * @param Room $room - * @param Participant $participant + * @param Participant $currentParticipant * @return array * @throws RoomNotFoundException */ - protected function formatRoom(Room $room, Participant $participant = null): array { + protected function formatRoom(Room $room, Participant $currentParticipant = null): array { - if ($participant instanceof Participant) { - $participantType = $participant->getParticipantType(); - $participantFlags = $participant->getInCallFlags(); - $favorite = $participant->isFavorite(); + if ($currentParticipant instanceof Participant) { + $participantType = $currentParticipant->getParticipantType(); + $participantFlags = $currentParticipant->getInCallFlags(); + $favorite = $currentParticipant->isFavorite(); } else { $participantType = Participant::GUEST; $participantFlags = Participant::FLAG_DISCONNECTED; @@ -218,12 +217,12 @@ class RoomController extends OCSController { 'lastMessage' => [], ]; - if (!$participant instanceof Participant) { + if (!$currentParticipant instanceof Participant) { return $roomData; } - if ($participant->getNotificationLevel() !== Participant::NOTIFY_DEFAULT) { - $roomData['notificationLevel'] = $participant->getNotificationLevel(); + if ($currentParticipant->getNotificationLevel() !== Participant::NOTIFY_DEFAULT) { + $roomData['notificationLevel'] = $currentParticipant->getNotificationLevel(); } if ($room->getObjectType() === 'share:password') { @@ -234,54 +233,45 @@ class RoomController extends OCSController { $currentUser = $this->userManager->get($this->userId); if ($currentUser instanceof IUser) { $unreadSince = $this->chatManager->getUnreadMarker($room, $currentUser); - if ($participant instanceof Participant) { - $lastMention = $participant->getLastMention(); + if ($currentParticipant instanceof Participant) { + $lastMention = $currentParticipant->getLastMention(); $roomData['unreadMention'] = $lastMention !== null && $unreadSince < $lastMention; } $roomData['unreadMessages'] = $this->chatManager->getUnreadCount($room, $unreadSince); } - // Sort by lastPing - /** @var array[] $participants */ - $participants = $room->getParticipants(); - $sortParticipants = function(array $participant1, array $participant2) { - return $participant2['lastPing'] - $participant1['lastPing']; - }; - uasort($participants['users'], $sortParticipants); - uasort($participants['guests'], $sortParticipants); - + $numActiveGuests = 0; + $cleanGuests = false; $participantList = []; - foreach ($participants['users'] as $userId => $data) { - $user = $this->userManager->get((string) $userId); - if ($user instanceof IUser) { - $participantList[(string)$user->getUID()] = [ - 'name' => $user->getDisplayName(), - 'type' => $data['participantType'], - 'call' => $data['inCall'], - 'sessionId' => $user->getSessionId(), - ]; - - try { - $participant = $room->getParticipant((string)$user->getUID()); - $participantList[(string)$user->getUID()]['participantFlags'] = $participant->getInCallFlags(); - } catch (ParticipantNotFoundException $e) { - // In case this happens we remove the user from the participants list - unset($participantList[(string)$user->getUID()]); - } - } + $participants = $room->getParticipants(); + uasort($participants, function(Participant $participant1, Participant $participant2) { + return $participant2->getLastPing() - $participant1->getLastPing(); + }); + foreach ($participants as $participant) { + if ($participant->isGuest()) { + if ($participant->getLastPing() <= time() - 100) { + $cleanGuests = true; + } else { + $numActiveGuests++; + } + } else { + $user = $this->userManager->get($participant->getUser()); + if ($user instanceof IUser) { + $participantList[(string)$user->getUID()] = [ + 'name' => $user->getDisplayName(), + 'type' => $participant->getParticipantType(), + 'call' => $participant->getInCallFlags(), + ]; + } - if ($data['sessionId'] !== '0' && $data['lastPing'] <= time() - 100) { - $room->leaveRoom((string) $userId); + if ($participant->getSessionId() !== '0' && $participant->getLastPing() <= time() - 100) { + $room->leaveRoom($participant->getUser()); + } } } - $activeGuests = array_filter($participants['guests'], function($data) { - return $data['lastPing'] > time() - 100; - }); - - $numActiveGuests = \count($activeGuests); - if ($numActiveGuests !== \count($participants['guests'])) { + if ($cleanGuests) { $room->cleanGuestParticipants(); } @@ -293,8 +283,8 @@ class RoomController extends OCSController { } $roomData = array_merge($roomData, [ - 'lastPing' => $participant->getLastPing(), - 'sessionId' => $participant->getSessionId(), + 'lastPing' => $currentParticipant->getLastPing(), + 'sessionId' => $currentParticipant->getSessionId(), 'participants' => $participantList, 'numGuests' => $numActiveGuests, 'lastMessage' => $lastMessage, @@ -715,7 +705,7 @@ class RoomController extends OCSController { return new DataResponse([], Http::STATUS_FORBIDDEN); } - $participants = $room->getParticipants(); + $participants = $room->getParticipantsLegacy(); $results = []; foreach ($participants['users'] as $userId => $participant) { diff --git a/lib/Controller/SignalingController.php b/lib/Controller/SignalingController.php index 45e2c2a63a..ad6fac52ae 100644 --- a/lib/Controller/SignalingController.php +++ b/lib/Controller/SignalingController.php @@ -210,32 +210,21 @@ class SignalingController extends OCSController { * @param Room $room * @return array[] */ - protected function getUsersInRoom(Room $room) { + protected function getUsersInRoom(Room $room): array { $usersInRoom = []; $participants = $room->getParticipants(time() - 30); - - foreach ($participants['users'] as $participant => $data) { - if ($data['sessionId'] === '0') { + foreach ($participants as $participant) { + if ($participant->getSessionId() === '0') { // User is not active continue; } $usersInRoom[] = [ - 'userId' => $participant, - 'roomId' => $room->getId(), - 'lastPing' => $data['lastPing'], - 'sessionId' => $data['sessionId'], - 'inCall' => $data['inCall'], - ]; - } - - foreach ($participants['guests'] as $data) { - $usersInRoom[] = [ - 'userId' => '', + 'userId' => $participant->getUser(), 'roomId' => $room->getId(), - 'lastPing' => $data['lastPing'], - 'sessionId' => $data['sessionId'], - 'inCall' => $data['inCall'], + 'lastPing' => $participant->getLastPing(), + 'sessionId' => $participant->getSessionId(), + 'inCall' => $participant->getInCallFlags(), ]; } diff --git a/lib/Room.php b/lib/Room.php index 379125e246..c025f3e792 100644 --- a/lib/Room.php +++ b/lib/Room.php @@ -272,7 +272,7 @@ class Room { } public function deleteRoom() { - $participants = $this->getParticipants(); + $participants = $this->getParticipantsLegacy(); $this->dispatcher->dispatch(self::class . '::preDeleteRoom', new GenericEvent($this, [ 'participants' => $participants, ])); @@ -785,11 +785,37 @@ class Room { $this->dispatcher->dispatch(self::class . '::postCleanGuests', new GenericEvent($this)); } + /** + * @param int $lastPing When the last ping is older than the given timestamp, the user is ignored + * @return Participant[] + */ + public function getParticipants($lastPing = 0): array { + $query = $this->db->getQueryBuilder(); + $query->select('*') + ->from('talk_participants') + ->where($query->expr()->eq('room_id', $query->createNamedParameter($this->getId(), IQueryBuilder::PARAM_INT))); + + if ($lastPing > 0) { + $query->andWhere($query->expr()->gt('last_ping', $query->createNamedParameter($lastPing, IQueryBuilder::PARAM_INT))); + } + + $result = $query->execute(); + + $participants = []; + while ($row = $result->fetch()) { + $participants[] = $this->manager->createParticipantObject($this, $row); + } + $result->closeCursor(); + + return $participants; + } + /** * @param int $lastPing When the last ping is older than the given timestamp, the user is ignored * @return array[] Array of users with [users => [userId => [lastPing, sessionId]], guests => [[lastPing, sessionId]]] + * @deprecated Use self::getParticipants() instead */ - public function getParticipants($lastPing = 0) { + public function getParticipantsLegacy($lastPing = 0): array { $query = $this->db->getQueryBuilder(); $query->select('*') ->from('talk_participants') @@ -833,7 +859,7 @@ class Room { */ public function getParticipantUserIds(int $lastPing = 0): array { $query = $this->db->getQueryBuilder(); - $query->select('*') + $query->select('user_id') ->from('talk_participants') ->where($query->expr()->eq('room_id', $query->createNamedParameter($this->getId(), IQueryBuilder::PARAM_INT))) ->andWhere($query->expr()->nonEmptyString('user_id')); diff --git a/lib/Signaling/BackendNotifier.php b/lib/Signaling/BackendNotifier.php index 9fd5403fc1..667e2495f2 100644 --- a/lib/Signaling/BackendNotifier.php +++ b/lib/Signaling/BackendNotifier.php @@ -235,7 +235,7 @@ class BackendNotifier{ $this->logger->info('Room participants modified: ' . $room->getToken() . ' ' . print_r($sessionIds, true), ['app' => 'spreed']); $changed = []; $users = []; - $participants = $room->getParticipants(); + $participants = $room->getParticipantsLegacy(); foreach ($participants['users'] as $userId => $participant) { $participant['userId'] = $userId; $users[] = $participant; @@ -273,7 +273,7 @@ class BackendNotifier{ $this->logger->info('Room in-call status changed: ' . $room->getToken() . ' ' . $flags . ' ' . print_r($sessionIds, true), ['app' => 'spreed']); $changed = []; $users = []; - $participants = $room->getParticipants(); + $participants = $room->getParticipantsLegacy(); foreach ($participants['users'] as $userId => $participant) { $participant['userId'] = $userId; if ($participant['inCall'] !== Participant::FLAG_DISCONNECTED) {