2023-07-19 06:03:05 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace BookStack\Activity\Notifications\Handlers;
|
|
|
|
|
|
|
|
use BookStack\Activity\Models\Loggable;
|
2023-08-04 11:51:29 -04:00
|
|
|
use BookStack\Activity\Notifications\Messages\PageCreationNotification;
|
2023-08-04 07:27:29 -04:00
|
|
|
use BookStack\Activity\Tools\EntityWatchers;
|
|
|
|
use BookStack\Activity\WatchLevels;
|
2023-08-04 11:51:29 -04:00
|
|
|
use BookStack\Entities\Models\Page;
|
|
|
|
use BookStack\Permissions\PermissionApplicator;
|
2023-08-04 07:27:29 -04:00
|
|
|
use BookStack\Users\Models\User;
|
2023-07-19 06:03:05 -04:00
|
|
|
|
|
|
|
class PageCreationNotificationHandler implements NotificationHandler
|
|
|
|
{
|
2023-08-04 07:27:29 -04:00
|
|
|
public function handle(string $activityType, Loggable|string $detail, User $user): void
|
2023-07-19 06:03:05 -04:00
|
|
|
{
|
2023-08-04 11:51:29 -04:00
|
|
|
if (!($detail instanceof Page)) {
|
|
|
|
throw new \InvalidArgumentException("Detail for page create notifications must be a page");
|
|
|
|
}
|
2023-08-04 07:27:29 -04:00
|
|
|
// No user-level preferences to care about here.
|
|
|
|
// Possible Scenarios:
|
|
|
|
// ✅ User watching parent chapter
|
|
|
|
// ✅ User watching parent book
|
|
|
|
// ❌ User ignoring parent book
|
|
|
|
// ❌ User ignoring parent chapter
|
|
|
|
// ❌ User watching parent book, ignoring chapter
|
|
|
|
// ✅ User watching parent book, watching chapter
|
|
|
|
// ❌ User ignoring parent book, ignoring chapter
|
|
|
|
// ✅ User ignoring parent book, watching chapter
|
|
|
|
|
|
|
|
// Get all relevant watchers
|
|
|
|
$watchers = new EntityWatchers($detail, WatchLevels::NEW);
|
2023-08-04 11:51:29 -04:00
|
|
|
$users = User::query()->whereIn('id', $watchers->getWatcherUserIds())->get();
|
2023-08-04 07:27:29 -04:00
|
|
|
|
2023-08-04 11:51:29 -04:00
|
|
|
// TODO - Clean this up, likely abstract to base class
|
|
|
|
// TODO - Prevent sending to current user
|
|
|
|
$permissions = app()->make(PermissionApplicator::class);
|
|
|
|
foreach ($users as $user) {
|
|
|
|
if ($user->can('receive-notifications') && $permissions->checkOwnableUserAccess($detail, 'view')) {
|
|
|
|
$user->notify(new PageCreationNotification($detail, $user));
|
|
|
|
}
|
|
|
|
}
|
2023-07-19 06:03:05 -04:00
|
|
|
}
|
|
|
|
}
|