1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
OpenLineChat.php
См. документацию.
1<?php
2
3namespace Bitrix\Im\V2\Chat;
4
5use Bitrix\Im\Recent;
6use Bitrix\Im\V2\Entity\User\User;
7use Bitrix\Im\V2\Message;
8use Bitrix\Im\V2\Message\Send\SendingConfig;
9use Bitrix\Im\V2\MessageCollection;
10use Bitrix\Im\V2\Relation\AddUsersConfig;
11use Bitrix\Im\V2\Relation\DeleteUserConfig;
12use Bitrix\Im\V2\RelationCollection;
13use Bitrix\Im\V2\Result;
14use Bitrix\ImOpenLines\Config;
15use Bitrix\ImOpenLines\Model\ChatIndexTable;
16use Bitrix\ImOpenLines;
17use Bitrix\Main\Application;
18use Bitrix\Main\Loader;
19use Bitrix\Pull\Event;
20
22{
23 protected const EXTRANET_CAN_SEE_HISTORY = true;
24
25 protected $entityMap = [
26 'entityId' => [
27 'connectorId',
28 'lineId',
29 'connectorChatId',
30 'connectorUserId',
31 ],
32 'entityData1' => [
33 'crmEnabled',
34 'crmEntityType',
35 'crmEntityId',
36 'pause',
37 'waitAction',
38 'sessionId',
39 'dateCreate',
40 'lineId',
41 'blockDate',
42 'blockReason',
43 ],
44 'entityData2' => [
45 'u0',
46 'leadId',
47 'u2',
48 'companyId',
49 'u4',
50 'contactId',
51 'u6',
52 'dealId',
53 ],
54 'entityData3' => [
55 'silentMode',
56 ],
57 ];
58 protected bool $isSessionFilled = false;
60 protected ?ImOpenLines\V2\Session\Session $session = null;
61
63 {
64 return $this;
65 }
66
67 public function read(bool $onlyRecent = false, bool $byEvent = false, bool $forceRead = false): Result
68 {
69 Recent::unread($this->getDialogId(), false, $this->getContext()->getUserId());
70
71 if ($onlyRecent)
72 {
73 $lastId = $this->getReadService()->getLastMessageIdInChat($this->chatId);
74
75 return (new Result())->setResult([
76 'CHAT_ID' => $this->chatId,
77 'LAST_ID' => $lastId,
78 'COUNTER' => $this->getReadService()->getCounterService()->getByChat($this->chatId),
79 'VIEWED_MESSAGES' => [],
80 ]);
81 }
82
83 return $this->readAllMessages($byEvent, $forceRead);
84 }
85
86 public function readAllMessages(bool $byEvent = false, bool $forceRead = false): Result
87 {
88 $result = $this->readMessages(null, $byEvent, $forceRead);
89
90 $userId = $this->getContext()->getUserId();
91 Application::getInstance()->addBackgroundJob(function () use ($byEvent, $forceRead, $userId) {
92 $chat = $this->withContextUser($userId);
93
94 if ($chat->getSelfRelation() === null)
95 {
96 $chat->readMessages(null, $byEvent, $forceRead);
97 }
98 });
99
100 return $result;
101 }
102
103 public function readMessages(?MessageCollection $messages, bool $byEvent = false, bool $forceRead = false): Result
104 {
105 if (!$forceRead && $this->getAuthorId() === 0)
106 {
107 return new Result();
108 }
109
110 return parent::readMessages($messages, $byEvent);
111 }
112
114 {
115 if (isset($this->fakeRelation))
116 {
117 return $this->fakeRelation;
118 }
119
120 return parent::getRelations();
121 }
122
123 public function setFakeRelation(array $userIds): self
124 {
125 $this->fakeRelation = RelationCollection::createFake($userIds, $this);
126
127 return $this;
128 }
129
130 public function unsetFakeRelation(): self
131 {
132 $this->fakeRelation = null;
133
134 return $this;
135 }
136
138 {
139 if ($config->fakeRelation())
140 {
141 $this->setFakeRelation([$config->fakeRelation()]);
142 }
143 elseif (Loader::includeModule('imopenlines'))
144 {
145 $userIds = (new ImOpenLines\Relation($this->getId()))->getRelationUserIds();
146 if (!empty($userIds))
147 {
148 $this->setFakeRelation($userIds);
149 }
150 }
151
152 return parent::onBeforeMessageSend($message, $config);
153 }
154
155 public function getLineData(): ?array
156 {
157 $session = $this->getSession();
158
159 if (!isset($session))
160 {
161 return null;
162 }
163
164 return [
165 'id' => $session->getId(),
166 'status' => $session->getStatus(),
167 'date_create' => $session->getDateCreate(),
168 'statusGroup' => $session->getStatusGroup(),
169 'queueId' => $session->getConfigId(),
170 'pinned' => $session->getPinMode(),
171 'isClosed' => $session->isClosed(),
172 'operatorId' => $session->getOperatorId(),
173 ];
174 }
175
176 public function getSessionId(): int
177 {
178 if (!isset($this->session))
179 {
180 return $this->getSession()?->getSessionId() ?? 0;
181 }
182
183 return $this->session->getSessionId();
184 }
185
186 protected function getSessionIdByEntityData(): int
187 {
188 if ($this->getEntityData1())
189 {
190 return (int)(explode('|', $this->getEntityData1())[5] ?? 0);
191 }
192
193 return 0;
194 }
195
196 public function getSession(): ?ImOpenLines\V2\Session\Session
197 {
198 if (isset($this->session))
199 {
200 return $this->session;
201 }
202
203 if (!Loader::includeModule('imopenlines'))
204 {
205 return null;
206 }
207
208 if ($sessionId = $this->getSessionIdByEntityData())
209 {
210 $this->session = ImOpenLines\V2\Session\Session::getInstance($sessionId);
211 }
212 else
213 {
214 $this->session = ImOpenLines\V2\Session\Session::getInstanceByChatId($this->chatId);
215 }
216
217 return $this->session;
218 }
219
220 protected function getDefaultType(): string
221 {
222 return self::IM_TYPE_OPEN_LINE;
223 }
224
225 protected function getDefaultEntityType(): string
226 {
227 return self::ENTITY_TYPE_LINE;
228 }
229
230 protected function checkAccessInternal(int $userId): Result
231 {
232 $checkResult = parent::checkAccessInternal($userId);
233
234 if ($checkResult->isSuccess())
235 {
236 return $checkResult;
237 }
238
239 $result = new Result();
240
241 if (!Loader::includeModule('imopenlines'))
242 {
243 return $result->addError(new ChatError(ChatError::ACCESS_DENIED));
244 }
245
246 $entityData = $this->getEntityData(true);
247 $canJoin = Config::canJoin(
248 $entityData['entityId']['lineId'] ?? 0,
249 $entityData['crmEntityType'] ?? null,
250 $entityData['crmEntityId'] ?? null
251 );
252
253 if (!$canJoin && !$this->checkAccessByRecentTable($userId))
254 {
256 }
257
258 return $result;
259 }
260
261 protected function checkAccessByRecentTable(int $userId): bool
262 {
263 $result = ImOpenLines\Model\RecentTable::query()
264 ->setSelect(['USER_ID'])
265 ->where('CHAT_ID', $this->getChatId())
266 ->where('USER_ID', $userId)
267 ->setLimit(1)
268 ->fetch()
269 ;
270
271 return $result !== false;
272 }
273
274 public function canUpdateOwnMessage(): bool
275 {
276 if (!Loader::includeModule('imopenlines'))
277 {
278 return false;
279 }
280
281 [$connectorType] = explode("|", $this->getEntityId() ?? '');
282
283 return in_array($connectorType, \Bitrix\ImOpenlines\Connector::getListCanUpdateOwnMessage(), true);
284 }
285
286 public function canDeleteOwnMessage(): bool
287 {
288 if (!Loader::includeModule('imopenlines'))
289 {
290 return false;
291 }
292
293 [$connectorType] = explode("|", $this->getEntityId() ?? '');
294
295 return in_array($connectorType, \Bitrix\ImOpenlines\Connector::getListCanDeleteOwnMessage(), true);
296 }
297
298 public function canDeleteMessage(): bool
299 {
300 if (!Loader::includeModule('imopenlines'))
301 {
302 return false;
303 }
304
305 [$connectorType] = explode("|", $this->getEntityId() ?? '');
306
307 return in_array($connectorType, \Bitrix\ImOpenlines\Connector::getListCanDeleteMessage(), true);
308 }
309
310 protected function validateAuthorId(int $authorId): Result
311 {
312 if ($authorId === 0)
313 {
314 return new Result();
315 }
316
317 return parent::validateAuthorId($authorId);
318 }
319
320 protected function prepareParams(array $params = []): Result
321 {
322 $params['AUTHOR_ID'] = 0;
323 return parent::prepareParams($params);
324 }
325
326 public function extendPullWatch(): void
327 {
328 if (Loader::includeModule('pull'))
329 {
330 \CPullWatch::Add($this->getContext()->getUserId(), "IM_PUBLIC_{$this->getId()}", true);
331 }
332 }
333
334 public function needToSendPublicPull(): bool
335 {
336 return true;
337 }
338
339 protected function needToUpdateOpenlinesRecent(): bool
340 {
341 return $this->getSessionId()
342 && Loader::includeModule('imopenlines')
343 && ImOpenLines\Recent::isRecentAvailableByStatus($this->getSession()?->getStatus())
344 ;
345 }
346
348 {
349 if ($this->needToUpdateOpenlinesRecent())
350 {
352
353 return new Result();
354 }
355
356 return parent::updateRecentAfterMessageSend($message, $config);
357 }
358
360 {
361 if (!Loader::includeModule('imopenlines'))
362 {
363 return new Result();
364 }
365
366 foreach ($this->getRelationsForSendMessage() as $relation)
367 {
368 ImOpenLines\Recent::setRecent(
369 $relation->getUserId(),
370 $this->getId(),
371 $message->getId(),
372 $this->getSessionId()
373 );
374 }
375
376 return new Result();
377 }
378
380 {
381 if ($this->hasFakeRelations())
382 {
383 return new Result();
384 }
385
386 return parent::updateRelationsAfterMessageSend($message);
387 }
388
390 {
391 if ($this->hasFakeRelations())
392 {
393 $counter = 1;
394 if ($this->getSession()?->isClosed())
395 {
396 $counter = 0;
397 }
398
399 $counters = [];
400 foreach ($this->fakeRelation as $fakeRelation)
401 {
402 $counters[$fakeRelation->getUserId()] = $counter;
403 }
404
405 return (new Result())->setResult(['COUNTERS' => $counters]);
406 }
407
408 return parent::updateCountersAfterMessageSend($message, $sendingConfig);
409 }
410
412 {
413 $fields = parent::getFieldsForRecent($userId, $message);
414 if (!empty($fields))
415 {
416 $fields['ITEM_OLID'] = $this->getSessionIdByEntityData();
417 }
418
419 return $fields;
420 }
421
423 {
424 $fields = parent::getUpdatedFieldsForRecent($message);
425 if (!empty($fields))
426 {
427 $fields['ITEM_OLID'] = $this->getSessionIdByEntityData();
428 }
429
430 return $fields;
431 }
432
433 protected function isValidToAdd(int $userId): bool
434 {
435 if(!parent::isValidToAdd($userId))
436 {
437 return false;
438 }
439
441
442 return $user->isConnector() || (!$user->isExtranet() && !$user->isConnector());
443 }
444
445 public function setExtranet(?bool $extranet): \Bitrix\Im\V2\Chat
446 {
447 return $this;
448 }
449
450 public function getExtranet(): ?bool
451 {
452 return false;
453 }
454
455 protected function updateStateAfterRelationsAdd(array $usersToAdd): self
456 {
457 parent::updateStateAfterRelationsAdd($usersToAdd);
458
459 if (Loader::includeModule('pull'))
460 {
461 foreach ($usersToAdd as $userId)
462 {
463 \CPullWatch::Delete($userId, 'IM_PUBLIC_' . $this->getId());
464 }
465 }
466
467 if ($this->needToUpdateOpenlinesRecent())
468 {
469 $this->addUsersToOpenlinesRecent($usersToAdd);
470 }
471
472 return $this;
473 }
474
475 protected function addUsersToOpenlinesRecent(array $userIds): self
476 {
477 $lastMessageId = $this->getLastMessageId();
478 foreach ($userIds as $userId)
479 {
480 ImOpenLines\Recent::setRecent($userId, $this->getId(), $lastMessageId, $this->getSessionId());
481 }
482
483 return $this;
484 }
485
486 protected function addUsersToRelation(array $usersToAdd, AddUsersConfig $config): void
487 {
488 $config = $config->setHideHistory(false);
489 parent::addUsersToRelation($usersToAdd, $config);
490 }
491
492 protected function addIndex(): \Bitrix\Im\V2\Chat
493 {
494 if (!Loader::includeModule('imopenlines'))
495 {
496 return $this;
497 }
498
499 ChatIndexTable::addIndex($this->getId(), $this->getTitle());
500
501 return $this;
502 }
503
504 protected function updateIndex(): \Bitrix\Im\V2\Chat
505 {
506 if (!Loader::includeModule('imopenlines'))
507 {
508 return $this;
509 }
510
511 ChatIndexTable::updateIndex($this->getId(), $this->getTitle());
512
513 return $this;
514 }
515
516 protected function hasFakeRelations(): bool
517 {
518 return isset($this->fakeRelation);
519 }
520
521 public function getPopupData(array $excludedList = []): \Bitrix\Im\V2\Rest\PopupData
522 {
523 $popupData = parent::getPopupData($excludedList);
524 $session = $this->getSession();
525
526 if (isset($session))
527 {
528 $popupData->add($session);
529 }
530
531 return $popupData;
532 }
533
534 protected function prepareMessageParamsFromUserDelete(string $message, bool $skipRecent): array
535 {
536 return [
537 'TO_CHAT_ID' => $this->getId(),
538 'MESSAGE' => $message,
539 'FROM_USER_ID' => $this->getContext()->getUserId(),
540 'SYSTEM' => 'Y',
541 'RECENT_ADD' => $skipRecent ? 'N' : 'Y',
542 'PARAMS' => ['CODE' => 'CHAT_LEAVE', 'NOTIFY' => 'Y'],
543 'PUSH' => 'N',
544 'SKIP_USER_CHECK' => 'Y',
545 'SKIP_COUNTER_INCREMENTS' => 'Y',
546 ];
547 }
548
550 {
551 return;
552 }
553}
if(!is_object($USER)||! $USER->IsAuthorized()) $userId
Определения check_mail.php:18
static unread($dialogId, $unread, $userId=null, ?int $markedId=null, ?string $itemTypes=null)
Определения recent.php:1674
const ACCESS_DENIED
Определения ChatError.php:19
getEntityData(bool $force=false)
Определения EntityChat.php:49
updateCountersAfterMessageSend(Message $message, SendingConfig $sendingConfig)
Определения OpenLineChat.php:389
sendNotificationUserDelete(int $userId, DeleteUserConfig $config)
Определения OpenLineChat.php:549
bool $isSessionFilled
Определения OpenLineChat.php:58
checkAccessInternal(int $userId)
Определения OpenLineChat.php:230
getFieldsForRecent(int $userId, Message $message)
Определения OpenLineChat.php:411
setFakeRelation(array $userIds)
Определения OpenLineChat.php:123
getUpdatedFieldsForRecent(Message $message)
Определения OpenLineChat.php:422
setExtranet(?bool $extranet)
Определения OpenLineChat.php:445
updateStateAfterRelationsAdd(array $usersToAdd)
Определения OpenLineChat.php:455
getPopupData(array $excludedList=[])
Определения OpenLineChat.php:521
validateAuthorId(int $authorId)
Определения OpenLineChat.php:310
updateOpenlinesRecentAfterMessageSend(Message $message)
Определения OpenLineChat.php:359
read(bool $onlyRecent=false, bool $byEvent=false, bool $forceRead=false)
Определения OpenLineChat.php:67
onBeforeMessageSend(Message $message, SendingConfig $config)
Определения OpenLineChat.php:137
setEntityMap(array $entityMap)
Определения OpenLineChat.php:62
isValidToAdd(int $userId)
Определения OpenLineChat.php:433
const EXTRANET_CAN_SEE_HISTORY
Определения OpenLineChat.php:23
ImOpenLines V2 Session Session $session
Определения OpenLineChat.php:60
readAllMessages(bool $byEvent=false, bool $forceRead=false)
Определения OpenLineChat.php:86
prepareParams(array $params=[])
Определения OpenLineChat.php:320
readMessages(?MessageCollection $messages, bool $byEvent=false, bool $forceRead=false)
Определения OpenLineChat.php:103
addUsersToOpenlinesRecent(array $userIds)
Определения OpenLineChat.php:475
RelationCollection $fakeRelation
Определения OpenLineChat.php:59
updateRecentAfterMessageSend(Message $message, SendingConfig $config)
Определения OpenLineChat.php:347
prepareMessageParamsFromUserDelete(string $message, bool $skipRecent)
Определения OpenLineChat.php:534
needToUpdateOpenlinesRecent()
Определения OpenLineChat.php:339
addUsersToRelation(array $usersToAdd, AddUsersConfig $config)
Определения OpenLineChat.php:486
updateRelationsAfterMessageSend(Message $message)
Определения OpenLineChat.php:379
checkAccessByRecentTable(int $userId)
Определения OpenLineChat.php:261
static createFake(array $userIds, Chat $chat)
Определения RelationCollection.php:91
static getInstance()
Определения application.php:98
Определения result.php:20
static getInstance()
Определения servicelocator.php:33
if(!\Bitrix\Main\Loader::includeModule('clouds')) $lastId
Определения sync.php:68
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
Определения Uuid.php:3
Определения ActionUuid.php:3
Определения handlers.php:8
$user
Определения mysql_to_pgsql.php:33
$message
Определения payment.php:8
$counter
Определения options.php:5
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
$config
Определения quickway.php:69
$messages
Определения template.php:8
if($inWords) echo htmlspecialcharsbx(Number2Word_Rus(roundEx($totalVatSum $params['CURRENCY']
Определения template.php:799
$counters
Определения options.php:100
$fields
Определения yandex_run.php:501