• Модуль: im
  • Путь к файлу: ~/bitrix/modules/im/lib/counter.php
  • Класс: BitrixImCounter
  • Вызов: Counter::countingLostCountersAgent
static function countingLostCountersAgent($notifyRelationId = 0, $chatRelationId = 0)
{
	return '';

	$foundNotify = false;
	$foundChat = false;

	$notifyStartId = intval($notifyRelationId);

	if ($notifyStartId >= 0)
	{
		$query = "
			SELECT COUNT(1) CNT, R.ID, R.USER_ID
			FROM b_im_message M
			INNER JOIN b_im_relation R ON R.CHAT_ID = M.CHAT_ID AND R.MESSAGE_TYPE = '".IM_MESSAGE_SYSTEM."' AND R.COUNTER = 0
			WHERE M.NOTIFY_READ <> 'Y' AND R.ID > ".$notifyStartId."
			GROUP BY R.ID, R.USER_ID
			HAVING CNT > 0
		";
		$cursor = BitrixMainApplication::getInstance()->getConnection()->query($query);

		$count = 0;
		while ($row = $cursor->fetch())
		{
			$notifyRelationId = $row['ID'];
			$foundNotify = true;

			BitrixImModelRelationTable::update($row['ID'], Array(
				'STATUS' => IM_STATUS_UNREAD,
				"MESSAGE_STATUS" => IM_MESSAGE_STATUS_RECEIVED,
				'COUNTER' => $row['CNT'],
			));

			$count++;
			if ($count > 100)
			{
				break;
			}
		}
	}

	$chatRelationId = intval($chatRelationId);

	if ($chatRelationId >= 0)
	{
		$query = "
			SELECT R.ID, R.COUNTER PREVIOUS_COUNTER, (
				SELECT COUNT(1) FROM b_im_message M WHERE M.CHAT_ID = R.CHAT_ID AND M.ID > R.LAST_ID
			) COUNTER
			FROM b_im_relation R
			WHERE R.STATUS <> ".IM_STATUS_READ." AND R.COUNTER = 0 AND R.ID > ".$chatRelationId."
			ORDER BY R.ID ASC
			LIMIT 0, 100;
		";
		$cursor = BitrixMainApplication::getInstance()->getConnection()->query($query);

		while ($row = $cursor->fetch())
		{
			$chatRelationId = $row['ID'];
			$foundChat = true;

			if ($row['COUNTER'] == 0)
			{
				$update = Array(
					'STATUS' => IM_STATUS_READ,
					"MESSAGE_STATUS" => IM_MESSAGE_STATUS_RECEIVED,
				);
			}
			else if ($row['PREVIOUS_COUNTER'] == $row['COUNTER'])
			{
				continue;
			}
			else
			{
				$update = Array(
					'COUNTER' => $row['COUNTER']
				);
			}
			BitrixImModelRelationTable::update($row['ID'], $update);
		}
	}

	if ($foundNotify || $foundChat)
	{
		return 'BitrixImCounter::countingLostCountersAgent('.($foundNotify? $notifyRelationId: -1).', '.($foundChat? $chatRelationId: -1).');';
	}
	else
	{
		return '';
	}
}