From 0868a7f23140a724ab733c004affb48323ce3cd2 Mon Sep 17 00:00:00 2001 From: Melroy van den Berg Date: Thu, 28 Nov 2024 22:57:13 +0100 Subject: [PATCH 01/10] Add additional checks for banned instances --- src/Service/ActivityPub/ApHttpClient.php | 24 ++++++++++++++++++++++-- src/Service/ActivityPubManager.php | 20 ++++++++++++++++++-- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/src/Service/ActivityPub/ApHttpClient.php b/src/Service/ActivityPub/ApHttpClient.php index a86ea66da..0e9322775 100644 --- a/src/Service/ActivityPub/ApHttpClient.php +++ b/src/Service/ActivityPub/ApHttpClient.php @@ -205,6 +205,11 @@ private function getActorCacheKey(string $apProfileId): string return 'ap_'.hash('sha256', $apProfileId); } + private function getCollectinCacheKey(string $apAddress): string + { + return 'ap_collection'.hash('sha256', $apAddress); + } + /** * Retrieve AP actor object (could be a user or magazine). * @@ -280,22 +285,37 @@ private function getActorObjectImpl(string $apProfileId): ?string return $response->getContent(false); } + /** + * Remove actor object from cache. + * + * @param string $apProfileId AP profile ID to remove from cache + */ public function invalidateActorObjectCache(string $apProfileId): void { $this->cache->delete($this->getActorCacheKey($apProfileId)); } + /** + * Remove collection object from cache. + * + * @param string $apAddress AP address to remove from cache + */ public function invalidateCollectionObjectCache(string $apAddress): void { - $this->cache->delete('ap_collection'.hash('sha256', $apAddress)); + $this->cache->delete($this->getCollectinCacheKey($apAddress)); } /** + * Retrieve AP collection object. First look in cache, then try to retrieve from AP server. + * And finally, save the response to cache. + * + * @return Response body + * * @throws InvalidArgumentException */ public function getCollectionObject(string $apAddress): ?array { - $key = 'ap_collection'.hash('sha256', $apAddress); + $key = $this->getCollectinCacheKey($apAddress); if ($this->cache->hasItem($key)) { /** @var CacheItem $item */ $item = $this->cache->getItem($key); diff --git a/src/Service/ActivityPubManager.php b/src/Service/ActivityPubManager.php index 397e58656..87b3851d1 100644 --- a/src/Service/ActivityPubManager.php +++ b/src/Service/ActivityPubManager.php @@ -126,7 +126,6 @@ public function createCcFromBody(string $body): array * * @return User|Magazine|null or Magazine or null on error * - * @throws InstanceBannedException * @throws InvalidApPostException * @throws InvalidArgumentException * @throws InvalidWebfingerException @@ -161,6 +160,11 @@ public function findActorOrCreate(?string $actorUrlOrHandle): User|Magazine|null $actorUrl = $this->webfinger($actorUrl)->getProfileId(); } + // Check if the instance is banned + if ($this->settingsManager->isBannedInstance($actorUrl)) { + return null; + } + if (\in_array( parse_url($actorUrl, PHP_URL_HOST), [$this->settingsManager->get('KBIN_DOMAIN'), 'localhost', '127.0.0.1'] @@ -279,7 +283,7 @@ public function webfinger(string $id): WebFinger return $this->webFingerFactory->get($handle); } - public function buildHandle(string $id): string + private function buildHandle(string $id): string { $port = !\is_null(parse_url($id, PHP_URL_PORT)) ? ':'.parse_url($id, PHP_URL_PORT) @@ -337,6 +341,10 @@ public function updateUser(string $actorUrl): ?User return $user; } + if ($this->settingsManager->isBannedInstance($actorUrl)) { + return null; + } + $actor = $this->apHttpClient->getActorObject($actorUrl); if (!$actor || !\is_array($actor)) { return null; @@ -511,6 +519,10 @@ public function updateMagazine(string $actorUrl): ?Magazine return $magazine; } + if ($this->settingsManager->isBannedInstance($actorUrl)) { + return null; + } + $actor = $this->apHttpClient->getActorObject($actorUrl); // Check if actor isn't empty (not set/null/empty array/etc.) @@ -868,6 +880,10 @@ public function handleExternalVideos(array $attachment): ?array */ public function updateActor(string $actorUrl): Magazine|User|null { + if ($this->settingsManager->isBannedInstance($actorUrl)) { + return null; + } + if ($this->userRepository->findOneBy(['apProfileId' => $actorUrl])) { return $this->updateUser($actorUrl); } elseif ($this->magazineRepository->findOneBy(['apProfileId' => $actorUrl])) { From 918af278721df09a8f6db859fb04ecc3e0b318fd Mon Sep 17 00:00:00 2001 From: Melroy van den Berg Date: Tue, 17 Dec 2024 23:25:42 +0100 Subject: [PATCH 02/10] Remove redundant checks and make method private --- src/Service/ActivityPub/ApHttpClient.php | 4 ++-- src/Service/ActivityPubManager.php | 12 ++---------- 2 files changed, 4 insertions(+), 12 deletions(-) diff --git a/src/Service/ActivityPub/ApHttpClient.php b/src/Service/ActivityPub/ApHttpClient.php index 0e9322775..0da770b9b 100644 --- a/src/Service/ActivityPub/ApHttpClient.php +++ b/src/Service/ActivityPub/ApHttpClient.php @@ -385,7 +385,7 @@ private function logRequestException(?ResponseInterface $response, string $reque // Often 400, 404 errors just return the full HTML page, so we don't want to log the full content of them // We truncate the content to 200 characters max. - $this->logger->error('[ApHttpClient::updateUser] {type} failed: {address}, ex: {e}: {msg}. Truncated content: {content}', [ + $this->logger->error('[ApHttpClient::logRequestException] {type} failed: {address}, ex: {e}: {msg}. Truncated content: {content}', [ 'type' => $requestType, 'address' => $requestUrl, 'e' => \get_class($e), @@ -394,7 +394,7 @@ private function logRequestException(?ResponseInterface $response, string $reque ]); // And only log the full content in debug log mode if ($content) { - $this->logger->debug('[ApHttpClient::updateUser] Full response body content: {content}', [ + $this->logger->debug('[ApHttpClient::logRequestException] Full response body content: {content}', [ 'content' => $content, ]); } diff --git a/src/Service/ActivityPubManager.php b/src/Service/ActivityPubManager.php index 87b3851d1..1a17d93ba 100644 --- a/src/Service/ActivityPubManager.php +++ b/src/Service/ActivityPubManager.php @@ -332,7 +332,7 @@ private function createUser(string $actorUrl): ?User * * @return ?User or null on error (e.g. actor not found) */ - public function updateUser(string $actorUrl): ?User + private function updateUser(string $actorUrl): ?User { $this->logger->info('[ActivityPubManager::updateUser] Updating user {name}', ['name' => $actorUrl]); $user = $this->userRepository->findOneBy(['apProfileId' => $actorUrl]); @@ -341,10 +341,6 @@ public function updateUser(string $actorUrl): ?User return $user; } - if ($this->settingsManager->isBannedInstance($actorUrl)) { - return null; - } - $actor = $this->apHttpClient->getActorObject($actorUrl); if (!$actor || !\is_array($actor)) { return null; @@ -510,7 +506,7 @@ private function createMagazine(string $actorUrl): ?Magazine * * @return ?Magazine or null on error */ - public function updateMagazine(string $actorUrl): ?Magazine + private function updateMagazine(string $actorUrl): ?Magazine { $this->logger->info('[ActivityPubManager::updateMagazine] Updating magazine "{magName}"', ['magName' => $actorUrl]); $magazine = $this->magazineRepository->findOneBy(['apProfileId' => $actorUrl]); @@ -519,10 +515,6 @@ public function updateMagazine(string $actorUrl): ?Magazine return $magazine; } - if ($this->settingsManager->isBannedInstance($actorUrl)) { - return null; - } - $actor = $this->apHttpClient->getActorObject($actorUrl); // Check if actor isn't empty (not set/null/empty array/etc.) From 4f4802ead6fe031d2a67379e1a0f8212261aeb27 Mon Sep 17 00:00:00 2001 From: Melroy van den Berg Date: Tue, 17 Dec 2024 23:32:54 +0100 Subject: [PATCH 03/10] Push isBannedInstance check down, after the single findOneBy call --- src/Service/ActivityPubManager.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/Service/ActivityPubManager.php b/src/Service/ActivityPubManager.php index 1a17d93ba..7dac868ea 100644 --- a/src/Service/ActivityPubManager.php +++ b/src/Service/ActivityPubManager.php @@ -160,11 +160,6 @@ public function findActorOrCreate(?string $actorUrlOrHandle): User|Magazine|null $actorUrl = $this->webfinger($actorUrl)->getProfileId(); } - // Check if the instance is banned - if ($this->settingsManager->isBannedInstance($actorUrl)) { - return null; - } - if (\in_array( parse_url($actorUrl, PHP_URL_HOST), [$this->settingsManager->get('KBIN_DOMAIN'), 'localhost', '127.0.0.1'] @@ -177,6 +172,11 @@ public function findActorOrCreate(?string $actorUrlOrHandle): User|Magazine|null return $this->userRepository->findOneBy(['username' => $name]); } + // Check if the instance is banned + if ($this->settingsManager->isBannedInstance($actorUrl)) { + return null; + } + $user = $this->userRepository->findOneBy(['apProfileId' => $actorUrl]); if ($user instanceof User) { $this->logger->debug('[ActivityPubManager::findActorOrCreate] Found remote user for url: "{url}" in db', ['url' => $actorUrl]); @@ -186,6 +186,7 @@ public function findActorOrCreate(?string $actorUrlOrHandle): User|Magazine|null return $user; } + $magazine = $this->magazineRepository->findOneBy(['apProfileId' => $actorUrl]); if ($magazine instanceof Magazine) { $this->logger->debug('[ActivityPubManager::findActorOrCreate] Found remote user for url: "{url}" in db', ['url' => $actorUrl]); From c9d3aafd332eefa1b3550d83c7bf7d60253613cb Mon Sep 17 00:00:00 2001 From: Melroy van den Berg Date: Tue, 17 Dec 2024 23:45:03 +0100 Subject: [PATCH 04/10] Use activityPubManager variable consistently --- .../ActivityPub/Inbox/ActivityHandler.php | 12 ++++++------ .../ActivityPub/Outbox/DeliverHandler.php | 4 ++-- .../ActivityPub/UpdateActorHandler.php | 4 ++-- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/MessageHandler/ActivityPub/Inbox/ActivityHandler.php b/src/MessageHandler/ActivityPub/Inbox/ActivityHandler.php index 8e7ab4a4c..14bbaf93b 100644 --- a/src/MessageHandler/ActivityPub/Inbox/ActivityHandler.php +++ b/src/MessageHandler/ActivityPub/Inbox/ActivityHandler.php @@ -42,7 +42,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, @@ -119,15 +119,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)); @@ -157,7 +157,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(); @@ -169,7 +169,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'); diff --git a/src/MessageHandler/ActivityPub/Outbox/DeliverHandler.php b/src/MessageHandler/ActivityPub/Outbox/DeliverHandler.php index 360cf635d..ee6ef5c4c 100644 --- a/src/MessageHandler/ActivityPub/Outbox/DeliverHandler.php +++ b/src/MessageHandler/ActivityPub/Outbox/DeliverHandler.php @@ -31,7 +31,7 @@ class DeliverHandler extends MbinMessageHandler public function __construct( private readonly EntityManagerInterface $entityManager, private readonly ApHttpClient $client, - private readonly ActivityPubManager $manager, + private readonly ActivityPubManager $activityPubManager, private readonly SettingsManager $settingsManager, private readonly LoggerInterface $logger, private readonly InstanceRepository $instanceRepository, @@ -120,7 +120,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 :('); diff --git a/src/MessageHandler/ActivityPub/UpdateActorHandler.php b/src/MessageHandler/ActivityPub/UpdateActorHandler.php index 7f9aa73de..c6bd668b7 100644 --- a/src/MessageHandler/ActivityPub/UpdateActorHandler.php +++ b/src/MessageHandler/ActivityPub/UpdateActorHandler.php @@ -21,7 +21,7 @@ class UpdateActorHandler extends MbinMessageHandler { public function __construct( private readonly EntityManagerInterface $entityManager, - private readonly ActivityPubManager $manager, + private readonly ActivityPubManager $activityPubManager, private readonly ApHttpClient $apHttpClient, private readonly LockFactory $lockFactory, private readonly UserRepository $userRepository, @@ -61,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, From 9b02e5ca6abdb8a8838afa0e559efe3813e84fba Mon Sep 17 00:00:00 2001 From: Melroy van den Berg Date: Tue, 17 Dec 2024 23:55:06 +0100 Subject: [PATCH 05/10] Also rename other generic manager to the correct variable name --- src/MessageHandler/ActivityPub/Inbox/AnnounceHandler.php | 4 ++-- src/MessageHandler/ActivityPub/Inbox/LikeHandler.php | 6 +++--- .../SentEntryCommentCreatedNotificationHandler.php | 4 ++-- .../SentEntryCommentDeletedNotificationHandler.php | 4 ++-- .../SentEntryCommentEditedNotificationHandler.php | 4 ++-- .../Notification/SentEntryCreatedNotificationHandler.php | 4 ++-- .../Notification/SentEntryDeletedNotificationHandler.php | 4 ++-- .../Notification/SentEntryEditedNotificationHandler.php | 4 ++-- .../Notification/SentMagazineBanNotificationHandler.php | 4 ++-- .../SentPostCommentCreatedNotificationHandler.php | 4 ++-- .../SentPostCommentDeletedNotificationHandler.php | 4 ++-- .../SentPostCommentEditedNotificationHandler.php | 4 ++-- .../Notification/SentPostCreatedNotificationHandler.php | 4 ++-- .../Notification/SentPostDeletedNotificationHandler.php | 4 ++-- .../Notification/SentPostEditedNotificationHandler.php | 4 ++-- src/Service/ActivityPub/Wrapper/MentionsWrapper.php | 4 ++-- 16 files changed, 33 insertions(+), 33 deletions(-) diff --git a/src/MessageHandler/ActivityPub/Inbox/AnnounceHandler.php b/src/MessageHandler/ActivityPub/Inbox/AnnounceHandler.php index 21e2b1110..053495eab 100644 --- a/src/MessageHandler/ActivityPub/Inbox/AnnounceHandler.php +++ b/src/MessageHandler/ActivityPub/Inbox/AnnounceHandler.php @@ -24,7 +24,7 @@ public function __construct( private readonly ActivityPubManager $activityPubManager, private readonly EntityManagerInterface $entityManager, private readonly MessageBusInterface $bus, - private readonly VoteManager $manager, + private readonly VoteManager $voteManager, private readonly VoteHandleSubscriber $voteHandleSubscriber, private readonly LoggerInterface $logger, ) { @@ -57,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(); diff --git a/src/MessageHandler/ActivityPub/Inbox/LikeHandler.php b/src/MessageHandler/ActivityPub/Inbox/LikeHandler.php index a2f92f044..840e2456c 100644 --- a/src/MessageHandler/ActivityPub/Inbox/LikeHandler.php +++ b/src/MessageHandler/ActivityPub/Inbox/LikeHandler.php @@ -29,7 +29,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); @@ -66,7 +66,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']) { @@ -78,7 +78,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); } } diff --git a/src/MessageHandler/Notification/SentEntryCommentCreatedNotificationHandler.php b/src/MessageHandler/Notification/SentEntryCommentCreatedNotificationHandler.php index 52b87dcdf..8d0d6dd2d 100644 --- a/src/MessageHandler/Notification/SentEntryCommentCreatedNotificationHandler.php +++ b/src/MessageHandler/Notification/SentEntryCommentCreatedNotificationHandler.php @@ -19,7 +19,7 @@ class SentEntryCommentCreatedNotificationHandler extends MbinMessageHandler public function __construct( private readonly EntityManagerInterface $entityManager, private readonly EntryCommentRepository $repository, - private readonly NotificationManager $manager + private readonly NotificationManager $notificationManager ) { parent::__construct($this->entityManager); } @@ -40,6 +40,6 @@ public function doWork(MessageInterface $message): void throw new UnrecoverableMessageHandlingException('Comment not found'); } - $this->manager->sendCreated($comment); + $this->notificationManager->sendCreated($comment); } } diff --git a/src/MessageHandler/Notification/SentEntryCommentDeletedNotificationHandler.php b/src/MessageHandler/Notification/SentEntryCommentDeletedNotificationHandler.php index b4eb80e36..c9d6487f4 100644 --- a/src/MessageHandler/Notification/SentEntryCommentDeletedNotificationHandler.php +++ b/src/MessageHandler/Notification/SentEntryCommentDeletedNotificationHandler.php @@ -19,7 +19,7 @@ class SentEntryCommentDeletedNotificationHandler extends MbinMessageHandler public function __construct( private readonly EntityManagerInterface $entityManager, private readonly EntryCommentRepository $repository, - private readonly NotificationManager $manager + private readonly NotificationManager $notificationManager ) { parent::__construct($this->entityManager); } @@ -40,6 +40,6 @@ public function doWork(MessageInterface $message): void throw new UnrecoverableMessageHandlingException('Comment not found'); } - $this->manager->sendDeleted($comment); + $this->notificationManager->sendDeleted($comment); } } diff --git a/src/MessageHandler/Notification/SentEntryCommentEditedNotificationHandler.php b/src/MessageHandler/Notification/SentEntryCommentEditedNotificationHandler.php index fc62b9ac5..e82539712 100644 --- a/src/MessageHandler/Notification/SentEntryCommentEditedNotificationHandler.php +++ b/src/MessageHandler/Notification/SentEntryCommentEditedNotificationHandler.php @@ -19,7 +19,7 @@ class SentEntryCommentEditedNotificationHandler extends MbinMessageHandler public function __construct( private readonly EntityManagerInterface $entityManager, private readonly EntryCommentRepository $repository, - private readonly NotificationManager $manager + private readonly NotificationManager $notificationManager ) { parent::__construct($this->entityManager); } @@ -40,6 +40,6 @@ public function doWork(MessageInterface $message): void throw new UnrecoverableMessageHandlingException('Comment not found'); } - $this->manager->sendEdited($comment); + $this->notificationManager->sendEdited($comment); } } diff --git a/src/MessageHandler/Notification/SentEntryCreatedNotificationHandler.php b/src/MessageHandler/Notification/SentEntryCreatedNotificationHandler.php index 647d4ac8d..97a4a8299 100644 --- a/src/MessageHandler/Notification/SentEntryCreatedNotificationHandler.php +++ b/src/MessageHandler/Notification/SentEntryCreatedNotificationHandler.php @@ -19,7 +19,7 @@ class SentEntryCreatedNotificationHandler extends MbinMessageHandler public function __construct( private readonly EntityManagerInterface $entityManager, private readonly EntryRepository $repository, - private readonly NotificationManager $manager + private readonly NotificationManager $notificationManager ) { parent::__construct($this->entityManager); } @@ -40,6 +40,6 @@ public function doWork(MessageInterface $message): void throw new UnrecoverableMessageHandlingException('Entry not found'); } - $this->manager->sendCreated($entry); + $this->notificationManager->sendCreated($entry); } } diff --git a/src/MessageHandler/Notification/SentEntryDeletedNotificationHandler.php b/src/MessageHandler/Notification/SentEntryDeletedNotificationHandler.php index 6a386f7b2..86945b58c 100644 --- a/src/MessageHandler/Notification/SentEntryDeletedNotificationHandler.php +++ b/src/MessageHandler/Notification/SentEntryDeletedNotificationHandler.php @@ -19,7 +19,7 @@ class SentEntryDeletedNotificationHandler extends MbinMessageHandler public function __construct( private readonly EntityManagerInterface $entityManager, private readonly EntryRepository $repository, - private readonly NotificationManager $manager + private readonly NotificationManager $notificationManager ) { parent::__construct($this->entityManager); } @@ -40,6 +40,6 @@ public function doWork(MessageInterface $message): void throw new UnrecoverableMessageHandlingException('Entry not found'); } - $this->manager->sendDeleted($entry); + $this->notificationManager->sendDeleted($entry); } } diff --git a/src/MessageHandler/Notification/SentEntryEditedNotificationHandler.php b/src/MessageHandler/Notification/SentEntryEditedNotificationHandler.php index 14d02c954..444858b5f 100644 --- a/src/MessageHandler/Notification/SentEntryEditedNotificationHandler.php +++ b/src/MessageHandler/Notification/SentEntryEditedNotificationHandler.php @@ -19,7 +19,7 @@ class SentEntryEditedNotificationHandler extends MbinMessageHandler public function __construct( private readonly EntityManagerInterface $entityManager, private readonly EntryRepository $repository, - private readonly NotificationManager $manager + private readonly NotificationManager $notificationManager ) { parent::__construct($this->entityManager); } @@ -40,6 +40,6 @@ public function doWork(MessageInterface $message): void throw new UnrecoverableMessageHandlingException('Entry not found'); } - $this->manager->sendEdited($entry); + $this->notificationManager->sendEdited($entry); } } diff --git a/src/MessageHandler/Notification/SentMagazineBanNotificationHandler.php b/src/MessageHandler/Notification/SentMagazineBanNotificationHandler.php index 817afc6ec..5cfac15c0 100644 --- a/src/MessageHandler/Notification/SentMagazineBanNotificationHandler.php +++ b/src/MessageHandler/Notification/SentMagazineBanNotificationHandler.php @@ -19,7 +19,7 @@ class SentMagazineBanNotificationHandler extends MbinMessageHandler public function __construct( private readonly EntityManagerInterface $entityManager, private readonly MagazineBanRepository $repository, - private readonly NotificationManager $manager + private readonly NotificationManager $notificationManager ) { parent::__construct($this->entityManager); } @@ -40,6 +40,6 @@ public function doWork(MessageInterface $message): void throw new UnrecoverableMessageHandlingException('Ban not found'); } - $this->manager->sendMagazineBanNotification($ban); + $this->notificationManager->sendMagazineBanNotification($ban); } } diff --git a/src/MessageHandler/Notification/SentPostCommentCreatedNotificationHandler.php b/src/MessageHandler/Notification/SentPostCommentCreatedNotificationHandler.php index 990f3740f..1af07e037 100644 --- a/src/MessageHandler/Notification/SentPostCommentCreatedNotificationHandler.php +++ b/src/MessageHandler/Notification/SentPostCommentCreatedNotificationHandler.php @@ -19,7 +19,7 @@ class SentPostCommentCreatedNotificationHandler extends MbinMessageHandler public function __construct( private readonly EntityManagerInterface $entityManager, private readonly PostCommentRepository $repository, - private readonly NotificationManager $manager + private readonly NotificationManager $notificationManager ) { parent::__construct($this->entityManager); } @@ -40,6 +40,6 @@ public function doWork(MessageInterface $message): void throw new UnrecoverableMessageHandlingException('Comment not found'); } - $this->manager->sendCreated($comment); + $this->notificationManager->sendCreated($comment); } } diff --git a/src/MessageHandler/Notification/SentPostCommentDeletedNotificationHandler.php b/src/MessageHandler/Notification/SentPostCommentDeletedNotificationHandler.php index e6fa8d958..19d4957fe 100644 --- a/src/MessageHandler/Notification/SentPostCommentDeletedNotificationHandler.php +++ b/src/MessageHandler/Notification/SentPostCommentDeletedNotificationHandler.php @@ -19,7 +19,7 @@ class SentPostCommentDeletedNotificationHandler extends MbinMessageHandler public function __construct( private readonly EntityManagerInterface $entityManager, private readonly PostCommentRepository $repository, - private readonly NotificationManager $manager + private readonly NotificationManager $notificationManager ) { parent::__construct($this->entityManager); } @@ -40,6 +40,6 @@ public function doWork(MessageInterface $message): void throw new UnrecoverableMessageHandlingException('Comment not found'); } - $this->manager->sendDeleted($comment); + $this->notificationManager->sendDeleted($comment); } } diff --git a/src/MessageHandler/Notification/SentPostCommentEditedNotificationHandler.php b/src/MessageHandler/Notification/SentPostCommentEditedNotificationHandler.php index 48fded6f0..98bd3d232 100644 --- a/src/MessageHandler/Notification/SentPostCommentEditedNotificationHandler.php +++ b/src/MessageHandler/Notification/SentPostCommentEditedNotificationHandler.php @@ -19,7 +19,7 @@ class SentPostCommentEditedNotificationHandler extends MbinMessageHandler public function __construct( private readonly EntityManagerInterface $entityManager, private readonly PostCommentRepository $repository, - private readonly NotificationManager $manager + private readonly NotificationManager $notificationManager ) { parent::__construct($this->entityManager); } @@ -40,6 +40,6 @@ public function doWork(MessageInterface $message): void throw new UnrecoverableMessageHandlingException('Comment not found'); } - $this->manager->sendEdited($comment); + $this->notificationManager->sendEdited($comment); } } diff --git a/src/MessageHandler/Notification/SentPostCreatedNotificationHandler.php b/src/MessageHandler/Notification/SentPostCreatedNotificationHandler.php index 80900b344..d0791b83f 100644 --- a/src/MessageHandler/Notification/SentPostCreatedNotificationHandler.php +++ b/src/MessageHandler/Notification/SentPostCreatedNotificationHandler.php @@ -19,7 +19,7 @@ class SentPostCreatedNotificationHandler extends MbinMessageHandler public function __construct( private readonly EntityManagerInterface $entityManager, private readonly PostRepository $repository, - private readonly NotificationManager $manager + private readonly NotificationManager $notificationManager ) { parent::__construct($this->entityManager); } @@ -40,6 +40,6 @@ public function doWork(MessageInterface $message): void throw new UnrecoverableMessageHandlingException('Post not found'); } - $this->manager->sendCreated($post); + $this->notificationManager->sendCreated($post); } } diff --git a/src/MessageHandler/Notification/SentPostDeletedNotificationHandler.php b/src/MessageHandler/Notification/SentPostDeletedNotificationHandler.php index 781d8740c..6343efa84 100644 --- a/src/MessageHandler/Notification/SentPostDeletedNotificationHandler.php +++ b/src/MessageHandler/Notification/SentPostDeletedNotificationHandler.php @@ -19,7 +19,7 @@ class SentPostDeletedNotificationHandler extends MbinMessageHandler public function __construct( private readonly EntityManagerInterface $entityManager, private readonly PostRepository $repository, - private readonly NotificationManager $manager + private readonly NotificationManager $notificationManager ) { parent::__construct($this->entityManager); } @@ -40,6 +40,6 @@ public function doWork(MessageInterface $message): void throw new UnrecoverableMessageHandlingException('Post not found'); } - $this->manager->sendDeleted($post); + $this->notificationManager->sendDeleted($post); } } diff --git a/src/MessageHandler/Notification/SentPostEditedNotificationHandler.php b/src/MessageHandler/Notification/SentPostEditedNotificationHandler.php index 0712cf894..19c1f3b5d 100644 --- a/src/MessageHandler/Notification/SentPostEditedNotificationHandler.php +++ b/src/MessageHandler/Notification/SentPostEditedNotificationHandler.php @@ -19,7 +19,7 @@ class SentPostEditedNotificationHandler extends MbinMessageHandler public function __construct( private readonly EntityManagerInterface $entityManager, private readonly PostRepository $repository, - private readonly NotificationManager $manager + private readonly NotificationManager $notificationManager ) { parent::__construct($this->entityManager); } @@ -40,6 +40,6 @@ public function doWork(MessageInterface $message): void throw new UnrecoverableMessageHandlingException('Post not found'); } - $this->manager->sendEdited($post); + $this->notificationManager->sendEdited($post); } } diff --git a/src/Service/ActivityPub/Wrapper/MentionsWrapper.php b/src/Service/ActivityPub/Wrapper/MentionsWrapper.php index 2e1f92ae7..455fe4d22 100644 --- a/src/Service/ActivityPub/Wrapper/MentionsWrapper.php +++ b/src/Service/ActivityPub/Wrapper/MentionsWrapper.php @@ -12,7 +12,7 @@ class MentionsWrapper { public function __construct( - private readonly ActivityPubManager $manager, + private readonly ActivityPubManager $activityPubManager, private readonly UrlGeneratorInterface $urlGenerator, private readonly MentionManager $mentionManager, private readonly SettingsManager $settingsManager @@ -26,7 +26,7 @@ public function build(?array $mentions, ?string $body = null): array $results = []; foreach ($mentions as $index => $mention) { try { - $actor = $this->manager->findActorOrCreate($mention); + $actor = $this->activityPubManager->findActorOrCreate($mention); if (!$actor) { continue; From fb726ecddf39514e612825e7300eb18b75b799da Mon Sep 17 00:00:00 2001 From: Melroy van den Berg Date: Fri, 27 Dec 2024 15:40:43 +0100 Subject: [PATCH 06/10] Remove double $ --- src/MessageHandler/ActivityPub/Inbox/LikeHandler.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/MessageHandler/ActivityPub/Inbox/LikeHandler.php b/src/MessageHandler/ActivityPub/Inbox/LikeHandler.php index dc018f0c5..6cff548df 100644 --- a/src/MessageHandler/ActivityPub/Inbox/LikeHandler.php +++ b/src/MessageHandler/ActivityPub/Inbox/LikeHandler.php @@ -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->favouriteManager->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']) { @@ -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->favouriteManager->toggle($actor, $entity, FavouriteManager::TYPE_UNLIKE); + $this->favouriteManager->toggle($actor, $entity, FavouriteManager::TYPE_UNLIKE); $this->voteManager->removeVote($entity, $actor); } } From abadbcc2009f3c2ee147379677a9ba9411895314 Mon Sep 17 00:00:00 2001 From: Melroy van den Berg Date: Sat, 28 Dec 2024 22:34:10 +0100 Subject: [PATCH 07/10] Fix cs fixer --- src/MessageHandler/ActivityPub/Inbox/AnnounceHandler.php | 1 - src/MessageHandler/ActivityPub/UpdateActorHandler.php | 1 - 2 files changed, 2 deletions(-) diff --git a/src/MessageHandler/ActivityPub/Inbox/AnnounceHandler.php b/src/MessageHandler/ActivityPub/Inbox/AnnounceHandler.php index 1e0ebba52..774835d53 100644 --- a/src/MessageHandler/ActivityPub/Inbox/AnnounceHandler.php +++ b/src/MessageHandler/ActivityPub/Inbox/AnnounceHandler.php @@ -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; diff --git a/src/MessageHandler/ActivityPub/UpdateActorHandler.php b/src/MessageHandler/ActivityPub/UpdateActorHandler.php index d00feca13..dc13ac5e8 100644 --- a/src/MessageHandler/ActivityPub/UpdateActorHandler.php +++ b/src/MessageHandler/ActivityPub/UpdateActorHandler.php @@ -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; From cdfd29af138314686261bdb70861d08bd9f1f359 Mon Sep 17 00:00:00 2001 From: Melroy van den Berg Date: Mon, 30 Dec 2024 21:02:02 +0100 Subject: [PATCH 08/10] Fix small ApHTTPClient issues --- src/Service/ActivityPub/ApHttpClient.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Service/ActivityPub/ApHttpClient.php b/src/Service/ActivityPub/ApHttpClient.php index dc8c63a14..7ed90fa4b 100644 --- a/src/Service/ActivityPub/ApHttpClient.php +++ b/src/Service/ActivityPub/ApHttpClient.php @@ -205,7 +205,7 @@ private function getActorCacheKey(string $apProfileId): string return 'ap_'.hash('sha256', $apProfileId); } - private function getCollectinCacheKey(string $apAddress): string + private function getCollectionCacheKey(string $apAddress): string { return 'ap_collection'.hash('sha256', $apAddress); } @@ -302,20 +302,20 @@ public function invalidateActorObjectCache(string $apProfileId): void */ public function invalidateCollectionObjectCache(string $apAddress): void { - $this->cache->delete($this->getCollectinCacheKey($apAddress)); + $this->cache->delete($this->getCollectionCacheKey($apAddress)); } /** * Retrieve AP collection object. First look in cache, then try to retrieve from AP server. * And finally, save the response to cache. * - * @return Response body + * @return JSON Response body (as PHP Object) * * @throws InvalidArgumentException */ public function getCollectionObject(string $apAddress): ?array { - $key = $this->getCollectinCacheKey($apAddress); + $key = $this->getCollectionCacheKey($apAddress); if ($this->cache->hasItem($key)) { /** @var CacheItem $item */ $item = $this->cache->getItem($key); From 9399f9e6083bbf2c5a29f36e223c487f075ac6c1 Mon Sep 17 00:00:00 2001 From: Melroy van den Berg Date: Tue, 31 Dec 2024 01:17:56 +0100 Subject: [PATCH 09/10] Fix @return type --- src/Service/ActivityPub/ApHttpClient.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Service/ActivityPub/ApHttpClient.php b/src/Service/ActivityPub/ApHttpClient.php index 7ed90fa4b..4e097c1c0 100644 --- a/src/Service/ActivityPub/ApHttpClient.php +++ b/src/Service/ActivityPub/ApHttpClient.php @@ -65,7 +65,7 @@ public function __construct( * * @param bool $decoded (optional) * - * @return array|string|null Response body + * @return array|string|null JSON Response body (as PHP Object) */ public function getActivityObject(string $url, bool $decoded = true): array|string|null { @@ -156,7 +156,7 @@ public function getInboxUrl(string $apProfileId): string * * @param string $url the URL of the user/magazine to get the webfinger object for * - * @return array|null the webfinger object + * @return array|null The webfinger object (as PHP Object) * * @throws InvalidWebfingerException|InvalidArgumentException */ @@ -213,7 +213,7 @@ private function getCollectionCacheKey(string $apAddress): string /** * Retrieve AP actor object (could be a user or magazine). * - * @return array|null key/value array of actor response body + * @return array|null key/value array of actor response body (as PHP Object) * * @throws InvalidApPostException|InvalidArgumentException */ @@ -309,7 +309,7 @@ public function invalidateCollectionObjectCache(string $apAddress): void * Retrieve AP collection object. First look in cache, then try to retrieve from AP server. * And finally, save the response to cache. * - * @return JSON Response body (as PHP Object) + * @return array|null JSON Response body (as PHP Object) * * @throws InvalidArgumentException */ From 6bc1d9b802c5ba378196062124cc8c86c242b8e8 Mon Sep 17 00:00:00 2001 From: Melroy van den Berg Date: Tue, 31 Dec 2024 15:18:57 +0100 Subject: [PATCH 10/10] Restore kernel interfaces --- src/MessageHandler/ActivityPub/Inbox/AnnounceHandler.php | 2 ++ src/MessageHandler/ActivityPub/UpdateActorHandler.php | 2 ++ 2 files changed, 4 insertions(+) diff --git a/src/MessageHandler/ActivityPub/Inbox/AnnounceHandler.php b/src/MessageHandler/ActivityPub/Inbox/AnnounceHandler.php index 774835d53..e13a1d695 100644 --- a/src/MessageHandler/ActivityPub/Inbox/AnnounceHandler.php +++ b/src/MessageHandler/ActivityPub/Inbox/AnnounceHandler.php @@ -14,6 +14,7 @@ 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; @@ -24,6 +25,7 @@ public function __construct( private readonly ActivityPubManager $activityPubManager, private readonly EntityManagerInterface $entityManager, private readonly MessageBusInterface $bus, + private readonly KernelInterface $kernel, private readonly VoteManager $voteManager, private readonly VoteHandleSubscriber $voteHandleSubscriber, private readonly LoggerInterface $logger, diff --git a/src/MessageHandler/ActivityPub/UpdateActorHandler.php b/src/MessageHandler/ActivityPub/UpdateActorHandler.php index dc13ac5e8..365121d98 100644 --- a/src/MessageHandler/ActivityPub/UpdateActorHandler.php +++ b/src/MessageHandler/ActivityPub/UpdateActorHandler.php @@ -13,6 +13,7 @@ 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; @@ -21,6 +22,7 @@ class UpdateActorHandler extends MbinMessageHandler { public function __construct( private readonly EntityManagerInterface $entityManager, + private readonly KernelInterface $kernel, private readonly ActivityPubManager $activityPubManager, private readonly ApHttpClient $apHttpClient, private readonly LockFactory $lockFactory,