From 40a72d9fca746704af7038190dfa633b0d5ce5db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Wed, 6 Sep 2023 15:47:59 +0200 Subject: [PATCH] NotificationMailer: send to correct destinees --- .../unreleased/Fixed-20230824-152038.yaml | 6 ------ .../unreleased/Fixed-20230906-154856.yaml | 5 +++++ .../Notification/Email/NotificationMailer.php | 19 ++++++++++++------- 3 files changed, 17 insertions(+), 13 deletions(-) delete mode 100644 .changes/unreleased/Fixed-20230824-152038.yaml create mode 100644 .changes/unreleased/Fixed-20230906-154856.yaml diff --git a/.changes/unreleased/Fixed-20230824-152038.yaml b/.changes/unreleased/Fixed-20230824-152038.yaml deleted file mode 100644 index 656bd4752..000000000 --- a/.changes/unreleased/Fixed-20230824-152038.yaml +++ /dev/null @@ -1,6 +0,0 @@ -kind: Fixed -body: Fix who receive reply notification when adding comment (recipees must receive - it, and sender not). -time: 2023-08-24T15:20:38.472382872+02:00 -custom: - Issue: "137" diff --git a/.changes/unreleased/Fixed-20230906-154856.yaml b/.changes/unreleased/Fixed-20230906-154856.yaml new file mode 100644 index 000000000..73fb2dc48 --- /dev/null +++ b/.changes/unreleased/Fixed-20230906-154856.yaml @@ -0,0 +1,5 @@ +kind: Fixed +body: Do not send an email to creator twice when adding a comment to a notification +time: 2023-09-06T15:48:56.991246312+02:00 +custom: + Issue: "" diff --git a/src/Bundle/ChillMainBundle/Notification/Email/NotificationMailer.php b/src/Bundle/ChillMainBundle/Notification/Email/NotificationMailer.php index a652231ac..8facf00b9 100644 --- a/src/Bundle/ChillMainBundle/Notification/Email/NotificationMailer.php +++ b/src/Bundle/ChillMainBundle/Notification/Email/NotificationMailer.php @@ -40,13 +40,18 @@ class NotificationMailer public function postPersistComment(NotificationComment $comment, PostPersistEventArgs $eventArgs): void { - foreach ( - array_merge( - $comment->getNotification()->getAddressees()->toArray(), - [$comment->getNotification()->getSender()] - ) as $dest - ) { - if (null !== $dest->getEmail() && $comment->getCreatedBy() === $dest) { + $dests = [$comment->getNotification()->getSender(), ...$comment->getNotification()->getAddressees()->toArray()]; + + $uniqueDests = []; + foreach ($dests as $dest) { + // avoid duplication + if (in_array(spl_object_hash($dest), $uniqueDests, true)) { + continue; + } + $uniqueDests[] = spl_object_hash($dest); + + // do not send if the sender does not have any email, nor to the creator of the comment + if (null === $dest->getEmail() || $comment->getCreatedBy() === $dest) { continue; } $email = new TemplatedEmail();