• Модуль: im
  • Путь к файлу: ~/bitrix/modules/im/lib/V2/Message/Delete/DeleteService.php
  • Класс: BitrixImV2MessageDeleteDeleteService
  • Вызов: DeleteService::canDelete
public function canDelete(): int
{
	if ($this->getContext()->getUser()->isSuperAdmin())
	{
		return self::DELETE_COMPLETE;
	}

	$userId = $this->getContext()->getUserId();

	// not chat access
	if (!$this->chat->hasAccess($userId))
	{
		return self::DELETE_NONE;
	}

	// get user role in this chat
	$removerRole = self::ROLE_USER;
	if ($userId === $this->chat->getAuthorId())
	{
		if ($this->chat->getType() !== Chat::IM_TYPE_PRIVATE)
		{
			$removerRole =  self::ROLE_OWNER;
		}
	}
	else
	{
		$relation = $this->chat->getSelfRelation();
		if ($relation && $relation->getManager())
		{
			$removerRole = self::ROLE_MANAGER;
		}
		elseif ($relation->getUser()->getExternalAuthId() === Bot::EXTERNAL_AUTH_ID)
		{
			return self::DELETE_COMPLETE;
		}
	}

	// determine the owner of the message
	$messageOwner = self::MESSAGE_OWN_OTHER;
	if ($messageAuthor = $this->message->getAuthor())
	{
		if ($messageAuthor->getId() === $userId)
		{
			$messageOwner = self::MESSAGE_OWN_SELF;
		}
		elseif($messageAuthor->getId() === $this->chat->getAuthorId())
		{
			$messageOwner = self::ROLE_OWNER;
		}
		else
		{
			$relations = $this->chat->getRelations(['USER_ID' => $messageAuthor->getId()]);
			if ($user = $relations->getByUserId($messageAuthor->getId(), $this->chat->getChatId()))
			{
				$messageOwner = self::ROLE_USER;
				if ($user->getManager())
				{
					$messageOwner = self::ROLE_MANAGER;
				}
			}
		}
	}

	if ($removerRole <= $messageOwner)
	{
		return self::DELETE_NONE;
	}

	if (
		$messageAuthor === self::ROLE_OWNER
		&& in_array($this->chat->getType(), [Chat::IM_TYPE_OPEN, Chat::IM_TYPE_CHANNEL], true)
	)
	{
		return self::DELETE_COMPLETE;
	}

	// message was read by someone other than the author
	if ($this->message->isViewedByOthers())
	{
		return self::DELETE_SOFT;
	}

	return self::DELETE_HARD;
}