• Модуль: imopenlines
  • Путь к файлу: ~/bitrix/modules/imopenlines/lib/chat.php
  • Класс: BitrixImOpenLinesChat
  • Вызов: Chat::setCounterRelationForChat
static function setCounterRelationForChat($chatId, array $users): Result
{
	$result = new Result();

	foreach ($users as $key=>$user)
	{
		$user = (int)$user;
		if($user > 0)
		{
			$users[$key] = $user;
		}
		else
		{
			unset($users[$key]);
		}
	}

	if(!empty($users))
	{
		$readService = new ReadService();
		$counters = $readService->getCounterService()->getByChatForEachUsers($chatId, $users);
		$lastId = $readService->getViewedService()->getLastMessageIdInChat($chatId) ?? 0;
		$lastMessageInChat = new Message();
		$lastMessageInChat->setMessageId($lastId)->setChatId($chatId);

		foreach ($counters as $userId => $counter)
		{
			if ($counter === 0 && $lastId !== 0)
			{
				$readService->withContextUser($userId)->unreadTo($lastMessageInChat);
			}
		}
	}
	else
	{
		$result->addError(new Error('No users to update counter relation', 'IMOL_NOT_USER_FOR_UPDATE_RELATION', __METHOD__, ['chatId' => $chatId, 'users' => $users, 'counter' => $counter]));
	}

	return $result;
}