Browse Source

Fix selecting the session when there could be multiple

Signed-off-by: Joas Schilling <coding@schilljs.com>
pull/5194/head
Joas Schilling 5 years ago
parent
commit
6ad31df01b
No known key found for this signature in database GPG Key ID: 7076EA9751AACDDA
  1. 4
      lib/Controller/RoomController.php
  2. 2
      lib/Manager.php
  3. 13
      lib/Model/SelectHelper.php
  4. 17
      lib/Room.php
  5. 37
      lib/Service/ParticipantService.php

4
lib/Controller/RoomController.php

@ -476,7 +476,7 @@ class RoomController extends AEnvironmentAwareController {
$numActiveGuests = 0;
$cleanGuests = false;
$participantList = [];
$participants = $this->participantService->getParticipantsForRoom($room, true); // FIXME NEEDS the session but can potentially kill APIv1?
$participants = $this->participantService->getSessionsAndParticipantsForRoom($room); // FIXME NEEDS the session but can potentially kill APIv1?
uasort($participants, function (Participant $participant1, Participant $participant2) {
$s1 = $participant1->getSession() ? $participant1->getSession()->getLastPing() : 0;
$s2 = $participant2->getSession() ? $participant2->getSession()->getLastPing() : 0;
@ -1110,7 +1110,7 @@ class RoomController extends AEnvironmentAwareController {
}
$maxPingAge = $this->timeFactory->getTime() - 100;
$participants = $this->participantService->getParticipantsForRoom($this->room, true);
$participants = $this->participantService->getSessionsAndParticipantsForRoom($this->room);
$results = $headers = $statuses = [];
if ($this->userId !== null

2
lib/Manager.php

@ -317,7 +317,7 @@ class Manager {
->where($query->expr()->isNotNull('a.id'));
if ($loadSession) {
$helper->selectSessionsTable($query);
$helper->selectSessionsTable($query); // FIXME ?
$query->leftJoin('a', 'talk_sessions', 's', $query->expr()->andX(
$query->expr()->eq('a.id', 's.attendee_id')
));

13
lib/Model/SelectHelper.php

@ -83,4 +83,17 @@ class SelectHelper {
->addSelect($alias . 'last_ping')
->selectAlias($alias . 'id', 's_id');
}
public function selectSessionsTableMax(IQueryBuilder $query, string $alias = 's'): void {
if ($alias !== '') {
$alias .= '.';
}
$query->selectAlias($query->func()->max($alias . 'attendee_id'), 'attendee_id')
->selectAlias($query->func()->max($alias . 'session_id'), 'session_id')
// BIT_OR would be better, but SQLite does not support something like it.
->selectAlias($query->func()->max($alias . 'in_call'), 'in_call')
->selectAlias($query->func()->max($alias . 'last_ping'), 'last_ping')
->selectAlias($query->func()->max($alias . 'id'), 's_id');
}
}

17
lib/Room.php

@ -432,13 +432,14 @@ class Room {
->setMaxResults(1);
if ($sessionId !== false) {
$helper->selectSessionsTable($query);
if ($sessionId !== null) {
$helper->selectSessionsTable($query);
$query->leftJoin('a', 'talk_sessions', 's', $query->expr()->andX(
$query->expr()->eq('s.session_id', $query->createNamedParameter($sessionId)),
$query->expr()->eq('a.id', 's.attendee_id')
));
} else {
$helper->selectSessionsTable($query); // FIXME PROBLEM
$query->leftJoin('a', 'talk_sessions', 's', $query->expr()->eq('a.id', 's.attendee_id'));
}
}
@ -499,7 +500,7 @@ class Room {
$helper = new SelectHelper();
$helper->selectAttendeesTable($query);
$query->from('talk_attendees', 'a')
->andWhere($query->expr()->eq('a.pin', $query->createNamedParameter($pin)))
->where($query->expr()->eq('a.pin', $query->createNamedParameter($pin)))
->andWhere($query->expr()->eq('a.room_id', $query->createNamedParameter($this->getId())))
->setMaxResults(1);
$result = $query->execute();
@ -526,18 +527,20 @@ class Room {
$helper = new SelectHelper();
$helper->selectAttendeesTable($query);
$query->from('talk_attendees', 'a')
->andWhere($query->expr()->eq('a.id', $query->createNamedParameter($attendeeId, IQueryBuilder::PARAM_INT)))
->where($query->expr()->eq('a.id', $query->createNamedParameter($attendeeId, IQueryBuilder::PARAM_INT)))
->andWhere($query->expr()->eq('a.room_id', $query->createNamedParameter($this->getId())))
->setMaxResults(1);
if ($sessionId !== false) {
$helper->selectSessionsTable($query);
if ($sessionId !== null) {
$helper->selectSessionsTable($query);
$query->leftJoin('a', 'talk_sessions', 's', $query->expr()->andX(
$query->expr()->eq('s.session_id', $query->createNamedParameter($sessionId)),
$query->expr()->eq('a.id', 's.attendee_id')
));
} else {
$helper->selectSessionsTableMax($query);
$query->groupBy('a.id');
$query->leftJoin('a', 'talk_sessions', 's', $query->expr()->eq('a.id', 's.attendee_id'));
}
}
@ -569,7 +572,7 @@ class Room {
$query = $this->db->getQueryBuilder();
$helper = new SelectHelper();
$helper->selectSessionsTable($query);
$helper->selectAttendeesTable($query);
$query->from('talk_attendees', 'a')
->andWhere($query->expr()->eq('a.actor_type', $query->createNamedParameter($actorType)))
->andWhere($query->expr()->eq('a.actor_id', $query->createNamedParameter($actorId)))
@ -577,13 +580,15 @@ class Room {
->setMaxResults(1);
if ($sessionId !== false) {
$helper->selectSessionsTable($query);
if ($sessionId !== null) {
$helper->selectSessionsTable($query);
$query->leftJoin('a', 'talk_sessions', 's', $query->expr()->andX(
$query->expr()->eq('s.session_id', $query->createNamedParameter($sessionId)),
$query->expr()->eq('a.id', 's.attendee_id')
));
} else {
$helper->selectSessionsTableMax($query);
$query->groupBy('a.id');
$query->leftJoin('a', 'talk_sessions', 's', $query->expr()->eq('a.id', 's.attendee_id'));
}
}

37
lib/Service/ParticipantService.php

@ -583,10 +583,9 @@ class ParticipantService {
/**
* @param Room $room
* @param bool $loadSession Loads a random session if possible for the users
* @return Participant[]
*/
public function getParticipantsForRoom(Room $room, bool $loadSession = false): array {
public function getParticipantsForRoom(Room $room): array {
$query = $this->connection->getQueryBuilder();
$helper = new SelectHelper();
@ -594,13 +593,30 @@ class ParticipantService {
$query->from('talk_attendees', 'a')
->where($query->expr()->eq('a.room_id', $query->createNamedParameter($room->getId(), IQueryBuilder::PARAM_INT)));
if ($loadSession) {
$helper->selectSessionsTable($query);
$query->leftJoin(
return $this->getParticipantsFromQuery($query, $room);
}
/**
* Get all sessions and attendees without a session for the room
*
* This will return multiple items for the same attendee if the attendee
* has multiple sessions in the room.
*
* @param Room $room
* @return Participant[]
*/
public function getSessionsAndParticipantsForRoom(Room $room): array {
$query = $this->connection->getQueryBuilder();
$helper = new SelectHelper();
$helper->selectAttendeesTable($query);
$helper->selectSessionsTable($query);
$query->from('talk_attendees', 'a')
->leftJoin(
'a', 'talk_sessions', 's',
$query->expr()->eq('s.attendee_id', 'a.id')
);
}
)
->where($query->expr()->eq('a.room_id', $query->createNamedParameter($room->getId(), IQueryBuilder::PARAM_INT)));
return $this->getParticipantsFromQuery($query, $room);
}
@ -622,7 +638,7 @@ class ParticipantService {
$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'));
->andWhere($query->expr()->isNotNull('a.id'));
if ($maxAge > 0) {
$query->andWhere($query->expr()->gt('s.last_ping', $query->createNamedParameter($maxAge, IQueryBuilder::PARAM_INT)));
@ -667,7 +683,7 @@ class ParticipantService {
$helper = new SelectHelper();
$helper->selectAttendeesTable($query);
$helper->selectSessionsTable($query);
$helper->selectSessionsTableMax($query);
$query->from('talk_attendees', 'a')
// Currently we only care if the user has a session at all, so we can select any: #ThisIsFine
->leftJoin(
@ -675,7 +691,8 @@ class ParticipantService {
$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()->eq('a.notification_level', $query->createNamedParameter($notificationLevel, IQueryBuilder::PARAM_INT)));
->andWhere($query->expr()->eq('a.notification_level', $query->createNamedParameter($notificationLevel, IQueryBuilder::PARAM_INT)))
->groupBy('a.id');
return $this->getParticipantsFromQuery($query, $room);
}

Loading…
Cancel
Save