Skip to content

Instantly share code, notes, and snippets.

@ssddanbrown
Last active December 1, 2022 12:24
Show Gist options
  • Save ssddanbrown/f14f7a5b0ab190899b6bd84ba9731f36 to your computer and use it in GitHub Desktop.
Save ssddanbrown/f14f7a5b0ab190899b6bd84ba9731f36 to your computer and use it in GitHub Desktop.
bookstack-notify-page-updates-for-favourites

This is a hack to BookStack, using the theme system, to email users about page updates to the pages they have favourited.

Setup

This uses the logical theme system.

  1. Within the BookStack install folder, you should have a themes folder.
  2. Create a themes/custom/functions.php file with the contents of the functions.php file example below.
  3. Customize the email message, if desired, by editing the lines of text within the toMail part at around lines 32-35.
  4. Add APP_THEME=custom to your .env file.

Note

The sending of emails may slow down page update actions, and these could be noisy if a user edits a page many times quickly. These customizations are not officially supported any may break upon, or conflict with, future updates. Quickly tested on BookStack v22.11.

<?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. Tested on v22.11.
// Note: This is not officially supported, may break upon update, and the email sending may slow down operations.
// Also, users could be spammed with emails on repeated updates.
// Also, might hit email system rate-limits.
// 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);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment