• Модуль: im
  • Путь к файлу: ~/bitrix/modules/im/lib/dialog.php
  • Класс: BitrixImDialog
  • Вызов: Dialog::getDialogId
static function getDialogId(int $chatId, $userId = null): string
{
	$userId = BitrixImCommon::getUserId($userId);
	if (!$userId)
	{
		return false;
	}

	$chat = BitrixImChat::getById($chatId);

	if (!$chat)
	{
		return false;
	}

	if ($chat['MESSAGE_TYPE'] !== Chat::TYPE_PRIVATE)
	{
		return "chat{$chat['ID']}";
	}

	$query = ChatTable::query()
		->setSelect(['DIALOG_ID' => 'RELATION.USER_ID'])
		->registerRuntimeField(
			'RELATION',
			(
				new OneToMany('RELATION', RelationTable::class, 'CHAT')
			)->configureJoinType('inner')
		)
		->where('ID', (int)$chatId)
		->where('TYPE', Chat::TYPE_PRIVATE)
		->whereNot('RELATION.USER_ID', $userId)
		->setLimit(1)
	;

	$queryResult = $query->fetch();
	if (!$queryResult)
	{
		return false;
	}

	return $queryResult['DIALOG_ID'];
}