From: Dan Brown Date: Mon, 20 Nov 2023 22:05:19 +0000 (+0000) Subject: Updated "notify-tagged-page-updates" hack for v23.10.4 X-Git-Url: http://source.bookstackapp.com/hacks/commitdiff_plain/c16f41f20c8e6ac2f38c3271a92a5409d0f89a09 Updated "notify-tagged-page-updates" hack for v23.10.4 HTTP Fns Hack update service pi_3OEeE4DUTTfhFsjz3bvEcZwE --- diff --git a/content/notify-tagged-page-updates/functions.php b/content/notify-tagged-page-updates/functions.php index bdf6ff9..6519d4c 100644 --- a/content/notify-tagged-page-updates/functions.php +++ b/content/notify-tagged-page-updates/functions.php @@ -1,13 +1,13 @@ page = $page; - $this->updater = $updater; - } + /** @var Page $page */ + $page = $this->detail; + $updater = $this->user; - public function toMail($notifiable) - { 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()); } } @@ -56,7 +50,7 @@ function notifyRequiredUsers(Page $page) { } // Get the roles named via the tag - $roleNames = array_filter(array_map(fn($str) => trim($str) , explode(',', $notifyTag->value))); + $roleNames = array_filter(array_map(fn ($str) => trim($str), explode(',', $notifyTag->value))); $roleIds = Role::query()->whereIn('display_name', $roleNames)->pluck('id'); if (count($roleNames) === 0 || $roleIds->isEmpty()) { return; @@ -64,11 +58,11 @@ function notifyRequiredUsers(Page $page) { // Get the users we want to notify, and the user that updated the page $usersToNotify = User::query() - ->whereExists(function($query) use ($roleIds) { - $query->select('user_id') - ->from('role_user') - ->whereColumn('users.id', '=', 'role_user.user_id') - ->whereIn('role_id', $roleIds); + ->whereExists(function ($query) use ($roleIds) { + $query->select('user_id') + ->from('role_user') + ->whereColumn('users.id', '=', 'role_user.user_id') + ->whereIn('role_id', $roleIds); }) ->where('id', '!=', $page->updated_by) ->get(); @@ -76,14 +70,14 @@ function notifyRequiredUsers(Page $page) { // Send a notification to each of the users we want to notify foreach ($usersToNotify as $user) { - $user->notify(new PageUpdatedNotification($page, $updater)); + $user->notify(new PageUpdateNotification($page, $updater)); usleep(100000); // Slight 0.1s delay to help rate limit abuse } } // 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) { notifyRequiredUsers($detail); } -}); \ No newline at end of file +}); diff --git a/content/notify-tagged-page-updates/index.md b/content/notify-tagged-page-updates/index.md index b9ccc1d..3ef027c 100644 --- a/content/notify-tagged-page-updates/index.md +++ b/content/notify-tagged-page-updates/index.md @@ -2,8 +2,8 @@ title = "Notify Page Updates for Tagged Books" author = "@ssddanbrown" date = 2022-12-01T20:00:00Z -updated = 2022-12-01T20:00:00Z -tested = "v22.11" +updated = 2023-11-20T22:00:00Z +tested = "v23.10.4" +++ @@ -14,10 +14,13 @@ For example, if a tag with name `Notify` and value `Admins, Viewers` is applied - The sending of emails may slow down page update actions, and these could be noisy if a user edits a page many times quickly. - You may run into email system rate-limits with the amount of emails being sent. +- By default, languages/translations are not handled in this example. +- This could be abused to send a mass of emails to user groups. +- You may prefer to use the in-platform notification system which was added in [BookStack v23.08](/blog/bookstack-release-v23-08/). #### Options -- You can customize the email message, if desired, by editing the lines of text within the toMail part at around lines 36-39 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 31-34 of the `functions.php` code. #### Code