From 6f561f57a674ce6eb608a0afd01dbcc8c7a26258 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Tue, 4 Jan 2022 22:35:23 +0100 Subject: [PATCH] Notification: send an email when a notification is created --- .../Notification/Email/NotificationMailer.php | 110 ++++++++++++++++++ ...non_system_notification_content.fr.md.twig | 20 ++++ ...il_notification_comment_persist.fr.md.twig | 19 +++ .../config/services/notification.yaml | 19 +++ .../accompanying_course_designation.md.twig | 4 +- 5 files changed, 171 insertions(+), 1 deletion(-) create mode 100644 src/Bundle/ChillMainBundle/Notification/Email/NotificationMailer.php create mode 100644 src/Bundle/ChillMainBundle/Resources/views/Notification/email_non_system_notification_content.fr.md.twig create mode 100644 src/Bundle/ChillMainBundle/Resources/views/Notification/email_notification_comment_persist.fr.md.twig diff --git a/src/Bundle/ChillMainBundle/Notification/Email/NotificationMailer.php b/src/Bundle/ChillMainBundle/Notification/Email/NotificationMailer.php new file mode 100644 index 000000000..658764c26 --- /dev/null +++ b/src/Bundle/ChillMainBundle/Notification/Email/NotificationMailer.php @@ -0,0 +1,110 @@ +mailer = $mailer; + $this->logger = $logger; + $this->translator = $translator; + } + + public function postPersistComment(NotificationComment $comment, LifecycleEventArgs $eventArgs): void + { + foreach ( + array_merge( + $comment->getNotification()->getAddressees()->toArray(), + [$comment->getNotification()->getSender()] + ) as $dest + ) { + if (null === $dest->getEmail() || $comment->getCreatedBy() !== $dest) { + continue; + } + $email = new TemplatedEmail(); + $email + ->to($dest->getEmail()) + ->subject('Re: [Chill] ' . $comment->getNotification()->getTitle()) + ->textTemplate('@ChillMain/Notification/email_notification_comment_persist.fr.md.twig') + ->context([ + 'comment' => $comment, + 'dest' => $dest, + ]); + + try { + $this->mailer->send($email); + } catch (TransportExceptionInterface $e) { + $this->logger->warning('[NotificationMailer] could not send an email notification about comment', [ + 'to' => $dest->getEmail(), + 'error_message' => $e->getMessage(), + 'error_trace' => $e->getTraceAsString(), + ]); + } + } + } + + /** + * Send a email after a notification is persisted. + */ + public function postPersistNotification(Notification $notification, LifecycleEventArgs $eventArgs): void + { + foreach ($notification->getAddressees() as $addressee) { + if (null === $addressee->getEmail()) { + continue; + } + + if ($notification->isSystem()) { + $email = new Email(); + $email + ->text($notification->getMessage()) + ->subject('[Chill] ' . $notification->getTitle()); + } else { + $email = new TemplatedEmail(); + $email + ->textTemplate('@ChillMain/Notification/email_non_system_notification_content.fr.md.twig') + ->context([ + 'notification' => $notification, + 'dest' => $addressee, + ]); + } + + $email->to($addressee->getEmail()); + + try { + $this->mailer->send($email); + } catch (TransportExceptionInterface $e) { + $this->logger->warning('[NotificationMailer] could not send an email notification', [ + 'to' => $addressee->getEmail(), + 'error_message' => $e->getMessage(), + 'error_trace' => $e->getTraceAsString(), + ]); + } + } + } +} diff --git a/src/Bundle/ChillMainBundle/Resources/views/Notification/email_non_system_notification_content.fr.md.twig b/src/Bundle/ChillMainBundle/Resources/views/Notification/email_non_system_notification_content.fr.md.twig new file mode 100644 index 000000000..62e41860b --- /dev/null +++ b/src/Bundle/ChillMainBundle/Resources/views/Notification/email_non_system_notification_content.fr.md.twig @@ -0,0 +1,20 @@ +{{ dest.label }}, + +{{ notification.sender.label }} a créé une notification pour vous: + +> {{ notification.title }} +> +> +{%- for line in notification.message|split("\n") %} +> {{ line }} +{%- if not loop.last %} +> +{%- endif %} +{%- endfor %} + +Vous pouvez visualiser la notification et y répondre ici: + +{{ absolute_url(path('chill_main_notification_show', {'_locale': 'fr', 'id': notification.id }, false)) }} + +-- +Le logiciel Chill diff --git a/src/Bundle/ChillMainBundle/Resources/views/Notification/email_notification_comment_persist.fr.md.twig b/src/Bundle/ChillMainBundle/Resources/views/Notification/email_notification_comment_persist.fr.md.twig new file mode 100644 index 000000000..b1244da39 --- /dev/null +++ b/src/Bundle/ChillMainBundle/Resources/views/Notification/email_notification_comment_persist.fr.md.twig @@ -0,0 +1,19 @@ +{{ dest.label }}, + +{{ comment.createdBy.label }} a créé un commentaire sur la notification "{{ comment.notification.title }}". + +Commentaire: + +{% for line in comment.content|split("\n") %} +> {{ line }} +{%- if not loop.last %} +> +{%- endif %} +{%- endfor %} + +Vous pouvez visualiser la notification et y répondre ici: + +{{ absolute_url(path('chill_main_notification_show', {'_locale': 'fr', 'id': comment.notification.id }, false)) }} + +-- +Le logiciel Chill diff --git a/src/Bundle/ChillMainBundle/config/services/notification.yaml b/src/Bundle/ChillMainBundle/config/services/notification.yaml index f98561bd9..8bfb41ad7 100644 --- a/src/Bundle/ChillMainBundle/config/services/notification.yaml +++ b/src/Bundle/ChillMainBundle/config/services/notification.yaml @@ -49,3 +49,22 @@ services: # set the 'lazy' option to TRUE to only instantiate listeners when they are used lazy: true method: 'onEditNotificationComment' + + Chill\MainBundle\Notification\Email\NotificationMailer: + autowire: true + autoconfigure: true + tags: + - + name: 'doctrine.orm.entity_listener' + event: 'postPersist' + entity: 'Chill\MainBundle\Entity\Notification' + # set the 'lazy' option to TRUE to only instantiate listeners when they are used + lazy: true + method: 'postPersistNotification' + - + name: 'doctrine.orm.entity_listener' + event: 'postPersist' + entity: 'Chill\MainBundle\Entity\NotificationComment' + # set the 'lazy' option to TRUE to only instantiate listeners when they are used + lazy: true + method: 'postPersistComment' diff --git a/src/Bundle/ChillPersonBundle/Resources/views/Notification/accompanying_course_designation.md.twig b/src/Bundle/ChillPersonBundle/Resources/views/Notification/accompanying_course_designation.md.twig index 515f57b0c..ce28da8f8 100644 --- a/src/Bundle/ChillPersonBundle/Resources/views/Notification/accompanying_course_designation.md.twig +++ b/src/Bundle/ChillPersonBundle/Resources/views/Notification/accompanying_course_designation.md.twig @@ -1,6 +1,8 @@ {{ 'period_notification.You are designated to a new period.'|trans }} -{{ 'period_notification.See it online'|trans }}: {{ path('chill_person_accompanying_course_index', {'accompanying_period_id': accompanyingCourse.id}, true) }} +{{ 'period_notification.See it online'|trans }}: + +{{ absolute_url(path('chill_person_accompanying_course_index', {'accompanying_period_id': accompanyingCourse.id}, false)) }} {{ 'period_notification.Persons are'|trans }}: