• Модуль: im
  • Путь к файлу: ~/bitrix/modules/im/classes/general/im_rest.php
  • Класс: CIMRestService
  • Вызов: CIMRestService::notifyAdd
static function notifyAdd($arParams, $n, CRestServer $server)
{
	global $USER;

	if ($server->getAuthType() == BitrixRestSessionAuthAuth::AUTH_TYPE)
	{
		throw new BitrixRestRestException("Access for this method not allowed by session authorization.", "WRONG_AUTH_TYPE", CRestServer::STATUS_FORBIDDEN);
	}

	$arParams = array_change_key_case($arParams, CASE_UPPER);

	if (isset($arParams['TO']))
	{
		$arParams['USER_ID'] = $arParams['TO'];
	}
	$arParams['USER_ID'] = intval($arParams['USER_ID']);
	if ($arParams['USER_ID'] <= 0)
	{
		throw new BitrixRestRestException("User ID can't be empty", "USER_ID_EMPTY", CRestServer::STATUS_WRONG_REQUEST);
	}

	if ($server->getMethod() == "im.notify.personal.add")
	{
		$arParams['TYPE'] = 'USER';
	}
	else if ($server->getMethod() == "im.notify.system.add")
	{
		$arParams['TYPE'] = 'SYSTEM';
	}
	else if (!isset($arParams['TYPE']) || !in_array($arParams['TYPE'], Array('USER', 'SYSTEM')))
	{
		$arParams['TYPE'] = 'USER';
	}

	$arParams['MESSAGE'] = trim($arParams['MESSAGE']);
	if ($arParams['MESSAGE'] == '')
	{
		throw new BitrixRestRestException("Message can't be empty", "MESSAGE_EMPTY", CRestServer::STATUS_WRONG_REQUEST);
	}

	$messageOut = "";
	$arParams['MESSAGE_OUT'] = trim($arParams['MESSAGE_OUT']);
	if ($arParams['TYPE'] == 'SYSTEM')
	{
		$fromUserId = 0;
		$notifyType = IM_NOTIFY_SYSTEM;

		$clientId = $server->getClientId();
		if ($clientId)
		{
			$result = BitrixRestAppTable::getList(array('filter' => array('=CLIENT_ID' => $clientId)));
			$result = $result->fetch();
			$moduleName = !empty($result['APP_NAME'])
				? $result['APP_NAME']
				: (!empty($result['APP_NAME_DEFAULT'])
					? $result['APP_NAME_DEFAULT']
					: $result['CODE']
				)
			;
			$message = $moduleName."#BR#".$arParams['MESSAGE'];

			if (!empty($arParams['MESSAGE_OUT']))
			{
				$messageOut = $moduleName."#BR#".$arParams['MESSAGE_OUT'];
			}
		}
		else
		{
			$message = $arParams['MESSAGE'];
		}
	}
	else
	{
		$fromUserId = $USER->GetID();
		$notifyType = IM_NOTIFY_FROM;
		$message = $arParams['MESSAGE'];
		if (!empty($arParams['MESSAGE_OUT']))
		{
			$messageOut = $arParams['MESSAGE_OUT'];
		}
	}

	$arMessageFields = array(
		"TO_USER_ID" => $arParams['USER_ID'],
		"FROM_USER_ID" => $fromUserId,
		"NOTIFY_TYPE" => $notifyType,
		"NOTIFY_MODULE" => "rest",
		"NOTIFY_EVENT" => "rest_notify",
		"NOTIFY_MESSAGE" => $message,
		"NOTIFY_MESSAGE_OUT" => $messageOut,
	);

	$clientId = $server->getClientId();
	if ($clientId)
	{
		if (!empty($arParams['TAG']))
		{
			$appKey = mb_substr(md5($server->getClientId()), 0, 5);
			$arMessageFields['NOTIFY_TAG'] = 'MP|'.$appKey.'|'.$arParams['TAG'];
		}
		if (!empty($arParams['SUB_TAG']))
		{
			$appKey = mb_substr(md5($server->getClientId()), 0, 5);
			$arMessageFields['NOTIFY_SUB_TAG'] = 'MP|'.$appKey.'|'.$arParams['SUB_TAG'];
		}
	}

	$attach = CIMMessageParamAttach::GetAttachByJson($arParams['ATTACH']);
	if ($attach)
	{
		if ($attach->IsAllowSize())
		{
			$arMessageFields['ATTACH'] = $attach;
		}
		else
		{
			throw new BitrixRestRestException("You have exceeded the maximum allowable size of attach", "ATTACH_OVERSIZE", CRestServer::STATUS_WRONG_REQUEST);
		}
	}
	else if ($arParams['ATTACH'])
	{
		throw new BitrixRestRestException("Incorrect attach params", "ATTACH_ERROR", CRestServer::STATUS_WRONG_REQUEST);
	}

	return CIMNotify::Add($arMessageFields);
}