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;
|
|
|
|
|
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;
|
2016-12-06 16:14:03 +03:00
|
|
|
use OCP\Security\ISecureRandom;
|
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;
|
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;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Room constructor.
|
|
|
|
*
|
|
|
|
* @param IDBConnection $db
|
2016-12-06 16:14:03 +03:00
|
|
|
* @param ISecureRandom $secureRandom
|
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-03-14 16:13:29 +03:00
|
|
|
public function __construct(IDBConnection $db, ISecureRandom $secureRandom, $id, $type, $token, $name) {
|
2016-11-11 18:12:45 +03:00
|
|
|
$this->db = $db;
|
2016-12-06 16:14:03 +03:00
|
|
|
$this->secureRandom = $secureRandom;
|
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;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2016-11-14 15:57:56 +03:00
|
|
|
public function deleteRoom() {
|
|
|
|
$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();
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
$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();
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
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) {
|
|
|
|
// Kick all guests
|
|
|
|
$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('userId', $query->createNamedParameter('')));
|
|
|
|
$query->execute();
|
|
|
|
}
|
|
|
|
|
2016-11-16 20:10:53 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-11-11 18:12:45 +03:00
|
|
|
/**
|
|
|
|
* @param IUser $user
|
|
|
|
*/
|
|
|
|
public function addUser(IUser $user) {
|
2017-07-08 10:50:54 +03:00
|
|
|
$this->addParticipant($user->getUID(), Participant::USER);
|
2017-04-21 15:27:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $participant
|
2017-07-08 10:50:54 +03:00
|
|
|
* @param int $participantType
|
2017-04-21 15:27:29 +03:00
|
|
|
* @param string $sessionId
|
|
|
|
*/
|
2017-07-08 10:50:54 +03:00
|
|
|
public function addParticipant($participant, $participantType, $sessionId = '0') {
|
2016-11-11 18:12:45 +03:00
|
|
|
$query = $this->db->getQueryBuilder();
|
|
|
|
$query->insert('spreedme_room_participants')
|
|
|
|
->values(
|
|
|
|
[
|
2017-04-21 15:27:29 +03:00
|
|
|
'userId' => $query->createNamedParameter($participant),
|
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),
|
2017-04-21 15:27:29 +03:00
|
|
|
'sessionId' => $query->createNamedParameter($sessionId),
|
2017-07-08 10:50:54 +03:00
|
|
|
'participantType' => $query->createNamedParameter($participantType, IQueryBuilder::PARAM_INT),
|
2016-11-11 18:12:45 +03:00
|
|
|
]
|
|
|
|
);
|
|
|
|
$query->execute();
|
2016-11-14 16:32:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param IUser $user
|
|
|
|
*/
|
|
|
|
public function removeUser(IUser $user) {
|
|
|
|
$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();
|
2016-11-11 18:12:45 +03:00
|
|
|
}
|
2016-11-14 13:54:54 +03:00
|
|
|
|
2016-11-21 16:00:56 +03:00
|
|
|
/**
|
|
|
|
* @param string $userId
|
2016-12-06 16:14:03 +03:00
|
|
|
* @return string
|
2016-11-21 16:00:56 +03:00
|
|
|
*/
|
2016-12-06 16:14:03 +03:00
|
|
|
public function enterRoomAsUser($userId) {
|
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) {
|
|
|
|
// User joining a public room, without being invited
|
|
|
|
$this->addParticipant($userId, $sessionId);
|
|
|
|
}
|
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
|
|
|
|
|
|
|
return $sessionId;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function enterRoomAsGuest() {
|
|
|
|
$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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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)))
|
|
|
|
->andWhere($query->expr()->eq('userId', $query->createNamedParameter('')))
|
|
|
|
->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'],
|
|
|
|
];
|
|
|
|
} 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
|
|
|
|
|
|
|
/**
|
|
|
|
* @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
|
|
|
}
|