Hacks & Customizations
These hacks are unsupported customizations meant as unofficial workarounds.
They can cause instability, introduce issues and may conflict with future updates. Apply at your own risk!

Notify Updates for Favourited pages

  • Author: @ssddanbrown
  • Created: 1st Dec 2022
  • Updated: 1st Dec 2022
  • Last Tested On: v22.11

This hack sends out page update notification emails to all users that have marked that page as a favourite.

Considerations

  • 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.

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.

Code

functions.php
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php

use BookStack\Actions\ActivityType;
use BookStack\Auth\User;
use BookStack\Entities\Models\Page;
use BookStack\Facades\Theme;
use BookStack\Notifications\MailNotification;
use BookStack\Theming\ThemeEvents;
use Illuminate\Notifications\Messages\MailMessage;

// This customization notifies page-updates via email to users that have marked
// that updated page as a favourite.

// 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)
    {
        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());
    }
}

// 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) {
    // 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)
        ->where('id', '!=', $page->updated_by)
        ->get();
    $updater = User::query()->findOrFail($page->updated_by);

    // Send a notification to each of the users we want to notify
    foreach ($usersToNotify as $user) {
        $user->notify(new PageUpdatedNotification($page, $updater));
    }
}

// Listen to page update events and kick-start our notification logic
Theme::listen(ThemeEvents::ACTIVITY_LOGGED, function(string $type, $detail) {
    if ($type === ActivityType::PAGE_UPDATE && $detail instanceof Page) {
        notifyThoseThatHaveFavouritedPage($detail);
    }
});

Request an Update

Hack not working on the latest version of BookStack?
You can request this hack to be updated & tested for a small one-time fee.
This helps keeps these hacks updated & maintained in a sustainable manner.


Latest Hacks