Skip to content

Commit

Permalink
Merge pull request #5258 from nextcloud/techdebt/noid/base-queries-on…
Browse files Browse the repository at this point in the history
…-sessions-where-it-makes-sense

Base queries on sessions where it makes sense
  • Loading branch information
nickvergessen authored Feb 24, 2021
2 parents 847aee9 + adc5996 commit 17afa97
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 23 deletions.
4 changes: 2 additions & 2 deletions lib/Controller/SignalingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -382,11 +382,11 @@ protected function getUsersInRoom(Room $room, int $pingTimestamp): array {
$timestamp = min($this->timeFactory->getTime() - (self::PULL_MESSAGES_TIMEOUT + 10), $pingTimestamp);
// "- 1" is needed because only the participants whose last ping is
// greater than the given timestamp are returned.
$participants = $this->participantService->getParticipantsForRoom($room);
$participants = $this->participantService->getParticipantsForAllSessions($room, $timestamp - 1);
foreach ($participants as $participant) {
$session = $participant->getSession();
if (!$session instanceof Session) {
// User is not active
// This is just to make Psalm happy, since we select by session it's always with one.
continue;
}

Expand Down
11 changes: 8 additions & 3 deletions lib/Service/ParticipantService.php
Original file line number Diff line number Diff line change
Expand Up @@ -603,22 +603,27 @@ public function getParticipantsForRoom(Room $room): array {

/**
* @param Room $room
* @param int $maxAge
* @return Participant[]
*/
public function getParticipantsWithSession(Room $room): array {
public function getParticipantsForAllSessions(Room $room, int $maxAge = 0): array {
$query = $this->connection->getQueryBuilder();

$helper = new SelectHelper();
$helper->selectAttendeesTable($query);
$helper->selectSessionsTable($query);
$query->from('talk_attendees', 'a')
$query->from('talk_sessions', 's')
->leftJoin(
'a', 'talk_sessions', 's',
's', 'talk_attendees', 'a',
$query->expr()->eq('s.attendee_id', 'a.id')
)
->where($query->expr()->eq('a.room_id', $query->createNamedParameter($room->getId(), IQueryBuilder::PARAM_INT)))
->andWhere($query->expr()->isNotNull('s.id'));

if ($maxAge > 0) {
$query->andWhere($query->expr()->gt('s.last_ping', $query->createNamedParameter($maxAge, IQueryBuilder::PARAM_INT)));
}

return $this->getParticipantsFromQuery($query, $room);
}

Expand Down
32 changes: 15 additions & 17 deletions lib/Signaling/BackendNotifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -311,38 +311,36 @@ public function roomInCallChanged(Room $room, int $flags, array $sessionIds): vo
$changed = [];
$users = [];

$participants = $this->participantService->getParticipantsForRoom($room);
$participants = $this->participantService->getParticipantsForAllSessions($room);
foreach ($participants as $participant) {
$session = $participant->getSession();
if (!$session instanceof Session) {
continue;
}

$attendee = $participant->getAttendee();
if ($attendee->getActorType() !== Attendee::ACTOR_USERS
&& $attendee->getActorType() !== Attendee::ACTOR_GUESTS) {
continue;
}

$data = [
'inCall' => Participant::FLAG_DISCONNECTED,
'lastPing' => 0,
'sessionId' => '0',
'inCall' => $session->getInCall(),
'lastPing' => $session->getLastPing(),
'sessionId' => $session->getSessionId(),
'nextcloudSessionId' => $session->getSessionId(),
'participantType' => $attendee->getParticipantType(),
];
if ($attendee->getActorType() === Attendee::ACTOR_USERS) {
$data['userId'] = $attendee->getActorId();
}

$session = $participant->getSession();
if ($session instanceof Session) {
$data['inCall'] = $session->getInCall();
$data['lastPing'] = $session->getLastPing();
$data['sessionId'] = $session->getSessionId();
$data['nextcloudSessionId'] = $session->getSessionId();

if ($session->getInCall() !== Participant::FLAG_DISCONNECTED) {
$users[] = $data;
}
if ($session->getInCall() !== Participant::FLAG_DISCONNECTED) {
$users[] = $data;
}

if (\in_array($session->getSessionId(), $sessionIds, true)) {
$changed[] = $data;
}
if (\in_array($session->getSessionId(), $sessionIds, true)) {
$changed[] = $data;
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Signaling/Messages.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public function addMessageForAllParticipants(Room $room, string $message): void
]
);

$participants = $this->participantService->getParticipantsWithSession($room);
$participants = $this->participantService->getParticipantsForAllSessions($room);
foreach ($participants as $participant) {
$session = $participant->getSession();
if ($session instanceof Session) {
Expand Down

0 comments on commit 17afa97

Please sign in to comment.