Browse Source

Get participants from the room

Signed-off-by: Joas Schilling <coding@schilljs.com>
pull/81/head
Joas Schilling 9 years ago
parent
commit
018a62bb41
No known key found for this signature in database GPG Key ID: E166FD8976B3BAC8
  1. 36
      lib/Controller/ApiController.php
  2. 2
      lib/Manager.php
  3. 24
      lib/Room.php

36
lib/Controller/ApiController.php

@ -93,30 +93,29 @@ class ApiController extends Controller {
/** /**
* @param int $roomId * @param int $roomId
* @return array * @return array
* @deprecated
*/ */
private function getActivePeers($roomId) { private function getActivePeers($roomId) {
$qb = $this->dbConnection->getQueryBuilder();
return $qb->select('*')
->from('spreedme_room_participants')
->where($qb->expr()->eq('roomId', $qb->createNamedParameter($roomId)))
->andWhere($qb->expr()->gt('lastPing', $qb->createNamedParameter(time() - 10)))
->execute()
->fetchAll();
try {
$room = $this->manager->getRoomById($roomId);
} catch (RoomNotFoundException $e) {
return [];
}
return $room->getParticipants(time() - 10);
} }
/** /**
* Get all participants for a room
*
* @param int $roomId * @param int $roomId
* @return array * @return array
* @deprecated
*/ */
private function getRoomParticipants($roomId) { private function getRoomParticipants($roomId) {
$qb = $this->dbConnection->getQueryBuilder();
return $qb->select('*')
->from('spreedme_room_participants')
->where($qb->expr()->eq('roomId', $qb->createNamedParameter($roomId)))
->execute()
->fetchAll();
try {
$room = $this->manager->getRoomById($roomId);
} catch (RoomNotFoundException $e) {
return [];
}
return $room->getParticipants();
} }
/** /**
@ -230,7 +229,12 @@ class ApiController extends Controller {
* @return JSONResponse * @return JSONResponse
*/ */
public function getPeersInRoom($roomId) { public function getPeersInRoom($roomId) {
return new JSONResponse($this->getActivePeers($roomId));
try {
$room = $this->manager->getRoomById($roomId);
} catch (RoomNotFoundException $e) {
return new JSONResponse([], Http::STATUS_NOT_FOUND);
}
return new JSONResponse($room->getParticipants());
} }
/** /**

2
lib/Manager.php

@ -78,7 +78,7 @@ class Manager {
->values( ->values(
[ [
'name' => $query->createNamedParameter($name), 'name' => $query->createNamedParameter($name),
'type' => $query->createNamedParameter($type),
'type' => $query->createNamedParameter($type, IQueryBuilder::PARAM_INT),
] ]
); );
$query->execute(); $query->execute();

24
lib/Room.php

@ -22,6 +22,7 @@
namespace OCA\Spreed; namespace OCA\Spreed;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection; use OCP\IDBConnection;
use OCP\IUser; use OCP\IUser;
@ -85,9 +86,30 @@ class Room {
[ [
'userId' => $query->createNamedParameter($user), 'userId' => $query->createNamedParameter($user),
'roomId' => $query->createNamedParameter($this->getId()), 'roomId' => $query->createNamedParameter($this->getId()),
'lastPing' => $query->createNamedParameter('0'),
'lastPing' => $query->createNamedParameter(0, IQueryBuilder::PARAM_INT),
] ]
); );
$query->execute(); $query->execute();
} }
/**
* @param int $lastPing When the last ping is older than the given timestamp, the user is ignored
* @return array[] Array of users with [userId, roomId, lastPing]
*/
public function getParticipants($lastPing = 0) {
$query = $this->db->getQueryBuilder();
$query->select('*')
->from('spreedme_room_participants')
->where($query->expr()->eq('roomId', $query->createNamedParameter($this->getId(), IQueryBuilder::PARAM_INT)));
if ($lastPing > 0) {
$query->andWhere($query->expr()->gt('lastPing', $query->createNamedParameter($lastPing, IQueryBuilder::PARAM_INT)));
}
$result = $query->execute();
$rows = $result->fetchAll();
$result->closeCursor();
return $rows;
}
} }
Loading…
Cancel
Save