- Модуль: im
- Путь к файлу: ~/bitrix/modules/im/lib/V2/Entity/User/User.php
- Класс: BitrixImV2EntityUserUser
- Вызов: User::getChatWith
public function getChatWith(int $userId): ?PrivateChat
{
$chatId = false;
if ($userId === $this->getId())
{
$result = FavoriteChat::find(['TO_USER_ID' => $userId])->getResult();
if ($result && isset($result['ID']))
{
$chatId = (int)$result['ID'];
}
}
else
{
$result = RelationTable::query()
->setSelect(['CHAT_ID'])
->registerRuntimeField(
'SELF',
new Reference(
'SELF',
RelationTable::class,
Join::on('this.CHAT_ID', 'ref.CHAT_ID'),
['join_type' => Join::TYPE_INNER]
)
)->where('USER_ID', $this->getId())
->where('SELF.USER_ID', $userId)
->where('MESSAGE_TYPE', IM_MESSAGE_PRIVATE)
->setLimit(1)
->fetch()
;
if ($result && isset($result['CHAT_ID']))
{
$chatId = (int)$result['CHAT_ID'];
}
}
if ($chatId !== false)
{
$chat = PrivateChat::getInstance($chatId);
if ($chat instanceof PrivateChat)
{
return $chat;
}
return null;
}
try
{
$createResult = (new PrivateChat())->add(['FROM_USER_ID' => $this->getId(), 'TO_USER_ID' => $userId]);
}
catch (SystemException $exception)
{
return null;
}
if (!$createResult->isSuccess())
{
return null;
}
return $createResult->getResult()['CHAT'];
}