• Модуль: im
  • Путь к файлу: ~/bitrix/modules/im/lib/lastsearch.php
  • Класс: BitrixImLastSearch
  • Вызов: LastSearch::add
static function add($dialogId, $userId = null)
{
	$userId = BitrixImCommon::getUserId($userId);
	if (!$userId)
	{
		return false;
	}

	if (!$dialogId || $dialogId == 'chat0')
	{
		return false;
	}

	if (!BitrixImCommon::isDialogId($dialogId))
	{
		return false;
	}

	if (BitrixImCommon::isChatId($dialogId))
	{
		$chatId = BitrixImDialog::getChatId($dialogId);
		$relations = Chat::getRelation($chatId, ['WITHOUT_COUNTERS' => 'Y']);
		if (!$relations[$userId])
		{
			return false;
		}

		$relationId = $relations[$userId]['ID'];
	}
	else
	{
		$relations = BitrixImDialog::getRelation($userId, $dialogId, ['WITHOUT_COUNTERS' => 'Y']);
		if (!$relations[$userId])
		{
			return false;
		}

		$chatId = $relations[$userId]['CHAT_ID'];
		$relationId = $relations[$userId]['ID'];
	}

	$orm = BitrixImModelLastSearchTable::getList(Array(
		'filter' => Array('USER_ID' => $userId, 'DIALOG_ID' => $dialogId),
		'order' => Array('ID' => 'DESC')
	));
	if ($orm->fetch())
	{
		return true;
	}

	$result = BitrixImModelLastSearchTable::add(Array(
		'USER_ID' => $userId,
		'DIALOG_ID' => $dialogId,
		'ITEM_CID' => $chatId,
		'ITEM_RID' => $relationId,
	));

	if (!$result->isSuccess())
		return false;

	$count = 0;
	$delete = Array();

	$orm = BitrixImModelLastSearchTable::getList(Array(
		'filter' => Array('USER_ID' => $userId),
		'order' => Array('ID' => 'DESC')
	));
	while ($row = $orm->fetch())
	{
		$count++;

		if ($count > self::LIMIT)
		{
			$delete[] = $row['ID'];
		}
	}

	foreach ($delete as $id)
	{
		BitrixImModelLastSearchTable::delete($id);
	}

	self::clearCache($userId);

	return $result->getId();
}