6use Bitrix\Im\V2\Common\ContextCustomer;
19 private function __construct()
29 if (isset(self::$instance))
34 self::$instance =
new static();
49 $type = $params[
'TYPE'] ?? $params[
'MESSAGE_TYPE'] ??
'';
55 if (is_numeric($params))
57 $params = [
'CHAT_ID' => (int)$params];
59 elseif (is_string($params))
61 $params = [
'DIALOG_ID' => $params];
64 $params[
'CHAT_ID'] = \Bitrix\Im\Dialog::getChatId($params[
'DIALOG_ID']);
68 $findResult = $this->
findChat($params);
70 if ($findResult->hasResult())
72 $chatParams = $findResult->getResult();
82 $addResult = $this->
addChat($params);
83 if ($addResult->hasResult())
85 $chat = $addResult->getResult()[
'CHAT'];
86 $chat->setContext($this->context);
102 $userId = $this->getContext()->getUserId();
107 'TO_USER_ID' => $userId,
110 return $this->
getChat($params);
122 'ENTITY_TYPE' => $entityType,
123 'ENTITY_ID' => $entityId,
126 return $this->
getChat($params);
146 'FROM_USER_ID' => $fromUserId,
147 'TO_USER_ID' => $toUserId,
150 return $this->
getChat($params);
160 $userId = $this->getContext()->getUserId();
165 'FROM_USER_ID' => $userId,
166 'TO_USER_ID' => $userId,
169 return $this->
getChat($params);
180 $type = $params[
'TYPE'] ?? $params[
'MESSAGE_TYPE'] ??
'';
181 $entityType = $params[
'ENTITY_TYPE'] ??
'';
185 case $entityType ===
'PERSONAL':
239 $chat->setContext($this->context);
250 $params = $params ?? [];
262 $params = $params ?? [];
274 $params = $params ?? [];
286 $params = $params ?? [];
298 $params = $params ?? [];
313 public function getChatById(
int $chatId):
Chat
315 $findResult = $this->
findChat([
'CHAT_ID' => $chatId]);
316 if ($findResult->hasResult())
318 $chatParams = $findResult->getResult();
321 $chat = $this->
initChat($chatParams);
360 if (isset($params[
'TYPE']))
362 $params[
'MESSAGE_TYPE'] = $params[
'TYPE'];
365 if (empty($params[
'CHAT_ID']) && !empty($params[
'DIALOG_ID']))
369 $params[
'CHAT_ID'] = \Bitrix\Im\Dialog::getChatId($params[
'DIALOG_ID']);
370 if (!isset($params[
'MESSAGE_TYPE']))
377 $params[
'TO_USER_ID'] = (int)$params[
'DIALOG_ID'];
382 if (!empty($params[
'CHAT_ID']) && (int)$params[
'CHAT_ID'] > 0)
384 $chatId = (int)$params[
'CHAT_ID'];
386 $cachedChat = $cache->getVars();
388 if ($cachedChat !==
false)
390 return $result->setResult($this->filterNonCachedFields($cachedChat));
393 $chat = \Bitrix\Im\Model\ChatTable::getByPrimary((
int)$params[
'CHAT_ID'])->fetch();
397 $cache->startDataCache();
398 $cache->endDataCache($chat);
405 return $result->setResult($chat);
408 switch ($params[
'MESSAGE_TYPE'] ??
'')
416 isset($params[
'TO_USER_ID'], $params[
'FROM_USER_ID'])
417 && $params[
'TO_USER_ID'] == $params[
'FROM_USER_ID']
431 isset($params[
'ENTITY_TYPE'])
439 $result =
Chat::find($params, $this->context);
449 private function filterNonCachedFields(array $chat): array
451 foreach (self::NON_CACHED_FIELDS as $key)
469 $addResult =
new Result();
471 $params[
'ENTITY_TYPE'] = $params[
'ENTITY_TYPE'] ??
'';
482 $params[
'SEARCHABLE'] =
'N';
485 switch ($params[
'ENTITY_TYPE'])
500 switch ($params[
'TYPE'])
503 if ($params[
'ENTITY_TYPE'])
505 $addResult = (
new EntityChat())->add($params);
508 $addResult = (
new GroupChat())->add($params);
511 $addResult = (
new OpenChat())->add($params);
552 $cacheTTL = defined(
"BX_COMP_MANAGED_CACHE") ? 18144000 : 1800;
553 $cacheId =
"chat_data_{$id}";
554 $cacheDir = $this->getCacheDir($id);
556 $cache->initCache($cacheTTL, $cacheId, $cacheDir);
561 private function getCacheDir(
int $id): string
563 $cacheSubDir = $id % 100;
565 return "/bx/imc/chatdata/5/{$cacheSubDir}/{$id}";
createOpenLineChat(?array $params=null)
createPrivateChat(?array $params=null)
getEntityChat(string $entityType, $entityId)
getNotifyFeed($userId=null)
getPrivateChat($fromUserId, $toUserId)
initChat(?array $params=null)
getPersonalChat($userId=null)
createPersonalChat(?array $params=null)
createNotifyFeed(?array $params=null)
createOpenChat(?array $params=null)
static find(array $params=[], ?Context $context=null)
static find(array $params=[], ?Context $context=null)
static find(array $params=[], ?Context $context=null)
static find(array $params, ?Context $context=null)
const ENTITY_TYPE_LIVECHAT
const ENTITY_TYPE_GENERAL
const ENTITY_TYPE_VIDEOCONF
static find(array $params, ?Context $context=null)
const ENTITY_TYPE_FAVORITE