• Модуль: im
  • Путь к файлу: ~/bitrix/modules/im/lib/controller/call.php
  • Класс: BitrixImControllerCall
  • Вызов: Call::createAction
public function createAction($type, $provider, $entityType, $entityId, $joinExisting = false)
{
	$currentUserId = $this->getCurrentUser()->getId();

	$lockName = static::getLockNameWithEntityId($entityType, $entityId, $currentUserId);
	if (!Application::getConnection()->lock($lockName, static::LOCK_TTL))
	{
		$this->errorCollection[] = new Error("Could not get exclusive lock", "could_not_lock");
		return null;
	}

	$call = $joinExisting ? CallFactory::searchActive($type, $provider, $entityType, $entityId, $currentUserId) : false;

	try
	{
		if ($call)
		{
			if (!$call->getAssociatedEntity()->checkAccess($currentUserId))
			{
				$this->errorCollection[] = new Error("You can not access this call", 'access_denied');
				Application::getConnection()->unlock($lockName);
				return null;
			}

			$isNew = false;
			if (!$call->hasUser($currentUserId))
			{
				$addedUser = $call->addUser($currentUserId);

				if (!$addedUser)
				{
					$this->errorCollection[] = new Error("User limit reached", "user_limit_reached");
					Application::getConnection()->unlock($lockName);
					return null;
				}
			}
		}
		else
		{
			$isNew = true;

			try {
				$call = CallFactory::createWithEntity($type, $provider, $entityType, $entityId, $currentUserId);
			} catch (Throwable $e) {
				$this->addError(new Error($e->getMessage(), $e->getCode()));
				Application::getConnection()->unlock($lockName);
				return null;
			}

			if (!$call->getAssociatedEntity()->canStartCall($currentUserId))
			{
				$this->errorCollection[] = new Error("You can not create this call", 'access_denied');
				Application::getConnection()->unlock($lockName);
				return null;
			}

			$initiator = $call->getUser($currentUserId);
			$initiator->update([
				'STATE' => CallUser::STATE_READY,
				'LAST_SEEN' => new DateTime(),
				'FIRST_JOINED' => new DateTime()
			]);
		}
	}
	catch(Exception $e)
	{
		$this->errorCollection[] = new Error(
			"Can't initiate a call. Server error. (" . ($status ?? "") . ")",
			"call_init_error");

		Application::getConnection()->unlock($lockName);
		return null;
	}

	$users = $call->getUsers();
	$publicChannels = Loader::includeModule('pull')
		? BitrixPullChannel::getPublicIds([
			'TYPE' => CPullChannel::TYPE_PRIVATE,
			'USERS' => $users,
			'JSON' => true
		])
		: []
	;

	Application::getConnection()->unlock($lockName);

	if ($provider !== 'Plain')
	{
		$this->sendPrecallInviteMessage($entityId, $entityType, $call);
	}

	return [
		'call' => $call->toArray(),
		'connectionData' => $call->getConnectionData($currentUserId),
		'isNew' => $isNew,
		'users' => $users,
		'userData' => Util::getUsers($users),
		'publicChannels' => $publicChannels,
		'logToken' => $call->getLogToken($currentUserId),
	];
}