Bitrix-D7
23.9
Загрузка...
Поиск...
Не найдено
chat.php
1
<?php
2
3
namespace
Bitrix\Im\Call\Integration
;
4
5
use
Bitrix\Im\Call\Call
;
6
use
Bitrix\Im\Call\CallUser
;
7
use
Bitrix\Im\Common
;
8
use
Bitrix\Im\Dialog
;
9
use
Bitrix\Im\V2\Message
;
10
use
Bitrix\Im\V2\Message\Params
;
11
use
Bitrix\Main\Application
;
12
use
Bitrix\Main\ArgumentException
;
13
use
Bitrix\Main\Localization\Loc
;
14
use
Bitrix\Main\UserTable
;
15
16
class
Chat
extends
AbstractEntity
17
{
18
protected
$chatId
;
19
public
$chatFields
;
20
protected
$chatUsers
= [];
21
22
const
MUTE_MESSAGE
=
true
;
23
24
public
function
__construct
(
Call
$call
,
$entityId
)
25
{
26
parent::__construct(
$call
,
$entityId
);
27
28
if
(
Common::isChatId
(
$entityId
) || (
int
)
$entityId
> 0)
29
{
30
$chatId
= \Bitrix\Im\Dialog::getChatId(
$entityId
, $this->initiatorId);
31
}
32
else
33
{
34
throw
new
ArgumentException
(
"Invalid chat id {$entityId}"
);
35
}
36
37
$result = \CIMChat::GetChatData([
38
'ID'
=>
$chatId
,
39
'USER_ID'
=> $this->
getCurrentUserId
(),
40
]);
41
42
if
($result[
'chat'
][
$chatId
])
43
{
44
$this->chatFields = $result[
'chat'
][
$chatId
];
45
}
46
if
(is_array($result[
'userInChat'
][
$chatId
]))
47
{
48
$users = $result[
'userInChat'
][
$chatId
];
49
$activeRealUsers = UserTable::getList([
50
'select'
=> [
'ID'
],
51
'filter'
=> [
52
'ID'
=> $users,
53
'=ACTIVE'
=>
'Y'
,
54
[
55
'LOGIC'
=>
'OR'
,
56
'=IS_REAL_USER'
=>
'Y'
,
57
'=EXTERNAL_AUTH_ID'
=> \
Bitrix
\Im\
Call
\
Auth::AUTH_TYPE
,
58
]
59
60
]
61
])->fetchAll();
62
$this->chatUsers = array_column($activeRealUsers,
'ID'
);
63
}
64
$this->chatId =
$chatId
;
65
}
66
72
public
function
getEntityType
()
73
{
74
return
EntityType::CHAT
;
75
}
76
77
public
function
getEntityId
($currentUserId = 0)
78
{
79
if
($this->chatFields[
'message_type'
] != IM_MESSAGE_PRIVATE || $currentUserId == 0)
80
{
81
return
$this->entityId
;
82
}
83
else
84
{
85
return
$this->call->getInitiatorId() == $currentUserId ? $this->entityId : $this->call->getInitiatorId();
86
}
87
}
88
89
public
function
getChatId
()
90
{
91
return
$this->chatId
;
92
}
93
99
public
function
getUsers
()
100
{
101
return
$this->chatUsers
;
102
}
103
110
public
function
checkAccess
(
int
$userId): bool
111
{
112
if
(
Common::isChatId
($this->entityId))
113
{
114
return
Dialog::hasAccess
($this->entityId, $userId);
115
}
116
117
// one-to-one dialog
118
return
($userId === (
int
)$this->entityId || $userId === (
int
)$this->initiatorId);
119
}
120
127
public
function
canStartCall
(
int
$userId): bool
128
{
129
if
(
Common::isChatId
($this->entityId))
130
{
131
return
Dialog::hasAccess
($this->entityId, $userId);
132
}
133
134
if
(
135
\CIMSettings::GetPrivacy(\CIMSettings::PRIVACY_CALL) == \CIMSettings::PRIVACY_RESULT_CONTACT
136
&& \CModule::IncludeModule(
'socialnetwork'
)
137
&& \CSocNetUser::IsFriendsAllowed()
138
&& !\CSocNetUserRelations::IsFriends($this->entityId, $userId)
139
)
140
{
141
return
false
;
142
}
143
144
if
(
145
\CIMSettings::GetPrivacy(\CIMSettings::PRIVACY_CALL, $this->entityId) === \CIMSettings::PRIVACY_RESULT_CONTACT
146
&& \CModule::IncludeModule(
'socialnetwork'
)
147
&& \CSocNetUser::IsFriendsAllowed()
148
&& !\CSocNetUserRelations::IsFriends($this->entityId, $userId)
149
)
150
{
151
return
false
;
152
}
153
154
return
true
;
155
}
156
163
public
function
getName
($currentUserId)
164
{
165
if
(!$this->chatFields)
166
{
167
return
false
;
168
}
169
170
if
($this->chatFields[
'message_type'
] === IM_MESSAGE_PRIVATE && count($this->chatUsers) === 2)
171
{
172
return \Bitrix\Im\User::getInstance($this->
getEntityId
($currentUserId))->getFullName();
173
}
174
if
($this->chatFields[
'message_type'
] !== IM_MESSAGE_PRIVATE)
175
{
176
return
$this->chatFields[
'name'
];
177
}
178
179
return
false
;
180
}
181
182
public
function
getAvatar
($currentUserId)
183
{
184
if
(!$this->chatFields)
185
{
186
return
false
;
187
}
188
189
if
($this->chatFields[
'message_type'
] === IM_MESSAGE_PRIVATE && count($this->chatUsers) === 2)
190
{
191
return \Bitrix\Im\User::getInstance($this->
getEntityId
($currentUserId))->getAvatarHr();
192
}
193
if
($this->chatFields[
'message_type'
] !== IM_MESSAGE_PRIVATE)
194
{
195
return
$this->chatFields[
'avatar'
];
196
}
197
198
return
false
;
199
}
200
201
public
function
getAvatarColor
($currentUserId)
202
{
203
if
(!$this->chatFields)
204
{
205
return
false
;
206
}
207
208
if
($this->chatFields[
'message_type'
] === IM_MESSAGE_PRIVATE && count($this->chatUsers) === 2)
209
{
210
return \Bitrix\Im\User::getInstance($this->
getEntityId
($currentUserId))->getColor();
211
}
212
if
($this->chatFields[
'message_type'
] !== IM_MESSAGE_PRIVATE)
213
{
214
return
$this->chatFields[
'color'
];
215
}
216
217
return
false
;
218
}
219
220
public
function
isPrivateChat
(): bool
221
{
222
return
$this->chatFields && $this->chatFields[
'message_type'
] === IM_MESSAGE_PRIVATE;
223
}
224
225
public
function
onUserAdd
($userId): bool
226
{
227
if
(!$this->
canExtendChat
())
228
{
229
return
false
;
230
}
231
if
($this->chatFields[
'message_type'
] == IM_MESSAGE_PRIVATE)
232
{
233
$chat = new \CIMChat();
234
235
$users =
$this->chatUsers
;
236
$users[] = $userId;
237
238
$chatId
= $chat->add([
'USERS'
=> $users]);
239
if
(!
$chatId
)
240
{
241
return
false
;
242
}
243
244
if
($this->call)
245
{
246
$this->call->setAssociatedEntity(static::getEntityType(),
'chat'
.
$chatId
);
247
// todo: remove when the calls are supported in the mobile
248
if
($this->call->getAssociatedEntity())
249
{
250
$this->call->getAssociatedEntity()->onCallCreate();
251
}
252
}
253
}
254
else
255
{
256
$chat = new \CIMChat();
257
$chatId
= \Bitrix\Im\Dialog::getChatId($this->
getEntityId
());
258
$result = $chat->addUser(
$chatId
, $userId);
259
}
260
261
return
true
;
262
}
263
264
public
function
onExistingUserInvite
($userId): bool
265
{
266
if
(isset($this->chatFields[
'message_type'
]) && $this->chatFields[
'message_type'
] === IM_MESSAGE_PRIVATE)
267
{
268
return
true
;
269
}
270
271
if
(!$this->
canExtendChat
())
272
{
273
return
false
;
274
}
275
276
$chat = new \CIMChat();
277
$chatId
= \Bitrix\Im\Dialog::getChatId($this->
getEntityId
());
278
279
return
$chat->addUser(
$chatId
, $userId);
280
}
281
282
public
function
onStateChange
($state, $prevState)
283
{
284
$initiatorId
= $this->call->getInitiatorId();
285
$initiator = \Bitrix\Im\User::getInstance(
$initiatorId
);
286
if
($state ===
Call::STATE_INVITING
&& $prevState ===
Call::STATE_NEW
)
287
{
288
// todo: return the call method when the calls are supported in the mobile
289
//$this->sendMessagesCallStart();
290
}
291
else
if
($state ===
Call::STATE_FINISHED
)
292
{
293
$message =
Loc::getMessage
(
"IM_CALL_INTEGRATION_CHAT_CALL_FINISHED"
);
294
$mute =
true
;
295
296
$userIds = array_values(array_filter($this->call->getUsers(),
function
($userId) use (
$initiatorId
)
297
{
298
return
$userId !=
$initiatorId
;
299
}));
300
301
if
(count($userIds) == 1)
302
{
303
$otherUser = \Bitrix\Im\User::getInstance($userIds[0]);
304
$otherUserState = $this->call->getUser($userIds[0]) ? $this->call->getUser($userIds[0])->getState() :
''
;
305
if
($otherUserState ==
CallUser::STATE_DECLINED
)
306
{
307
$message =
Loc::getMessage
(
"IM_CALL_INTEGRATION_CHAT_CALL_USER_DECLINED_"
. $otherUser->getGender(), [
308
'#NAME#'
=> $otherUser->getFullName(
false
)
309
]);
310
}
311
else
if
($otherUserState ==
CallUser::STATE_BUSY
)
312
{
313
$message =
Loc::getMessage
(
"IM_CALL_INTEGRATION_CHAT_CALL_USER_BUSY_"
. $otherUser->getGender(), [
314
'#NAME#'
=> $otherUser->getFullName(
false
)
315
]);
316
$mute =
false
;
317
}
318
else
if
($otherUserState ==
CallUser::STATE_UNAVAILABLE
|| $otherUserState ==
CallUser::STATE_CALLING
)
319
{
320
$message =
Loc::getMessage
(
"IM_CALL_INTEGRATION_CHAT_CALL_MISSED_"
. $initiator->getGender(), [
321
'#NAME#'
=> $initiator->getFullName(
false
)
322
]);
323
$mute =
false
;
324
}
325
}
326
327
$this->
sendMessageDeferred
($message, $mute);
328
}
329
}
330
331
public
function
onCallCreate
(): bool
332
{
333
$this->
sendMessagesCallStart
();
334
335
return
true
;
336
}
337
338
public
function
sendMessagesCallStart
(): void
339
{
340
$this->
sendMessageDeferred
(
Loc::getMessage
(
"IM_CALL_INTEGRATION_CHAT_CALL_STARTED"
, [
341
"#ID#"
=>
'[B]'
.$this->call->getId().
'[/B]'
342
]), self::MUTE_MESSAGE);
343
}
344
345
public
function
sendMessageDeferred
($message, $muted =
false
)
346
{
347
Application::getInstance
()->addBackgroundJob([$this,
'sendMessage'
], [$message, $muted]);
348
}
349
350
public
function
isBroadcast
()
351
{
352
return
$this->chatFields[
'entity_type'
] === \
Bitrix\Im\Alias::ENTITY_TYPE_VIDEOCONF
353
&& $this->chatFields[
'entity_data_1'
] ===
'BROADCAST'
354
;
355
}
356
357
public
function
sendMessage
($message, $muted =
false
)
358
{
359
\CIMMessenger::add([
360
'DIALOG_ID'
=> $this->entityId,
361
'FROM_USER_ID'
=> $this->
getCall
()->getInitiatorId(),
362
'MESSAGE'
=> $message,
363
'SYSTEM'
=>
'Y'
,
364
'PUSH'
=>
'N'
,
365
'PARAMS'
=> [
366
'NOTIFY'
=> $muted?
'N'
:
'Y'
,
367
]
368
]);
369
}
370
371
public
function
toArray
($currentUserId = 0)
372
{
373
if
($currentUserId == 0)
374
{
375
$currentUserId =
$this->initiatorId
;
376
}
377
378
return
[
379
'type'
=> $this->
getEntityType
(),
380
'id'
=> $this->
getEntityId
($currentUserId),
381
'name'
=> $this->
getName
($currentUserId),
382
'avatar'
=> $this->
getAvatar
($currentUserId),
383
'avatarColor'
=> $this->
getAvatarColor
($currentUserId),
384
'advanced'
=> [
385
'chatType'
=> $this->chatFields[
'type'
],
386
'entityType'
=> $this->chatFields[
'entity_type'
],
387
'entityId'
=> $this->chatFields[
'entity_id'
],
388
'entityData1'
=> $this->chatFields[
'entity_data_1'
],
389
'entityData2'
=> $this->chatFields[
'entity_data_2'
],
390
'entityData3'
=> $this->chatFields[
'entity_data_3'
]
391
]
392
];
393
}
394
395
public
function
canExtendChat
(): bool
396
{
397
if
(!$this->chatFields)
398
{
399
return
false
;
400
}
401
if
($this->chatFields[
'message_type'
] === IM_MESSAGE_PRIVATE)
402
{
403
return
true
;
404
}
405
$entityType = $this->chatFields[
'entity_type'
];
406
$options = \CIMChat::GetChatOptions();
407
return
(
bool
)($options[$entityType][
'EXTEND'
] ??
true
);
408
}
409
}
Bitrix\Im\Alias\ENTITY_TYPE_VIDEOCONF
const ENTITY_TYPE_VIDEOCONF
Definition
alias.php:13
Bitrix\Im\Call\Auth\AUTH_TYPE
const AUTH_TYPE
Definition
auth.php:13
Bitrix\Im\Call\Call
Definition
call.php:22
Bitrix\Im\Call\Call\STATE_FINISHED
const STATE_FINISHED
Definition
call.php:26
Bitrix\Im\Call\Call\STATE_INVITING
const STATE_INVITING
Definition
call.php:24
Bitrix\Im\Call\Call\STATE_NEW
const STATE_NEW
Definition
call.php:23
Bitrix\Im\Call\CallUser
Definition
calluser.php:10
Bitrix\Im\Call\CallUser\STATE_BUSY
const STATE_BUSY
Definition
calluser.php:16
Bitrix\Im\Call\CallUser\STATE_DECLINED
const STATE_DECLINED
Definition
calluser.php:15
Bitrix\Im\Call\CallUser\STATE_CALLING
const STATE_CALLING
Definition
calluser.php:14
Bitrix\Im\Call\CallUser\STATE_UNAVAILABLE
const STATE_UNAVAILABLE
Definition
calluser.php:12
Bitrix\Im\Call\Integration\AbstractEntity
Definition
abstractentity.php:8
Bitrix\Im\Call\Integration\AbstractEntity\$call
$call
Definition
abstractentity.php:12
Bitrix\Im\Call\Integration\AbstractEntity\getCall
getCall()
Definition
abstractentity.php:29
Bitrix\Im\Call\Integration\AbstractEntity\$entityId
$entityId
Definition
abstractentity.php:9
Bitrix\Im\Call\Integration\AbstractEntity\$initiatorId
$initiatorId
Definition
abstractentity.php:10
Bitrix\Im\Call\Integration\AbstractEntity\getCurrentUserId
getCurrentUserId()
Definition
abstractentity.php:76
Bitrix\Im\Call\Integration\Chat
Definition
chat.php:17
Bitrix\Im\Call\Integration\Chat\getChatId
getChatId()
Definition
chat.php:89
Bitrix\Im\Call\Integration\Chat\getUsers
getUsers()
Definition
chat.php:99
Bitrix\Im\Call\Integration\Chat\getAvatarColor
getAvatarColor($currentUserId)
Definition
chat.php:201
Bitrix\Im\Call\Integration\Chat\isPrivateChat
isPrivateChat()
Definition
chat.php:220
Bitrix\Im\Call\Integration\Chat\canStartCall
canStartCall(int $userId)
Definition
chat.php:127
Bitrix\Im\Call\Integration\Chat\canExtendChat
canExtendChat()
Definition
chat.php:395
Bitrix\Im\Call\Integration\Chat\onExistingUserInvite
onExistingUserInvite($userId)
Definition
chat.php:264
Bitrix\Im\Call\Integration\Chat\onCallCreate
onCallCreate()
Definition
chat.php:331
Bitrix\Im\Call\Integration\Chat\onUserAdd
onUserAdd($userId)
Definition
chat.php:225
Bitrix\Im\Call\Integration\Chat\__construct
__construct(Call $call, $entityId)
Definition
chat.php:24
Bitrix\Im\Call\Integration\Chat\sendMessagesCallStart
sendMessagesCallStart()
Definition
chat.php:338
Bitrix\Im\Call\Integration\Chat\$chatFields
$chatFields
Definition
chat.php:19
Bitrix\Im\Call\Integration\Chat\getName
getName($currentUserId)
Definition
chat.php:163
Bitrix\Im\Call\Integration\Chat\onStateChange
onStateChange($state, $prevState)
Definition
chat.php:282
Bitrix\Im\Call\Integration\Chat\isBroadcast
isBroadcast()
Definition
chat.php:350
Bitrix\Im\Call\Integration\Chat\checkAccess
checkAccess(int $userId)
Definition
chat.php:110
Bitrix\Im\Call\Integration\Chat\sendMessage
sendMessage($message, $muted=false)
Definition
chat.php:357
Bitrix\Im\Call\Integration\Chat\toArray
toArray($currentUserId=0)
Definition
chat.php:371
Bitrix\Im\Call\Integration\Chat\$chatUsers
$chatUsers
Definition
chat.php:20
Bitrix\Im\Call\Integration\Chat\sendMessageDeferred
sendMessageDeferred($message, $muted=false)
Definition
chat.php:345
Bitrix\Im\Call\Integration\Chat\getEntityId
getEntityId($currentUserId=0)
Definition
chat.php:77
Bitrix\Im\Call\Integration\Chat\MUTE_MESSAGE
const MUTE_MESSAGE
Definition
chat.php:22
Bitrix\Im\Call\Integration\Chat\getEntityType
getEntityType()
Definition
chat.php:72
Bitrix\Im\Call\Integration\Chat\getAvatar
getAvatar($currentUserId)
Definition
chat.php:182
Bitrix\Im\Call\Integration\Chat\$chatId
$chatId
Definition
chat.php:18
Bitrix\Im\Call\Integration\EntityType\CHAT
const CHAT
Definition
entitytype.php:12
Bitrix\Im\Common
Definition
common.php:7
Bitrix\Im\Common\isChatId
static isChatId($id)
Definition
common.php:64
Bitrix\Im\Dialog
Definition
dialog.php:9
Bitrix\Im\Dialog\hasAccess
static hasAccess($dialogId, $userId=null)
Definition
dialog.php:143
Bitrix\Im\V2\Message\Params
Definition
Params.php:22
Bitrix\Main\Application
Definition
application.php:28
Bitrix\Main\Application\getInstance
static getInstance()
Definition
application.php:95
Bitrix\Main\ArgumentException
Definition
exception.php:34
Bitrix\Main\Localization\Loc
Definition
loc.php:11
Bitrix\Main\Localization\Loc\getMessage
static getMessage($code, $replace=null, $language=null)
Definition
loc.php:29
Bitrix\Main\UserTable
Definition
user.php:46
Bitrix\Im\Call\Integration
Definition
abstractentity.php:3
Bitrix\Im\V2\Message
Definition
AdditionalMessagePopupItem.php:3
Bitrix
modules
im
lib
call
integration
chat.php
Создано системой
1.10.0