• Модуль: im
  • Путь к файлу: ~/bitrix/modules/im/lib/recent.php
  • Класс: BitrixImRecent
  • Вызов: Recent::show
static function show($dialogId, $options = [], $userId = null)
{
	$userId = Common::getUserId($userId);
	if (!$userId)
	{
		return false;
	}

	$chatId = Dialog::getChatId($dialogId, $userId);
	if (Common::isChatId($dialogId))
	{
		$entityId = $chatId;
	}
	else
	{
		$entityId = (int)$dialogId;
	}

	$relation = BitrixImModelRelationTable::getList([
		'select' => [
			'ID',
			'TYPE' => 'CHAT.TYPE',
			'LAST_MESSAGE_ID' => 'CHAT.LAST_MESSAGE_ID',
			'LAST_MESSAGE_DATE' => 'MESSAGE.DATE_CREATE'
		],
		'filter' => [
			'=CHAT_ID' => $chatId,
			'=USER_ID' => $userId
		],
		'runtime' => [
			new BitrixMainEntityReferenceField(
				'MESSAGE',
				'BitrixImModelMessageTable',
				["=ref.ID" => "this.CHAT.LAST_MESSAGE_ID"],
				["join_type" => "LEFT"]
			),
		]
	])->fetch();

	if ($relation)
	{
		$relationId = $relation['ID'];
		$entityType = $relation['TYPE'];
		$messageId = $relation['LAST_MESSAGE_ID'];
		$messageDate = $relation['LAST_MESSAGE_DATE'];
	}
	else if (
		isset($options['CHAT_DATA']['TYPE'])
		&& isset($options['CHAT_DATA']['LAST_MESSAGE_ID'])
	)
	{
		$relationId = 0;
		$entityType = $options['CHAT_DATA']['TYPE'];
		$messageId = $options['CHAT_DATA']['LAST_MESSAGE_ID'];
		$messageDate = $options['CHAT_DATA']['LAST_MESSAGE_DATE'];
	}
	else
	{
		$chat = BitrixImModelChatTable::getList([
			'select' => [
				'TYPE',
				'LAST_MESSAGE_ID',
				'LAST_MESSAGE_DATE' => 'MESSAGE.DATE_CREATE'
			],
			'filter' => [
				'=ID' => $chatId,
			],
			'runtime' => [
				new BitrixMainEntityReferenceField(
					'MESSAGE',
					'BitrixImModelMessageTable',
					["=ref.ID" => "this.LAST_MESSAGE_ID"],
					["join_type" => "LEFT"]
				),
			]
		])->fetch();
		if (!$chat)
		{
			return false;
		}

		$relationId = 0;
		$entityType = $chat['TYPE'];
		$messageId = $chat['LAST_MESSAGE_ID'];
		$messageDate = $chat['LAST_MESSAGE_DATE'];
	}

	$sessionId = 0;
	if ($entityType == IM_MESSAGE_OPEN_LINE)
	{
		if (isset($options['SESSION_ID']))
		{
			$sessionId = (int)$options['SESSION_ID'];
		}
		else if (BitrixMainLoader::includeModule('imopenlines'))
		{
			$session = BitrixImOpenLinesModelSessionTable::getList([
				'select' => ['ID'],
				'filter' => ['=CHAT_ID' => $chatId],
				'order' => ['ID' => 'DESC'],
				'limit' => 1,
			])->fetch();
			if ($session)
			{
				$sessionId = $session['ID'];
			}
		}
	}

	CIMContactList::SetRecent($temp = [
		'ENTITY_TYPE' => $entityType,
		'ENTITY_ID' => $entityId,
		'MESSAGE_ID' => $messageId,
		'MESSAGE_DATE' => $messageDate,
		'CHAT_ID' => $chatId,
		'RELATION_ID' => $relationId,
		'SESSION_ID' => $sessionId,
		'USER_ID' => $userId,
	]);

	if (!BitrixMainLoader::includeModule("pull"))
	{
		return true;
	}

	$data = BitrixImRecent::getElement($entityType, $entityId, $userId, ['JSON' => true]);
	if ($data)
	{
		BitrixPullEvent::add($userId, [
			'module_id' => 'im',
			'command' => 'chatShow',
			'params' => $data,
			'extra' => BitrixImCommon::getPullExtra()
		]);
	}

	return true;
}