• Модуль: im
  • Путь к файлу: ~/bitrix/modules/im/classes/general/im_message.php
  • Класс: CIMMessage
  • Вызов: CIMMessage::SetLastId
static function SetLastId($chatId, $userId, $lastId = null)
{
	$chatId = intval($chatId);
	$userId = intval($userId);

	$lastIdIsNull = $lastId === null;
	$lastId = intval($lastId);

	if ($chatId <= 0 || $userId <= 0)
		return false;

	$updateCounters = false;
	$relations = BitrixImChat::getRelation($chatId, Array(
		//'SELECT' => Array('ID', 'CHAT_ID', 'LAST_ID', 'LAST_SEND_ID', 'STATUS', 'USER_ID', 'NOTIFY_BLOCK', 'MESSAGE_TYPE'),
		'SELECT' => Array('ID', 'CHAT_ID', 'LAST_ID', 'USER_ID', 'NOTIFY_BLOCK', 'MESSAGE_TYPE'),
		'FILTER' => Array(
			'USER_ID' => $userId
		),
		'REAL_COUNTERS' => $lastIdIsNull? 'Y': Array(
			'LAST_ID' => $lastId
		)
	));
	if (isset($relations[$userId]))
	{
		$relation = $relations[$userId];
	}
	else
	{
		return false;
	}



	/*$update = array();
	if (!$lastIdIsNull)
	{
		if ($relation["LAST_ID"] < $lastId)
		{
			$relation["LAST_ID"] = $update["LAST_ID"] = $lastId;
		}
		if ($relation["LAST_SEND_ID"] < $lastId)
		{
			$relation["LAST_SEND_ID"] = $update["LAST_SEND_ID"] = $lastId;
		}
	}

	if ($relation['COUNTER'] > 0)
	{
		if ($relation['STATUS'] == IM_STATUS_READ)
		{
			$relation["STATUS"] = $update["STATUS"] = IM_STATUS_NOTIFY;
		}

		$filter = [
			'=CHAT_ID' => $relation['CHAT_ID']
		];
		if ($lastId > 0)
		{
			$filter['>ID'] = $lastId;
		}

		$firstUnreadMessage = BitrixImModelMessageTable::getList([
			'select' => ['ID'],
			'filter' => $filter,
			'limit' => 1,
		])->fetch();

		$update["LAST_ID"] = $relation["LAST_ID"] = $lastId;
		$update["UNREAD_ID"] = $firstUnreadMessage? $firstUnreadMessage['ID']: 0;
	}
	else
	{
		if ($relation['STATUS'] != IM_STATUS_READ)
		{
			$relation["STATUS"] = $update["STATUS"] = IM_STATUS_READ;
		}
		$relation["UNREAD_ID"] = $update["UNREAD_ID"] = 0;
	}

	if ($relation['COUNTER'] != $relation['PREVIOUS_COUNTER'])
	{
		$update["COUNTER"] = $relation['COUNTER'];
		$updateCounters = true;
	}*/

	$relation['CHAT_MESSAGE_STATUS'] = (new BitrixImV2MessageReadService($userId))->getChatMessageStatus($chatId);

	/*if ($update)
	{
		if ($relation['STATUS'] == IM_STATUS_READ)
		{
			$relation["LAST_READ"] = $update["LAST_READ"] = new BitrixMainTypeDateTime();
			$relation["MESSAGE_STATUS"] = $update["MESSAGE_STATUS"] = IM_MESSAGE_STATUS_DELIVERED;
		}
		else
		{
			$relation["LAST_READ"] = $update["LAST_READ"] = '';
			$relation["MESSAGE_STATUS"] = $update["MESSAGE_STATUS"] = IM_MESSAGE_STATUS_RECEIVED;
		}

		BitrixImModelRelationTable::update($relation["ID"], $update);

		if ($relation['MESSAGE_TYPE'] != IM_MESSAGE_OPEN_LINE)
		{
			$orm = BitrixImModelRelationTable::getList(array(
				'filter' => Array(
					'=CHAT_ID' => $chatId
				)
			));
			if ($relation['STATUS'] == IM_STATUS_READ)
			{
				BitrixImModelChatTable::update($chatId, Array('LAST_MESSAGE_STATUS' => IM_MESSAGE_STATUS_DELIVERED));
				$relation['CHAT_MESSAGE_STATUS'] = IM_MESSAGE_STATUS_DELIVERED;

				while ($row = $orm->fetch())
				{
					if ($userId == $row['USER_ID'])
					{
						$updateCounters = true;
						continue;
					}
					BitrixImCounter::clearCache($row['USER_ID']);
				}
			}
			else
			{
				$relations = Array();
				while ($row = $orm->fetch())
				{
					$relations[] = $row['USER_ID'];
					if ($row['MESSAGE_STATUS'] == IM_MESSAGE_STATUS_DELIVERED)
					{
						$relation['CHAT_MESSAGE_STATUS'] = IM_MESSAGE_STATUS_DELIVERED;
						$relations = Array();
						break;
					}
				}
				if ($relations)
				{
					BitrixImModelChatTable::update($chatId, Array('LAST_MESSAGE_STATUS' => IM_MESSAGE_STATUS_RECEIVED));
					$relation['CHAT_MESSAGE_STATUS'] = IM_MESSAGE_STATUS_RECEIVED;
					foreach ($relations as $relationUserId)
					{
						if ($userId == $relationUserId)
						{
							$updateCounters = true;
							continue;
						}
						BitrixImCounter::clearCache($relationUserId);
					}
				}
			}
		}
	}

	if ($updateCounters)
	{
		BitrixImCounter::clearCache($userId);
	}*/

	return $relation;
}