3 namespace BookStack\Activity\Notifications\Handlers;
5 use BookStack\Activity\Models\Loggable;
6 use BookStack\Activity\Notifications\Messages\BaseActivityNotification;
7 use BookStack\Entities\Models\Entity;
8 use BookStack\Permissions\PermissionApplicator;
9 use BookStack\Users\Models\User;
10 use Illuminate\Support\Facades\Log;
12 abstract class BaseNotificationHandler implements NotificationHandler
15 * @param class-string<BaseActivityNotification> $notification
16 * @param int[] $userIds
18 protected function sendNotificationToUserIds(string $notification, array $userIds, User $initiator, string|Loggable $detail, Entity $relatedModel): void
20 $users = User::query()->whereIn('id', array_unique($userIds))->get();
22 foreach ($users as $user) {
23 // Prevent sending to the user that initiated the activity
24 if ($user->id === $initiator->id) {
28 // Prevent sending of the user does not have notification permissions
29 if (!$user->can('receive-notifications')) {
33 // Prevent sending if the user does not have access to the related content
34 $permissions = new PermissionApplicator($user);
35 if (!$permissions->checkOwnableUserAccess($relatedModel, 'view')) {
39 // Send the notification
41 $user->notify(new $notification($detail, $initiator));
42 } catch (\Exception $exception) {
43 Log::error("Failed to send email notification to user [id:{$user->id}] with error: {$exception->getMessage()}");