• Модуль: im
  • Путь к файлу: ~/bitrix/modules/im/classes/general/im_chat.php
  • Класс: CIMChat
  • Вызов: CIMChat::SetReadMessage
public function SetReadMessage($chatId, $lastId = null, $byEvent = false)
{
	global $DB;

	$chatId = intval($chatId);
	if ($chatId <= 0)
		return false;

	CIMMessenger::SpeedFileDelete($this->user_id, IM_SPEED_GROUP);

	$chat = IMV2Chat::getInstance($chatId);

	if (
		$chat->getType() == IM_MESSAGE_OPEN_LINE
		&& $chat->getAuthorId() == 0
	)
	{
		return false;
	}

	$readService = new IMV2MessageReadService($this->user_id);

	$startId = $readService->getLastIdByChatId($chatId);
	$counter = 0;
	$viewedMessages = [];

	if (isset($lastId))
	{
		$message = new BitrixImV2Message();
		$message->setMessageId((int)$lastId)->setChatId($chatId);
		$readResult = $readService->readTo($message);
		$counter = $readResult->getResult()['COUNTER'];
		$viewedMessages = $readResult->getResult()['VIEWED_MESSAGES'];
	}
	else
	{
		$counter = $readService->readAllInChat($chatId)->getResult()['COUNTER'];
	}

	$relation = CIMMessage::SetLastId($chatId, $this->user_id, 0);

	if (!$relation)
	{
		return false;
	}

	$endId = (int)($relation['LAST_ID'] ?? 0);

	/*BitrixMainApplication::getConnection()->query(
		"UPDATE b_im_recent SET DATE_UPDATE = NOW() WHERE USER_ID = ".$this->user_id." AND ITEM_CID = ".intval($chatId)
	);*/

	if (CModule::IncludeModule("pull"))
	{
		CIMNotify::DeleteBySubTag("IM_MESS_".$chatId.'_'.$this->user_id, false, false);
		CPushManager::DeleteFromQueueBySubTag($this->user_id, 'IM_MESS');

		if (
			$chat->getEntityType() == 'LIVECHAT'
			|| !BitrixImUser::getInstance($this->user_id)->isConnector()
		)
		{
			BitrixPullEvent::add($this->user_id, Array(
				'module_id' => 'im',
				'command' => 'readMessageChat',
				'params' => Array(
					'dialogId' => 'chat'.$chatId,
					'chatId' => (int)$chatId,
					'lastId' => $endId,
					'counter' => $counter,
					'muted' => $relation['NOTIFY_BLOCK'] === 'Y',
					'unread' => ImRecent::isUnread($this->user_id, $relation['MESSAGE_TYPE'], 'chat'.$chatId),
					'lines' => $relation['MESSAGE_TYPE'] === IM_MESSAGE_OPEN_LINE,
					'viewedMessages' => $viewedMessages,
				),
				'extra' => BitrixImCommon::getPullExtra()
			));
		}

		$arRelation = self::GetRelationById($chatId, false, true, false);
		unset($arRelation[$this->user_id]);

		$pushMessage = Array(
			'module_id' => 'im',
			'command' => 'readMessageChatOpponent',
			'expiry' => 600,
			'params' => Array(
				'dialogId' => 'chat'.$chatId,
				'chatId' => (int)$chatId,
				'userId' => (int)$this->user_id,
				'userName' => BitrixImUser::getInstance($this->user_id)->getFullName(false),
				'lastId' => $endId,
				'date' => date('c', time()),
				'viewedMessages' => $viewedMessages,
				'chatMessageStatus' => $relation['CHAT_MESSAGE_STATUS'],
			),
			'extra' => BitrixImCommon::getPullExtra()
		);
		if ($chat->getEntityType() == 'LINES')
		{
			foreach ($arRelation as $rel)
			{
				if ($rel["EXTERNAL_AUTH_ID"] == 'imconnector')
				{
					unset($arRelation[$rel["USER_ID"]]);
				}
			}
		}
		if (count($arRelation) < 200)
		{
			BitrixPullEvent::add(array_keys($arRelation), $pushMessage);
			if ($chat->getType() == IM_MESSAGE_OPEN  || $chat->getType() == IM_MESSAGE_OPEN_LINE)
			{
				CPullWatch::AddToStack('IM_PUBLIC_'.$chatId, $pushMessage);
			}
		}
	}

	foreach(GetModuleEvents("im", "OnAfterChatRead", true) as $arEvent)
	{
		ExecuteModuleEventEx($arEvent, array(Array(
			'CHAT_ID' => $chat->getChatId(),
			'CHAT_ENTITY_TYPE' => $chat->getEntityType(),
			'CHAT_ENTITY_ID' => $chat->getEntityId(),
			'START_ID' => $startId,
			'END_ID' => $endId,
			'COUNT' => $relation['COUNTER'],
			'USER_ID' => $this->user_id,
			'BY_EVENT' => $byEvent
		)));
	}

	return Array(
		'DIALOG_ID' => 'chat'.$chatId,
		'CHAT_ID' => (int)$chatId,
		'LAST_ID' => $endId,
		'COUNTER' => (int)$relation['COUNTER']
	);
}