Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
zoom.php
1<?php
2
4
7
18
19class Zoom
20{
21 private const CONFERENCE_INSTANT_TYPE = 1;
22 private const CONFERENCE_SCHEDULED_TYPE = 2;
23 private const DEFAULT_DURATION_MINUTES = "30";
24
25 private const PERSONAL_CHAT = 'dialog';
26 private const GROUP_CHAT = 'chat';
27
28 private $accessToken;
29 private $userId;
30 private $chatId;
31 private $chatType;
32 private $zoomChatName;
33 private $zoomSocServ;
34
42 public function __construct(int $userId, string $chatId)
43 {
44 global $USER;
45
46 if($userId === null)
47 {
48 if(is_object($USER) && $USER->IsAuthorized())
49 {
50 $this->userId = $USER->GetID();
51 }
52 }
53 else
54 {
55 $this->userId = $userId;
56 }
57
58 if (\Bitrix\Im\Common::isChatId($chatId))
59 {
60 $this->chatType = self::GROUP_CHAT;
61 $this->chatId = \Bitrix\Im\Dialog::getChatId($chatId);
62 }
63 else
64 {
65 $this->chatType = self::PERSONAL_CHAT;
66 $this->chatId = $chatId;
67 }
68
69 $this->zoomChatName = $this->prepareZoomChatName($chatId);
70
71 $accessToken = $this->getAccessToken();
72 if ($accessToken)
73 {
74 $this->accessToken = $accessToken;
75 }
76 }
77
84 public static function isActive(): bool
85 {
86 if (Loader::includeModule('socialservices'))
87 {
88 return (new \CSocServAuthManager())->isActiveAuthService('zoom');
89 }
90
91 return false;
92 }
93
104 public static function isConnected($userId): bool
105 {
106 if (!Loader::includeModule('socialservices'))
107 {
108 return false;
109 }
110
111 if (\CZoomInterface::isConnected($userId))
112 {
113 return true;
114 }
115
116 return false;
117 }
118
125 public static function isAvailable(): bool
126 {
127 if (!Loader::includeModule('bitrix24'))
128 {
129 return true;
130 }
131
132 return \Bitrix\Bitrix24\Feature::isFeatureEnabled("im_zoom_integration");
133 }
134
141 public function getAccessToken()
142 {
143 if (!Loader::includeModule('socialservices'))
144 {
145 return false;
146 }
147
148 $this->zoomSocServ = new \CSocServZoom($this->userId);
149 $this->zoomSocServ->getUrl('');
150 $accessToken = $this->zoomSocServ->getStorageToken();
151
152 if (!empty($accessToken))
153 {
154 return $accessToken;
155 }
156
157 return false;
158 }
159
165 public function getImChatConferenceUrl(): ?string
166 {
167 $confUrl = null;
168 $existedConf = $this->getExistedChatConference();
169
170 if (!is_array($existedConf) || (!empty($this->accessToken) && $this->isConferenceExpired($existedConf)))
171 {
172 $newConfResult = $this->requestNewChatConference();
173 if ($newConfResult->isSuccess())
174 {
175 $newConferenceData = $newConfResult->getData();
176
177 $confUrl = $newConferenceData['join_url'];
178 }
179 }
180 elseif (is_array($existedConf))
181 {
182 $confUrl = $existedConf['CONFERENCE_URL'];
183 }
184
185 return $confUrl;
186 }
187
188 private function getExistedChatConference(): ?array
189 {
190 if (Loader::includeModule('socialservices'))
191 {
192 $conf = ZoomMeetingTable::getList(array(
193 'filter' => array(
194 '=ENTITY_ID' => $this->chatId,
195 '=ENTITY_TYPE_ID' => $this->chatType,
196 ),
197 'select' => ['CONFERENCE_URL', 'CONFERENCE_EXTERNAL_ID'],
198 'limit' => 1
199 ))->fetch();
200
201 if (is_array($conf))
202 {
203 return $conf;
204 }
205 }
206
207 return null;
208 }
209
210 private function requestNewChatConference(): Result
211 {
212 $result = new Result();
213
214 $startTime = (new DateTime())
215 ->setTimeZone(new \DateTimeZone('UTC'))
216 ->add('1 MINUTE')
217 ->format(DATE_ATOM);
218
219 $randomSequence = new \Bitrix\Main\Type\RandomSequence($this->zoomChatName.$startTime);
220 $password = $randomSequence->randString(10);
221
222 $requestParams = [
223 'ENTITY_ID' => $this->chatId,
224 'ENTITY_TYPE_ID' => $this->chatType,
225 'topic' => $this->zoomChatName,
226 'type' => self::CONFERENCE_SCHEDULED_TYPE,
227 'start_time' => $startTime,
228 'duration' => self::DEFAULT_DURATION_MINUTES,
229 'password' => $password,
230 'settings' => [
231 'waiting_room' => 'false',
232 'participant_video' => 'true',
233 'host_video' => 'true',
234 'join_before_host' => 'true',
235 'approval_type' => "2",
236 ],
237 ];
238
239 if ($this->zoomSocServ instanceof \CSocServZoom)
240 {
241 $createResult = $this->zoomSocServ->createConference($requestParams);
242 if (!$createResult->isSuccess())
243 {
244 return $result->addErrors($createResult->getErrors());
245 }
246 $conferenceData = $createResult->getData();
247
248 }
249 else
250 {
251 return $result->addError(new Error('Could not create zoom instance'));
252 }
253
254 return $result->setData($conferenceData);
255 }
256
263 public function requestConferenceById(int $confId): ?array
264 {
265 $conference = null;
266 if ($this->zoomSocServ instanceof \CSocServZoom)
267 {
268 $conference = $this->zoomSocServ->getConferenceById($confId);
269 }
270
271 return $conference;
272 }
273
274 private function isConferenceExpired(array $confData): bool
275 {
276 $confId = $confData['CONFERENCE_EXTERNAL_ID'];
277 $conference = $this->requestConferenceById($confId);
278
279 if (is_array($conference))
280 {
281 return false;
282 }
283
284 $meeting = ZoomMeetingTable::getRow([
285 'filter' => [
286 '=CONFERENCE_EXTERNAL_ID' => $confId,
287 '=ENTITY_TYPE_ID' => $this->chatType,
288 ],
289 'select' => ['ID'],
290 ]);
291
292 if ($meeting !== null)
293 {
294 ZoomMeetingTable::delete($meeting['ID']);
295 }
296
297 return true;
298 }
299
312 public function getRichMessageFields($dialogId, string $link, int $userId): array
313 {
314 $chatId = Dialog::getChatId($dialogId);
315 $attach = new \CIMMessageParamAttach(null, \CIMMessageParamAttach::CHAT);
316
317 $messageFields = [
318 'SYSTEM' => 'Y',
319 'URL_PREVIEW' => 'N',
320 'MESSAGE' => '[B]'.Loc::getMessage('IM_ZOOM_MESSAGE_CONFERENCE_CREATED').'[/B]',
321 ];
322
323 //chat
324 if (Common::isChatId($dialogId))
325 {
326 $chatData = \Bitrix\Im\Chat::getById($chatId);
327 $richChatData = [
328 'NAME' => $chatData['NAME'],
329 'CHAT_ID' => $chatId
330 ];
331 $attach->AddChat($richChatData);
332 $attach->AddDelimiter(['SIZE' => 300, 'COLOR' => '#c6c6c6']);
333
334 $messageFields['FROM_USER_ID'] = 0;
335 $messageFields['DIALOG_ID'] = $dialogId;
336 $messageFields['MESSAGE_TYPE'] = IM_MESSAGE_CHAT;
337 }
338 else //dialog
339 {
340 $messageFields['FROM_USER_ID'] = $userId;
341 $messageFields['TO_USER_ID'] = $dialogId;
342 $messageFields['TO_CHAT_ID'] = $chatId;
343 $messageFields['MESSAGE_TYPE'] = IM_MESSAGE_PRIVATE;
344 }
345
346 $attach->AddMessage(Loc::getMessage('IM_ZOOM_MESSAGE_JOIN_LINK'));
347 $attach->AddLink(['LINK' => $link]);
348 $messageFields['ATTACH'] = $attach;
349
350 return $messageFields;
351 }
352
353 private function prepareZoomChatName($dialogId): string
354 {
355 //chat
356 if (\Bitrix\Im\Common::isChatId($dialogId))
357 {
358 $chatInfo = \Bitrix\Im\Chat::getById($this->chatId);
359 $zoomChatName = "Bitrix24: " . $chatInfo['NAME'];
360 }
361 else //dialog
362 {
363 $chatUsers = \Bitrix\Im\Chat::getUsers(Dialog::getChatId($dialogId));
364 foreach ($chatUsers as $chatUser)
365 {
366 $usersLastNames[] = $chatUser["last_name"];
367 }
368
369 if (isset($usersLastNames))
370 {
371 $usersLastNames = implode(" <-> ", $usersLastNames);
372 $zoomChatName = "Bitrix24: ".$usersLastNames;
373 }
374 else
375 {
376 $zoomChatName = "Bitrix24";
377 }
378 }
379
380 return $zoomChatName;
381 }
382}
requestConferenceById(int $confId)
Definition zoom.php:263
__construct(int $userId, string $chatId)
Definition zoom.php:42
getRichMessageFields($dialogId, string $link, int $userId)
Definition zoom.php:312
static isConnected($userId)
Definition zoom.php:104
static isChatId($id)
Definition common.php:64
static getChatId($dialogId, $userId=null)
Definition dialog.php:91
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29