• Модуль: im
  • Путь к файлу: ~/bitrix/modules/im/lib/V2/Chat.php
  • Класс: BitrixImV2Chat
  • Вызов: Chat::fillRole
static function fillRole(array $chats, ?int $userId = null): void
{
	//todo: replace to ChatCollection
	$chatIdsToFill = [];
	$userId ??= ImV2EntityUserUser::getCurrent()->getId();

	foreach ($chats as $chat)
	{
		if ($chat->getAuthorId() === $userId)
		{
			$chat->role = self::ROLE_OWNER;
		}
		else
		{
			$id = $chat->getId();
			$chatIdsToFill[$id] = $id;
		}
	}

	if (empty($chatIdsToFill))
	{
		return;
	}

	$result = RelationTable::query()
		->setSelect(['CHAT_ID', 'MANAGER'])
		->where('USER_ID', $userId)
		->whereIn('CHAT_ID', $chatIdsToFill)
		->fetchAll()
	;
	$isManager = [];

	foreach ($result as $row)
	{
		$isManager[(int)$row['CHAT_ID']] = $row['MANAGER'] === 'Y';
	}

	foreach ($chats as $chat)
	{
		if (!isset($chatIdsToFill[$chat->getId()]))
		{
			continue;
		}
		if (!isset($isManager[$chat->getId()]))
		{
			$chat->role = self::ROLE_GUEST;
		}
		elseif ($isManager[$chat->getId()])
		{
			$chat->role = self::ROLE_MANAGER;
		}
		else
		{
			$chat->role = self::ROLE_MEMBER;
		}
	}
}