• Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/lib/integration/im/chat.php
  • Класс: Bitrix\Crm\Integration\Im\Chat
  • Вызов: Chat::joinChat
static function joinChat($params = [])
{
	if (!\Bitrix\Main\Loader::includeModule('im'))
	{
		return false;
	}

	$entityType = $params['ENTITY_TYPE'];
	$entityTypeId = \CCrmOwnerType::ResolveID($entityType);
	$entityId = (int)$params['ENTITY_ID'];
	$userId = (int)$params['USER_ID'];

	if (empty($entityType) || empty($entityId) || empty($userId))
	{
		return false;
	}

	if (!self::checkPermission($entityTypeId, $entityId, $userId))
	{
		return false;
	}

	$chatData = \Bitrix\Im\Model\ChatTable::getList(Array(
		'select' => [
			'ID',
			'RELATION_USER_ID' => 'RELATION.USER_ID',
		],
		'filter' => [
			'=ENTITY_TYPE' => self::CHAT_ENTITY_TYPE,
			'=ENTITY_ID' => $entityType.'|'.$entityId,
		],
		'runtime' => Array(
			new \Bitrix\Main\Entity\ReferenceField(
				'RELATION',
				'\Bitrix\Im\Model\RelationTable',
				array(
					"=ref.CHAT_ID" => "this.ID",
					"=ref.USER_ID" => new \Bitrix\Main\DB\SqlExpression('?', $userId)
				),
				array("join_type"=>"LEFT")
			)
		)
	))->fetch();
	if ($chatData)
	{
		if (!$chatData['RELATION_USER_ID'])
		{
			$chat = new \CIMChat(0);
			$chat->AddUser($chatData['ID'], [$userId], false);
		}

		return $chatData['ID'];
	}
	else
	{
		return self::createChat([
			'ENTITY_TYPE' => $entityType,
			'ENTITY_ID' => $entityId,
			'USER_ID' => $userId,
			'ENABLE_PERMISSION_CHECK' => false,
		]);
	}
}