| 
									
										
										
										
											2023-08-05 21:19:23 +08:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace BookStack\Activity\Notifications\Handlers; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-15 21:39:39 +08:00
										 |  |  | use BookStack\Activity\Models\Loggable; | 
					
						
							| 
									
										
										
										
											2023-08-05 21:19:23 +08:00
										 |  |  | use BookStack\Activity\Notifications\Messages\BaseActivityNotification; | 
					
						
							|  |  |  | use BookStack\Entities\Models\Entity; | 
					
						
							|  |  |  | use BookStack\Permissions\PermissionApplicator; | 
					
						
							|  |  |  | use BookStack\Users\Models\User; | 
					
						
							| 
									
										
										
										
											2024-12-13 05:45:52 +08:00
										 |  |  | use Illuminate\Support\Facades\Log; | 
					
						
							| 
									
										
										
										
											2023-08-05 21:19:23 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | abstract class BaseNotificationHandler implements NotificationHandler | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param class-string<BaseActivityNotification> $notification | 
					
						
							|  |  |  |      * @param int[] $userIds | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2023-08-15 21:39:39 +08:00
										 |  |  |     protected function sendNotificationToUserIds(string $notification, array $userIds, User $initiator, string|Loggable $detail, Entity $relatedModel): void | 
					
						
							| 
									
										
										
										
											2023-08-05 21:19:23 +08:00
										 |  |  |     { | 
					
						
							|  |  |  |         $users = User::query()->whereIn('id', array_unique($userIds))->get(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         foreach ($users as $user) { | 
					
						
							|  |  |  |             // Prevent sending to the user that initiated the activity
 | 
					
						
							|  |  |  |             if ($user->id === $initiator->id) { | 
					
						
							|  |  |  |                 continue; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // Prevent sending of the user does not have notification permissions
 | 
					
						
							|  |  |  |             if (!$user->can('receive-notifications')) { | 
					
						
							|  |  |  |                 continue; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // Prevent sending if the user does not have access to the related content
 | 
					
						
							| 
									
										
										
										
											2023-08-18 00:57:31 +08:00
										 |  |  |             $permissions = new PermissionApplicator($user); | 
					
						
							|  |  |  |             if (!$permissions->checkOwnableUserAccess($relatedModel, 'view')) { | 
					
						
							| 
									
										
										
										
											2023-08-05 21:19:23 +08:00
										 |  |  |                 continue; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // Send the notification
 | 
					
						
							| 
									
										
										
										
											2024-12-13 05:45:52 +08:00
										 |  |  |             try { | 
					
						
							|  |  |  |                 $user->notify(new $notification($detail, $initiator)); | 
					
						
							|  |  |  |             } catch (\Exception $exception) { | 
					
						
							|  |  |  |                 Log::error("Failed to send email notification to user [id:{$user->id}] with error: {$exception->getMessage()}"); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2023-08-05 21:19:23 +08:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |