diff --git a/src/Repository/MagazineSubscriptionRepository.php b/src/Repository/MagazineSubscriptionRepository.php index b34436567..0e9064110 100644 --- a/src/Repository/MagazineSubscriptionRepository.php +++ b/src/Repository/MagazineSubscriptionRepository.php @@ -31,6 +31,9 @@ public function __construct(ManagerRegistry $registry) parent::__construct($registry, MagazineSubscription::class); } + /** + * @return MagazineSubscription[] + */ public function findNewEntrySubscribers(Entry $entry): array { return $this->createQueryBuilder('ms') @@ -46,7 +49,10 @@ public function findNewEntrySubscribers(Entry $entry): array ->getResult(); } - public function findNewPostSubscribers(Post $post) + /** + * @return MagazineSubscription[] + */ + public function findNewPostSubscribers(Post $post): array { return $this->createQueryBuilder('ms') ->addSelect('u') diff --git a/src/Service/Notification/EntryCommentNotificationManager.php b/src/Service/Notification/EntryCommentNotificationManager.php index 017e71321..55ca0b736 100644 --- a/src/Service/Notification/EntryCommentNotificationManager.php +++ b/src/Service/Notification/EntryCommentNotificationManager.php @@ -59,10 +59,10 @@ public function sendCreated(ContentInterface $subject): void if ($subject->user->isBanned || $subject->user->isDeleted || $subject->user->isTrashed() || $subject->user->isSoftDeleted()) { return; } + if (!$subject instanceof EntryComment) { + throw new \LogicException(); + } - /** - * @var EntryComment $subject - */ $users = $this->sendMentionedNotification($subject); $users = $this->sendUserReplyNotification($subject, $users); $this->sendMagazineSubscribersNotification($subject, $users); @@ -223,17 +223,17 @@ private function notifyMagazine(Notification $notification): void public function sendEdited(ContentInterface $subject): void { - /* - * @var EntryComment $subject - */ + if (!$subject instanceof EntryComment) { + throw new \LogicException(); + } $this->notifyMagazine(new EntryCommentEditedNotification($subject->user, $subject)); } public function sendDeleted(ContentInterface $subject): void { - /* - * @var EntryComment $subject - */ + if (!$subject instanceof EntryComment) { + throw new \LogicException(); + } $this->notifyMagazine($notification = new EntryCommentDeletedNotification($subject->user, $subject)); } diff --git a/src/Service/Notification/EntryNotificationManager.php b/src/Service/Notification/EntryNotificationManager.php index 722aa8adc..a7f97bb24 100644 --- a/src/Service/Notification/EntryNotificationManager.php +++ b/src/Service/Notification/EntryNotificationManager.php @@ -12,6 +12,7 @@ use App\Entity\EntryMentionedNotification; use App\Entity\Magazine; use App\Entity\Notification; +use App\Entity\User; use App\Event\NotificationCreatedEvent; use App\Factory\MagazineFactory; use App\Repository\MagazineLogRepository; @@ -22,6 +23,7 @@ use App\Service\ImageManager; use App\Service\MentionManager; use App\Service\SettingsManager; +use App\Service\UserManager; use App\Utils\IriGenerator; use Doctrine\ORM\EntityManagerInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface; @@ -47,17 +49,21 @@ public function __construct( private readonly EntityManagerInterface $entityManager, private readonly ImageManager $imageManager, private readonly GenerateHtmlClassService $classService, + private readonly UserManager $userManager, private readonly SettingsManager $settingsManager ) { } - // @todo check if author is on the block list public function sendCreated(ContentInterface $subject): void { if ($subject->user->isBanned || $subject->user->isDeleted || $subject->user->isTrashed() || $subject->user->isSoftDeleted()) { return; } + if (!$subject instanceof Entry) { + throw new \LogicException(); + } + /* * @var Entry $subject */ @@ -66,7 +72,7 @@ public function sendCreated(ContentInterface $subject): void // Notify mentioned $mentions = MentionManager::clearLocal($this->mentionManager->extract($subject->body)); foreach ($this->mentionManager->getUsersFromArray($mentions) as $user) { - if (!$user->apId) { + if (!$user->apId && !$user->isBlocked($subject->user)) { $notification = new EntryMentionedNotification($user, $subject); $this->entityManager->persist($notification); $this->eventDispatcher->dispatch(new NotificationCreatedEvent($notification)); @@ -74,6 +80,7 @@ public function sendCreated(ContentInterface $subject): void } // Notify subscribers + /** @var User[] $subscribers */ $subscribers = $this->merge( $this->getUsersToNotify($this->magazineRepository->findNewEntrySubscribers($subject)), [] // @todo user followers @@ -82,9 +89,11 @@ public function sendCreated(ContentInterface $subject): void $subscribers = array_filter($subscribers, fn ($s) => !\in_array($s->username, $mentions ?? [])); foreach ($subscribers as $subscriber) { - $notification = new EntryCreatedNotification($subscriber, $subject); - $this->entityManager->persist($notification); - $this->eventDispatcher->dispatch(new NotificationCreatedEvent($notification)); + if (!$subscriber->isBlocked($subject->user)) { + $notification = new EntryCreatedNotification($subscriber, $subject); + $this->entityManager->persist($notification); + $this->eventDispatcher->dispatch(new NotificationCreatedEvent($notification)); + } } $this->entityManager->flush(); @@ -144,17 +153,17 @@ private function getResponse(Notification $notification): string public function sendEdited(ContentInterface $subject): void { - /* - * @var Entry $subject - */ + if (!$subject instanceof Entry) { + throw new \LogicException(); + } $this->notifyMagazine(new EntryEditedNotification($subject->user, $subject)); } public function sendDeleted(ContentInterface $subject): void { - /* - * @var Entry $subject - */ + if (!$subject instanceof Entry) { + throw new \LogicException(); + } $this->notifyMagazine($notification = new EntryDeletedNotification($subject->user, $subject)); } diff --git a/src/Service/Notification/NotificationTrait.php b/src/Service/Notification/NotificationTrait.php index 4ccfebc46..33347f9bd 100644 --- a/src/Service/Notification/NotificationTrait.php +++ b/src/Service/Notification/NotificationTrait.php @@ -4,8 +4,16 @@ namespace App\Service\Notification; +use App\Entity\MagazineSubscription; +use App\Entity\User; + trait NotificationTrait { + /** + * @param MagazineSubscription[] $subscriptions + * + * @return User[] + */ private function getUsersToNotify(array $subscriptions): array { return array_map(fn ($sub) => $sub->user, $subscriptions); diff --git a/src/Service/Notification/PostCommentNotificationManager.php b/src/Service/Notification/PostCommentNotificationManager.php index 25b2674b8..d7a8241a1 100644 --- a/src/Service/Notification/PostCommentNotificationManager.php +++ b/src/Service/Notification/PostCommentNotificationManager.php @@ -53,16 +53,15 @@ public function __construct( ) { } - // @todo check if author is on the block list public function sendCreated(ContentInterface $subject): void { if ($subject->user->isBanned || $subject->user->isDeleted || $subject->user->isTrashed() || $subject->user->isSoftDeleted()) { return; } + if (!$subject instanceof PostComment) { + throw new \LogicException(); + } - /** - * @var PostComment $subject - */ $users = $this->sendMentionedNotification($subject); $users = $this->sendUserReplyNotification($subject, $users); $this->sendMagazineSubscribersNotification($subject, $users); @@ -222,17 +221,17 @@ private function notifyMagazine(Notification $notification): void public function sendEdited(ContentInterface $subject): void { - /* - * @var PostComment $subject - */ + if (!$subject instanceof PostComment) { + throw new \LogicException(); + } $this->notifyMagazine(new PostCommentEditedNotification($subject->user, $subject)); } public function sendDeleted(ContentInterface $subject): void { - /* - * @var PostComment $subject - */ + if (!$subject instanceof PostComment) { + throw new \LogicException(); + } $this->notifyMagazine($notification = new PostCommentDeletedNotification($subject->user, $subject)); } diff --git a/src/Service/Notification/PostNotificationManager.php b/src/Service/Notification/PostNotificationManager.php index 7661adebe..662e569c3 100644 --- a/src/Service/Notification/PostNotificationManager.php +++ b/src/Service/Notification/PostNotificationManager.php @@ -11,6 +11,7 @@ use App\Entity\PostDeletedNotification; use App\Entity\PostEditedNotification; use App\Entity\PostMentionedNotification; +use App\Entity\User; use App\Event\NotificationCreatedEvent; use App\Factory\MagazineFactory; use App\Repository\MagazineLogRepository; @@ -50,27 +51,29 @@ public function __construct( ) { } - // @todo check if author is on the block list public function sendCreated(ContentInterface $subject): void { if ($subject->user->isBanned || $subject->user->isDeleted || $subject->user->isTrashed() || $subject->user->isSoftDeleted()) { return; } + if (!$subject instanceof Post) { + throw new \LogicException(); + } - /* - * @var Post $subject - */ $this->notifyMagazine(new PostCreatedNotification($subject->user, $subject)); // Notify mentioned $mentions = MentionManager::clearLocal($this->mentionManager->extract($subject->body)); foreach ($this->mentionManager->getUsersFromArray($mentions) as $user) { - $notification = new PostMentionedNotification($user, $subject); - $this->entityManager->persist($notification); - $this->eventDispatcher->dispatch(new NotificationCreatedEvent($notification)); + if (!$user->isBlocked($subject->user)) { + $notification = new PostMentionedNotification($user, $subject); + $this->entityManager->persist($notification); + $this->eventDispatcher->dispatch(new NotificationCreatedEvent($notification)); + } } // Notify subscribers + /** @var User[] $subscribers */ $subscribers = $this->merge( $this->getUsersToNotify($this->magazineRepository->findNewPostSubscribers($subject)), [] // @todo user followers @@ -79,15 +82,17 @@ public function sendCreated(ContentInterface $subject): void $subscribers = array_filter($subscribers, fn ($s) => !\in_array($s->username, $mentions ?? [])); foreach ($subscribers as $subscriber) { - $notification2 = new PostCreatedNotification($subscriber, $subject); - $this->entityManager->persist($notification2); - $this->eventDispatcher->dispatch(new NotificationCreatedEvent($notification2)); + if (!$subscriber->isBlocked($subject->user)) { + $notification2 = new PostCreatedNotification($subscriber, $subject); + $this->entityManager->persist($notification2); + $this->eventDispatcher->dispatch(new NotificationCreatedEvent($notification2)); + } } $this->entityManager->flush(); } - private function notifyMagazine(Notification $notification) + private function notifyMagazine(Notification $notification): void { if (false === $this->settingsManager->get('KBIN_MERCURE_ENABLED')) { return; @@ -139,18 +144,18 @@ private function getResponse(Notification $notification): string public function sendEdited(ContentInterface $subject): void { - /* - * @var Post $subject - */ + if (!$subject instanceof Post) { + throw new \LogicException(); + } $this->notifyMagazine(new PostEditedNotification($subject->user, $subject)); } - public function sendDeleted(ContentInterface $post): void + public function sendDeleted(ContentInterface $subject): void { - /* - * @var Post $post - */ - $this->notifyMagazine($notification = new PostDeletedNotification($post->user, $post)); + if (!$subject instanceof Post) { + throw new \LogicException(); + } + $this->notifyMagazine($notification = new PostDeletedNotification($subject->user, $subject)); } public function purgeNotifications(Post $post): void