]> BookStack Code Mirror - hacks/commitdiff
Updated notify-favourite-pages hack
authorDan Brown <redacted>
Fri, 5 Jul 2024 14:09:56 +0000 (15:09 +0100)
committerDan Brown <redacted>
Fri, 5 Jul 2024 14:09:56 +0000 (15:09 +0100)
content/notify-favourited-pages/functions.php
content/notify-favourited-pages/index.md

index 955b9bf98c25ec09f57c079f80b3053a43932761..fbd8b45758da0d83ace4a587525d1f6a20e52ca8 100644 (file)
@@ -1,41 +1,34 @@
 <?php
 
-use BookStack\Actions\ActivityType;
-use BookStack\Auth\User;
+use BookStack\Activity\ActivityType;
+use BookStack\Activity\Notifications\Messages\BaseActivityNotification;
 use BookStack\Entities\Models\Page;
 use BookStack\Facades\Theme;
-use BookStack\Notifications\MailNotification;
 use BookStack\Theming\ThemeEvents;
+use BookStack\Users\Models\User;
 use Illuminate\Notifications\Messages\MailMessage;
 
-// This customization notifies page-updates via email to users that have marked
-// that updated page as a favourite.
+// This customization notifies page-updates via email to all users that have marked
+// that updated page as a favourite, excluding the user performing the update
 
 // This notification class represents the notification that'll be sent to users.
 // The text of the notification can be customized within the 'toMail' function.
-class PageUpdatedNotification extends MailNotification {
-
-    protected Page $page;
-    protected User $updater;
-
-    public function __construct(Page $page, User $updater)
-    {
-        $this->page = $page;
-        $this->updater = $updater;
-    }
-
-    public function toMail($notifiable)
+class PageUpdatedNotification extends BaseActivityNotification {
+    public function toMail($notifiable): MailMessage
     {
+        /** @var Page $page */
+        $page = $this->detail;
+        $updater = $this->user;
         return (new MailMessage())
             ->subject('BookStack page update notification')
-            ->line("The page \"{$this->page->name}\" has been updated by \"{$this->updater->name}\"")
-            ->action('View Page', $this->page->getUrl());
+            ->line("The page \"{$page->name}\" has been updated by \"{$updater->name}\"")
+            ->action('View Page', $page->getUrl());
     }
 }
 
 // This function does the work of sending notifications to the relevant users that have
 // marked the given page as a favourite.
-function notifyThoseThatHaveFavouritedPage(Page $page) {
+function notifyThoseThatHaveFavouritedPage(Page $page): void {
     // Find those we need to notify, and find the current updater of the page
     $userIds = $page->favourites()->pluck('user_id');
     $usersToNotify = User::query()->whereIn('id', $userIds)
@@ -50,7 +43,7 @@ function notifyThoseThatHaveFavouritedPage(Page $page) {
 }
 
 // Listen to page update events and kick-start our notification logic
-Theme::listen(ThemeEvents::ACTIVITY_LOGGED, function(string $type, $detail) {
+Theme::listen(ThemeEvents::ACTIVITY_LOGGED, function (string $type, $detail) {
     if ($type === ActivityType::PAGE_UPDATE && $detail instanceof Page) {
         notifyThoseThatHaveFavouritedPage($detail);
     }
index 4005db3c9770987740e2d061a1deb146102a4961..d622fea8040a35bd4b3ff71a430c70d02f8d17be 100644 (file)
@@ -2,11 +2,10 @@
 title = "Notify Updates for Favourited pages"
 author = "@ssddanbrown"
 date = 2022-12-01T20:00:00Z
-updated = 2022-12-01T20:00:00Z
-tested = "v22.11"
+updated = 2024-07-05T14:00:00Z
+tested = "v24.05.2"
 +++
 
-
 This hack sends out page update notification emails to all users that have marked that page as a favourite.
 
 #### Considerations
@@ -16,7 +15,7 @@ This hack sends out page update notification emails to all users that have marke
 
 #### Options
 
-- You can customize the email message, if desired, by editing the lines of text within the toMail part at around lines 30-32 of the `functions.php` code.
+- You can customize the email message, if desired, by editing the lines of text within the toMail part at around lines 23-25 of the `functions.php` code.
 
 #### Code