3 namespace BookStack\Activity\Notifications\Handlers;
5 use BookStack\Activity\Models\Loggable;
6 use BookStack\Activity\Notifications\Messages\PageCreationNotification;
7 use BookStack\Activity\Tools\EntityWatchers;
8 use BookStack\Activity\WatchLevels;
9 use BookStack\Entities\Models\Page;
10 use BookStack\Permissions\PermissionApplicator;
11 use BookStack\Users\Models\User;
13 class PageCreationNotificationHandler implements NotificationHandler
15 public function handle(string $activityType, Loggable|string $detail, User $user): void
17 if (!($detail instanceof Page)) {
18 throw new \InvalidArgumentException("Detail for page create notifications must be a page");
20 // No user-level preferences to care about here.
21 // Possible Scenarios:
22 // ✅ User watching parent chapter
23 // ✅ User watching parent book
24 // ❌ User ignoring parent book
25 // ❌ User ignoring parent chapter
26 // ❌ User watching parent book, ignoring chapter
27 // ✅ User watching parent book, watching chapter
28 // ❌ User ignoring parent book, ignoring chapter
29 // ✅ User ignoring parent book, watching chapter
31 // Get all relevant watchers
32 $watchers = new EntityWatchers($detail, WatchLevels::NEW);
33 $users = User::query()->whereIn('id', $watchers->getWatcherUserIds())->get();
35 // TODO - Clean this up, likely abstract to base class
36 // TODO - Prevent sending to current user
37 $permissions = app()->make(PermissionApplicator::class);
38 foreach ($users as $user) {
39 if ($user->can('receive-notifications') && $permissions->checkOwnableUserAccess($detail, 'view')) {
40 $user->notify(new PageCreationNotification($detail, $user));