-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathAPI.php
More file actions
316 lines (245 loc) · 10.6 KB
/
API.php
File metadata and controls
316 lines (245 loc) · 10.6 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
<?php
namespace OCA\BigBlueButton\BigBlueButton;
use BigBlueButton\BigBlueButton;
use BigBlueButton\Core\Record;
use BigBlueButton\Parameters\CreateMeetingParameters;
use BigBlueButton\Parameters\DeleteRecordingsParameters;
use BigBlueButton\Parameters\GetRecordingsParameters;
use BigBlueButton\Parameters\IsMeetingRunningParameters;
use BigBlueButton\Parameters\JoinMeetingParameters;
use OCA\BigBlueButton\AppInfo\Application;
use OCA\BigBlueButton\AvatarRepository;
use OCA\BigBlueButton\Crypto;
use OCA\BigBlueButton\Db\Room;
use OCA\BigBlueButton\Event\MeetingStartedEvent;
use OCA\BigBlueButton\UrlHelper;
use OCP\App\IAppManager;
use OCP\Defaults;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IConfig;
use OCP\IL10N;
use OCP\IRequest;
use OCP\IURLGenerator;
class API {
/** @var IConfig */
private $config;
/** @var IURLGenerator */
private $urlGenerator;
/** @var BigBlueButton|null */
private $server;
/** @var Crypto */
private $crypto;
/** @var IEventDispatcher */
private $eventDispatcher;
/** @var IL10N */
private $l10n;
/** @var UrlHelper */
private $urlHelper;
/** @var Defaults */
private $defaults;
/** @var IAppManager */
private $appManager;
/** @var AvatarRepository */
private $avatarRepository;
/** @var IRequest */
private $request;
public function __construct(
IConfig $config,
IURLGenerator $urlGenerator,
Crypto $crypto,
IEventDispatcher $eventDispatcher,
IL10N $l10n,
UrlHelper $urlHelper,
Defaults $defaults,
IAppManager $appManager,
AvatarRepository $avatarRepository,
IRequest $request
) {
$this->config = $config;
$this->urlGenerator = $urlGenerator;
$this->crypto = $crypto;
$this->eventDispatcher = $eventDispatcher;
$this->l10n = $l10n;
$this->urlHelper = $urlHelper;
$this->defaults = $defaults;
$this->appManager = $appManager;
$this->avatarRepository = $avatarRepository;
$this->request = $request;
}
private function getServer(): BigBlueButton {
if (!$this->server) {
$apiUrl = $this->config->getAppValue('bbb', 'api.url');
$secret = $this->config->getAppValue('bbb', 'api.secret');
$this->server = new BigBlueButton($apiUrl, $secret);
}
return $this->server;
}
/**
* Create join url.
*
* @return string join url
*/
public function createJoinUrl(Room $room, float $creationTime, string $displayname, bool $isModerator, ?string $uid = null) {
$password = $isModerator ? $room->moderatorPassword : $room->attendeePassword;
$joinMeetingParams = new JoinMeetingParameters($room->uid, $displayname, $password);
// ensure that float is not converted to a string in scientific notation
$joinMeetingParams->setCreateTime(sprintf("%.0f", $creationTime));
$joinMeetingParams->setJoinViaHtml5(true);
$joinMeetingParams->setRedirect(true);
// set the guest parameter for everyone but moderators to send all users to the waiting room if setting is selected
$joinMeetingParams->setGuest((($room->access === Room::ACCESS_WAITING_ROOM_ALL) && !$isModerator) || $uid === null);
$joinMeetingParams->addUserData('bbb_listen_only_mode', $room->getListenOnly());
$joinMeetingParams->addUserData('bbb_skip_check_audio_on_first_join', !$room->getMediaCheck()); // 2.3
$joinMeetingParams->addUserData('bbb_skip_video_preview_on_first_join', !$room->getMediaCheck()); // 2.3
if ($room->getCleanLayout()) {
$joinMeetingParams->addUserData('bbb_auto_swap_layout', true);
$joinMeetingParams->addUserData('bbb_show_participants_on_login', false);
$joinMeetingParams->addUserData('bbb_show_public_chat_on_login', false);
}
if ($this->config->getAppValue('bbb', 'join.theme') === 'true') {
$primaryColor = $this->defaults->getColorPrimary();
$textColor = $this->defaults->getTextColorPrimary();
$joinMeetingParams->addUserData('bbb_custom_style', ":root{--nc-primary-color:$primaryColor;--nc-primary-text-color:$textColor;--nc-bg-color:#444;--color-primary:var(--nc-primary-color);--btn-primary-color:var(--nc-primary-text-color);--color-text:#222;--loader-bg:var(--nc-bg-color);--user-list-bg:#fff;--user-list-text:#222;--list-item-bg-hover:#f5f5f5;--item-focus-border:var(--nc-primary-color);--color-off-white:#fff;--color-gray-dark:var(--nc-bg-color);}body{background-color:var(--nc-bg-color);}.overlay--1aTlbi{background-color:var(--nc-bg-color);}.userlistPad--o5KDX{border-right: 1px solid #ededed;}.scrollStyle--Ckr4w{background: transparent;}.item--yl1AH:hover, .item--yl1AH:focus{color:--nc-primary-text-color;}#message-input:focus{box-shadow:0 0 0 1px var(--nc-primary-color);border-color:--nc-primary-color;}.active--Z1SuO2X{border-radius:5px;}");
}
if ($uid) {
$avatarUrl = $this->avatarRepository->getAvatarUrl($room, $uid);
$joinMeetingParams->setUserID($uid);
$joinMeetingParams->setAvatarURL($avatarUrl);
}
return $this->getServer()->getJoinMeetingURL($joinMeetingParams);
}
/**
* Create meeting room.
*
* @return float|int creation time
*/
public function createMeeting(Room $room, Presentation $presentation = null) {
$bbb = $this->getServer();
$meetingParams = $this->buildMeetingParams($room, $presentation);
try {
$response = $bbb->createMeeting($meetingParams);
} catch (\Exception $e) {
throw new \Exception('Can not process create request: ' . $bbb->getCreateMeetingUrl($meetingParams));
}
if (!$response->success()) {
throw new \Exception('Can not create meeting: ' . $response->getMessage());
}
if ($response->getMessageKey() !== 'duplicateWarning') {
$this->eventDispatcher->dispatch(MeetingStartedEvent::class, new MeetingStartedEvent($room));
}
return $response->getCreationTime();
}
private function buildMeetingParams(Room $room, Presentation $presentation = null): CreateMeetingParameters {
$createMeetingParams = new CreateMeetingParameters($room->uid, $room->name);
$createMeetingParams->setAttendeePW($room->attendeePassword);
$createMeetingParams->setModeratorPW($room->moderatorPassword);
$createMeetingParams->setRecord($room->record);
$createMeetingParams->setAllowStartStopRecording($room->record);
$createMeetingParams->setLogoutURL($room->logoutURL);
$createMeetingParams->setMuteOnStart($room->getJoinMuted());
$createMeetingParams->addMeta('bbb-origin-version', $this->appManager->getAppVersion(Application::ID));
$createMeetingParams->addMeta('bbb-origin', \method_exists($this->defaults, 'getProductName') ? $this->defaults->getProductName() : 'Nextcloud');
$createMeetingParams->addMeta('bbb-origin-server-name', $this->request->getServerHost());
$mac = $this->crypto->calculateHMAC($room->uid);
$endMeetingUrl = $this->urlGenerator->linkToRouteAbsolute('bbb.hook.meetingEnded', ['token' => $room->uid, 'mac' => $mac]);
$createMeetingParams->setEndCallbackUrl($endMeetingUrl);
$recordingReadyUrl = $this->urlGenerator->linkToRouteAbsolute('bbb.hook.recordingReady', ['token' => $room->uid, 'mac' => $mac]);
$createMeetingParams->setRecordingReadyCallbackUrl($recordingReadyUrl);
$invitationUrl = $this->urlHelper->linkToInvitationAbsolute($room);
$createMeetingParams->setModeratorOnlyMessage($this->l10n->t('To invite someone to the meeting, send them this link: %s', [$invitationUrl]));
if (!empty($room->welcome)) {
$createMeetingParams->setWelcome($room->welcome);
}
if ($room->maxParticipants > 0) {
$createMeetingParams->setMaxParticipants($room->maxParticipants);
}
if ($presentation !== null && $presentation->isValid()) {
/** @psalm-suppress InvalidArgument */
$createMeetingParams->addPresentation($presentation->getUrl(), null, $presentation->getFilename());
}
if ($room->access === Room::ACCESS_WAITING_ROOM || $room->access === Room::ACCESS_WAITING_ROOM_ALL) {
$createMeetingParams->setGuestPolicyAskModerator();
}
return $createMeetingParams;
}
public function getRecording(string $recordId) {
$recordingParams = new GetRecordingsParameters();
$recordingParams->setRecordID($recordId);
$recordingParams->setState('any');
$response = $this->getServer()->getRecordings($recordingParams);
if (!$response->success()) {
throw new \Exception('Could not process get recording request');
}
$records = $response->getRecords();
if (count($records) === 0) {
throw new \Exception('Found no record with given id');
}
return $this->recordToArray($records[0]);
}
public function getRecordings(Room $room): array {
$recordingParams = new GetRecordingsParameters();
$recordingParams->setMeetingID($room->uid);
$recordingParams->setState('processing,processed,published,unpublished');
$response = $this->getServer()->getRecordings($recordingParams);
if (!$response->success()) {
throw new \Exception('Could not process get recordings request');
}
$records = $response->getRecords();
return array_map(function ($record) {
return $this->recordToArray($record);
}, $records);
}
public function deleteRecording(string $recordingId): bool {
$deleteParams = new DeleteRecordingsParameters($recordingId);
$response = $this->getServer()->deleteRecordings($deleteParams);
return $response->isDeleted();
}
/**
* @return (array|bool|int|string)[]
*
* @psalm-return array{id: string, meetingId: string, name: string, published: bool, state: string, startTime: string, participants: int, type: string, length: string, url: string, metas: array}
*/
private function recordToArray(Record $record): array {
return [
'id' => $record->getRecordId(),
'meetingId' => $record->getMeetingId(),
'name' => $record->getName(),
'published' => $record->isPublished(),
'state' => $record->getState(),
'startTime' => $record->getStartTime(),
'participants' => $record->getParticipantCount(),
'type' => $record->getPlaybackType(),
'length' => $record->getPlaybackLength(),
'url' => $record->getPlaybackUrl(),
'metas' => $record->getMetas(),
];
}
public function check(string $url, string $secret): string {
$server = new BigBlueButton($url, $secret);
$meetingParams = new IsMeetingRunningParameters('foobar');
try {
$response = $server->isMeetingRunning($meetingParams);
if (!$response->success() && !$response->failed()) {
return 'invalid-url';
}
if (!$response->success()) {
return 'invalid-secret';
}
return 'success';
} catch (\Exception $e) {
return 'invalid-url';
}
}
/**
* @param null|string $url
*/
public function getVersion(?string $url = null) {
$server = $url === null ? $this->getServer() : new BigBlueButton($url, '');
return $server->getApiVersion()->getVersion();
}
public function isRunning(Room $room): bool {
$isMeetingRunningParams = new IsMeetingRunningParameters($room->getUid());
$response = $this->getServer()->isMeetingRunning($isMeetingRunningParams);
return $response->success() && $response->isRunning();
}
}