• Модуль: im
  • Путь к файлу: ~/bitrix/modules/im/lib/integration/ui/entityselector/chatprovider.php
  • Класс: BitrixImIntegrationUIEntitySelectorChatProvider
  • Вызов: ChatProvider::getChatIds
static function getChatIds(array $options, array $chatTypes): array
{
	if (!isset($options['searchableChatTypes']) || !is_array($options['searchableChatTypes']))
	{
		return [];
	}

	$currentUserId = User::getInstance()->getId();

	$query = ChatTable::query();
	$query->addSelect('ID');

	$searchQuery = $options['searchQuery'] ?? '';
	if (self::isValidSearchQuery($searchQuery))
	{
		$query->registerRuntimeField(
			'CHAT_INDEX',
			new Reference(
				'CHAT_INDEX',
				ChatIndexTable::class,
				Join::on('this.ID', 'ref.CHAT_ID'),
				['join_type' => Join::TYPE_INNER]
			)
		);
	}

	$filteredChatTypes = [];
	$relationJoinType = Join::TYPE_INNER;
	if (
		count($chatTypes) === 1
		&& in_array(Chat::TYPE_OPEN, $chatTypes, true)
		&& static::shouldSearchChatType(Chat::TYPE_OPEN, $options)
	)
	{
		$relationJoinType = Join::TYPE_LEFT;
		$filteredChatTypes[] = Chat::TYPE_OPEN;
	}
	$query->registerRuntimeField(
		'RELATION',
		new Reference(
			'RELATION',
			RelationTable::class,
			Join::on('this.ID', 'ref.CHAT_ID')->where('ref.USER_ID', $currentUserId),
			['join_type' => $relationJoinType]
		)
	);

	if (self::isValidSearchQuery($searchQuery))
	{
		$filter = Query::filter()->logic('and');
		static::addFilterBySearchQuery($filter, $searchQuery);
		$query->where($filter);
	}

	$chatTypesFilter = Query::filter()->logic('or');
	if (
		static::shouldSearchChatType(Chat::TYPE_GROUP, $options)
		&& in_array(Chat::TYPE_GROUP, $chatTypes, true)
	)
	{
		$groupChatFilter =
			Query::filter()
				->logic('and')
				->where('TYPE', '=', Chat::TYPE_GROUP)
				->where(Query::filter()
					->logic('or')
					->where('ENTITY_TYPE', '!=', 'SUPPORT24_QUESTION')
					->whereNull('ENTITY_TYPE')
				)
				->where('RELATION.USER_ID', '=', $currentUserId)
		;

		$chatTypesFilter->where($groupChatFilter);
		$filteredChatTypes[] = Chat::TYPE_GROUP;
	}


	if (
		static::shouldSearchChatType(Chat::TYPE_OPEN_LINE, $options)
		&& in_array(Chat::TYPE_OPEN_LINE, $chatTypes, true)
	)
	{
		$openLineFilter =
			Query::filter()
				->logic('and')
				->where('TYPE', '=', Chat::TYPE_OPEN_LINE)
				->where('RELATION.USER_ID', '=', $currentUserId)
		;

		$chatTypesFilter->where($openLineFilter);
		$filteredChatTypes[] = Chat::TYPE_OPEN_LINE;
	}

	if (
		static::shouldSearchChatType(Chat::TYPE_OPEN, $options)
		&& in_array(Chat::TYPE_OPEN, $chatTypes, true)
	)
	{
		$channelFilter =
			Query::filter()
				->logic('and')
				->where('TYPE', '=', Chat::TYPE_OPEN)
		;

		$chatTypesFilter->where($channelFilter);
		$filteredChatTypes[] = Chat::TYPE_OPEN;
	}
	if (empty($filteredChatTypes))
	{
		return [];
	}

	$query->where($chatTypesFilter);

	if (isset($options['chatIds']) && is_array($options['chatIds']))
	{
		$query->whereIn('ID', $options['chatIds']);
	}

	if (isset($options['order']) && is_array($options['order']))
	{
		$query->setOrder($options['order']);
	}
	else
	{
		$query->setOrder(['LAST_MESSAGE_ID' => 'DESC']);
	}

	if (isset($options['limit']))
	{
		$query->setLimit($options['limit']);
	}

	$chatIdList = [];
	foreach ($query->exec() as $chat)
	{
		$chatIdList[] = $chat['ID'];
	}

	return $chatIdList;
}