• Модуль: im
  • Путь к файлу: ~/bitrix/modules/im/lib/recent.php
  • Класс: BitrixImRecent
  • Вызов: Recent::get
static function get($userId = null, $options = [])
{
	$onlyOpenlinesOption = $options['ONLY_OPENLINES'] ?? null;
	$skipOpenlinesOption = $options['SKIP_OPENLINES'] ?? null;
	$skipChat = $options['SKIP_CHAT'] ?? null;
	$skipDialog = $options['SKIP_DIALOG'] ?? null;

	$userId = BitrixImCommon::getUserId($userId);
	if (!$userId)
	{
		return false;
	}

	$showOpenlines = (
		BitrixMainLoader::includeModule('imopenlines')
		&& ($onlyOpenlinesOption === 'Y' || $skipOpenlinesOption !== 'Y')
	);

	$generalChatId = CIMChat::GetGeneralChatId();

	$ormParams = self::getOrmParams([
		'USER_ID' => $userId,
		'SHOW_OPENLINES' => $showOpenlines,
		'WITHOUT_COMMON_USERS' => true
	]);

	$lastSyncDateOption = $options['LAST_SYNC_DATE'] ?? null;
	if ($lastSyncDateOption)
	{
		$maxLimit = (new BitrixMainTypeDateTime())->add('-7 days');
		if ($maxLimit > $options['LAST_SYNC_DATE'])
		{
			$options['LAST_SYNC_DATE'] = $maxLimit;
		}
		$ormParams['filter']['>=DATE_UPDATE'] = $options['LAST_SYNC_DATE'];
	}
	else if ($options['ONLY_OPENLINES'] !== 'Y')
	{
		$ormParams['filter']['>=DATE_UPDATE'] = (new BitrixMainTypeDateTime())->add('-30 days');
	}

	$skipTypes = [];
	if ($onlyOpenlinesOption === 'Y')
	{
		$ormParams['filter'][] = [
			'=ITEM_TYPE' => IM_MESSAGE_OPEN_LINE
		];
	}
	else
	{
		if ($options['SKIP_OPENLINES'] === 'Y')
		{
			$skipTypes[] = IM_MESSAGE_OPEN_LINE;
		}
		if ($skipChat === 'Y')
		{
			$skipTypes[] = IM_MESSAGE_OPEN;
			$skipTypes[] = IM_MESSAGE_CHAT;
		}
		if ($skipDialog === 'Y')
		{
			$skipTypes[] = IM_MESSAGE_PRIVATE;
		}
		if (!empty($skipTypes))
		{
			$ormParams['filter'][] = [
				'!@ITEM_TYPE' => $skipTypes
			];
		}
	}

	if (!isset($options['LAST_SYNC_DATE']))
	{
		if (isset($options['OFFSET']))
		{
			$ormParams['offset'] = $options['OFFSET'];
		}
		if (isset($options['LIMIT']))
		{
			$ormParams['limit'] = $options['LIMIT'];
		}
		if (isset($options['ORDER']))
		{
			$ormParams['order'] = $options['ORDER'];
		}
	}

	$result = [];
	$orm = BitrixImModelRecentTable::getList($ormParams);
	$rows = $orm->fetchAll();
	$rows = self::prepareRows($rows, $userId);
	foreach ($rows as $row)
	{
		$isUser = $row['ITEM_TYPE'] == IM_MESSAGE_PRIVATE;
		$id = $isUser? (int)$row['ITEM_ID']: 'chat'.$row['ITEM_ID'];

		if ($isUser)
		{
			if (isset($result[$id]) && !$row['ITEM_MID'])
			{
				continue;
			}
		}
		else if (isset($result[$id]))
		{
			continue;
		}

		$item = self::formatRow($row, [
			'GENERAL_CHAT_ID' => $generalChatId,
			'GET_ORIGINAL_TEXT' => $options['GET_ORIGINAL_TEXT'] ?? null,
		]);
		if (!$item)
		{
			continue;
		}

		$result[$id] = $item;
	}
	$result = array_values($result);

	BitrixMainTypeCollection::sortByColumn(
		$result,
		['PINNED' => SORT_DESC, 'MESSAGE' => SORT_DESC, 'ID' => SORT_DESC],
		[
			'ID' => function($row) {
				return $row;
			},
			'MESSAGE' => function($row) {
				return $row['DATE'] instanceof BitrixMainTypeDateTime ? $row['DATE']->getTimeStamp() : 0;
			},
		]
	);

	if ($options['JSON'])
	{
		foreach ($result as $index => $item)
		{
			$result[$index] = self::jsonRow($item);
		}
	}

	return $result;
}