From 0121e5e0e328f3e28a45eb0ccb13b805aa3afa7c Mon Sep 17 00:00:00 2001 From: partydragen Date: Sun, 4 May 2025 12:19:00 +0200 Subject: [PATCH 1/4] Revert "WIP" This reverts commit 7592d7b642f8ac3b00b6c1322f098fa1cd6308d2. --- .../DefaultRevamp/email/notification.html | 16 ----------- modules/Core/classes/Core/Notification.php | 17 +++-------- .../NotificationEmailTemplate.php | 22 --------------- modules/Core/classes/Tasks/MassMessage.php | 14 ++++++---- modules/Forum/pages/forum/view_topic.php | 28 +++++++++---------- 5 files changed, 25 insertions(+), 72 deletions(-) delete mode 100644 custom/templates/DefaultRevamp/email/notification.html delete mode 100644 modules/Core/classes/Email_Templates/NotificationEmailTemplate.php diff --git a/custom/templates/DefaultRevamp/email/notification.html b/custom/templates/DefaultRevamp/email/notification.html deleted file mode 100644 index 794ecd2c83..0000000000 --- a/custom/templates/DefaultRevamp/email/notification.html +++ /dev/null @@ -1,16 +0,0 @@ - - - - - [Sitename] • [Title] - - - -
-

[Greeting]

-

[Content]

-
- [Thanks]
[Sitename] -
- - \ No newline at end of file diff --git a/modules/Core/classes/Core/Notification.php b/modules/Core/classes/Core/Notification.php index 46fc9dcbf9..293f706bbe 100644 --- a/modules/Core/classes/Core/Notification.php +++ b/modules/Core/classes/Core/Notification.php @@ -34,20 +34,19 @@ class Notification { */ public function __construct( string $type, - string|LanguageKey $title, - string|LanguageKey $content, + AlertTemplate $alertTemplate, + EmailTemplate $emailTemplate, int|array $recipients, int $authorId, bool $bypassNotificationSettings = false, - ?string $link = null, ) { if (!in_array($type, array_column(self::getTypes(), 'key'))) { throw new NotificationTypeNotFoundException("Type $type not registered"); } $this->_type = $type; - $this->_alertTemplate = new AlertTemplate($title, $content, $link); - $this->_emailTemplate = new NotificationEmailTemplate($title, $content, $link); + $this->_alertTemplate = $alertTemplate; + $this->_emailTemplate = $emailTemplate; $this->_authorId = $authorId; $this->_bypassNotificationSettings = $bypassNotificationSettings; @@ -73,14 +72,6 @@ public function __construct( }, $recipients); } - public function setAlertTemplate(AlertTemplate $template): void { - $this->_alertTemplate = $template; - } - - public function setEmailTemplate(EmailTemplate $template): void { - $this->_emailTemplate = $template; - } - public function send(): void { /** @var array $recipient */ foreach ($this->_recipients as $recipient) { diff --git a/modules/Core/classes/Email_Templates/NotificationEmailTemplate.php b/modules/Core/classes/Email_Templates/NotificationEmailTemplate.php deleted file mode 100644 index 399e376a77..0000000000 --- a/modules/Core/classes/Email_Templates/NotificationEmailTemplate.php +++ /dev/null @@ -1,22 +0,0 @@ -subject = $subject; - - $this->addPlaceholder('[Title]', $subject); - $this->addPlaceholder('[Content]', $content); - $this->addPlaceholder('[Link]', $link); - - parent::__construct(); - } - - public function subject(): string - { - return $this->subject; - } -} \ No newline at end of file diff --git a/modules/Core/classes/Tasks/MassMessage.php b/modules/Core/classes/Tasks/MassMessage.php index 22c9fb4230..94b54d00fb 100644 --- a/modules/Core/classes/Tasks/MassMessage.php +++ b/modules/Core/classes/Tasks/MassMessage.php @@ -42,16 +42,18 @@ public function run(): string { $notification = new Notification( 'mass_message', - $title, - $content, + new AlertTemplate( + $title, + $content, + ), + new MassMessageEmailTemplate( + $title, + $content, + ), array_map(static fn ($r) => $r->id, $recipients->results()), $this->getUserId(), (bool) $this->getData()['bypass_notification_settings'], ); - $notification->setEmailTemplate(new MassMessageEmailTemplate( - $title, - $content, - )); $notification->send(); $this->setOutput(['userIds' => $whereVars, 'start' => $start, 'end' => $end, 'next_status' => $nextStatus]); diff --git a/modules/Forum/pages/forum/view_topic.php b/modules/Forum/pages/forum/view_topic.php index 7f6d5a80cb..0bfe740fcd 100644 --- a/modules/Forum/pages/forum/view_topic.php +++ b/modules/Forum/pages/forum/view_topic.php @@ -358,25 +358,23 @@ $notification = new Notification( 'forum_topic_reply', - $topic->topic_title, - $original_content, + new AlertTemplate( + new LanguageKey('forum', 'new_reply_in_topic', [ + 'author' => $user->data()->username, 'topic' => $topic->topic_title + ], ROOT_PATH . '/modules/Forum/language'), + null, + $post_link, + ), + new ForumTopicReplyEmailTemplate( + $user, + $topic->topic_title, + $original_content, + $post_link, + ), $users_following, $user->data()->id, ); - $notification->setAlertTemplate(new AlertTemplate( - new LanguageKey('forum', 'new_reply_in_topic', [ - 'author' => $user->data()->username, 'topic' => $topic->topic_title - ], ROOT_PATH . '/modules/Forum/language'), - null, - $post_link, - )); - $notification->setEmailTemplate(new ForumTopicReplyEmailTemplate( - $user, - $topic->topic_title, - $original_content, - $post_link, - )); $notification->send(); if (count($users_following)) { From db48515a8826aa3a386276d12a9037d0e5207a28 Mon Sep 17 00:00:00 2001 From: partydragen Date: Sun, 4 May 2025 12:19:57 +0200 Subject: [PATCH 2/4] Reapply "WIP" This reverts commit 0121e5e0e328f3e28a45eb0ccb13b805aa3afa7c. --- .../DefaultRevamp/email/notification.html | 16 +++++++++++ modules/Core/classes/Core/Notification.php | 17 ++++++++--- .../NotificationEmailTemplate.php | 22 +++++++++++++++ modules/Core/classes/Tasks/MassMessage.php | 14 ++++------ modules/Forum/pages/forum/view_topic.php | 28 ++++++++++--------- 5 files changed, 72 insertions(+), 25 deletions(-) create mode 100644 custom/templates/DefaultRevamp/email/notification.html create mode 100644 modules/Core/classes/Email_Templates/NotificationEmailTemplate.php diff --git a/custom/templates/DefaultRevamp/email/notification.html b/custom/templates/DefaultRevamp/email/notification.html new file mode 100644 index 0000000000..794ecd2c83 --- /dev/null +++ b/custom/templates/DefaultRevamp/email/notification.html @@ -0,0 +1,16 @@ + + + + + [Sitename] • [Title] + + + +
+

[Greeting]

+

[Content]

+
+ [Thanks]
[Sitename] +
+ + \ No newline at end of file diff --git a/modules/Core/classes/Core/Notification.php b/modules/Core/classes/Core/Notification.php index 293f706bbe..46fc9dcbf9 100644 --- a/modules/Core/classes/Core/Notification.php +++ b/modules/Core/classes/Core/Notification.php @@ -34,19 +34,20 @@ class Notification { */ public function __construct( string $type, - AlertTemplate $alertTemplate, - EmailTemplate $emailTemplate, + string|LanguageKey $title, + string|LanguageKey $content, int|array $recipients, int $authorId, bool $bypassNotificationSettings = false, + ?string $link = null, ) { if (!in_array($type, array_column(self::getTypes(), 'key'))) { throw new NotificationTypeNotFoundException("Type $type not registered"); } $this->_type = $type; - $this->_alertTemplate = $alertTemplate; - $this->_emailTemplate = $emailTemplate; + $this->_alertTemplate = new AlertTemplate($title, $content, $link); + $this->_emailTemplate = new NotificationEmailTemplate($title, $content, $link); $this->_authorId = $authorId; $this->_bypassNotificationSettings = $bypassNotificationSettings; @@ -72,6 +73,14 @@ public function __construct( }, $recipients); } + public function setAlertTemplate(AlertTemplate $template): void { + $this->_alertTemplate = $template; + } + + public function setEmailTemplate(EmailTemplate $template): void { + $this->_emailTemplate = $template; + } + public function send(): void { /** @var array $recipient */ foreach ($this->_recipients as $recipient) { diff --git a/modules/Core/classes/Email_Templates/NotificationEmailTemplate.php b/modules/Core/classes/Email_Templates/NotificationEmailTemplate.php new file mode 100644 index 0000000000..399e376a77 --- /dev/null +++ b/modules/Core/classes/Email_Templates/NotificationEmailTemplate.php @@ -0,0 +1,22 @@ +subject = $subject; + + $this->addPlaceholder('[Title]', $subject); + $this->addPlaceholder('[Content]', $content); + $this->addPlaceholder('[Link]', $link); + + parent::__construct(); + } + + public function subject(): string + { + return $this->subject; + } +} \ No newline at end of file diff --git a/modules/Core/classes/Tasks/MassMessage.php b/modules/Core/classes/Tasks/MassMessage.php index 94b54d00fb..22c9fb4230 100644 --- a/modules/Core/classes/Tasks/MassMessage.php +++ b/modules/Core/classes/Tasks/MassMessage.php @@ -42,18 +42,16 @@ public function run(): string { $notification = new Notification( 'mass_message', - new AlertTemplate( - $title, - $content, - ), - new MassMessageEmailTemplate( - $title, - $content, - ), + $title, + $content, array_map(static fn ($r) => $r->id, $recipients->results()), $this->getUserId(), (bool) $this->getData()['bypass_notification_settings'], ); + $notification->setEmailTemplate(new MassMessageEmailTemplate( + $title, + $content, + )); $notification->send(); $this->setOutput(['userIds' => $whereVars, 'start' => $start, 'end' => $end, 'next_status' => $nextStatus]); diff --git a/modules/Forum/pages/forum/view_topic.php b/modules/Forum/pages/forum/view_topic.php index 0bfe740fcd..7f6d5a80cb 100644 --- a/modules/Forum/pages/forum/view_topic.php +++ b/modules/Forum/pages/forum/view_topic.php @@ -358,23 +358,25 @@ $notification = new Notification( 'forum_topic_reply', - new AlertTemplate( - new LanguageKey('forum', 'new_reply_in_topic', [ - 'author' => $user->data()->username, 'topic' => $topic->topic_title - ], ROOT_PATH . '/modules/Forum/language'), - null, - $post_link, - ), - new ForumTopicReplyEmailTemplate( - $user, - $topic->topic_title, - $original_content, - $post_link, - ), + $topic->topic_title, + $original_content, $users_following, $user->data()->id, ); + $notification->setAlertTemplate(new AlertTemplate( + new LanguageKey('forum', 'new_reply_in_topic', [ + 'author' => $user->data()->username, 'topic' => $topic->topic_title + ], ROOT_PATH . '/modules/Forum/language'), + null, + $post_link, + )); + $notification->setEmailTemplate(new ForumTopicReplyEmailTemplate( + $user, + $topic->topic_title, + $original_content, + $post_link, + )); $notification->send(); if (count($users_following)) { From 3f98b194c5af5a96e7e1f9caf5b7685315766c1a Mon Sep 17 00:00:00 2001 From: partydragen Date: Sun, 4 May 2025 12:23:16 +0200 Subject: [PATCH 3/4] Revert "Merge branch 'tb/email-template-classes' into email-template-classes-example" This reverts commit 0bf776e7b14a15cb14b02e318292362e997b68f8, reversing changes made to db48515a8826aa3a386276d12a9037d0e5207a28. --- .../DefaultRevamp/email/notification.html | 16 +++++++++++ modules/Core/classes/Core/Notification.php | 17 ++++++++--- .../NotificationEmailTemplate.php | 22 +++++++++++++++ modules/Core/classes/Tasks/MassMessage.php | 14 ++++------ modules/Forum/pages/forum/view_topic.php | 28 ++++++++++--------- 5 files changed, 72 insertions(+), 25 deletions(-) create mode 100644 custom/templates/DefaultRevamp/email/notification.html create mode 100644 modules/Core/classes/Email_Templates/NotificationEmailTemplate.php diff --git a/custom/templates/DefaultRevamp/email/notification.html b/custom/templates/DefaultRevamp/email/notification.html new file mode 100644 index 0000000000..794ecd2c83 --- /dev/null +++ b/custom/templates/DefaultRevamp/email/notification.html @@ -0,0 +1,16 @@ + + + + + [Sitename] • [Title] + + + +
+

[Greeting]

+

[Content]

+
+ [Thanks]
[Sitename] +
+ + \ No newline at end of file diff --git a/modules/Core/classes/Core/Notification.php b/modules/Core/classes/Core/Notification.php index 293f706bbe..46fc9dcbf9 100644 --- a/modules/Core/classes/Core/Notification.php +++ b/modules/Core/classes/Core/Notification.php @@ -34,19 +34,20 @@ class Notification { */ public function __construct( string $type, - AlertTemplate $alertTemplate, - EmailTemplate $emailTemplate, + string|LanguageKey $title, + string|LanguageKey $content, int|array $recipients, int $authorId, bool $bypassNotificationSettings = false, + ?string $link = null, ) { if (!in_array($type, array_column(self::getTypes(), 'key'))) { throw new NotificationTypeNotFoundException("Type $type not registered"); } $this->_type = $type; - $this->_alertTemplate = $alertTemplate; - $this->_emailTemplate = $emailTemplate; + $this->_alertTemplate = new AlertTemplate($title, $content, $link); + $this->_emailTemplate = new NotificationEmailTemplate($title, $content, $link); $this->_authorId = $authorId; $this->_bypassNotificationSettings = $bypassNotificationSettings; @@ -72,6 +73,14 @@ public function __construct( }, $recipients); } + public function setAlertTemplate(AlertTemplate $template): void { + $this->_alertTemplate = $template; + } + + public function setEmailTemplate(EmailTemplate $template): void { + $this->_emailTemplate = $template; + } + public function send(): void { /** @var array $recipient */ foreach ($this->_recipients as $recipient) { diff --git a/modules/Core/classes/Email_Templates/NotificationEmailTemplate.php b/modules/Core/classes/Email_Templates/NotificationEmailTemplate.php new file mode 100644 index 0000000000..399e376a77 --- /dev/null +++ b/modules/Core/classes/Email_Templates/NotificationEmailTemplate.php @@ -0,0 +1,22 @@ +subject = $subject; + + $this->addPlaceholder('[Title]', $subject); + $this->addPlaceholder('[Content]', $content); + $this->addPlaceholder('[Link]', $link); + + parent::__construct(); + } + + public function subject(): string + { + return $this->subject; + } +} \ No newline at end of file diff --git a/modules/Core/classes/Tasks/MassMessage.php b/modules/Core/classes/Tasks/MassMessage.php index 94b54d00fb..22c9fb4230 100644 --- a/modules/Core/classes/Tasks/MassMessage.php +++ b/modules/Core/classes/Tasks/MassMessage.php @@ -42,18 +42,16 @@ public function run(): string { $notification = new Notification( 'mass_message', - new AlertTemplate( - $title, - $content, - ), - new MassMessageEmailTemplate( - $title, - $content, - ), + $title, + $content, array_map(static fn ($r) => $r->id, $recipients->results()), $this->getUserId(), (bool) $this->getData()['bypass_notification_settings'], ); + $notification->setEmailTemplate(new MassMessageEmailTemplate( + $title, + $content, + )); $notification->send(); $this->setOutput(['userIds' => $whereVars, 'start' => $start, 'end' => $end, 'next_status' => $nextStatus]); diff --git a/modules/Forum/pages/forum/view_topic.php b/modules/Forum/pages/forum/view_topic.php index 0bfe740fcd..7f6d5a80cb 100644 --- a/modules/Forum/pages/forum/view_topic.php +++ b/modules/Forum/pages/forum/view_topic.php @@ -358,23 +358,25 @@ $notification = new Notification( 'forum_topic_reply', - new AlertTemplate( - new LanguageKey('forum', 'new_reply_in_topic', [ - 'author' => $user->data()->username, 'topic' => $topic->topic_title - ], ROOT_PATH . '/modules/Forum/language'), - null, - $post_link, - ), - new ForumTopicReplyEmailTemplate( - $user, - $topic->topic_title, - $original_content, - $post_link, - ), + $topic->topic_title, + $original_content, $users_following, $user->data()->id, ); + $notification->setAlertTemplate(new AlertTemplate( + new LanguageKey('forum', 'new_reply_in_topic', [ + 'author' => $user->data()->username, 'topic' => $topic->topic_title + ], ROOT_PATH . '/modules/Forum/language'), + null, + $post_link, + )); + $notification->setEmailTemplate(new ForumTopicReplyEmailTemplate( + $user, + $topic->topic_title, + $original_content, + $post_link, + )); $notification->send(); if (count($users_following)) { From c33b58edf6bb58465b8469ca5115faab1a0e1955 Mon Sep 17 00:00:00 2001 From: partydragen Date: Sun, 4 May 2025 15:25:53 +0200 Subject: [PATCH 4/4] Add link to notification email --- .../DefaultRevamp/email/notification.html | 1 + modules/Core/classes/Core/Notification.php | 15 ++++++++------- .../Email_Templates/NotificationEmailTemplate.php | 4 +++- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/custom/templates/DefaultRevamp/email/notification.html b/custom/templates/DefaultRevamp/email/notification.html index 794ecd2c83..d79dcbc02e 100644 --- a/custom/templates/DefaultRevamp/email/notification.html +++ b/custom/templates/DefaultRevamp/email/notification.html @@ -9,6 +9,7 @@

[Greeting]

[Content]

+ [Link]
[Thanks]
[Sitename]
diff --git a/modules/Core/classes/Core/Notification.php b/modules/Core/classes/Core/Notification.php index 46fc9dcbf9..6e31fdb5eb 100644 --- a/modules/Core/classes/Core/Notification.php +++ b/modules/Core/classes/Core/Notification.php @@ -23,12 +23,13 @@ class Notification { /** * Instantiate a new notification * - * @param string $type Type of notification - * @param AlertTemplate $alertTemplate Alert template - * @param EmailTemplate $emailTemplate Email template - * @param int|int[] $recipients Notification recipient or recipients - array of user IDs - * @param int $authorId User ID that sent the notification - * @param bool $bypassNotificationSettings Whether to bypass the user's notification settings + * @param string $type Type of notification + * @param string|LanguageKey $title Title of notification + * @param string|LanguageKey $content Notification content. For alerts, if $alertUrl is set, this will ignored. If $alertUrl is not set, this will be the content of the alert. This will always be the content of the email. + * @param int|int[] $recipients Notification recipient or recipients - array of user IDs + * @param int $authorId User ID that sent the notification + * @param bool $bypassNotificationSettings Whether to bypass the user's notification settings + * @param ?string $link Optional URL to link to when clicking the alert * * @throws NotificationTypeNotFoundException */ @@ -46,7 +47,7 @@ public function __construct( } $this->_type = $type; - $this->_alertTemplate = new AlertTemplate($title, $content, $link); + $this->_alertTemplate = new AlertTemplate($title, $link ? null : $content, $link); $this->_emailTemplate = new NotificationEmailTemplate($title, $content, $link); $this->_authorId = $authorId; $this->_bypassNotificationSettings = $bypassNotificationSettings; diff --git a/modules/Core/classes/Email_Templates/NotificationEmailTemplate.php b/modules/Core/classes/Email_Templates/NotificationEmailTemplate.php index 399e376a77..828b82e719 100644 --- a/modules/Core/classes/Email_Templates/NotificationEmailTemplate.php +++ b/modules/Core/classes/Email_Templates/NotificationEmailTemplate.php @@ -10,7 +10,9 @@ public function __construct(string $subject, string $content, ?string $link) $this->addPlaceholder('[Title]', $subject); $this->addPlaceholder('[Content]', $content); - $this->addPlaceholder('[Link]', $link); + + // Register all notifications to nl2_alerts even if alert preference is off? Then all links can be sent to /user/alerts/?id={ID} then it will direct user + mark as read + $this->addPlaceholder('[Link]', $link ?? URL::getSelfURL()); parent::__construct(); }