Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add additional checks for banned instances + various code improvements along the way #1258

Merged
merged 19 commits into from
Dec 31, 2024
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/MessageHandler/ActivityPub/Inbox/ActivityHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function __construct(
private readonly SignatureValidator $signatureValidator,
private readonly SettingsManager $settingsManager,
private readonly MessageBusInterface $bus,
private readonly ActivityPubManager $manager,
private readonly ActivityPubManager $activityPubManager,
private readonly ApHttpClient $apHttpClient,
private readonly InstanceRepository $instanceRepository,
private readonly RemoteInstanceManager $remoteInstanceManager,
Expand Down Expand Up @@ -121,15 +121,15 @@ public function doWork(MessageInterface $message): void

try {
if (isset($payload['actor']) || isset($payload['attributedTo'])) {
if (!$this->verifyInstanceDomain($payload['actor'] ?? $this->manager->getSingleActorFromAttributedTo($payload['attributedTo']))) {
if (!$this->verifyInstanceDomain($payload['actor'] ?? $this->activityPubManager->getSingleActorFromAttributedTo($payload['attributedTo']))) {
return;
}
$user = $this->manager->findActorOrCreate($payload['actor'] ?? $this->manager->getSingleActorFromAttributedTo($payload['attributedTo']));
$user = $this->activityPubManager->findActorOrCreate($payload['actor'] ?? $this->activityPubManager->getSingleActorFromAttributedTo($payload['attributedTo']));
} else {
if (!$this->verifyInstanceDomain($payload['id'])) {
return;
}
$user = $this->manager->findActorOrCreate($payload['id']);
$user = $this->activityPubManager->findActorOrCreate($payload['id']);
}
} catch (\Exception $e) {
$this->logger->error('[ActivityHandler::doWork] Payload: '.json_encode($payload));
Expand Down Expand Up @@ -159,7 +159,7 @@ private function handle(?array $payload)
if ('Announce' === $payload['type']) {
// we check for an array here, because boosts are announces with an url (string) as the object
if (\is_array($payload['object'])) {
$actorObject = $this->manager->findActorOrCreate($payload['actor']);
$actorObject = $this->activityPubManager->findActorOrCreate($payload['actor']);
if ($actorObject instanceof Magazine && $actorObject->lastOriginUpdate < (new \DateTime())->modify('-3 hours')) {
if (isset($payload['object']['type']) && 'Create' === $payload['object']['type']) {
$actorObject->lastOriginUpdate = new \DateTime();
Expand All @@ -171,7 +171,7 @@ private function handle(?array $payload)
$payload = $payload['object'];
$actor = $payload['actor'] ?? $payload['attributedTo'] ?? null;
if ($actor) {
$user = $this->manager->findActorOrCreate($actor);
$user = $this->activityPubManager->findActorOrCreate($actor);
if ($user instanceof User && null === $user->apId) {
// don't do anything if we get an announce activity for something a local user did (unless it's a boost, see comment above)
$this->logger->warning('[ActivityHandler::handle] Ignoring this message because it announces an activity from a local user');
Expand Down
6 changes: 2 additions & 4 deletions src/MessageHandler/ActivityPub/Inbox/AnnounceHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use App\Service\VoteManager;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
use Symfony\Component\Messenger\MessageBusInterface;

Expand All @@ -25,8 +24,7 @@ public function __construct(
private readonly ActivityPubManager $activityPubManager,
private readonly EntityManagerInterface $entityManager,
private readonly MessageBusInterface $bus,
private readonly KernelInterface $kernel,
private readonly VoteManager $manager,
private readonly VoteManager $voteManager,
private readonly VoteHandleSubscriber $voteHandleSubscriber,
private readonly LoggerInterface $logger,
) {
Expand Down Expand Up @@ -59,7 +57,7 @@ public function doWork(MessageInterface $message): void
$actor = $this->activityPubManager->findActorOrCreate($message->payload['actor']);

if ($actor instanceof User) {
$this->manager->upvote($entity, $actor);
$this->voteManager->upvote($entity, $actor);
$this->voteHandleSubscriber->clearCache($entity);
} else {
$entity->lastActive = new \DateTime();
Expand Down
6 changes: 3 additions & 3 deletions src/MessageHandler/ActivityPub/Inbox/LikeHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function __construct(
private readonly ActivityPubManager $activityPubManager,
private readonly VoteManager $voteManager,
private readonly MessageBusInterface $bus,
private readonly FavouriteManager $manager,
private readonly FavouriteManager $favouriteManager,
private readonly LoggerInterface $logger,
) {
parent::__construct($this->entityManager, $this->kernel);
Expand Down Expand Up @@ -68,7 +68,7 @@ public function doWork(MessageInterface $message): void
$actor = $this->activityPubManager->findActorOrCreate($message->payload['actor']);
// Check if actor and entity aren't empty
if (!empty($actor) && !empty($entity)) {
$this->manager->toggle($actor, $entity, FavouriteManager::TYPE_LIKE);
$this->favouriteManager->toggle($actor, $entity, FavouriteManager::TYPE_LIKE);
}
} elseif ('Undo' === $message->payload['type']) {
if ('Like' === $message->payload['object']['type']) {
Expand All @@ -80,7 +80,7 @@ public function doWork(MessageInterface $message): void
$actor = $this->activityPubManager->findActorOrCreate($message->payload['actor']);
// Check if actor and entity aren't empty
if (!empty($actor) && !empty($entity)) {
$this->manager->toggle($actor, $entity, FavouriteManager::TYPE_UNLIKE);
$this->favouriteManager->toggle($actor, $entity, FavouriteManager::TYPE_UNLIKE);
$this->voteManager->removeVote($entity, $actor);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/MessageHandler/ActivityPub/Outbox/DeliverHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function __construct(
private readonly EntityManagerInterface $entityManager,
private readonly KernelInterface $kernel,
private readonly ApHttpClient $client,
private readonly ActivityPubManager $manager,
private readonly ActivityPubManager $activityPubManager,
private readonly SettingsManager $settingsManager,
private readonly LoggerInterface $logger,
private readonly InstanceRepository $instanceRepository,
Expand Down Expand Up @@ -122,7 +122,7 @@ public function doWork(MessageInterface $message): void
$url = $message->payload['actor'];
}
$this->logger->debug("Getting Actor for url: $url");
$actor = $this->manager->findActorOrCreate($url);
$actor = $this->activityPubManager->findActorOrCreate($url);

if (!$actor) {
$this->logger->debug('got no actor :(');
Expand Down
6 changes: 2 additions & 4 deletions src/MessageHandler/ActivityPub/UpdateActorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use App\Service\ActivityPubManager;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Lock\LockFactory;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;

Expand All @@ -22,8 +21,7 @@ class UpdateActorHandler extends MbinMessageHandler
{
public function __construct(
private readonly EntityManagerInterface $entityManager,
private readonly KernelInterface $kernel,
melroy89 marked this conversation as resolved.
Show resolved Hide resolved
private readonly ActivityPubManager $manager,
private readonly ActivityPubManager $activityPubManager,
private readonly ApHttpClient $apHttpClient,
private readonly LockFactory $lockFactory,
private readonly UserRepository $userRepository,
Expand Down Expand Up @@ -63,7 +61,7 @@ public function doWork(MessageInterface $message): void
$this->apHttpClient->invalidateActorObjectCache($actorUrl);
}
if ($message->force || $actor->apFetchedAt < (new \DateTime())->modify('-1 hour')) {
$this->manager->updateActor($actorUrl);
$this->activityPubManager->updateActor($actorUrl);
} else {
$this->logger->debug('not updating actor {url}: last updated is recent: {fetched}', [
'url' => $actorUrl,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function __construct(
private readonly EntityManagerInterface $entityManager,
private readonly KernelInterface $kernel,
private readonly EntryCommentRepository $repository,
private readonly NotificationManager $manager,
private readonly NotificationManager $notificationManager,
) {
parent::__construct($this->entityManager, $this->kernel);
}
Expand All @@ -42,6 +42,6 @@ public function doWork(MessageInterface $message): void
throw new UnrecoverableMessageHandlingException('Comment not found');
}

$this->manager->sendCreated($comment);
$this->notificationManager->sendCreated($comment);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function __construct(
private readonly EntityManagerInterface $entityManager,
private readonly KernelInterface $kernel,
private readonly EntryCommentRepository $repository,
private readonly NotificationManager $manager,
private readonly NotificationManager $notificationManager,
) {
parent::__construct($this->entityManager, $this->kernel);
}
Expand All @@ -42,6 +42,6 @@ public function doWork(MessageInterface $message): void
throw new UnrecoverableMessageHandlingException('Comment not found');
}

$this->manager->sendDeleted($comment);
$this->notificationManager->sendDeleted($comment);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function __construct(
private readonly EntityManagerInterface $entityManager,
private readonly KernelInterface $kernel,
private readonly EntryCommentRepository $repository,
private readonly NotificationManager $manager,
private readonly NotificationManager $notificationManager,
) {
parent::__construct($this->entityManager, $this->kernel);
}
Expand All @@ -42,6 +42,6 @@ public function doWork(MessageInterface $message): void
throw new UnrecoverableMessageHandlingException('Comment not found');
}

$this->manager->sendEdited($comment);
$this->notificationManager->sendEdited($comment);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function __construct(
private readonly EntityManagerInterface $entityManager,
private readonly KernelInterface $kernel,
private readonly EntryRepository $repository,
private readonly NotificationManager $manager,
private readonly NotificationManager $notificationManager,
) {
parent::__construct($this->entityManager, $this->kernel);
}
Expand All @@ -42,6 +42,6 @@ public function doWork(MessageInterface $message): void
throw new UnrecoverableMessageHandlingException('Entry not found');
}

$this->manager->sendCreated($entry);
$this->notificationManager->sendCreated($entry);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function __construct(
private readonly EntityManagerInterface $entityManager,
private readonly KernelInterface $kernel,
private readonly EntryRepository $repository,
private readonly NotificationManager $manager,
private readonly NotificationManager $notificationManager,
) {
parent::__construct($this->entityManager, $this->kernel);
}
Expand All @@ -42,6 +42,6 @@ public function doWork(MessageInterface $message): void
throw new UnrecoverableMessageHandlingException('Entry not found');
}

$this->manager->sendDeleted($entry);
$this->notificationManager->sendDeleted($entry);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function __construct(
private readonly EntityManagerInterface $entityManager,
private readonly KernelInterface $kernel,
private readonly EntryRepository $repository,
private readonly NotificationManager $manager,
private readonly NotificationManager $notificationManager,
) {
parent::__construct($this->entityManager, $this->kernel);
}
Expand All @@ -42,6 +42,6 @@ public function doWork(MessageInterface $message): void
throw new UnrecoverableMessageHandlingException('Entry not found');
}

$this->manager->sendEdited($entry);
$this->notificationManager->sendEdited($entry);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function __construct(
private readonly EntityManagerInterface $entityManager,
private readonly KernelInterface $kernel,
private readonly MagazineBanRepository $repository,
private readonly NotificationManager $manager,
private readonly NotificationManager $notificationManager,
) {
parent::__construct($this->entityManager, $this->kernel);
}
Expand All @@ -42,6 +42,6 @@ public function doWork(MessageInterface $message): void
throw new UnrecoverableMessageHandlingException('Ban not found');
}

$this->manager->sendMagazineBanNotification($ban);
$this->notificationManager->sendMagazineBanNotification($ban);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function __construct(
private readonly EntityManagerInterface $entityManager,
private readonly KernelInterface $kernel,
private readonly PostCommentRepository $repository,
private readonly NotificationManager $manager,
private readonly NotificationManager $notificationManager,
) {
parent::__construct($this->entityManager, $this->kernel);
}
Expand All @@ -42,6 +42,6 @@ public function doWork(MessageInterface $message): void
throw new UnrecoverableMessageHandlingException('Comment not found');
}

$this->manager->sendCreated($comment);
$this->notificationManager->sendCreated($comment);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function __construct(
private readonly EntityManagerInterface $entityManager,
private readonly KernelInterface $kernel,
private readonly PostCommentRepository $repository,
private readonly NotificationManager $manager,
private readonly NotificationManager $notificationManager,
) {
parent::__construct($this->entityManager, $this->kernel);
}
Expand All @@ -42,6 +42,6 @@ public function doWork(MessageInterface $message): void
throw new UnrecoverableMessageHandlingException('Comment not found');
}

$this->manager->sendDeleted($comment);
$this->notificationManager->sendDeleted($comment);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function __construct(
private readonly EntityManagerInterface $entityManager,
private readonly KernelInterface $kernel,
private readonly PostCommentRepository $repository,
private readonly NotificationManager $manager,
private readonly NotificationManager $notificationManager,
) {
parent::__construct($this->entityManager, $this->kernel);
}
Expand All @@ -42,6 +42,6 @@ public function doWork(MessageInterface $message): void
throw new UnrecoverableMessageHandlingException('Comment not found');
}

$this->manager->sendEdited($comment);
$this->notificationManager->sendEdited($comment);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function __construct(
private readonly EntityManagerInterface $entityManager,
private readonly KernelInterface $kernel,
private readonly PostRepository $repository,
private readonly NotificationManager $manager,
private readonly NotificationManager $notificationManager,
) {
parent::__construct($this->entityManager, $this->kernel);
}
Expand All @@ -42,6 +42,6 @@ public function doWork(MessageInterface $message): void
throw new UnrecoverableMessageHandlingException('Post not found');
}

$this->manager->sendCreated($post);
$this->notificationManager->sendCreated($post);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function __construct(
private readonly EntityManagerInterface $entityManager,
private readonly KernelInterface $kernel,
private readonly PostRepository $repository,
private readonly NotificationManager $manager,
private readonly NotificationManager $notificationManager,
) {
parent::__construct($this->entityManager, $this->kernel);
}
Expand All @@ -42,6 +42,6 @@ public function doWork(MessageInterface $message): void
throw new UnrecoverableMessageHandlingException('Post not found');
}

$this->manager->sendDeleted($post);
$this->notificationManager->sendDeleted($post);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function __construct(
private readonly EntityManagerInterface $entityManager,
private readonly KernelInterface $kernel,
private readonly PostRepository $repository,
private readonly NotificationManager $manager,
private readonly NotificationManager $notificationManager,
) {
parent::__construct($this->entityManager, $this->kernel);
}
Expand All @@ -42,6 +42,6 @@ public function doWork(MessageInterface $message): void
throw new UnrecoverableMessageHandlingException('Post not found');
}

$this->manager->sendEdited($post);
$this->notificationManager->sendEdited($post);
}
}
Loading
Loading