-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathRoomService.php
More file actions
192 lines (159 loc) · 4.84 KB
/
Copy pathRoomService.php
File metadata and controls
192 lines (159 loc) · 4.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php
namespace OCA\BigBlueButton\Service;
use Exception;
use OCA\BigBlueButton\Db\Room;
use OCA\BigBlueButton\Db\RoomMapper;
use OCA\BigBlueButton\Event\RoomCreatedEvent;
use OCA\BigBlueButton\Event\RoomDeletedEvent;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IConfig;
use OCP\Security\ISecureRandom;
class RoomService {
/** @var RoomMapper */
private $mapper;
/** @var IConfig */
private $config;
/** @var IEventDispatcher */
private $eventDispatcher;
/** @var ISecureRandom */
private $random;
public function __construct(
RoomMapper $mapper,
IConfig $config,
IEventDispatcher $eventDispatcher,
ISecureRandom $random) {
$this->mapper = $mapper;
$this->config = $config;
$this->eventDispatcher = $eventDispatcher;
$this->random = $random;
}
public function findAll(string $userId, array $groupIds, array $circleIds): array {
return $this->mapper->findAll($userId, $groupIds, $circleIds);
}
private function handleException(Exception $e): void {
if ($e instanceof DoesNotExistException ||
$e instanceof MultipleObjectsReturnedException) {
throw new RoomNotFound($e->getMessage());
} else {
throw $e;
}
}
/**
* @throws RoomNotFound
*/
public function find(int $id): Room {
try {
return $this->mapper->find($id);
// 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);
}
}
public function findByUid(string $uid): ?Room {
try {
return $this->mapper->findByUid($uid);
} catch (Exception $e) {
// $this->handleException($e);
return null;
}
}
/**
* @return array<Room>
*/
public function findByUserId(string $userId): array {
return $this->mapper->findByUserId($userId);
}
public function create(string $name, string $welcome, int $maxParticipants, bool $record, string $access, string $userId): \OCP\AppFramework\Db\Entity {
$room = new Room();
$mediaCheck = $this->config->getAppValue('bbb', 'join.mediaCheck', 'true') === 'true';
$room->setUid($this->humanReadableRandom(16));
$room->setName($name);
$room->setWelcome($welcome);
$room->setMaxParticipants(\max($maxParticipants, 0));
$room->setAttendeePassword($this->humanReadableRandom(32));
$room->setModeratorPassword($this->humanReadableRandom(32));
$room->setRecord($record);
$room->setAccess($access);
$room->setUserId($userId);
$room->setListenOnly(true);
$room->setMediaCheck($mediaCheck);
$room->setCleanLayout(false);
$room->setJoinMuted(false);
$room->setLogoutURL('');
if ($access === Room::ACCESS_PASSWORD) {
$room->setPassword($this->humanReadableRandom(8));
}
$createdRoom = $this->mapper->insert($room);
$this->eventDispatcher->dispatch(RoomCreatedEvent::class, new RoomCreatedEvent($createdRoom));
return $createdRoom;
}
/**
* @param null|string $moderatorToken
*
* @return \OCP\AppFramework\Db\Entity|null
*/
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,
bool $cleanLayout,
bool $joinMuted,
string $logoutURL) {
try {
$room = $this->mapper->find($id);
if ($room->access !== $access) {
$room->setPassword($access === Room::ACCESS_PASSWORD ? $this->humanReadableRandom(8) : null);
}
if ($room->moderatorToken !== $moderatorToken) {
$room->setModeratorToken(empty($moderatorToken) ? null : $this->humanReadableRandom(16));
}
$room->setName($name);
$room->setWelcome($welcome);
$room->setMaxParticipants(\max($maxParticipants, 0));
$room->setRecord($record);
$room->setAccess($access);
$room->setEveryoneIsModerator($everyoneIsModerator);
$room->setRequireModerator($requireModerator);
$room->setListenOnly($listenOnly);
$room->setMediaCheck($mediaCheck);
$room->setCleanLayout($cleanLayout);
$room->setJoinMuted($joinMuted);
$room->setLogoutURL($logoutURL);
return $this->mapper->update($room);
} catch (Exception $e) {
$this->handleException($e);
}
}
/**
* @return Room|null
*/
public function delete(int $id) {
try {
$room = $this->mapper->find($id);
$this->mapper->delete($room);
$this->eventDispatcher->dispatch(RoomDeletedEvent::class, new RoomDeletedEvent($room));
return $room;
} catch (Exception $e) {
$this->handleException($e);
}
}
/**
* @param int $length
*/
private function humanReadableRandom(int $length) {
return $this->random->generate($length, \OCP\Security\ISecureRandom::CHAR_HUMAN_READABLE);
}
}