• Модуль: im
  • Путь к файлу: ~/bitrix/modules/im/classes/general/im_message.php
  • Класс: CIMMessage
  • Вызов: CIMMessage::SetLastIdForUnread
static function SetLastIdForUnread($chatId, $userId, $lastId)
{
	$message = new BitrixImV2Message();
	$message->setMessageId($lastId)->setChatId($chatId);
	$ownRelation = BitrixImV2Chat::getInstance($chatId)->getSelfRelation();

	if ($ownRelation === null)
	{
		return false;
	}

	$firstUnreadMessage = BitrixImModelMessageTable::getList([
		'select' => ['ID'],
		'filter' => [
			'=CHAT_ID' => $chatId,
			' $lastId
		],
		'limit' => 1,
		'order' => Array('ID' => 'DESC')
	])->fetch();

	$firstUnreadMessage = intval($firstUnreadMessage['ID']);
	$sql = "
		UPDATE b_im_relation
		SET LAST_ID=(CASE WHEN LAST_ID < {$firstUnreadMessage} THEN LAST_ID ELSE {$firstUnreadMessage} END)
		WHERE CHAT_ID={$chatId} AND USER_ID={$userId}
	";
	Application::getConnection()->queryExecute($sql);
	$readService = new BitrixImV2MessageReadService($userId);
	$readService->getCounterService()->addStartingFrom($lastId, $ownRelation);
	$relation = self::SetLastId($chatId, $userId, $lastId);
	$counter = $relation['COUNTER'];
	$time = microtime(true);
	$readService->getViewedService()->deleteStartingFrom($message);
	$readService->sendPush($message->getChatId(), [$userId], $counter, $time);

	return $relation;
}