Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
chat.php
1<?php
2namespace Bitrix\Im;
3
15
16Loc::loadMessages(__FILE__);
17
18class Chat
19{
20 const TYPE_SYSTEM = 'S';
21 const TYPE_PRIVATE = 'P';
22 const TYPE_OPEN = 'O';
23 const TYPE_THREAD = 'T';
24 const TYPE_GROUP = 'C';
25 const TYPE_CHANNEL = 'N';
26 const TYPE_OPEN_LINE = 'L';
27
28 const STATUS_UNREAD = 0;
29 const STATUS_NOTIFY = 1;
30 const STATUS_READ = 2;
31
32 const LIMIT_SEND_EVENT = 30;
33
34 const FILTER_LIMIT = 50;
35
36 public static function getTypes()
37 {
38 return Array(self::TYPE_GROUP, self::TYPE_OPEN_LINE, self::TYPE_OPEN, self::TYPE_THREAD, \Bitrix\Im\V2\Chat::IM_TYPE_COPILOT);
39 }
40
41 public static function getType($chatData)
42 {
43 $messageType = $chatData["TYPE"] ?? $chatData["CHAT_TYPE"] ?? '';
44 $entityType = $chatData["ENTITY_TYPE"] ?? $chatData["CHAT_ENTITY_TYPE"] ?? '';
45
46 $messageType = trim($messageType);
47 $entityType = trim($entityType);
48
49 $chatId = null;
50 if (isset($chatData['ID']))
51 {
52 $chatId = (int)$chatData['ID'];
53 }
54 else if (isset($chatData['CHAT_ID']))
55 {
56 $chatId = (int)$chatData['CHAT_ID'];
57 }
58
59 if ($messageType == IM_MESSAGE_PRIVATE)
60 {
61 $result = 'private';
62 }
63 else if ($messageType === \Bitrix\Im\V2\Chat::IM_TYPE_COPILOT)
64 {
65 $result = 'copilot';
66 }
67 else if (!empty($entityType))
68 {
69 // convert to camelCase
70 $result = str_replace('_', '', lcfirst(ucwords(mb_strtolower($entityType), '_')));
71 }
72 else if ($chatId && $chatId === (int)\CIMChat::GetGeneralChatId())
73 {
74 $result = 'general';
75 }
76 else
77 {
78 $result = $messageType == IM_MESSAGE_OPEN? 'open': 'chat';
79 }
80
81 return htmlspecialcharsbx($result);
82 }
83
84 public static function getRelation($chatId, $params = [])
85 {
86 $chatId = intval($chatId);
87 if ($chatId <= 0)
88 {
89 return false;
90 }
91
92 $connection = Application::getInstance()->getConnection();
93
94 $selectFields = '';
95 if (isset($params['SELECT']))
96 {
97 $params['SELECT'][] = 'ID';
98 $params['SELECT'][] = 'USER_ID';
99 $map = \Bitrix\Im\Model\RelationTable::getMap();
100 foreach ($params['SELECT'] as $key => $value)
101 {
102 if (is_int($key) && isset($map[$value]))
103 {
104 $selectFields .= "R.{$value}, ";
105 unset($map[$value]);
106 }
107 else if (!is_int($key) && isset($map[$key]))
108 {
109 $value = (string)$value;
110 $selectFields .= "R.{$key} '{$connection->getSqlHelper()->forSql($value)}', ";
111 unset($map[$value]);
112 }
113 }
114 }
115 if (!$selectFields)
116 {
117 $selectFields = 'R.*, ';
118 }
119
120 $withUserFields = false;
121 if (isset($params['USER_DATA']) && $params['USER_DATA'] == 'Y')
122 {
123 $withUserFields = true;
124 $list = Array('ACTIVE', 'EXTERNAL_AUTH_ID');
125 foreach ($list as $key)
126 {
127 $selectFields .= "U.{$key} USER_DATA_{$key}, ";
128 }
129 }
130 $skipUsers = false;
131 $skipUserInactiveSql = '';
132 if (isset($params['SKIP_INACTIVE_USER']) && $params['SKIP_INACTIVE_USER'] === 'Y')
133 {
134 $skipUsers = true;
135 $skipUserInactiveSql = "AND U.ACTIVE = 'Y'";
136 }
137
138 $skipUserTypes = $params['SKIP_USER_TYPES'] ?? [];
139 if (isset($params['SKIP_CONNECTOR']) && $params['SKIP_CONNECTOR'] === 'Y')
140 {
141 $skipUserTypes[] = 'imconnector';
142 }
143
144 $skipUserTypesSql = '';
145 if (!empty($skipUserTypes))
146 {
147 $skipUsers = true;
148 if (count($skipUserTypes) === 1)
149 {
150 $skipUserTypesSql = "AND (U.EXTERNAL_AUTH_ID != '".$connection->getSqlHelper()->forSql($skipUserTypes[0])."' OR U.EXTERNAL_AUTH_ID IS NULL)";
151 }
152 else
153 {
154 $skipUserTypes = array_map(function($type) use ($connection) {
155 return $connection->getSqlHelper()->forSql($type);
156 }, $skipUserTypes);
157
158 $skipUserTypesSql = "AND (U.EXTERNAL_AUTH_ID NOT IN ('".implode("','", $skipUserTypes)."') OR U.EXTERNAL_AUTH_ID IS NULL)";
159 }
160 }
161
162 $whereFields = '';
163 if (isset($params['FILTER']))
164 {
165 $map = \Bitrix\Im\Model\RelationTable::getMap();
166 foreach ($params['FILTER'] as $key => $value)
167 {
168 if (!isset($map[$key]))
169 {
170 continue;
171 }
172
173 if (is_int($value))
174 {
175 }
176 else if (is_bool($value))
177 {
178 $value = $value? "'Y'": "'N'";
179 }
180 else if (is_string($value))
181 {
182 $value = "'{$connection->getSqlHelper()->forSql($value)}'";
183 }
184 else
185 {
186 continue;
187 }
188
189 $whereFields .= " AND R.{$key} = {$value}";
190 }
191 }
192
193 /*$skipUnmodifiedRecords = false;
194 if (isset($params['SKIP_RELATION_WITH_UNMODIFIED_COUNTERS']) && $params['SKIP_RELATION_WITH_UNMODIFIED_COUNTERS'] == 'Y')
195 {
196 $skipUnmodifiedRecords = true;
197 }*/
198
199 /*$sqlSelectCounter = 'R.LAST_ID, R.COUNTER, R.COUNTER PREVIOUS_COUNTER';
200
201 $customCounter = false;
202 $customMaxId = 0;
203 $customMinId = 0;
204 $counters = [];
205
206 if (isset($params['REAL_COUNTERS']) && $params['REAL_COUNTERS'] != 'N' || $skipUnmodifiedRecords)
207 {
208 if (is_array($params['REAL_COUNTERS']) && isset($params['REAL_COUNTERS']['LAST_ID']))
209 {
210 $sqlSelectCounter = "R.COUNTER PREVIOUS_COUNTER, (
211 SELECT COUNT(1) FROM b_im_message M WHERE M.CHAT_ID = R.CHAT_ID AND M.ID > ".intval($params['REAL_COUNTERS']['LAST_ID'])."
212 ) COUNTER";
213 }
214 else
215 {
216 $customCounter = true;
217 $query = $connection->query("
218 SELECT ID FROM b_im_message
219 WHERE CHAT_ID = {$chatId}
220 ORDER BY DATE_CREATE DESC, ID DESC
221 LIMIT 100
222 ");
223 $messageCounter = 0;
224 while ($row = $query->fetch())
225 {
226 if (!$customMaxId)
227 {
228 $customMaxId = $row['ID'];
229 }
230 $counters[$row['ID']] = $messageCounter++;
231 $customMinId = $row['ID'];
232 }
233 }
234 }*/
235
236 $limit = '';
237 if (isset($params['LIMIT']))
238 {
239 $limit = 'LIMIT '.(int)$params['LIMIT'];
240 }
241
242 $offset = '';
243 if (isset($params['OFFSET']))
244 {
245 $offset = 'OFFSET '.(int)$params['OFFSET'];
246 }
247
248 $orderField = 'R.ID';
249
250 if (isset($params['LAST_USER_ID']) && (int)$params['LAST_USER_ID'] >= 0)
251 {
252 $lastUserId = (int)$params['LAST_USER_ID'];
253 $whereFields .= " AND R.USER_ID > {$lastUserId}";
254 $orderField = 'R.USER_ID';
255 }
256
257 $selectFields = rtrim($selectFields, ', ');
258 $sql = "
259 SELECT {$selectFields}
260 FROM b_im_relation R
261 ".($withUserFields && !$skipUsers? "LEFT JOIN b_user U ON R.USER_ID = U.ID": "")."
262 ".($skipUsers? "INNER JOIN b_user U ON R.USER_ID = U.ID {$skipUserInactiveSql} {$skipUserTypesSql}": "")."
263 WHERE R.CHAT_ID = {$chatId} {$whereFields}
264 ORDER BY {$orderField} ASC
265 {$limit} {$offset}
266 ";
267 $relations = array();
268 $query = $connection->query($sql);
269 while ($row = $query->fetch())
270 {
271 /*if ($customCounter)
272 {
273 if (isset($counters[$row['LAST_ID']]))
274 {
275 $row['COUNTER'] = $counters[$row['LAST_ID']];
276 }
277 else if ($row['LAST_ID'] < $customMinId)
278 {
279 $row['COUNTER'] = count($counters);
280 }
281 else if ($row['LAST_ID'] > $customMaxId)
282 {
283 $row['COUNTER'] = 0;
284 }
285 }
286 else
287 {
288 $row['COUNTER'] = $row['COUNTER'] > 99? 100: (int)$row['COUNTER'];
289 }
290
291 $row['PREVIOUS_COUNTER'] = (int)$row['PREVIOUS_COUNTER'];
292
293 if ($skipUnmodifiedRecords && $row['COUNTER'] == $row['PREVIOUS_COUNTER'])
294 {
295 continue;
296 }*/
297
298 foreach ($row as $key => $value)
299 {
300 if (mb_strpos($key, 'USER_DATA_') === 0)
301 {
302 $row['USER_DATA'][mb_substr($key, 10)] = $value;
303 unset($row[$key]);
304 }
305 }
306
307 $relations[$row['USER_ID']] = $row;
308 }
309
310 // region New counter
311 // todo: select counter only if it's need
312 if (!isset($params['WITHOUT_COUNTERS']) || $params['WITHOUT_COUNTERS'] !== 'Y')
313 {
314 $userIds = array_keys($relations);
315 $readService = new ReadService();
316 $counters = $readService->getCounterService()->getByChatForEachUsers($chatId, $userIds);
317 $lastIdInChat = $readService->getLastMessageIdInChat($chatId);
318 $lastReads = $readService->getViewedService()->getDateViewedByMessageIdForEachUser($lastIdInChat, $userIds);
319 foreach ($relations as $userId => $relation)
320 {
321 $counter = $counters[$userId] ?? 0;
322 $counter = $counter > 99 ? 100 : $counter;
323 $relations[$userId]['COUNTER'] = $counter;
324 $relations[$userId]['LAST_READ'] = $lastReads[$userId] ?? null;
325 }
326 }
327 // endregion
328
329 return $relations;
330 }
331
332 public static function mute($chatId, $action, $userId = null)
333 {
334 $userId = \Bitrix\Im\Common::getUserId($userId);
335 if (!$userId)
336 {
337 return false;
338 }
339
340 $chatId = intval($chatId);
341 if (!$chatId)
342 {
343 return false;
344 }
345
346 $action = $action === true? 'Y': 'N';
347
348 (new CounterService())->withContextUser($userId)->updateIsMuted($chatId, $action);
349
350 $relation = self::getRelation($chatId, Array(
351 'SELECT' => Array('ID', 'MESSAGE_TYPE', 'NOTIFY_BLOCK', 'COUNTER'),
352 'FILTER' => Array(
353 'USER_ID' => $userId
354 ),
355 ));
356 if (!$relation)
357 {
358 return false;
359 }
360
361 if ($relation[$userId]['NOTIFY_BLOCK'] == $action)
362 {
363 return true;
364 }
365
366 \Bitrix\Im\Model\RelationTable::update($relation[$userId]['ID'], array('NOTIFY_BLOCK' => $action));
367
368 Recent::clearCache($userId);
369 //Counter::clearCache($userId);
370
371 if (\Bitrix\Main\Loader::includeModule('pull'))
372 {
373 $element = \Bitrix\Im\Model\RecentTable::getList([
374 'select' => ['USER_ID', 'ITEM_TYPE', 'ITEM_ID', 'UNREAD'],
375 'filter' => [
376 '=USER_ID' => $userId,
377 '=ITEM_TYPE' => $relation[$userId]['MESSAGE_TYPE'],
378 '=ITEM_ID' => $chatId
379 ]
380 ])->fetch();
381
382 $counter = $relation[$userId]['COUNTER'];
383
384 \Bitrix\Pull\Event::add($userId, Array(
385 'module_id' => 'im',
386 'command' => 'chatMuteNotify',
387 'params' => Array(
388 'chatId' => $chatId,
389 'dialogId' => 'chat'.$chatId,
390 'muted' => $action == 'Y',
391 'mute' => $action == 'Y', // TODO remove this later
392 'counter' => $counter,
393 'lines' => $element['ITEM_TYPE'] === self::TYPE_OPEN_LINE,
394 'unread' => ($element['UNREAD'] ?? 'N') === 'Y',
395 ),
396 'extra' => \Bitrix\Im\Common::getPullExtra()
397 ));
398 }
399
400 $chat = \Bitrix\Im\Chat::getById($chatId);
401 foreach(\Bitrix\Main\EventManager::getInstance()->findEventHandlers("im", "OnAfterChatMuteNotify") as $event)
402 {
403 ExecuteModuleEventEx($event, [[
404 'CHAT_ID' => $chatId,
405 'USER_ID' => $userId,
406 'MUTE' => $action == 'Y',
407 'CHAT' => $chat,
408 ]]);
409 }
410
411 return true;
412 }
413
414 public static function getMessageCount($chatId, $userId = null)
415 {
416 $chatId = intval($chatId);
417 if (!$chatId)
418 {
419 return false;
420 }
421
422 $userId = \Bitrix\Im\Common::getUserId($userId);
423 if (!$userId)
424 {
425 return false;
426 }
427
428 $relationData = \Bitrix\Im\Model\RelationTable::getList(Array(
429 'select' => Array('START_ID'),
430 'filter' => Array('=CHAT_ID' => $chatId, '=USER_ID' => $userId)
431 ))->fetch();
432
433 if (!$relationData || $relationData['START_ID'] == 0)
434 {
435 $counter = \Bitrix\Im\Model\MessageTable::getCount(['=CHAT_ID' => $chatId]);
436 }
437 else
438 {
439 $counter = \Bitrix\Im\Model\MessageTable::getCount(['=CHAT_ID' => $chatId, '>=ID' => $relationData['START_ID']]);
440 }
441
442 return $counter > 0 ? $counter : 0;
443 }
444
445 public static function hasAccess($chatId)
446 {
447 $chatId = intval($chatId);
448 if (!$chatId)
449 {
450 return false;
451 }
452
453 return \Bitrix\Im\Dialog::hasAccess('chat'.$chatId);
454 }
455
462 public static function getMessages($chatId, $userId = null, $options = Array())
463 {
464 $userId = \Bitrix\Im\Common::getUserId($userId);
465 if (!$userId)
466 {
467 return false;
468 }
469
470 $chatData = \Bitrix\Im\Model\ChatTable::getList(Array(
471 'select' => Array(
472 'CHAT_ID' => 'ID',
473 'CHAT_TYPE' => 'TYPE',
474 'CHAT_ENTITY_TYPE' => 'ENTITY_TYPE',
475 'CHAT_ENTITY_ID' => 'ENTITY_ID',
476 'RELATION_USER_ID' => 'RELATION.USER_ID',
477 'RELATION_START_ID' => 'RELATION.START_ID',
478 //'RELATION_UNREAD_ID' => 'RELATION.UNREAD_ID',
479 'RELATION_LAST_ID' => 'RELATION.LAST_ID',
480 //'RELATION_STATUS' => 'RELATION.STATUS',
481 //'RELATION_COUNTER' => 'RELATION.COUNTER'
482 ),
483 'filter' => Array('=ID' => $chatId),
484 'runtime' => Array(
485 new \Bitrix\Main\Entity\ReferenceField(
486 'RELATION',
487 '\Bitrix\Im\Model\RelationTable',
488 array(
489 "=ref.CHAT_ID" => "this.ID",
490 "=ref.USER_ID" => new \Bitrix\Main\DB\SqlExpression('?i', $userId)
491 ),
492 array("join_type"=>"LEFT")
493 )
494 )
495 ))->fetch();
496 if (!$chatData)
497 {
498 return false;
499 }
500
501 $readService = new ReadService($userId);
502
503 $chatData['RELATION_UNREAD_ID'] = $readService->getCounterService()->getIdFirstUnreadMessage($chatId) ?? 0;
504 $chatData['RELATION_COUNTER'] = $readService->getCounterService()->getByChat($chatId);
505 $chatData['RELATION_START_ID'] = (int)$chatData['RELATION_START_ID'];
506
507 if (isset($options['LIMIT']))
508 {
509 $options['LIMIT'] = intval($options['LIMIT']);
510 $limit = $options['LIMIT'] >= 100? 100: $options['LIMIT'];
511 }
512 else
513 {
514 $limit = 50;
515 }
516
517 $filter = Array(
518 '=CHAT_ID' => $chatId
519 );
520
521 $fileSort = 'ASC';
522 $startFromUnread = false;
523 if (
524 !isset($options['LAST_ID'])
525 && !isset($options['FIRST_ID'])
526 //&& $chatData['RELATION_STATUS'] != \Bitrix\Im\Chat::STATUS_READ
527 && $chatData['RELATION_COUNTER'] > 0
528 )
529 {
530 if ($chatData['RELATION_COUNTER'] > $limit)
531 {
532 $startFromUnread = true;
533 $options['FIRST_ID'] = $chatData['RELATION_LAST_ID'];
534 }
535 else
536 {
537 $limit += $chatData['RELATION_COUNTER'];
538 }
539 }
540
541 if (isset($options['FIRST_ID']))
542 {
543 $orderId = [];
544 $orderResult = [];
545
546 if ($chatData['RELATION_START_ID'] > 0 && intval($options['FIRST_ID']) < $chatData['RELATION_START_ID'])
547 {
548 $filter['>=ID'] = $chatData['RELATION_START_ID'];
549 }
550 else
551 {
552 if (intval($options['FIRST_ID']) > 0)
553 {
554 $filter['>ID'] = $options['FIRST_ID'];
555 }
556 }
557 }
558 else
559 {
560 $fileSort = 'DESC';
561 $orderId = Array('CHAT_ID' => 'ASC', 'ID' => 'DESC');
562 $orderResult = Array('ID' => 'DESC');
563
564 if ($chatData['RELATION_START_ID'] > 0)
565 {
566 $filter['>=ID'] = $chatData['RELATION_START_ID'];
567 }
568
569 if (isset($options['LAST_ID']) && intval($options['LAST_ID']) > 0)
570 {
571 $filter['<ID'] = intval($options['LAST_ID']);
572 }
573 }
574
575 $orm = \Bitrix\Im\Model\MessageTable::getList(array(
576 'select' => ['ID'],
577 'filter' => $filter,
578 'order' => $orderId,
579 'limit' => $limit
580 ));
581 $ids = array_map(fn ($item) => $item['ID'], $orm->fetchAll());
582 if (empty($ids))
583 {
584 $result = [
585 'CHAT_ID' => (int)$chatId,
586 'MESSAGES' => [],
587 'USERS' => [],
588 'FILES' => [],
589 ];
590 if ($options['JSON'])
591 {
592 $result = array_change_key_case($result, CASE_LOWER);
593 }
594 return $result;
595 }
596
597 $orm = \Bitrix\Im\Model\MessageTable::getList(array(
598 'select' => [
599 'ID', 'AUTHOR_ID', 'DATE_CREATE', 'NOTIFY_EVENT', 'MESSAGE',
600 'USER_LAST_ACTIVITY_DATE' => 'AUTHOR.LAST_ACTIVITY_DATE',
601 /*'USER_IDLE' => 'STATUS.IDLE',
602 'USER_MOBILE_LAST_DATE' => 'STATUS.MOBILE_LAST_DATE',
603 'USER_DESKTOP_LAST_DATE' => 'STATUS.DESKTOP_LAST_DATE',*/
604 'MESSAGE_UUID' => 'UUID.UUID',
605 ],
606 'filter' => ['=ID' => $ids],
607 'order' => $orderResult,
608 ));
609
610 $users = Array();
611
612 $userOptions = ['SKIP_ONLINE' => 'Y'];
613 if ($options['JSON'] == 'Y')
614 {
615 $userOptions['JSON'] = 'Y';
616 }
617 if ($chatData['CHAT_ENTITY_TYPE'] == 'LIVECHAT')
618 {
619 [$lineId] = explode('|', $chatData['CHAT_ENTITY_ID']);
620 $userOptions['LIVECHAT'] = $lineId;
621 $userOptions['USER_CODE'] = 'livechat|' . $lineId . '|' . $chatData['CHAT_ID'] . '|' . $userId;
622 }
623
624 $messages = Array();
625 while($message = $orm->fetch())
626 {
627 if ($message['NOTIFY_EVENT'] == 'private_system')
628 {
629 $message['AUTHOR_ID'] = 0;
630 }
631
632 if (isset($options['USER_TAG_SPREAD']) && $options['USER_TAG_SPREAD'] === 'Y')
633 {
634 $message['MESSAGE'] = preg_replace_callback("/\[USER=([0-9]{1,})\]\[\/USER\]/i", Array('\Bitrix\Im\Text', 'modifyShortUserTag'), $message['MESSAGE']);
635 }
636
637 $messages[$message['ID']] = Array(
638 'ID' => (int)$message['ID'],
639 'CHAT_ID' => (int)$chatId,
640 'AUTHOR_ID' => (int)$message['AUTHOR_ID'],
641 'DATE' => $message['DATE_CREATE'],
642 'TEXT' => (string)\Bitrix\Im\Text::parse($message['MESSAGE']),
643 'UNREAD' => $chatData['RELATION_USER_ID'] > 0 && $chatData['RELATION_LAST_ID'] < $message['ID'],
644 'UUID' => $message['MESSAGE_UUID'],
645 );
646 $messages[$message['ID']]['REPLACES'] = \Bitrix\Im\Text::getReplaceMap($messages[$message['ID']]['TEXT']);
647 if ($message['AUTHOR_ID'] && !isset($users[$message['AUTHOR_ID']]))
648 {
649 $user = User::getInstance($message['AUTHOR_ID'])->getArray($userOptions);
650 $user['last_activity_date'] = $message['USER_LAST_ACTIVITY_DATE']? date('c', $message['USER_LAST_ACTIVITY_DATE']->getTimestamp()): false;
651 $user['desktop_last_date'] = false;
652 $user['mobile_last_date'] = false;
653 $user['idle'] = false;
654
655 $users[$message['AUTHOR_ID']] = $user;
656 }
657 if ($options['CONVERT_TEXT'])
658 {
659 $messages[$message['ID']]['TEXT_CONVERTED'] = \Bitrix\Im\Text::parseLegacyFormat($message['MESSAGE']);
660 }
661 }
662
663 $params = \CIMMessageParam::Get(array_keys($messages));
664
665 $fileIds = Array();
666 foreach ($params as $messageId => $param)
667 {
668 $messages[$messageId]['PARAMS'] = empty($param)? []: $param;
669
670 if (
671 empty($messages[$messageId]['TEXT'])
672 && !isset($param['FILE_ID'])
673 && !isset($param['KEYBOARD'])
674 && !isset($param['ATTACH'])
675 )
676 {
677 $messages[$messageId]['TEXT'] = Loc::getMessage('IM_CHAT_MESSAGE_DELETED');
678 $messages[$messageId]['PARAMS']['IS_DELETED'] = 'Y';
679 }
680
681 if (isset($param['FILE_ID']))
682 {
683 foreach ($param['FILE_ID'] as $fileId)
684 {
685 $fileIds[$fileId] = $fileId;
686 }
687 }
688
689 if (isset($param['CHAT_USER']) && is_array($param['CHAT_USER']))
690 {
691 foreach ($param['CHAT_USER'] as $paramsUserId)
692 {
693 $users[$paramsUserId] = User::getInstance($paramsUserId)->getArray($userOptions);
694 }
695 }
696 }
697
698 $disappearing = DisappearService::getMessagesDisappearingTime(array_keys($messages));
699 foreach ($messages as $messageId => $message)
700 {
701 if (
702 isset($disappearing[$messageId])
703 && $disappearing[$messageId]['DATE_REMOVE'] instanceof DateTime
704 )
705 {
706 $messages[$messageId]['DISAPPEARING_DATE'] = $disappearing[$messageId]['DATE_REMOVE']->format(DATE_ATOM);
707 }
708 else
709 {
710 $messages[$messageId]['DISAPPEARING_DATE'] = null;
711 }
712 }
713
714 $messages = \CIMMessageLink::prepareShow($messages, $params);
715
716 $files = \CIMDisk::GetFiles($chatId, $fileIds);
717
718 $result = Array(
719 'CHAT_ID' => (int)$chatId,
720 'MESSAGES' => $messages,
721 'USERS' => array_values($users),
722 'FILES' => array_values($files),
723 );
724
725 if (count($files) && $fileSort == 'DESC')
726 {
727 $result['FILES'] = array_reverse($result['FILES']);
728 }
729
730 if ($startFromUnread)
731 {
732 $result['MESSAGES'] = array_reverse($result['MESSAGES']);
733 $additionalMessages = self::getMessages($chatId, $userId, [
734 'LIMIT' => $limit,
735 'LAST_ID' => $chatData['RELATION_UNREAD_ID']
736 ]);
737 $result['MESSAGES'] = array_merge($result['MESSAGES'], $additionalMessages['MESSAGES']);
738 }
739
740 if ($options['JSON'])
741 {
742 foreach ($result['MESSAGES'] as $key => $value)
743 {
744 if ($value['DATE'] instanceof \Bitrix\Main\Type\DateTime)
745 {
746 $result['MESSAGES'][$key]['DATE'] = date('c', $value['DATE']->getTimestamp());
747 }
748
749 if (isset($value['PARAMS']['CHAT_LAST_DATE']) && $value['PARAMS']['CHAT_LAST_DATE'] instanceof \Bitrix\Main\Type\DateTime)
750 {
751 $result['MESSAGES'][$key]['PARAMS']['CHAT_LAST_DATE'] = date('c', $value['PARAMS']['CHAT_LAST_DATE']->getTimestamp());
752 }
753
754 if (is_array($value['REPLACES']) && !empty($value['REPLACES']))
755 {
756 $result['MESSAGES'][$key]['REPLACES'] = Common::toJson($value['REPLACES']);
757 }
758
759 $result['MESSAGES'][$key] = array_change_key_case($result['MESSAGES'][$key], CASE_LOWER);
760 }
761 $result['MESSAGES'] = array_values($result['MESSAGES']);
762
763 foreach ($result['FILES'] as $key => $value)
764 {
765 if ($value['date'] instanceof \Bitrix\Main\Type\DateTime)
766 {
767 $result['FILES'][$key]['date'] = date('c', $value['date']->getTimestamp());
768 }
769
770 foreach (['urlPreview', 'urlShow', 'urlDownload'] as $field)
771 {
772 $url = $result['FILES'][$key][$field];
773 if (is_string($url) && $url && mb_strpos($url, 'http') !== 0)
774 {
775 $result['FILES'][$key][$field] = \Bitrix\Im\Common::getPublicDomain().$url;
776 }
777 }
778
779 }
780
781 $result = array_change_key_case($result, CASE_LOWER);
782 }
783
784 return $result;
785 }
786
787 public static function getUsers($chatId, $options = []): array
788 {
789 $params = [
790 'SELECT' => ['ID', 'USER_ID'],
791 'SKIP_INACTIVE_USER' => 'Y',
792 'WITHOUT_COUNTERS' => 'Y',
793 ];
794
795 $skipExternal = isset($options['SKIP_EXTERNAL']) || isset($options['SKIP_EXTERNAL_EXCEPT_TYPES']);
796 if ($skipExternal)
797 {
798 $exceptType = $options['SKIP_EXTERNAL_EXCEPT_TYPES'] ?? [];
799 $params['SKIP_USER_TYPES'] = \Bitrix\Im\Model\UserTable::filterExternalUserTypes($exceptType);
800 }
801
802 if (isset($options['LIMIT']))
803 {
804 $params['LIMIT'] = $options['LIMIT'];
805 }
806 if (isset($options['OFFSET']))
807 {
808 $params['OFFSET'] = $options['OFFSET'];
809 }
810 if (isset($options['LAST_ID']))
811 {
812 $params['LAST_USER_ID'] = (int)$options['LAST_ID'];
813 }
814
815 $json = isset($options['JSON']) && $options['JSON'] === 'Y' ? 'Y' : 'N';
816 $users = [];
817 $relations = self::getRelation($chatId, $params);
818 foreach ($relations as $user)
819 {
820 $userData = \Bitrix\Im\User::getInstance($user['USER_ID'])->getArray([
821 'JSON' => $json
822 ]);
823
824 if ($userData['bot'])
825 {
826 $converter = new Converter(Converter::TO_SNAKE | Converter::TO_LOWER | Converter::KEYS);
827
828 $botData = \Bitrix\Im\V2\Entity\User\Data\BotData::getInstance((int)$user['USER_ID'])->toRestFormat();
829 $userData['bot_data'] = (!empty($botData)) ? $converter->process($botData) : null;
830 }
831 else
832 {
833 $userData['bot_data'] = null;
834 }
835
836 $users[] = $userData;
837 }
838
839 return $users;
840 }
841
851 public static function getById($id, $params = array())
852 {
853 $userId = \Bitrix\Im\Common::getUserId();
854 if (!$userId)
855 {
856 return false;
857 }
858
859 $checkAccessParam = $params['CHECK_ACCESS'] ?? null;
860 $chats = self::getList(Array(
861 'FILTER' => Array('ID' => $id),
862 'SKIP_ACCESS_CHECK' => $checkAccessParam === 'Y'? 'N': 'Y',
863 ));
864 if ($chats)
865 {
866 $chat = $chats[0];
867 }
868 else
869 {
870 return false;
871 }
872
873 if (isset($params['LOAD_READED']) && $params['LOAD_READED'])
874 {
875 $userOptions = ['SKIP_ONLINE' => 'Y'];
876 if ($chat['ENTITY_TYPE'] == 'LIVECHAT')
877 {
878 [$lineId] = explode('|', $chat['CHAT_ENTITY_ID']);
879 $userOptions['LIVECHAT'] = $lineId;
880 $userOptions['USER_CODE'] = 'livechat|' . $lineId . '|' . $id . '|' . $userId;
881 }
882
883 $relations = self::getRelation($id);
884
885 $chat['READED_LIST'] = [];
886 $chat['MANAGER_LIST'] = [];
887 foreach ($relations as $relation)
888 {
889 if (
890 $relation['USER_ID'] != $userId
891 //&& $relation['STATUS'] == self::STATUS_READ
892 && \Bitrix\Im\User::getInstance($relation['USER_ID'])->isActive()
893 )
894 {
895 $user = \Bitrix\Im\User::getInstance($relation['USER_ID'])->getArray($userOptions);
896 $chat['READED_LIST'][] = [
897 'USER_ID' => (int)$relation['USER_ID'],
898 'USER_NAME' => $user['NAME'],
899 'MESSAGE_ID' => (int)$relation['LAST_ID'],
900 'DATE' => $relation['LAST_READ'],
901 ];
902 }
903
904 if ($relation['MANAGER'] === 'Y')
905 {
906 $chat['MANAGER_LIST'][] = (int)$relation['USER_ID'];
907 }
908 }
909
910 // region v2
911
912 $chatInstance = \Bitrix\Im\V2\Chat::getInstance((int)$id);
913 $chat['LAST_MESSAGE_VIEWS'] = $chatInstance->getLastMessageViews();
914
915 // endregion
916 }
917
918 if ($params['JSON'] ?? null)
919 {
920 $chat = self::toJson($chat);
921 }
922
923 return $chat;
924 }
925
926 public static function getList($params = array())
927 {
928 $params = is_array($params)? $params: Array();
929
930 if (!isset($params['CURRENT_USER']) && is_object($GLOBALS['USER']))
931 {
932 $params['CURRENT_USER'] = $GLOBALS['USER']->GetID();
933 }
934
935 $params['CURRENT_USER'] = intval($params['CURRENT_USER']);
936
937 $params['SKIP_ACCESS_CHECK'] = $params['SKIP_ACCESS_CHECK'] === 'Y'? 'Y': 'N';
938
939 $userId = $params['CURRENT_USER'];
940 if ($userId <= 0)
941 {
942 return false;
943 }
944
945 $enableLimit = false;
946 if (isset($params['OFFSET']))
947 {
948 $filterLimit = intval($params['LIMIT']);
949 $filterLimit = $filterLimit <= 0? self::FILTER_LIMIT: $filterLimit;
950
951 $filterOffset = intval($params['OFFSET']);
952
953 $enableLimit = true;
954 }
955 else
956 {
957 $filterLimit = false;
958 $filterOffset = false;
959 }
960
961 $ormParams = self::getListParams($params);
962 if (!$ormParams)
963 {
964 return false;
965 }
966 if ($enableLimit)
967 {
968 $ormParams['offset'] = $filterOffset;
969 $ormParams['limit'] = $filterLimit;
970 }
971 if (isset($params['ORDER']))
972 {
973 $ormParams['order'] = $params['ORDER'];
974 }
975
976 $orm = \Bitrix\Im\Model\ChatTable::getList($ormParams);
977 $chatsRaw = $orm->fetchAll();
978 $chatsRaw = self::fillCounterData($chatsRaw);
979
980 $chats = array();
981 foreach ($chatsRaw as $chatRaw)
982 {
983 $chats[] = self::formatChatData($chatRaw);
984 }
985
986 if (isset($params['JSON']) && $params['JSON'])
987 {
988 $chats = self::toJson($chats);
989 }
990
991 return $chats;
992 }
993
994 public static function formatChatData($chat): array
995 {
996 $generalChatId = \CIMChat::GetGeneralChatId();
997 $avatar = \CIMChat::GetAvatarImage($chat['AVATAR'], 200, false);
998 $color = $chat['COLOR'] <> ''? Color::getColor($chat['COLOR']): Color::getColorByNumber($chat['ID']);
999
1000 if ($generalChatId == $chat['ID'])
1001 {
1002 $chat["ENTITY_TYPE"] = 'GENERAL';
1003 }
1004
1005 $chatType = \Bitrix\Im\Chat::getType($chat);
1006
1007 $muteList = Array();
1008 if ($chat['RELATION_NOTIFY_BLOCK'] == 'Y')
1009 {
1010 $muteList[] = (int)$chat['RELATION_USER_ID'];
1011 }
1012
1013 $counter = (int)$chat['RELATION_COUNTER'];
1014 $startCounter = (int)$chat['RELATION_START_COUNTER'];
1015 $userCounter = (int)$chat['USER_COUNT'];
1016 $unreadId = (int)$chat['RELATION_UNREAD_ID'];
1017 $lastMessageId = (int)$chat['LAST_MESSAGE_ID'];
1018
1019 $publicOption = '';
1020 if ($chat['ALIAS_NAME'])
1021 {
1022 $publicOption = [
1023 'code' => $chat['ALIAS_NAME'],
1024 'link' => Alias::getPublicLink($chat['ENTITY_TYPE'], $chat['ALIAS_NAME'])
1025 ];
1026 }
1027
1028 $options = \CIMChat::GetChatOptions();
1029 $restrictions = $options['DEFAULT'];
1030
1031 if ($chat["ENTITY_TYPE"] && in_array($chat["ENTITY_TYPE"], array_keys($options), true))
1032 {
1033 $restrictions = $options[$chat['ENTITY_TYPE']];
1034 }
1035
1036 return Array(
1037 'ID' => (int)$chat['ID'],
1038 'NAME' => $chat['TITLE'],
1039 'DESCRIPTION' => $chat['DESCRIPTION'],
1040 'OWNER' => (int)$chat['AUTHOR_ID'],
1041 'EXTRANET' => $chat['EXTRANET'] == 'Y',
1042 'AVATAR' => $avatar,
1043 'COLOR' => $color,
1044 'TYPE' => $chatType,
1045 'COUNTER' => $counter,
1046 'USER_COUNTER' => $userCounter,
1047 'MESSAGE_COUNT' => (int)$chat['MESSAGE_COUNT'] - $startCounter,
1048 'UNREAD_ID' => $unreadId,
1049 'RESTRICTIONS' => $restrictions,
1050 'LAST_MESSAGE_ID' => $lastMessageId,
1051 'LAST_ID' => (int)$chat['RELATION_LAST_ID'],
1052 'MARKED_ID' => (int)$chat['MARKED_ID'],
1053 'DISK_FOLDER_ID' => (int)$chat['DISK_FOLDER_ID'],
1054 'ENTITY_TYPE' => (string)$chat['ENTITY_TYPE'],
1055 'ENTITY_ID' => (string)$chat['ENTITY_ID'],
1056 'ENTITY_DATA_1' => (string)$chat['ENTITY_DATA_1'],
1057 'ENTITY_DATA_2' => (string)$chat['ENTITY_DATA_2'],
1058 'ENTITY_DATA_3' => (string)$chat['ENTITY_DATA_3'],
1059 'MUTE_LIST' => $muteList,
1060 'DATE_CREATE' => $chat['DATE_CREATE'],
1061 'MESSAGE_TYPE' => $chat["TYPE"],
1062 'DISAPPEARING_TIME' => (int)$chat['DISAPPEARING_TIME'],
1063 'PUBLIC' => $publicOption,
1064 'ROLE' => mb_strtolower(self::getRole($chat)),
1065 'ENTITY_LINK' => EntityLink::getInstance(\CIMChat::initChatByArray($chat))->toArray(),
1066 'AI_PROVIDER' => $chatType === 'copilot' ? AIHelper::getProviderName() : null,
1067 'PERMISSIONS' => [
1068 'MANAGE_USERS_ADD' => mb_strtolower((string)$chat['MANAGE_USERS_ADD']),
1069 'MANAGE_USERS_DELETE' => mb_strtolower((string)$chat['MANAGE_USERS_DELETE']),
1070 'MANAGE_UI' => mb_strtolower((string)$chat['MANAGE_UI']),
1071 'MANAGE_SETTINGS' => mb_strtolower((string)$chat['MANAGE_SETTINGS']),
1072 'CAN_POST' => mb_strtolower((string)$chat['CAN_POST']),
1073 ],
1074 );
1075 }
1076
1077 public static function getListParams($params)
1078 {
1079 if (!isset($params['CURRENT_USER']) && is_object($GLOBALS['USER']))
1080 {
1081 $params['CURRENT_USER'] = $GLOBALS['USER']->GetID();
1082 }
1083
1084 $params['CURRENT_USER'] = intval($params['CURRENT_USER']);
1085
1086 $userId = $params['CURRENT_USER'];
1087 if ($userId <= 0)
1088 {
1089 return null;
1090 }
1091
1092 $filter = [];
1093 $runtime = [];
1094
1095 $find = null;
1096 $field = '*INDEX.SEARCH_CONTENT';
1097
1098 if (isset($params['FILTER']['SEARCH']))
1099 {
1100 $find = (string)$params['FILTER']['SEARCH'];
1101 }
1102 elseif (isset($params['FILTER']['SEARCH_OL']) && Loader::includeModule('imopenlines'))
1103 {
1104 $find = (string)$params['FILTER']['SEARCH_OL'];
1105 $field = '*OL_INDEX.SEARCH_TITLE';
1106 }
1107
1108 if (isset($params['FILTER']['ID']))
1109 {
1110 $filter['=ID'] = $params['FILTER']['ID'];
1111 }
1112 else if (isset($find))
1113 {
1114 $helper = Application::getConnection()->getSqlHelper();
1115 if (Model\ChatIndexTable::getEntity()->fullTextIndexEnabled('SEARCH_CONTENT'))
1116 {
1117 $find = trim($find);
1118 $find = \Bitrix\Main\Search\Content::prepareStringToken($find);
1119
1120 if (\Bitrix\Main\Search\Content::canUseFulltextSearch($find, \Bitrix\Main\Search\Content::TYPE_MIXED))
1121 {
1122 $filter[$field] = $find;
1123 }
1124 else
1125 {
1126 return null;
1127 }
1128 }
1129 else
1130 {
1131 if (mb_strlen($find) < 3)
1132 {
1133 return null;
1134 }
1135
1136 $filter['%=INDEX.SEARCH_TITLE'] = $helper->forSql($find).'%';
1137 }
1138 }
1139
1140 if ($params['SKIP_ACCESS_CHECK'] === 'Y')
1141 {
1142 // do nothing
1143 }
1144 else if (
1145 User::getInstance($params['CURRENT_USER'])->isExtranet()
1146 || User::getInstance($params['CURRENT_USER'])->isBot()
1147 )
1148 {
1149 $filter['=TYPE'] = [
1154 ];
1155 if (User::getInstance($params['CURRENT_USER'])->isBot() && Loader::includeModule('imopenlines'))
1156 {
1157 $filter['=TYPE'][] = self::TYPE_OPEN_LINE;
1158 $filter[] = [
1159 'LOGIC' => 'OR',
1160 [
1161 '=RELATION.USER_ID' => $params['CURRENT_USER']
1162 ],
1163 [
1164 '=RECENT_OL.USER_ID' => $params['CURRENT_USER']
1165 ]
1166 ];
1167 }
1168 else
1169 {
1170 $filter['=RELATION.USER_ID'] = $params['CURRENT_USER'];
1171 }
1172 }
1173 else
1174 {
1175 $condition = [
1176 'LOGIC' => 'OR',
1177 [
1178 '=TYPE' => self::TYPE_OPEN,
1179 ],
1180 [
1181 '=TYPE' => self::TYPE_GROUP,
1182 '=RELATION.USER_ID' => $params['CURRENT_USER']
1183 ],
1184 [
1185 '=TYPE' => self::TYPE_THREAD,
1186 '=RELATION.USER_ID' => $params['CURRENT_USER']
1187 ],
1188 [
1189 '=TYPE' => self::TYPE_PRIVATE,
1190 '=RELATION.USER_ID' => $params['CURRENT_USER']
1191 ],
1192 [
1193 '=TYPE' => self::TYPE_OPEN_LINE,
1194 '=RELATION.USER_ID' => $params['CURRENT_USER']
1195 ],
1196 ];
1197 if (Loader::includeModule('imopenlines'))
1198 {
1199 $condition[] = [
1200 '=TYPE' => self::TYPE_OPEN_LINE,
1201 '=RECENT_OL.USER_ID' => $params['CURRENT_USER']
1202 ];
1203 }
1204 $filter[] = $condition;
1205 }
1206
1207 $runtime[] = new \Bitrix\Main\Entity\ReferenceField(
1208 'RELATION',
1209 'Bitrix\Im\Model\RelationTable',
1210 array(
1211 "=ref.CHAT_ID" => "this.ID",
1212 "=ref.USER_ID" => new \Bitrix\Main\DB\SqlExpression('?i', $params['CURRENT_USER']),
1213 ),
1214 array("join_type"=>"LEFT")
1215 );
1216 if (Loader::includeModule('imopenlines'))
1217 {
1218 $runtime[] = new \Bitrix\Main\Entity\ReferenceField(
1219 'RECENT_OL',
1220 \Bitrix\ImOpenLines\Model\RecentTable::class,
1221 array(
1222 "=ref.CHAT_ID" => "this.ID",
1223 "=ref.USER_ID" => new \Bitrix\Main\DB\SqlExpression('?i', $params['CURRENT_USER']),
1224 ),
1225 array("join_type"=>"LEFT")
1226 );
1227 }
1228
1229 return [
1230 'select' => [
1231 '*',
1232 'RELATION_USER_ID' => 'RELATION.USER_ID',
1233 'RELATION_MANAGER' => 'RELATION.MANAGER',
1234 'RELATION_NOTIFY_BLOCK' => 'RELATION.NOTIFY_BLOCK',
1235 //'RELATION_COUNTER' => 'RELATION.COUNTER',
1236 'RELATION_START_COUNTER' => 'RELATION.START_COUNTER',
1237 'RELATION_LAST_ID' => 'RELATION.LAST_ID',
1238 //'RELATION_STATUS' => 'RELATION.STATUS',
1239 //'RELATION_UNREAD_ID' => 'RELATION.UNREAD_ID',
1240 'ALIAS_NAME' => 'ALIAS.ALIAS',
1241 ],
1242 'filter' => $filter,
1243 'runtime' => $runtime
1244 ];
1245 }
1246
1247 public static function toJson($array)
1248 {
1249 return \Bitrix\Im\Common::toJson($array, false);
1250 }
1251
1252 public static function isUserInChat($chatId, $userId = 0) : bool
1253 {
1254 if ($userId === 0)
1255 {
1256 $userId = \Bitrix\Im\Common::getUserId();
1257 }
1258
1259 if (!$userId)
1260 {
1261 return false;
1262 }
1263
1264 $result = \Bitrix\Im\Model\RelationTable::getList(
1265 [
1266 'select' => ["ID"],
1267 'filter' => [
1268 '=USER_ID' => $userId,
1269 '=CHAT_ID' => $chatId
1270 ]
1271 ]
1272 )->fetch();
1273
1274 return (bool)$result['ID'];
1275 }
1276
1277 public static function isUserKickedFromChat($chatId, $userId = 0) : bool
1278 {
1279 if ($userId === 0)
1280 {
1281 $userId = \Bitrix\Im\Common::getUserId();
1282 }
1283
1284 if (!$userId)
1285 {
1286 return false;
1287 }
1288
1289 $result = BlockUserTable::getList(
1290 [
1291 'select' => ["ID"],
1292 'filter' => [
1293 '=USER_ID' => $userId,
1294 '=CHAT_ID' => $chatId
1295 ]
1296 ]
1297 )->fetch();
1298
1299 return is_array($result) && (bool)$result['ID'];
1300 }
1301
1302 public static function checkReplicaDeprecatedAgent(): string
1303 {
1304 return '';
1305 }
1306
1322 public static function isActionAllowed($dialogId, $action, $entityType = null): bool
1323 {
1324 if (!\Bitrix\Im\Common::isChatId($dialogId))
1325 {
1326 return true;
1327 }
1328
1329 $chatOptions = \CIMChat::GetChatOptions();
1330 $isAllowedByDefault = (bool)($chatOptions['DEFAULT'][$action] ?? true);
1331
1332 if ($entityType && $chatOptions[$entityType])
1333 {
1334 return (bool)($chatOptions[$entityType][$action] ?? $isAllowedByDefault);
1335 }
1336
1337 if ($entityType)
1338 {
1339 return $isAllowedByDefault;
1340 }
1341
1342 $chatId = \Bitrix\Im\Dialog::getChatId($dialogId);
1343 if (!$chatId)
1344 {
1345 return $isAllowedByDefault;
1346 }
1347
1348 $generalChatId = (int)\CIMChat::GetGeneralChatId();
1349 if ($chatId === $generalChatId)
1350 {
1351 return (bool)($chatOptions['GENERAL'][$action] ?? $isAllowedByDefault);
1352 }
1353
1354 $chat = \Bitrix\Im\Model\ChatTable::getList([
1355 'select' => [
1356 'ID',
1357 'ENTITY_TYPE',
1358 ],
1359 'filter' => [
1360 'ID' => $chatId,
1361 ]
1362 ])->fetch();
1363
1364 $entityType = ($chat && $chat['ENTITY_TYPE']) ? $chat['ENTITY_TYPE'] : null;
1365
1366 if ($entityType && $chatOptions[$entityType])
1367 {
1368 return (bool)($chatOptions[$entityType][$action] ?? $isAllowedByDefault);
1369 }
1370
1371 return $isAllowedByDefault;
1372 }
1373
1385 public static function getOwnerById($dialogId): ?int
1386 {
1387 $chatId = \Bitrix\Im\Dialog::getChatId($dialogId);
1388 if (!$chatId)
1389 {
1390 return null;
1391 }
1392
1393 $chat = \Bitrix\Im\Model\ChatTable::getList([
1394 'select' => [
1395 'ID',
1396 'AUTHOR_ID',
1397 ],
1398 'filter' => [
1399 'ID' => $chatId,
1400 ]
1401 ])->fetch();
1402
1403 return ($chat && is_numeric($chat['AUTHOR_ID'])) ? (int)$chat['AUTHOR_ID'] : null;
1404 }
1405
1406 public static function fillCounterData(array $chats): array
1407 {
1408 if (empty($chats))
1409 {
1410 return [];
1411 }
1412
1413 $userId = \Bitrix\Im\Common::getUserId();
1414 $readService = new ReadService($userId);
1415
1416 $chatIds = [];
1417
1418 foreach ($chats as $chat)
1419 {
1420 $chatIds[] = (int)$chat['ID'];
1421 }
1422
1423 $counters = $readService->getCounterService()->getForEachChat($chatIds);
1424 $unreadIds = $readService->getCounterService()->getIdFirstUnreadMessageForEachChats($chatIds);
1425 $markedIds = Recent::getMarkedIdByChatIds($userId, $chatIds);
1426
1427 foreach ($chats as $key => $chat)
1428 {
1429 $id = (int)$chat['ID'];
1430 $chats[$key]['RELATION_COUNTER'] = $counters[$id] ?? 0;
1431 $chats[$key]['RELATION_UNREAD_ID'] = $unreadIds[$id] ?? 0;
1432 $chats[$key]['MARKED_ID'] = $markedIds[$id] ?? 0;
1433 }
1434
1435 return $chats;
1436 }
1437
1438 private static function getRole(array $chat): string
1439 {
1440 if (!isset($chat['RELATION_USER_ID']))
1441 {
1442 return \Bitrix\Im\V2\Chat::ROLE_GUEST;
1443 }
1444 if ((int)$chat['RELATION_USER_ID'] === (int)$chat['AUTHOR_ID'])
1445 {
1446 return \Bitrix\Im\V2\Chat::ROLE_OWNER;
1447 }
1448 if ($chat['RELATION_MANAGER'] === 'Y')
1449 {
1450 return \Bitrix\Im\V2\Chat::ROLE_MANAGER;
1451 }
1452
1453 return \Bitrix\Im\V2\Chat::ROLE_MEMBER;
1454 }
1455}
static getPublicLink($type, $alias)
Definition alias.php:210
static isUserInChat($chatId, $userId=0)
Definition chat.php:1252
static fillCounterData(array $chats)
Definition chat.php:1406
const STATUS_READ
Definition chat.php:30
const TYPE_CHANNEL
Definition chat.php:25
const TYPE_THREAD
Definition chat.php:23
static getTypes()
Definition chat.php:36
static hasAccess($chatId)
Definition chat.php:445
static getMessageCount($chatId, $userId=null)
Definition chat.php:414
const TYPE_PRIVATE
Definition chat.php:21
const FILTER_LIMIT
Definition chat.php:34
const STATUS_UNREAD
Definition chat.php:28
static getType($chatData)
Definition chat.php:41
static getUsers($chatId, $options=[])
Definition chat.php:787
static getMessages($chatId, $userId=null, $options=Array())
Definition chat.php:462
static getById($id, $params=array())
Definition chat.php:851
static getListParams($params)
Definition chat.php:1077
static getList($params=array())
Definition chat.php:926
static formatChatData($chat)
Definition chat.php:994
static isActionAllowed($dialogId, $action, $entityType=null)
Definition chat.php:1322
const TYPE_OPEN
Definition chat.php:22
const TYPE_OPEN_LINE
Definition chat.php:26
const TYPE_SYSTEM
Definition chat.php:20
static isUserKickedFromChat($chatId, $userId=0)
Definition chat.php:1277
const TYPE_GROUP
Definition chat.php:24
const LIMIT_SEND_EVENT
Definition chat.php:32
const STATUS_NOTIFY
Definition chat.php:29
static toJson($array)
Definition chat.php:1247
static getOwnerById($dialogId)
Definition chat.php:1385
static getRelation($chatId, $params=[])
Definition chat.php:84
static mute($chatId, $action, $userId=null)
Definition chat.php:332
static checkReplicaDeprecatedAgent()
Definition chat.php:1302
static getColorByNumber($number)
Definition color.php:144
static getColor($code)
Definition color.php:121
static toJson($array, $camelCase=true)
Definition common.php:90
static isChatId($id)
Definition common.php:64
static getPullExtra()
Definition common.php:128
static clearCache($userId=null)
Definition recent.php:1667
static getMarkedIdByChatIds(int $userId, array $chatIds)
Definition recent.php:1484
static parse($text, $params=Array())
Definition text.php:24
static getInstance($userId=null)
Definition user.php:44
static getMessagesDisappearingTime(array $messageIds)
static getConnection($name="")
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
$GLOBALS['____1444769544']
Definition license.php:1