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

	$messageId = null;
	$chatId = null;

	if (isset($arParams['MESSAGE_ID']) && (int)$arParams['MESSAGE_ID'] > 0)
	{
		$messageId = (int)$arParams['MESSAGE_ID'];

		$message = new BitrixImV2Message($messageId);

		if ($message->getMessageId() === null)
		{
			throw new BitrixRestRestException('Message not found', BitrixImV2MessageMessageError::MESSAGE_NOT_FOUND, CRestServer::STATUS_WRONG_REQUEST);
		}

		$chat = $message->getChat();
	}
	elseif (isset($arParams['CHAT_ID']) && (int)$arParams['CHAT_ID'] > 0)
	{
		$chatId = (int)$arParams['CHAT_ID'];

		$chat = BitrixImV2Chat::getInstance($chatId);
	}
	else
	{
		throw new BitrixRestRestException('Message ID and chat ID can`t be empty together', 'CHAT_ID_MESSAGE_ID_EMPTY', CRestServer::STATUS_WRONG_REQUEST);
	}

	if (!isset($arParams['CALENDAR_ID']) || (int)$arParams['CALENDAR_ID'] <= 0)
	{
		throw new BitrixRestRestException('CALENDAR_ID can`t be empty', 'CALENDAR_ID_EMPTY', CRestServer::STATUS_WRONG_REQUEST);
	}

	if (!$chat->hasAccess())
	{
		throw new BitrixRestRestException('You do not have access to this chat', BitrixImV2RestRestError::ACCESS_ERROR, CRestServer::STATUS_FORBIDDEN);
	}

	if (isset($messageId))
	{
		$startId = $chat->getStartId();

		if ($messageId < $startId)
		{
			throw new BitrixRestRestException('You do not have access to this message', BitrixImV2MessageMessageError::MESSAGE_ACCESS_ERROR, CRestServer::STATUS_FORBIDDEN);
		}
	}

	$chatId = $chat->getChatId();

	$calendarId = $arParams['CALENDAR_ID'];

	$calendarService = new BitrixImV2LinkCalendarCalendarService();
	$calendar = BitrixImV2EntityCalendarCalendarItem::initById($calendarId);

	global $USER;
	$userId = (int)$USER->GetID();
	if ($userId !== $calendar->getCreatedBy())
	{
		throw new BitrixRestRestException('You do not have access to this calendar event', BitrixImV2EntityCalendarCalendarError::ACCESS_ERROR, CRestServer::STATUS_FORBIDDEN);
	}

	$saveResult = $calendarService->registerCalendar($chatId, $messageId, $calendar);
	if (!$saveResult->isSuccess())
	{
		$error = $saveResult->getErrors()[0];
		if (isset($error))
		{
			throw new BitrixRestRestException($error->getMessage(), $error->getCode(), CRestServer::STATUS_WRONG_REQUEST);
		}
	}

	return $saveResult->isSuccess();
}