2016-10-19 13:00:17 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2016 Lukas Reschke <lukas@statuscode.ch>
|
2016-11-11 18:00:14 +03:00
|
|
|
* @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com>
|
2016-10-19 13:00:17 +03:00
|
|
|
*
|
2016-11-14 17:34:51 +03:00
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
|
|
|
*
|
2016-10-19 13:00:17 +03:00
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\Spreed;
|
|
|
|
|
2017-09-11 18:17:13 +03:00
|
|
|
use OCA\Spreed\Exceptions\InvalidPasswordException;
|
2017-09-22 15:31:34 +03:00
|
|
|
use OCA\Spreed\Exceptions\ParticipantNotFoundException;
|
2016-11-14 13:54:54 +03:00
|
|
|
use OCP\DB\QueryBuilder\IQueryBuilder;
|
2016-11-11 18:12:45 +03:00
|
|
|
use OCP\IDBConnection;
|
|
|
|
use OCP\IUser;
|
2017-09-21 11:58:20 +03:00
|
|
|
use OCP\Security\IHasher;
|
2016-12-06 16:14:03 +03:00
|
|
|
use OCP\Security\ISecureRandom;
|
2017-09-14 11:21:59 +03:00
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
|
|
|
use Symfony\Component\EventDispatcher\GenericEvent;
|
2016-11-11 18:12:45 +03:00
|
|
|
|
2016-10-19 13:00:17 +03:00
|
|
|
class Room {
|
|
|
|
const ONE_TO_ONE_CALL = 1;
|
2016-11-09 16:08:46 +03:00
|
|
|
const GROUP_CALL = 2;
|
2016-11-17 18:15:55 +03:00
|
|
|
const PUBLIC_CALL = 3;
|
2016-11-11 18:00:14 +03:00
|
|
|
|
2016-11-11 18:12:45 +03:00
|
|
|
/** @var IDBConnection */
|
|
|
|
private $db;
|
2016-12-06 16:14:03 +03:00
|
|
|
/** @var ISecureRandom */
|
|
|
|
private $secureRandom;
|
2017-09-14 11:21:59 +03:00
|
|
|
/** @var EventDispatcherInterface */
|
|
|
|
private $dispatcher;
|
2017-09-21 11:58:20 +03:00
|
|
|
/** @var IHasher */
|
|
|
|
private $hasher;
|
2016-11-11 18:12:45 +03:00
|
|
|
|
|
|
|
/** @var int */
|
|
|
|
private $id;
|
|
|
|
/** @var int */
|
|
|
|
private $type;
|
|
|
|
/** @var string */
|
2017-03-14 16:13:29 +03:00
|
|
|
private $token;
|
|
|
|
/** @var string */
|
2016-11-11 18:12:45 +03:00
|
|
|
private $name;
|
2017-09-11 18:17:13 +03:00
|
|
|
/** @var string */
|
|
|
|
private $password;
|
2016-11-11 18:12:45 +03:00
|
|
|
|
2017-07-10 17:50:07 +03:00
|
|
|
/** @var string */
|
|
|
|
protected $currentUser;
|
2017-07-08 12:12:41 +03:00
|
|
|
/** @var Participant */
|
|
|
|
protected $participant;
|
|
|
|
|
2016-11-11 18:12:45 +03:00
|
|
|
/**
|
|
|
|
* Room constructor.
|
|
|
|
*
|
|
|
|
* @param IDBConnection $db
|
2016-12-06 16:14:03 +03:00
|
|
|
* @param ISecureRandom $secureRandom
|
2017-09-14 11:21:59 +03:00
|
|
|
* @param EventDispatcherInterface $dispatcher
|
2017-09-21 11:58:20 +03:00
|
|
|
* @param IHasher $hasher
|
2016-11-11 18:12:45 +03:00
|
|
|
* @param int $id
|
|
|
|
* @param int $type
|
2017-03-14 16:13:29 +03:00
|
|
|
* @param string $token
|
2016-11-11 18:12:45 +03:00
|
|
|
* @param string $name
|
2017-09-11 18:17:13 +03:00
|
|
|
* @param string $password
|
2016-11-11 18:12:45 +03:00
|
|
|
*/
|
2017-09-21 11:58:20 +03:00
|
|
|
public function __construct(IDBConnection $db, ISecureRandom $secureRandom, EventDispatcherInterface $dispatcher, IHasher $hasher, $id, $type, $token, $name, $password) {
|
2016-11-11 18:12:45 +03:00
|
|
|
$this->db = $db;
|
2016-12-06 16:14:03 +03:00
|
|
|
$this->secureRandom = $secureRandom;
|
2017-09-14 11:21:59 +03:00
|
|
|
$this->dispatcher = $dispatcher;
|
2017-09-21 11:58:20 +03:00
|
|
|
$this->hasher = $hasher;
|
2016-11-11 18:00:14 +03:00
|
|
|
$this->id = $id;
|
|
|
|
$this->type = $type;
|
2017-03-14 16:13:29 +03:00
|
|
|
$this->token = $token;
|
2016-11-11 18:00:14 +03:00
|
|
|
$this->name = $name;
|
2017-09-11 18:17:13 +03:00
|
|
|
$this->password = $password;
|
2016-11-11 18:00:14 +03:00
|
|
|
}
|
|
|
|
|
2016-11-11 18:12:45 +03:00
|
|
|
/**
|
|
|
|
* @return int
|
|
|
|
*/
|
2016-11-11 18:00:14 +03:00
|
|
|
public function getId() {
|
|
|
|
return $this->id;
|
|
|
|
}
|
|
|
|
|
2016-11-11 18:12:45 +03:00
|
|
|
/**
|
|
|
|
* @return int
|
|
|
|
*/
|
2016-11-11 18:00:14 +03:00
|
|
|
public function getType() {
|
|
|
|
return $this->type;
|
|
|
|
}
|
|
|
|
|
2017-03-14 16:13:29 +03:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getToken() {
|
|
|
|
return $this->token;
|
|
|
|
}
|
|
|
|
|
2016-11-11 18:12:45 +03:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-11-11 18:00:14 +03:00
|
|
|
public function getName() {
|
|
|
|
return $this->name;
|
|
|
|
}
|
2016-11-11 18:12:45 +03:00
|
|
|
|
2017-09-11 18:17:13 +03:00
|
|
|
/**
|
2017-09-21 11:58:20 +03:00
|
|
|
* @return bool
|
2017-09-11 18:17:13 +03:00
|
|
|
*/
|
2017-09-21 11:58:20 +03:00
|
|
|
public function hasPassword() {
|
|
|
|
return $this->password !== '';
|
2017-09-11 18:17:13 +03:00
|
|
|
}
|
|
|
|
|
2017-07-08 12:12:41 +03:00
|
|
|
/**
|
2017-07-10 17:50:07 +03:00
|
|
|
* @param string $userId
|
2017-07-08 12:12:41 +03:00
|
|
|
* @param Participant $participant
|
|
|
|
*/
|
2017-07-10 17:50:07 +03:00
|
|
|
public function setParticipant($userId, Participant $participant) {
|
|
|
|
$this->currentUser = $userId;
|
2017-07-08 12:12:41 +03:00
|
|
|
$this->participant = $participant;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $userId
|
|
|
|
* @return Participant
|
2017-09-22 15:31:34 +03:00
|
|
|
* @throws ParticipantNotFoundException When the user is not a participant
|
2017-07-08 12:12:41 +03:00
|
|
|
*/
|
|
|
|
public function getParticipant($userId) {
|
2017-07-10 17:50:07 +03:00
|
|
|
if (!is_string($userId) || $userId === '') {
|
2017-09-22 15:31:34 +03:00
|
|
|
throw new ParticipantNotFoundException('Not a user');
|
2017-07-10 17:50:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->currentUser === $userId && $this->participant instanceof Participant) {
|
2017-07-08 12:12:41 +03:00
|
|
|
return $this->participant;
|
|
|
|
}
|
|
|
|
|
|
|
|
$query = $this->db->getQueryBuilder();
|
|
|
|
$query->select('*')
|
|
|
|
->from('spreedme_room_participants')
|
|
|
|
->where($query->expr()->eq('userId', $query->createNamedParameter($userId)))
|
|
|
|
->andWhere($query->expr()->eq('roomId', $query->createNamedParameter($this->getId())));
|
|
|
|
$result = $query->execute();
|
|
|
|
$row = $result->fetch();
|
|
|
|
$result->closeCursor();
|
|
|
|
|
|
|
|
if ($row === false) {
|
2017-09-22 15:31:34 +03:00
|
|
|
throw new ParticipantNotFoundException('User is not a participant');
|
2017-07-08 12:12:41 +03:00
|
|
|
}
|
|
|
|
|
2017-07-10 17:50:07 +03:00
|
|
|
if ($this->currentUser === $userId) {
|
|
|
|
$this->participant = new Participant($this->db, $this, $row['userId'], (int) $row['participantType'], (int) $row['lastPing'], $row['sessionId']);
|
|
|
|
return $this->participant;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Participant($this->db, $this, $row['userId'], (int) $row['participantType'], (int) $row['lastPing'], $row['sessionId']);
|
2017-07-08 12:12:41 +03:00
|
|
|
}
|
|
|
|
|
2017-09-19 17:46:54 +03:00
|
|
|
/**
|
|
|
|
* @param string $sessionId
|
|
|
|
* @return Participant
|
2017-09-22 15:31:34 +03:00
|
|
|
* @throws ParticipantNotFoundException When the user is not a participant
|
2017-09-19 17:46:54 +03:00
|
|
|
*/
|
|
|
|
public function getParticipantBySession($sessionId) {
|
|
|
|
if (!is_string($sessionId) || $sessionId === '') {
|
2017-09-22 15:31:34 +03:00
|
|
|
throw new ParticipantNotFoundException('Not a user');
|
2017-09-19 17:46:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$query = $this->db->getQueryBuilder();
|
|
|
|
$query->select('*')
|
|
|
|
->from('spreedme_room_participants')
|
|
|
|
->where($query->expr()->eq('sessionId', $query->createNamedParameter($sessionId)))
|
|
|
|
->andWhere($query->expr()->eq('roomId', $query->createNamedParameter($this->getId())));
|
|
|
|
$result = $query->execute();
|
|
|
|
$row = $result->fetch();
|
|
|
|
$result->closeCursor();
|
|
|
|
|
|
|
|
if ($row === false) {
|
2017-09-22 15:31:34 +03:00
|
|
|
throw new ParticipantNotFoundException('User is not a participant');
|
2017-09-19 17:46:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return new Participant($this->db, $this, $row['userId'], (int) $row['participantType'], (int) $row['lastPing'], $row['sessionId']);
|
|
|
|
}
|
|
|
|
|
2016-11-14 15:57:56 +03:00
|
|
|
public function deleteRoom() {
|
2017-09-25 14:20:51 +03:00
|
|
|
$this->dispatcher->dispatch(self::class . '::preDeleteRoom', new GenericEvent($this));
|
|
|
|
|
2016-11-14 15:57:56 +03:00
|
|
|
$query = $this->db->getQueryBuilder();
|
|
|
|
|
|
|
|
// Delete all participants
|
|
|
|
$query->delete('spreedme_room_participants')
|
|
|
|
->where($query->expr()->eq('roomId', $query->createNamedParameter($this->getId(), IQueryBuilder::PARAM_INT)));
|
|
|
|
$query->execute();
|
|
|
|
|
|
|
|
// Delete room
|
|
|
|
$query->delete('spreedme_rooms')
|
|
|
|
->where($query->expr()->eq('id', $query->createNamedParameter($this->getId(), IQueryBuilder::PARAM_INT)));
|
|
|
|
$query->execute();
|
2017-09-25 14:20:51 +03:00
|
|
|
|
|
|
|
$this->dispatcher->dispatch(self::class . '::postDeleteRoom', new GenericEvent($this));
|
2016-11-14 15:57:56 +03:00
|
|
|
}
|
|
|
|
|
2016-12-21 20:03:49 +03:00
|
|
|
/**
|
|
|
|
* @param string $newName Currently it is only allowed to rename: Room::GROUP_CALL, Room::PUBLIC_CALL
|
|
|
|
* @return bool True when the change was valid, false otherwise
|
|
|
|
*/
|
|
|
|
public function setName($newName) {
|
|
|
|
if ($newName === $this->getName()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->getType() === self::ONE_TO_ONE_CALL) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-09-25 14:20:51 +03:00
|
|
|
$oldName = $this->getName();
|
|
|
|
|
|
|
|
$this->dispatcher->dispatch(self::class . '::preSetName', new GenericEvent($this, [
|
|
|
|
'newName' => $newName,
|
|
|
|
'oldName' => $oldName,
|
|
|
|
]));
|
|
|
|
|
2016-12-21 20:03:49 +03:00
|
|
|
$query = $this->db->getQueryBuilder();
|
|
|
|
$query->update('spreedme_rooms')
|
|
|
|
->set('name', $query->createNamedParameter($newName))
|
|
|
|
->where($query->expr()->eq('id', $query->createNamedParameter($this->getId(), IQueryBuilder::PARAM_INT)));
|
|
|
|
$query->execute();
|
2017-07-20 17:41:16 +03:00
|
|
|
$this->name = $newName;
|
2016-12-21 20:03:49 +03:00
|
|
|
|
2017-09-25 14:20:51 +03:00
|
|
|
$this->dispatcher->dispatch(self::class . '::postSetName', new GenericEvent($this, [
|
|
|
|
'newName' => $newName,
|
|
|
|
'oldName' => $oldName,
|
|
|
|
]));
|
|
|
|
|
2016-12-21 20:03:49 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-09-11 18:17:13 +03:00
|
|
|
/**
|
|
|
|
* @param string $password Currently it is only allowed to have a password for Room::PUBLIC_CALL
|
|
|
|
* @return bool True when the change was valid, false otherwise
|
|
|
|
*/
|
|
|
|
public function setPassword($password) {
|
|
|
|
if ($this->getType() !== self::PUBLIC_CALL) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-09-21 11:58:20 +03:00
|
|
|
$hash = $this->hasher->hash($password);
|
|
|
|
|
2017-09-11 18:17:13 +03:00
|
|
|
$query = $this->db->getQueryBuilder();
|
|
|
|
$query->update('spreedme_rooms')
|
2017-09-21 11:58:20 +03:00
|
|
|
->set('password', $query->createNamedParameter($hash))
|
2017-09-11 18:17:13 +03:00
|
|
|
->where($query->expr()->eq('id', $query->createNamedParameter($this->getId(), IQueryBuilder::PARAM_INT)));
|
|
|
|
$query->execute();
|
2017-09-21 11:58:20 +03:00
|
|
|
$this->password = $hash;
|
2017-09-11 18:17:13 +03:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-11-16 20:10:53 +03:00
|
|
|
/**
|
2016-11-17 18:15:55 +03:00
|
|
|
* @param int $newType Currently it is only allowed to change to: Room::GROUP_CALL, Room::PUBLIC_CALL
|
2016-11-16 20:10:53 +03:00
|
|
|
* @return bool True when the change was valid, false otherwise
|
|
|
|
*/
|
|
|
|
public function changeType($newType) {
|
|
|
|
if ($newType === $this->getType()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-12-06 16:14:03 +03:00
|
|
|
if (!in_array($newType, [Room::GROUP_CALL, Room::PUBLIC_CALL], true)) {
|
2016-11-16 20:10:53 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-11-21 14:16:51 +03:00
|
|
|
$oldType = $this->getType();
|
|
|
|
|
2017-09-25 14:20:51 +03:00
|
|
|
$this->dispatcher->dispatch(self::class . '::preChangeType', new GenericEvent($this, [
|
|
|
|
'newType' => $newType,
|
|
|
|
'oldType' => $oldType,
|
|
|
|
]));
|
|
|
|
|
2016-11-16 20:10:53 +03:00
|
|
|
$query = $this->db->getQueryBuilder();
|
|
|
|
$query->update('spreedme_rooms')
|
|
|
|
->set('type', $query->createNamedParameter($newType, IQueryBuilder::PARAM_INT))
|
|
|
|
->where($query->expr()->eq('id', $query->createNamedParameter($this->getId(), IQueryBuilder::PARAM_INT)));
|
|
|
|
$query->execute();
|
|
|
|
|
|
|
|
$this->type = (int) $newType;
|
|
|
|
|
2016-11-21 14:16:51 +03:00
|
|
|
if ($oldType === Room::PUBLIC_CALL) {
|
2017-07-25 18:20:49 +03:00
|
|
|
// Kick all guests and users that were not invited
|
2016-11-21 14:16:51 +03:00
|
|
|
$query = $this->db->getQueryBuilder();
|
|
|
|
$query->delete('spreedme_room_participants')
|
|
|
|
->where($query->expr()->eq('roomId', $query->createNamedParameter($this->getId(), IQueryBuilder::PARAM_INT)))
|
2017-07-25 18:20:49 +03:00
|
|
|
->andWhere($query->expr()->in('participantType', $query->createNamedParameter([Participant::GUEST, Participant::USER_SELF_JOINED], IQueryBuilder::PARAM_INT_ARRAY)));
|
2016-11-21 14:16:51 +03:00
|
|
|
$query->execute();
|
|
|
|
}
|
|
|
|
|
2017-09-25 14:20:51 +03:00
|
|
|
$this->dispatcher->dispatch(self::class . '::postChangeType', new GenericEvent($this, [
|
|
|
|
'newType' => $newType,
|
|
|
|
'oldType' => $oldType,
|
|
|
|
]));
|
|
|
|
|
2016-11-16 20:10:53 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-11-11 18:12:45 +03:00
|
|
|
/**
|
2017-09-25 14:42:52 +03:00
|
|
|
* @param array[] ...$participants
|
2016-11-11 18:12:45 +03:00
|
|
|
*/
|
2017-09-25 14:42:52 +03:00
|
|
|
public function addUsers(array ...$participants) {
|
|
|
|
$this->dispatcher->dispatch(self::class . '::preAddUsers', new GenericEvent($this, [
|
|
|
|
'users' => $participants,
|
|
|
|
]));
|
2017-04-21 15:27:29 +03:00
|
|
|
|
2016-11-11 18:12:45 +03:00
|
|
|
$query = $this->db->getQueryBuilder();
|
|
|
|
$query->insert('spreedme_room_participants')
|
|
|
|
->values(
|
|
|
|
[
|
2017-09-25 14:42:52 +03:00
|
|
|
'userId' => $query->createParameter('userId'),
|
|
|
|
'sessionId' => $query->createParameter('sessionId'),
|
|
|
|
'participantType' => $query->createParameter('participantType'),
|
2016-11-11 18:12:45 +03:00
|
|
|
'roomId' => $query->createNamedParameter($this->getId()),
|
2016-11-14 13:54:54 +03:00
|
|
|
'lastPing' => $query->createNamedParameter(0, IQueryBuilder::PARAM_INT),
|
2016-11-11 18:12:45 +03:00
|
|
|
]
|
|
|
|
);
|
2017-09-25 14:42:52 +03:00
|
|
|
|
|
|
|
foreach ($participants as $participant) {
|
|
|
|
$query->setParameter('userId', $participant['userId'])
|
|
|
|
->setParameter('sessionId', isset($participant['sessionId']) ? $participant['sessionId'] : '0')
|
|
|
|
->setParameter('participantType', isset($participant['participantType']) ? $participant['participantType'] : Participant::USER, IQueryBuilder::PARAM_INT);
|
|
|
|
|
|
|
|
$query->execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->dispatcher->dispatch(self::class . '::postAddUsers', new GenericEvent($this, [
|
|
|
|
'users' => $participants,
|
|
|
|
]));
|
2016-11-14 16:32:31 +03:00
|
|
|
}
|
|
|
|
|
2017-07-10 17:50:07 +03:00
|
|
|
/**
|
|
|
|
* @param string $participant
|
|
|
|
* @param int $participantType
|
|
|
|
*/
|
|
|
|
public function setParticipantType($participant, $participantType) {
|
|
|
|
$query = $this->db->getQueryBuilder();
|
|
|
|
$query->update('spreedme_room_participants')
|
|
|
|
->set('participantType', $query->createNamedParameter($participantType, IQueryBuilder::PARAM_INT))
|
|
|
|
->where($query->expr()->eq('roomId', $query->createNamedParameter($this->getId(), IQueryBuilder::PARAM_INT)))
|
|
|
|
->andWhere($query->expr()->eq('userId', $query->createNamedParameter($participant)));
|
|
|
|
$query->execute();
|
|
|
|
}
|
|
|
|
|
2016-11-14 16:32:31 +03:00
|
|
|
/**
|
|
|
|
* @param IUser $user
|
|
|
|
*/
|
|
|
|
public function removeUser(IUser $user) {
|
2017-09-25 14:20:51 +03:00
|
|
|
$this->dispatcher->dispatch(self::class . '::preRemoveUser', new GenericEvent($this, [
|
|
|
|
'user' => $user,
|
|
|
|
]));
|
|
|
|
|
2016-11-14 16:32:31 +03:00
|
|
|
$query = $this->db->getQueryBuilder();
|
|
|
|
$query->delete('spreedme_room_participants')
|
2016-11-16 13:02:37 +03:00
|
|
|
->where($query->expr()->eq('roomId', $query->createNamedParameter($this->getId(), IQueryBuilder::PARAM_INT)))
|
2016-11-14 16:32:31 +03:00
|
|
|
->andWhere($query->expr()->eq('userId', $query->createNamedParameter($user->getUID())));
|
|
|
|
$query->execute();
|
2017-09-25 14:20:51 +03:00
|
|
|
|
|
|
|
$this->dispatcher->dispatch(self::class . '::postRemoveUser', new GenericEvent($this, [
|
|
|
|
'user' => $user,
|
|
|
|
]));
|
2016-11-11 18:12:45 +03:00
|
|
|
}
|
2016-11-14 13:54:54 +03:00
|
|
|
|
2017-09-19 17:46:54 +03:00
|
|
|
/**
|
|
|
|
* @param Participant $participant
|
|
|
|
*/
|
|
|
|
public function removeParticipantBySession(Participant $participant) {
|
2017-09-25 14:20:51 +03:00
|
|
|
$this->dispatcher->dispatch(self::class . '::preRemoveBySession', new GenericEvent($this, [
|
|
|
|
'participant' => $participant,
|
|
|
|
]));
|
|
|
|
|
2017-09-19 17:46:54 +03:00
|
|
|
$query = $this->db->getQueryBuilder();
|
|
|
|
$query->delete('spreedme_room_participants')
|
|
|
|
->where($query->expr()->eq('roomId', $query->createNamedParameter($this->getId(), IQueryBuilder::PARAM_INT)))
|
|
|
|
->andWhere($query->expr()->eq('sessionId', $query->createNamedParameter($participant->getSessionId())));
|
|
|
|
$query->execute();
|
2017-09-25 14:20:51 +03:00
|
|
|
|
|
|
|
$this->dispatcher->dispatch(self::class . '::postRemoveBySession', new GenericEvent($this, [
|
|
|
|
'participant' => $participant,
|
|
|
|
]));
|
2017-09-19 17:46:54 +03:00
|
|
|
}
|
|
|
|
|
2016-11-21 16:00:56 +03:00
|
|
|
/**
|
|
|
|
* @param string $userId
|
2017-09-11 18:17:13 +03:00
|
|
|
* @param string $password
|
2016-12-06 16:14:03 +03:00
|
|
|
* @return string
|
2017-09-11 18:17:13 +03:00
|
|
|
* @throws InvalidPasswordException
|
2016-11-21 16:00:56 +03:00
|
|
|
*/
|
2017-09-11 18:17:13 +03:00
|
|
|
public function enterRoomAsUser($userId, $password) {
|
2017-09-14 11:21:59 +03:00
|
|
|
$this->dispatcher->dispatch(self::class . '::preUserEnterRoom', new GenericEvent($this));
|
|
|
|
|
2016-11-21 16:00:56 +03:00
|
|
|
$query = $this->db->getQueryBuilder();
|
|
|
|
$query->update('spreedme_room_participants')
|
2016-12-06 16:14:03 +03:00
|
|
|
->set('sessionId', $query->createParameter('sessionId'))
|
2016-11-21 16:00:56 +03:00
|
|
|
->where($query->expr()->eq('roomId', $query->createNamedParameter($this->getId(), IQueryBuilder::PARAM_INT)))
|
|
|
|
->andWhere($query->expr()->eq('userId', $query->createNamedParameter($userId)));
|
2016-12-06 16:14:03 +03:00
|
|
|
|
|
|
|
$sessionId = $this->secureRandom->generate(255);
|
|
|
|
$query->setParameter('sessionId', $sessionId);
|
2017-04-21 15:27:29 +03:00
|
|
|
$result = $query->execute();
|
|
|
|
|
|
|
|
if ($result === 0) {
|
2017-09-22 16:35:53 +03:00
|
|
|
if ($this->hasPassword() && !$this->hasher->verify($password, $this->password)) {
|
2017-09-11 18:17:13 +03:00
|
|
|
throw new InvalidPasswordException();
|
|
|
|
}
|
|
|
|
|
2017-04-21 15:27:29 +03:00
|
|
|
// User joining a public room, without being invited
|
2017-09-25 14:42:52 +03:00
|
|
|
$this->addUsers([
|
|
|
|
'userId' => $userId,
|
|
|
|
'participantType' => Participant::USER_SELF_JOINED,
|
|
|
|
'sessionId' => $sessionId,
|
|
|
|
]);
|
2017-04-21 15:27:29 +03:00
|
|
|
}
|
2016-11-21 16:00:56 +03:00
|
|
|
|
2016-12-06 16:14:03 +03:00
|
|
|
while (!$this->isSessionUnique($sessionId)) {
|
|
|
|
$sessionId = $this->secureRandom->generate(255);
|
|
|
|
$query->setParameter('sessionId', $sessionId);
|
|
|
|
$query->execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
$query = $this->db->getQueryBuilder();
|
2016-11-21 16:00:56 +03:00
|
|
|
$query->update('spreedme_room_participants')
|
|
|
|
->set('sessionId', $query->createNamedParameter('0'))
|
|
|
|
->where($query->expr()->neq('roomId', $query->createNamedParameter($this->getId(), IQueryBuilder::PARAM_INT)))
|
|
|
|
->andWhere($query->expr()->eq('userId', $query->createNamedParameter($userId)));
|
|
|
|
$query->execute();
|
2016-12-06 16:14:03 +03:00
|
|
|
|
2017-09-14 11:21:59 +03:00
|
|
|
$this->dispatcher->dispatch(self::class . '::postUserEnterRoom', new GenericEvent($this));
|
|
|
|
|
2016-12-06 16:14:03 +03:00
|
|
|
return $sessionId;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-09-11 18:17:13 +03:00
|
|
|
* @param string $password
|
2016-12-06 16:14:03 +03:00
|
|
|
* @return string
|
2017-09-11 18:17:13 +03:00
|
|
|
* @throws InvalidPasswordException
|
2016-12-06 16:14:03 +03:00
|
|
|
*/
|
2017-09-11 18:17:13 +03:00
|
|
|
public function enterRoomAsGuest($password) {
|
2017-09-14 11:21:59 +03:00
|
|
|
$this->dispatcher->dispatch(self::class . '::preGuestEnterRoom', new GenericEvent($this));
|
|
|
|
|
2017-09-22 16:35:53 +03:00
|
|
|
if ($this->hasPassword() && !$this->hasher->verify($password, $this->password)) {
|
2017-09-11 18:17:13 +03:00
|
|
|
throw new InvalidPasswordException();
|
|
|
|
}
|
|
|
|
|
2016-12-06 16:14:03 +03:00
|
|
|
$sessionId = $this->secureRandom->generate(255);
|
|
|
|
while (!$this->db->insertIfNotExist('*PREFIX*spreedme_room_participants', [
|
|
|
|
'userId' => '',
|
|
|
|
'roomId' => $this->getId(),
|
|
|
|
'lastPing' => 0,
|
|
|
|
'sessionId' => $sessionId,
|
2017-07-08 10:50:54 +03:00
|
|
|
'participantType' => Participant::GUEST,
|
2016-12-06 16:14:03 +03:00
|
|
|
], ['sessionId'])) {
|
|
|
|
$sessionId = $this->secureRandom->generate(255);
|
|
|
|
}
|
|
|
|
|
2017-09-14 11:21:59 +03:00
|
|
|
$this->dispatcher->dispatch(self::class . '::postGuestEnterRoom', new GenericEvent($this));
|
|
|
|
|
2016-12-06 16:14:03 +03:00
|
|
|
return $sessionId;
|
2016-11-21 16:00:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $sessionId
|
2016-12-06 16:14:03 +03:00
|
|
|
* @return bool
|
2016-11-21 16:00:56 +03:00
|
|
|
*/
|
2016-12-06 16:14:03 +03:00
|
|
|
protected function isSessionUnique($sessionId) {
|
2016-11-21 16:00:56 +03:00
|
|
|
$query = $this->db->getQueryBuilder();
|
2016-12-06 16:14:03 +03:00
|
|
|
$query->selectAlias($query->createFunction('COUNT(*)'), 'num_sessions')
|
|
|
|
->from('spreedme_room_participants')
|
|
|
|
->where($query->expr()->eq('sessionId', $query->createNamedParameter($sessionId)));
|
|
|
|
$result = $query->execute();
|
|
|
|
$numSessions = (int) $result->fetchColumn();
|
|
|
|
$result->closeCursor();
|
|
|
|
|
|
|
|
return $numSessions === 1;
|
2016-11-21 16:00:56 +03:00
|
|
|
}
|
|
|
|
|
2016-11-23 12:29:22 +03:00
|
|
|
public function cleanGuestParticipants() {
|
|
|
|
$query = $this->db->getQueryBuilder();
|
|
|
|
$query->delete('spreedme_room_participants')
|
|
|
|
->where($query->expr()->eq('roomId', $query->createNamedParameter($this->getId(), IQueryBuilder::PARAM_INT)))
|
2017-08-03 17:19:25 +03:00
|
|
|
->andWhere($query->expr()->emptyString('userId'))
|
2016-11-23 12:29:22 +03:00
|
|
|
->andWhere($query->expr()->lte('lastPing', $query->createNamedParameter(time() - 30, IQueryBuilder::PARAM_INT)));
|
|
|
|
$query->execute();
|
|
|
|
}
|
|
|
|
|
2016-11-14 13:54:54 +03:00
|
|
|
/**
|
|
|
|
* @param int $lastPing When the last ping is older than the given timestamp, the user is ignored
|
2016-11-21 14:16:51 +03:00
|
|
|
* @return array[] Array of users with [users => [userId => [lastPing, sessionId]], guests => [[lastPing, sessionId]]]
|
2016-11-14 13:54:54 +03:00
|
|
|
*/
|
|
|
|
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();
|
2016-11-14 15:57:56 +03:00
|
|
|
|
2016-11-21 14:16:51 +03:00
|
|
|
$users = $guests = [];
|
2016-11-14 15:57:56 +03:00
|
|
|
while ($row = $result->fetch()) {
|
2016-11-21 14:16:51 +03:00
|
|
|
if ($row['userId'] !== '' && $row['userId'] !== null) {
|
|
|
|
$users[$row['userId']] = [
|
|
|
|
'lastPing' => (int) $row['lastPing'],
|
|
|
|
'sessionId' => $row['sessionId'],
|
2017-07-12 17:38:49 +03:00
|
|
|
'participantType' => (int) $row['participantType'],
|
2016-11-21 14:16:51 +03:00
|
|
|
];
|
|
|
|
} else {
|
|
|
|
$guests[] = [
|
|
|
|
'lastPing' => (int) $row['lastPing'],
|
|
|
|
'sessionId' => $row['sessionId'],
|
|
|
|
];
|
|
|
|
}
|
2016-11-14 15:57:56 +03:00
|
|
|
}
|
2016-11-14 13:54:54 +03:00
|
|
|
$result->closeCursor();
|
|
|
|
|
2016-11-21 14:16:51 +03:00
|
|
|
return [
|
|
|
|
'users' => $users,
|
|
|
|
'guests' => $guests,
|
|
|
|
];
|
2016-11-14 13:54:54 +03:00
|
|
|
}
|
2016-11-14 14:04:43 +03:00
|
|
|
|
2017-09-22 11:21:03 +03:00
|
|
|
/**
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
public function getActiveSessions() {
|
|
|
|
$query = $this->db->getQueryBuilder();
|
|
|
|
$query->select('sessionId')
|
|
|
|
->from('spreedme_room_participants')
|
|
|
|
->where($query->expr()->eq('roomId', $query->createNamedParameter($this->getId(), IQueryBuilder::PARAM_INT)))
|
|
|
|
->andWhere($query->expr()->neq('sessionId', $query->createNamedParameter('0')));
|
|
|
|
$result = $query->execute();
|
|
|
|
|
|
|
|
$sessions = [];
|
|
|
|
while ($row = $result->fetch()) {
|
|
|
|
$sessions[] = $row['sessionId'];
|
|
|
|
}
|
|
|
|
$result->closeCursor();
|
|
|
|
|
|
|
|
return $sessions;
|
|
|
|
}
|
|
|
|
|
2016-11-14 14:04:43 +03:00
|
|
|
/**
|
|
|
|
* @param int $lastPing When the last ping is older than the given timestamp, the user is ignored
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getNumberOfParticipants($lastPing = 0) {
|
|
|
|
$query = $this->db->getQueryBuilder();
|
|
|
|
$query->selectAlias($query->createFunction('COUNT(*)'), 'num_participants')
|
|
|
|
->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();
|
2016-12-11 13:43:13 +03:00
|
|
|
$row = $result->fetch();
|
2016-11-14 14:04:43 +03:00
|
|
|
$result->closeCursor();
|
|
|
|
|
2016-11-22 02:28:56 +03:00
|
|
|
return isset($row['num_participants']) ? (int) $row['num_participants'] : 0;
|
2016-11-14 14:04:43 +03:00
|
|
|
}
|
2016-11-14 16:18:32 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $participant
|
2016-11-21 14:16:51 +03:00
|
|
|
* @param string $sessionId
|
2016-11-14 16:18:32 +03:00
|
|
|
* @param int $timestamp
|
|
|
|
*/
|
2016-11-21 14:16:51 +03:00
|
|
|
public function ping($participant, $sessionId, $timestamp) {
|
2016-11-14 16:18:32 +03:00
|
|
|
$query = $this->db->getQueryBuilder();
|
|
|
|
$query->update('spreedme_room_participants')
|
|
|
|
->set('lastPing', $query->createNamedParameter($timestamp, IQueryBuilder::PARAM_INT))
|
2016-11-21 14:16:51 +03:00
|
|
|
->where($query->expr()->eq('userId', $query->createNamedParameter((string) $participant)))
|
|
|
|
->andWhere($query->expr()->eq('sessionId', $query->createNamedParameter($sessionId)))
|
2016-11-14 16:18:32 +03:00
|
|
|
->andWhere($query->expr()->eq('roomId', $query->createNamedParameter($this->getId(), IQueryBuilder::PARAM_INT)));
|
|
|
|
|
|
|
|
$query->execute();
|
|
|
|
}
|
2016-10-19 13:00:17 +03:00
|
|
|
}
|