]> BookStack Code Mirror - bookstack/blob - app/Activity/Notifications/Handlers/BaseNotificationHandler.php
Add optional OIDC avatar fetching from the “picture” claim
[bookstack] / app / Activity / Notifications / Handlers / BaseNotificationHandler.php
1 <?php
2
3 namespace BookStack\Activity\Notifications\Handlers;
4
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;
11
12 abstract class BaseNotificationHandler implements NotificationHandler
13 {
14     /**
15      * @param class-string<BaseActivityNotification> $notification
16      * @param int[] $userIds
17      */
18     protected function sendNotificationToUserIds(string $notification, array $userIds, User $initiator, string|Loggable $detail, Entity $relatedModel): void
19     {
20         $users = User::query()->whereIn('id', array_unique($userIds))->get();
21
22         foreach ($users as $user) {
23             // Prevent sending to the user that initiated the activity
24             if ($user->id === $initiator->id) {
25                 continue;
26             }
27
28             // Prevent sending of the user does not have notification permissions
29             if (!$user->can('receive-notifications')) {
30                 continue;
31             }
32
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')) {
36                 continue;
37             }
38
39             // Send the notification
40             try {
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()}");
44             }
45         }
46     }
47 }