2020-04-26 11:36:41 +02:00
|
|
|
<?php
|
2020-06-19 09:28:58 +02:00
|
|
|
|
2020-04-26 11:36:41 +02:00
|
|
|
namespace OCA\BigBlueButton\Service;
|
|
|
|
|
|
|
|
use Exception;
|
|
|
|
|
|
|
|
use OCA\BigBlueButton\Db\Room;
|
|
|
|
use OCA\BigBlueButton\Db\RoomMapper;
|
2020-09-07 22:12:49 +02:00
|
|
|
use OCA\BigBlueButton\Event\RoomCreatedEvent;
|
2020-09-23 12:33:09 +02:00
|
|
|
|
2020-09-07 22:12:49 +02:00
|
|
|
use OCA\BigBlueButton\Event\RoomDeletedEvent;
|
2020-09-23 12:33:09 +02:00
|
|
|
use OCP\AppFramework\Db\DoesNotExistException;
|
|
|
|
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
|
|
|
|
use OCP\EventDispatcher\IEventDispatcher;
|
2020-04-26 11:36:41 +02:00
|
|
|
|
2020-06-19 09:28:58 +02:00
|
|
|
class RoomService {
|
2020-04-26 11:36:41 +02:00
|
|
|
|
|
|
|
/** @var RoomMapper */
|
|
|
|
private $mapper;
|
|
|
|
|
2020-09-07 22:12:49 +02:00
|
|
|
/** @var IEventDispatcher */
|
|
|
|
private $eventDispatcher;
|
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
RoomMapper $mapper,
|
|
|
|
IEventDispatcher $eventDispatcher) {
|
2020-04-26 11:36:41 +02:00
|
|
|
$this->mapper = $mapper;
|
2020-09-07 22:12:49 +02:00
|
|
|
$this->eventDispatcher = $eventDispatcher;
|
2020-04-26 11:36:41 +02:00
|
|
|
}
|
|
|
|
|
2020-08-29 13:20:28 +02:00
|
|
|
public function findAll(string $userId, array $groupIds, array $circleIds): array {
|
|
|
|
return $this->mapper->findAll($userId, $groupIds, $circleIds);
|
2020-04-26 11:36:41 +02:00
|
|
|
}
|
|
|
|
|
2020-06-19 09:28:58 +02:00
|
|
|
private function handleException(Exception $e): void {
|
2020-04-26 11:36:41 +02:00
|
|
|
if ($e instanceof DoesNotExistException ||
|
|
|
|
$e instanceof MultipleObjectsReturnedException) {
|
|
|
|
throw new RoomNotFound($e->getMessage());
|
|
|
|
} else {
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-19 10:49:40 +02:00
|
|
|
/**
|
|
|
|
* @throws RoomNotFound
|
|
|
|
*/
|
2021-02-13 16:14:40 +01:00
|
|
|
public function find(int $id): Room {
|
2020-04-26 11:36:41 +02:00
|
|
|
try {
|
2020-06-17 10:56:28 +02:00
|
|
|
return $this->mapper->find($id);
|
2020-04-26 11:36:41 +02:00
|
|
|
|
|
|
|
// in order to be able to plug in different storage backends like files
|
|
|
|
// for instance it is a good idea to turn storage related exceptions
|
|
|
|
// into service related exceptions so controllers and service users
|
|
|
|
// have to deal with only one type of exception
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$this->handleException($e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-24 15:23:26 +01:00
|
|
|
public function findByUid(string $uid): ?Room {
|
2020-04-26 11:36:41 +02:00
|
|
|
try {
|
|
|
|
return $this->mapper->findByUid($uid);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
// $this->handleException($e);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-18 19:30:44 +02:00
|
|
|
/**
|
|
|
|
* @return array<Room>
|
|
|
|
*/
|
|
|
|
public function findByUserId(string $userId): array {
|
|
|
|
return $this->mapper->findByUserId($userId);
|
|
|
|
}
|
|
|
|
|
2021-02-24 15:23:26 +01:00
|
|
|
public function create(string $name, string $welcome, int $maxParticipants, bool $record, string $access, string $userId): \OCP\AppFramework\Db\Entity {
|
2020-04-26 11:36:41 +02:00
|
|
|
$room = new Room();
|
|
|
|
|
|
|
|
$room->setUid(\OC::$server->getSecureRandom()->generate(16, \OCP\Security\ISecureRandom::CHAR_HUMAN_READABLE));
|
|
|
|
$room->setName($name);
|
|
|
|
$room->setWelcome($welcome);
|
2020-08-27 17:21:34 +02:00
|
|
|
$room->setMaxParticipants(\max($maxParticipants, 0));
|
2020-04-26 11:36:41 +02:00
|
|
|
$room->setAttendeePassword(\OC::$server->getSecureRandom()->generate(32, \OCP\Security\ISecureRandom::CHAR_HUMAN_READABLE));
|
|
|
|
$room->setModeratorPassword(\OC::$server->getSecureRandom()->generate(32, \OCP\Security\ISecureRandom::CHAR_HUMAN_READABLE));
|
|
|
|
$room->setRecord($record);
|
2020-08-27 17:21:34 +02:00
|
|
|
$room->setAccess($access);
|
2020-04-26 11:36:41 +02:00
|
|
|
$room->setUserId($userId);
|
2021-06-30 11:33:10 +02:00
|
|
|
$room->setListenOnly(true);
|
|
|
|
$room->setMediaCheck(true);
|
|
|
|
$room->setCleanLayout(false);
|
2021-07-30 12:12:42 +02:00
|
|
|
$room->setJoinMuted(false);
|
2020-04-26 11:36:41 +02:00
|
|
|
|
2020-09-28 15:05:52 +02:00
|
|
|
if ($access === Room::ACCESS_PASSWORD) {
|
|
|
|
$room->setPassword($this->humanReadableRandom(8));
|
|
|
|
}
|
|
|
|
|
2020-09-07 22:12:49 +02:00
|
|
|
$createdRoom = $this->mapper->insert($room);
|
|
|
|
|
|
|
|
$this->eventDispatcher->dispatch(RoomCreatedEvent::class, new RoomCreatedEvent($createdRoom));
|
|
|
|
|
|
|
|
return $createdRoom;
|
2020-04-26 11:36:41 +02:00
|
|
|
}
|
|
|
|
|
2021-02-13 16:14:40 +01:00
|
|
|
/**
|
|
|
|
* @param null|string $moderatorToken
|
2021-02-24 15:23:26 +01:00
|
|
|
*
|
|
|
|
* @return \OCP\AppFramework\Db\Entity|null
|
2021-02-13 16:14:40 +01:00
|
|
|
*/
|
2021-04-19 14:47:15 +02:00
|
|
|
public function update(
|
|
|
|
int $id,
|
|
|
|
string $name,
|
|
|
|
string $welcome,
|
|
|
|
int $maxParticipants,
|
|
|
|
bool $record,
|
|
|
|
string $access,
|
|
|
|
bool $everyoneIsModerator,
|
|
|
|
bool $requireModerator,
|
|
|
|
?string $moderatorToken,
|
|
|
|
bool $listenOnly,
|
|
|
|
bool $mediaCheck,
|
2021-07-30 12:12:42 +02:00
|
|
|
bool $cleanLayout,
|
|
|
|
bool $joinMuted) {
|
2020-04-26 11:36:41 +02:00
|
|
|
try {
|
2020-06-17 10:56:28 +02:00
|
|
|
$room = $this->mapper->find($id);
|
2020-04-26 11:36:41 +02:00
|
|
|
|
2020-06-04 18:56:55 +02:00
|
|
|
if ($room->access !== $access) {
|
|
|
|
$room->setPassword($access === Room::ACCESS_PASSWORD ? $this->humanReadableRandom(8) : null);
|
|
|
|
}
|
|
|
|
|
2021-01-22 19:12:55 +01:00
|
|
|
if ($room->moderatorToken !== $moderatorToken) {
|
|
|
|
$room->setModeratorToken(empty($moderatorToken) ? null : $this->humanReadableRandom(16));
|
|
|
|
}
|
|
|
|
|
2020-04-26 11:36:41 +02:00
|
|
|
$room->setName($name);
|
|
|
|
$room->setWelcome($welcome);
|
2020-08-27 17:21:34 +02:00
|
|
|
$room->setMaxParticipants(\max($maxParticipants, 0));
|
2020-04-26 11:36:41 +02:00
|
|
|
$room->setRecord($record);
|
2020-06-04 18:56:55 +02:00
|
|
|
$room->setAccess($access);
|
2020-06-17 08:19:54 +02:00
|
|
|
$room->setEveryoneIsModerator($everyoneIsModerator);
|
2020-08-29 14:37:50 +02:00
|
|
|
$room->setRequireModerator($requireModerator);
|
2021-04-19 14:47:15 +02:00
|
|
|
$room->setListenOnly($listenOnly);
|
|
|
|
$room->setMediaCheck($mediaCheck);
|
|
|
|
$room->setCleanLayout($cleanLayout);
|
2021-07-30 12:12:42 +02:00
|
|
|
$room->setJoinMuted($joinMuted);
|
2020-04-26 11:36:41 +02:00
|
|
|
|
|
|
|
return $this->mapper->update($room);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$this->handleException($e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-24 15:23:26 +01:00
|
|
|
/**
|
|
|
|
* @return Room|null
|
|
|
|
*/
|
2021-02-13 16:14:40 +01:00
|
|
|
public function delete(int $id) {
|
2020-04-26 11:36:41 +02:00
|
|
|
try {
|
2020-06-17 10:56:28 +02:00
|
|
|
$room = $this->mapper->find($id);
|
2020-09-18 14:36:31 +02:00
|
|
|
|
2020-04-26 11:36:41 +02:00
|
|
|
$this->mapper->delete($room);
|
2020-09-07 22:12:49 +02:00
|
|
|
|
|
|
|
$this->eventDispatcher->dispatch(RoomDeletedEvent::class, new RoomDeletedEvent($room));
|
|
|
|
|
2020-04-26 11:36:41 +02:00
|
|
|
return $room;
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$this->handleException($e);
|
|
|
|
}
|
|
|
|
}
|
2020-06-04 18:56:55 +02:00
|
|
|
|
2021-02-13 16:14:40 +01:00
|
|
|
/**
|
|
|
|
* @param int $length
|
|
|
|
*/
|
|
|
|
private function humanReadableRandom(int $length) {
|
2020-06-04 18:56:55 +02:00
|
|
|
return \OC::$server->getSecureRandom()->generate($length, \OCP\Security\ISecureRandom::CHAR_HUMAN_READABLE);
|
|
|
|
}
|
2020-04-26 11:36:41 +02:00
|
|
|
}
|