Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow logged in users to join public rooms again #296

Merged
merged 1 commit into from
Apr 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/Controller/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public function getRooms() {
*/
public function getRoom($token) {
try {
$room = $this->manager->getRoomByToken($token);
$room = $this->manager->getRoomForParticipantByToken($token, $this->userId);
return new JSONResponse($this->formatRoom($room));
} catch (RoomNotFoundException $e) {
return new JSONResponse([], Http::STATUS_NOT_FOUND);
Expand Down Expand Up @@ -509,7 +509,7 @@ public function makePrivate($roomId) {
*/
public function ping($token) {
try {
$room = $this->manager->getRoomByToken($token);
$room = $this->manager->getRoomForParticipantByToken($token, $this->userId);
} catch (RoomNotFoundException $e) {
return new JSONResponse([], Http::STATUS_NOT_FOUND);
}
Expand Down
23 changes: 17 additions & 6 deletions lib/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ public function getRoomsForParticipant($participant) {
}

/**
* Does *not* return public rooms for participants that have not been invited
*
* @param int $roomId
* @param string $participant
* @return Room
Expand All @@ -87,6 +89,7 @@ public function getRoomForParticipant($roomId, $participant) {
->where($query->expr()->eq('id', $query->createNamedParameter($roomId, IQueryBuilder::PARAM_INT)));

if ($participant !== null) {
// Non guest user
$query->leftJoin('r', 'spreedme_room_participants', 'p', $query->expr()->andX(
$query->expr()->eq('p.userId', $query->createNamedParameter($participant)),
$query->expr()->eq('p.roomId', 'r.id')
Expand All @@ -112,6 +115,9 @@ public function getRoomForParticipant($roomId, $participant) {
}

/**
* Also returns public rooms for participants that have not been invited,
* so they can join.
*
* @param string $token
* @param string $participant
* @return Room
Expand All @@ -121,14 +127,15 @@ public function getRoomForParticipantByToken($token, $participant) {
$query = $this->db->getQueryBuilder();
$query->select('*')
->from('spreedme_rooms', 'r')
->where($query->expr()->eq('token', $query->createNamedParameter($token)));
->where($query->expr()->eq('token', $query->createNamedParameter($token)))
->setMaxResults(1);

if ($participant !== null) {
// Non guest user
$query->leftJoin('r', 'spreedme_room_participants', 'p', $query->expr()->andX(
$query->expr()->eq('p.userId', $query->createNamedParameter($participant)),
$query->expr()->eq('p.roomId', 'r.id')
))
->andWhere($query->expr()->isNotNull('p.userId'));
));
}

$result = $query->execute();
Expand All @@ -141,11 +148,15 @@ public function getRoomForParticipantByToken($token, $participant) {

$room = new Room($this->db, $this->secureRandom, (int) $row['id'], (int) $row['type'], $row['token'], $row['name']);

if ($participant === null && $room->getType() !== Room::PUBLIC_CALL) {
throw new RoomNotFoundException();
if ($room->getType() === Room::PUBLIC_CALL) {
return $room;
}

return $room;
if ($participant !== null && $row['userId'] === $participant) {
return $room;
}

throw new RoomNotFoundException();
}

/**
Expand Down
19 changes: 16 additions & 3 deletions lib/Room.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,14 +171,22 @@ public function changeType($newType) {
* @param IUser $user
*/
public function addUser(IUser $user) {
$this->addParticipant($user->getUID());
}

/**
* @param string $participant
* @param string $sessionId
*/
public function addParticipant($participant, $sessionId = '0') {
$query = $this->db->getQueryBuilder();
$query->insert('spreedme_room_participants')
->values(
[
'userId' => $query->createNamedParameter($user->getUID()),
'userId' => $query->createNamedParameter($participant),
'roomId' => $query->createNamedParameter($this->getId()),
'lastPing' => $query->createNamedParameter(0, IQueryBuilder::PARAM_INT),
'sessionId' => $query->createNamedParameter('0'),
'sessionId' => $query->createNamedParameter($sessionId),
]
);
$query->execute();
Expand Down Expand Up @@ -208,7 +216,12 @@ public function enterRoomAsUser($userId) {

$sessionId = $this->secureRandom->generate(255);
$query->setParameter('sessionId', $sessionId);
$query->execute();
$result = $query->execute();

if ($result === 0) {
// User joining a public room, without being invited
$this->addParticipant($userId, $sessionId);
}

while (!$this->isSessionUnique($sessionId)) {
$sessionId = $this->secureRandom->generate(255);
Expand Down