Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
signaling.php
1<?php
2
3namespace Bitrix\Im\Call;
4
9
11{
12 protected $call;
13
14 public function __construct(Call $call)
15 {
16 $this->call = $call;
17 }
18
19 public function sendInviteToUser(int $senderId, int $toUserId, $invitedUsers, $isLegacyMobile, bool $video = false, bool $sendPush = true)
20 {
21 $users = $this->call->getUsers();
22
23 $parentCall = $this->call->getParentId() ? Call::loadWithId($this->call->getParentId()) : null;
24 $skipPush = $parentCall ? $parentCall->getUsers() : [];
25 $skipPush = array_flip($skipPush);
26
27 $associatedEntity = $this->call->getAssociatedEntity();
28 $isBroadcast = ($associatedEntity instanceof Chat) && $associatedEntity->isBroadcast();
29
30 $config = [
31 'call' => $this->call->toArray(($senderId == $toUserId ? $toUserId : 0)),
32 'users' => $users,
33 'invitedUsers' => $invitedUsers,
34 'userData' => Util::getUsers($users),
35 'senderId' => $senderId,
36 'publicIds' => $this->getPublicIds([$toUserId]),
37 'isLegacyMobile' => $isLegacyMobile,
38 'video' => $video,
39 'logToken' => $this->call->getLogToken($toUserId),
40 ];
41 $connectionData = $this->call->getConnectionData($toUserId);
42 if ($connectionData !== null)
43 {
44 $config['connectionData'] = $connectionData;
45 }
46
47 if (!isset($skipPush[$toUserId]) && $sendPush && !$isBroadcast)
48 {
49 $push = $this->getInvitePush($senderId, $toUserId, $isLegacyMobile, $video);
50 }
51
52 $this->send('Call::incoming', $toUserId, $config, $push);
53 }
54
55 public function sendInvite(int $senderId, array $toUserIds, $isLegacyMobile, bool $video = false, bool $sendPush = true)
56 {
57 $users = $this->call->getUsers();
58
59 $parentCall = $this->call->getParentId() ? Call::loadWithId($this->call->getParentId()) : null;
60 $skipPush = $parentCall ? $parentCall->getUsers() : [];
61 $skipPush = array_flip($skipPush);
62
63 $associatedEntity = $this->call->getAssociatedEntity();
64 $isBroadcast = ($associatedEntity instanceof Chat) && $associatedEntity->isBroadcast();
65
66 foreach ($toUserIds as $toUserId)
67 {
68 $config = [
69 'call' => $this->call->toArray((count($toUserIds) == 1 ? $toUserId : 0)),
70 'users' => $users,
71 'invitedUsers' => $toUserIds,
72 'userData' => Util::getUsers($users),
73 'senderId' => $senderId,
74 'publicIds' => $this->getPublicIds($users),
75 'isLegacyMobile' => $isLegacyMobile,
76 'video' => $video,
77 'logToken' => $this->call->getLogToken($toUserId),
78 ];
79 if (!isset($skipPush[$toUserId]) && $sendPush && !$isBroadcast)
80 {
81 $push = $this->getInvitePush($senderId, $toUserId, $isLegacyMobile, $video);
82 }
83
84 $this->send('Call::incoming', $toUserId, $config, $push);
85 }
86 }
87
88 protected function getInvitePush(int $senderId, int $toUserId, $isLegacyMobile, $video)
89 {
90 $users = $this->call->getUsers();
91 $associatedEntity = $this->call->getAssociatedEntity();
92 $name = $associatedEntity ? $associatedEntity->getName($toUserId) : Loc::getMessage('IM_CALL_INVITE_NA');
93
94 $email = null;
95 $phone = null;
96 if ($associatedEntity instanceof Chat)
97 {
98 if ($associatedEntity->isPrivateChat())
99 {
100 $userInstance = \Bitrix\Im\User::getInstance($senderId);
101 $email = $userInstance->getEmail();
102 $phone = $userInstance->getPhone();
103 $phone = preg_replace("/[^0-9#*+,;]/", "", $phone);
104 }
105 $avatar = $associatedEntity->getAvatar($toUserId);
106 }
107
108 $pushText = Loc::getMessage('IM_CALL_INVITE', ['#USER_NAME#' => $name]);
109 $pushTag = 'IM_CALL_'.$this->call->getId();
110 $push = [
111 'message' => $pushText,
112 'expiry' => 0,
113 'params' => [
114 'ACTION' => 'IMINV_'.$this->call->getId()."_".time()."_".($video ? 'Y' : 'N'),
115 'PARAMS' => [
116 'type' => 'internal',
117 'callerName' => htmlspecialcharsback($name),
118 'callerAvatar' => $avatar ?? '',
119 'call' => $this->call->toArray($toUserId),
120 'video' => $video,
121 'users' => $users,
122 'isLegacyMobile' => $isLegacyMobile,
123 'senderId' => $senderId,
124 'senderEmail' => $email,
125 'senderPhone' => $phone,
126 'logToken' => $this->call->getLogToken($toUserId),
127 'ts' => time(),
128 ]
129 ],
130 'advanced_params' => [
131 'id' => $pushTag,
132 'notificationsToCancel' => [$pushTag],
133 'androidHighPriority' => true,
134 'useVibration' => true,
135 'isVoip' => true,
136 'callkit' => true,
137 ],
138 'sound' => 'call.aif',
139 'send_immediately' => 'Y'
140 ];
141
142 return $push;
143 }
144
145 public function sendUsersJoined(int $senderId, array $joinedUsers)
146 {
147 $config = array(
148 'call' => $this->call->toArray(),
149 'users' => $joinedUsers,
150 'userData' => Util::getUsers($joinedUsers),
151 'senderId' => $senderId,
152 'publicIds' => $this->getPublicIds($joinedUsers),
153 );
154
155 return $this->send('Call::usersJoined', $this->call->getUsers(), $config);
156
157 }
158
159 public function sendUsersInvited(int $senderId, array $toUserIds, array $users)
160 {
161 $config = array(
162 'call' => $this->call->toArray(),
163 'users' => $users,
164 'userData' => Util::getUsers($users),
165 'senderId' => $senderId,
166 'publicIds' => $this->getPublicIds($users),
167 );
168
169 return $this->send('Call::usersInvited', $toUserIds, $config);
170 }
171
172 public function sendAssociatedEntityReplaced(int $senderId)
173 {
174 $config = array(
175 'call' => $this->call->toArray(),
176 'senderId' => $senderId,
177 );
178
179 $toUserIds = $this->call->getUsers();
180
181 return $this->send('Call::associatedEntityReplaced', $toUserIds, $config);
182 }
183
184 public function sendAnswer(int $senderId, $callInstanceId, $isLegacyMobile)
185 {
186 $config = array(
187 'call' => $this->call->toArray(),
188 'senderId' => $senderId,
189 'callInstanceId' => $callInstanceId,
190 'isLegacyMobile' => $isLegacyMobile,
191 );
192
193 $toUserIds = array_diff($this->call->getUsers(), [$senderId]);
194 $this->send('Call::answer', $toUserIds, $config, null, 3600);
195
196 $push = [
197 'send_immediately' => 'Y',
198 'expiry' => 0,
199 'params' => [],
200 'advanced_params' => [
201 'id' => 'IM_CALL_'.$this->call->getId().'_ANSWER',
202 'notificationsToCancel' => ['IM_CALL_'.$this->call->getId()],
203 'isVoip' => true,
204 'callkit' => true,
205 'filterCallback' => [static::class, 'filterPushesForApple'],
206 ]
207 ];
208
209 $this->send('Call::answer', $senderId, $config, $push, 3600);
210 }
211
212 public function sendPing(int $senderId, $requestId)
213 {
214 $config = array(
215 'requestId' => $requestId,
216 'callId' => $this->call->getId(),
217 'senderId' => $senderId
218 );
219
220 $toUserIds = $this->call->getUsers();
221 $toUserIds = array_filter($toUserIds, function ($value) use ($senderId) {
222 return $value != $senderId;
223 });
224 return $this->send('Call::ping', $toUserIds, $config, null, 0);
225 }
226
227 public function sendNegotiationNeeded(int $senderId, int $toUserId, $restart)
228 {
229 return $this->send('Call::negotiationNeeded', $toUserId, array(
230 'senderId' => $senderId,
231 'restart' => $restart
232 ));
233 }
234
235 public function sendConnectionOffer(int $senderId, int $toUserId, string $connectionId, string $offerSdp, string $userAgent)
236 {
237 return $this->send('Call::connectionOffer', $toUserId, array(
238 'senderId' => $senderId,
239 'connectionId' => $connectionId,
240 'sdp' => $offerSdp,
241 'userAgent' => $userAgent
242 ));
243 }
244
245 public function sendConnectionAnswer(int $senderId, int $toUserId, string $connectionId, string $answerSdp, string $userAgent)
246 {
247 return $this->send('Call::connectionAnswer', $toUserId, array(
248 'senderId' => $senderId,
249 'connectionId' => $connectionId,
250 'sdp' => $answerSdp,
251 'userAgent' => $userAgent
252 ));
253 }
254
255 public function sendIceCandidates(int $senderId, int $toUserId, string $connectionId, array $iceCandidates)
256 {
257 return $this->send('Call::iceCandidate', $toUserId, array(
258 'senderId' => $senderId,
259 'connectionId' => $connectionId,
260 'candidates' => $iceCandidates
261 ));
262 }
263
264 public function sendHangup(int $senderId, array $toUserIds, ?string $callInstanceId, $code = 200)
265 {
266 $config = [
267 'senderId' => $senderId,
268 'callInstanceId' => $callInstanceId,
269 'code' => $code,
270 ];
271
272 $push = [
273 'send_immediately' => 'Y',
274 //'expiry' => 0,
275 'params' => [],
276 'advanced_params' => [
277 'id' => 'IM_CALL_'.$this->call->getId().'_FINISH',
278 'notificationsToCancel' => ['IM_CALL_'.$this->call->getId()],
279 'callkit' => true,
280 'filterCallback' => [static::class, 'filterPushesForApple'],
281 ]
282 ];
283
284 return $this->send('Call::hangup', $toUserIds, $config, $push, 3600);
285 }
286
287 public function sendFinish()
288 {
289 $push = [
290 'send_immediately' => 'Y',
291 //'expiry' => 0,
292 'params' => [],
293 'advanced_params' => [
294 'id' => 'IM_CALL_'.$this->call->getId().'_FINISH',
295 'notificationsToCancel' => ['IM_CALL_'.$this->call->getId()],
296 'callkit' => true,
297 'filterCallback' => [static::class, 'filterPushesForApple'],
298 ]
299 ];
300
301 return $this->send('Call::finish', $this->call->getUsers(), [], $push, 3600);
302 }
303
304 public static function filterPushesForApple($message, $deviceType, $deviceToken)
305 {
306 if (!Loader::includeModule('pull'))
307 {
308 return false;
309 }
310 $result = !in_array(
311 $deviceType,
312 [
313 \CPushDescription::TYPE_APPLE,
314 \CPushDescription::TYPE_APPLE_VOIP,
315 ],
316 true)
317 ;
318 return $result;
319 }
320
321 protected function getPublicIds(array $userIds)
322 {
323 if (!Loader::includeModule('pull'))
324 {
325 return [];
326 }
327
328 return \Bitrix\Pull\Channel::getPublicIds([
329 'USERS' => $userIds,
330 'JSON' => true
331 ]);
332 }
333
334 protected function send(string $command, $users, array $params = [], $push = null, $ttl = 5)
335 {
336 if (!Loader::includeModule('pull'))
337 return false;
338
339 if (!isset($params['call']))
340 $params['call'] = ['ID' => $this->call->getId()];
341
342 if (!isset($params['callId']))
343 $params['callId'] = $this->call->getId();
344
345 \Bitrix\Pull\Event::add($users, array(
346 'module_id' => 'im',
347 'command' => $command,
348 'params' => $params,
349 'push' => $push,
350 'expiry' => $ttl
351 ));
352
353 return true;
354 }
355}
static loadWithId($id)
Definition call.php:859
sendHangup(int $senderId, array $toUserIds, ?string $callInstanceId, $code=200)
getPublicIds(array $userIds)
getInvitePush(int $senderId, int $toUserId, $isLegacyMobile, $video)
Definition signaling.php:88
static filterPushesForApple($message, $deviceType, $deviceToken)
sendUsersInvited(int $senderId, array $toUserIds, array $users)
sendAnswer(int $senderId, $callInstanceId, $isLegacyMobile)
sendConnectionOffer(int $senderId, int $toUserId, string $connectionId, string $offerSdp, string $userAgent)
sendAssociatedEntityReplaced(int $senderId)
send(string $command, $users, array $params=[], $push=null, $ttl=5)
sendInviteToUser(int $senderId, int $toUserId, $invitedUsers, $isLegacyMobile, bool $video=false, bool $sendPush=true)
Definition signaling.php:19
sendPing(int $senderId, $requestId)
sendUsersJoined(int $senderId, array $joinedUsers)
sendInvite(int $senderId, array $toUserIds, $isLegacyMobile, bool $video=false, bool $sendPush=true)
Definition signaling.php:55
sendConnectionAnswer(int $senderId, int $toUserId, string $connectionId, string $answerSdp, string $userAgent)
sendIceCandidates(int $senderId, int $toUserId, string $connectionId, array $iceCandidates)
sendNegotiationNeeded(int $senderId, int $toUserId, $restart)
static getUsers(array $idList)
Definition util.php:10
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29