Skip to content

Commit

Permalink
Check whether the author is blocked before notifying
Browse files Browse the repository at this point in the history
When sending create or mentioned notifications check whether the author is blocked beforehand
  • Loading branch information
BentiGorlich committed Sep 9, 2024
1 parent 0f9604e commit 64145cc
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 50 deletions.
8 changes: 7 additions & 1 deletion src/Repository/MagazineSubscriptionRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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')
Expand Down
18 changes: 9 additions & 9 deletions src/Service/Notification/EntryCommentNotificationManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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));
}

Expand Down
31 changes: 20 additions & 11 deletions src/Service/Notification/EntryNotificationManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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
*/
Expand All @@ -66,14 +72,15 @@ 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));
}
}

// Notify subscribers
/** @var User[] $subscribers */
$subscribers = $this->merge(
$this->getUsersToNotify($this->magazineRepository->findNewEntrySubscribers($subject)),
[] // @todo user followers
Expand All @@ -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();
Expand Down Expand Up @@ -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));
}

Expand Down
8 changes: 8 additions & 0 deletions src/Service/Notification/NotificationTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
19 changes: 9 additions & 10 deletions src/Service/Notification/PostCommentNotificationManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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));
}

Expand Down
43 changes: 24 additions & 19 deletions src/Service/Notification/PostNotificationManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 64145cc

Please sign in to comment.