• Модуль: pull
  • Путь к файлу: ~/bitrix/modules/pull/classes/general/pull_channel.php
  • Класс: CPullChannel
  • Вызов: CPullChannel::Add
static function Add($userId, $channelId = null, $publicChannelId = null, $channelType = self::TYPE_PRIVATE)
{
	global $DB;

	$userId = intval($userId);
	$cache_id="b_pchc_".$userId."_".$channelType;

	if ($userId)
	{
		$user = BitrixMainUserTable::getById($userId)->fetch();
		if ($user['ACTIVE'] == 'N')
		{
			return false;
		}
	}

	$userId = intval($userId);
	$channelId = is_null($channelId)? self::GetNewChannelId(): $channelId;
	if (is_null($publicChannelId))
	{
		$publicChannelId = $userId > 0? self::GetNewChannelId(): '';
	}

	$channelFields = [
		'USER_ID' => (int)$userId,
		'CHANNEL_ID' => $channelId,
		'CHANNEL_PUBLIC_ID' => $publicChannelId,
		'CHANNEL_TYPE' => $channelType,
		'LAST_ID' => 0,
		'DATE_CREATE' => new BitrixMainTypeDateTime(),
	];

	$isChannelAdded = false;
	try
	{
		$result = BitrixPullChannelTable::add($channelFields);
		if (!$result->isSuccess())
		{
			foreach ($result->getErrors() as $error)
			{
				$exception = new BitrixMainSystemException($error->getMessage());
				BitrixMainApplication::getInstance()->getExceptionHandler()->writeToLog($exception);
			}

			return false;
		}

		$isChannelAdded = true;
	}
	catch (Throwable $exception)
	{
		if (!mb_strpos($exception->getMessage(), '1062'))
		{
			BitrixMainApplication::getInstance()->getExceptionHandler()->writeToLog($exception);

			return false;
		}
	}

	if ($isChannelAdded)
	{
		$arChannel = Array(
			'CHANNEL_ID' => $channelId,
			'CHANNEL_PUBLIC_ID' => $publicChannelId,
			'CHANNEL_TYPE' => $channelType,
			'DATE_CREATE' => time(),
			'LAST_ID' => 0,
		);
		self::SaveToCache($cache_id, $arChannel);

		if (CPullOptions::GetQueueServerStatus() && CPullOptions::GetQueueServerVersion() < 3 && !CPullOptions::IsServerShared())
		{
			self::Send($channelId, BitrixPullCommon::jsonEncode(Array(
				'module_id' => 'pull',
				'command' => 'open',
				'expiry' => 1,
				'params' => Array(),
				'extra' => Array(
					'server_time' => date('c'),
					'server_time_unix' => microtime(true),
					'server_name' => COption::GetOptionString('main', 'server_name', $_SERVER['SERVER_NAME']),
					'revision_web' => PULL_REVISION_WEB,
					'revision_mobile' => PULL_REVISION_MOBILE,
				),
			)));
		}
	}
	else
	{
		CTimeZone::Disable();
		$strSql = "
				SELECT CHANNEL_ID, CHANNEL_PUBLIC_ID, ".$DB->DatetimeToTimestampFunction('DATE_CREATE')." DATE_CREATE, LAST_ID
				FROM b_pull_channel
				WHERE USER_ID = ".$userId." AND CHANNEL_TYPE = '".$DB->ForSQL($channelType)."'
		";
		CTimeZone::Enable();
		$res = $DB->Query($strSql);
		$arChannel = $res->Fetch();
		if (!$arChannel)
		{
			return false;
		}
		$channelId = $arChannel['CHANNEL_ID'];
		self::SaveToCache($cache_id, $arChannel);

		if (CPullOptions::GetQueueServerStatus() && CPullOptions::GetQueueServerVersion() < 3 && !CPullOptions::IsServerShared())
		{
			self::Send($channelId, BitrixPullCommon::jsonEncode(Array(
				'module_id' => 'pull',
				'command' => 'open_exists',
				'expiry' => 1,
				'params' => Array(),
				'extra' => Array(
					'server_time' => date('c'),
					'server_time_unix' => microtime(true),
					'server_name' => COption::GetOptionString('main', 'server_name', $_SERVER['SERVER_NAME']),
					'revision_web' => PULL_REVISION_WEB,
					'revision_mobile' => PULL_REVISION_MOBILE,
				),
			)));
		}
	}

	return $arChannel;
}