Notifications: Got core notification logic working for new pages

Also rolled out watch UI to chapter and page views
This commit is contained in:
Dan Brown 2023-08-04 16:51:29 +01:00
parent 9779c1a357
commit 18ae67a138
No known key found for this signature in database
GPG Key ID: 46D9F943C24A2EF9
8 changed files with 60 additions and 14 deletions

View File

@ -3,17 +3,20 @@
namespace BookStack\Activity\Notifications\Handlers;
use BookStack\Activity\Models\Loggable;
use BookStack\Activity\Models\Watch;
use BookStack\Activity\Notifications\Messages\PageCreationNotification;
use BookStack\Activity\Tools\EntityWatchers;
use BookStack\Activity\WatchLevels;
use BookStack\Entities\Models\Page;
use BookStack\Permissions\PermissionApplicator;
use BookStack\Users\Models\User;
class PageCreationNotificationHandler implements NotificationHandler
{
public function handle(string $activityType, Loggable|string $detail, User $user): void
{
// TODO
if (!($detail instanceof Page)) {
throw new \InvalidArgumentException("Detail for page create notifications must be a page");
}
// No user-level preferences to care about here.
// Possible Scenarios:
// ✅ User watching parent chapter
@ -27,8 +30,15 @@ class PageCreationNotificationHandler implements NotificationHandler
// Get all relevant watchers
$watchers = new EntityWatchers($detail, WatchLevels::NEW);
$users = User::query()->whereIn('id', $watchers->getWatcherUserIds())->get();
// TODO - need to check entity visibility and receive-notifications permissions.
// Maybe abstract this to a generic late-stage filter?
// 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));
}
}
}
}

View File

@ -10,7 +10,14 @@ use Illuminate\Database\Eloquent\Builder;
class EntityWatchers
{
/**
* @var int[]
*/
protected array $watchers = [];
/**
* @var int[]
*/
protected array $ignorers = [];
public function __construct(
@ -20,16 +27,35 @@ class EntityWatchers
$this->build();
}
public function getWatcherUserIds(): array
{
return $this->watchers;
}
protected function build(): void
{
$watches = $this->getRelevantWatches();
// TODO - De-dupe down watches per-user across entity types
// so we end up with [user_id => status] values
// then filter to current watch level, considering ignores,
// then populate the class watchers/ignores with ids.
// Sort before de-duping, so that the order looped below follows book -> chapter -> page ordering
usort($watches, function (Watch $watchA, Watch $watchB) {
$entityTypeDiff = $watchA->watchable_type <=> $watchB->watchable_type;
return $entityTypeDiff === 0 ? ($watchA->user_id <=> $watchB->user_id) : $entityTypeDiff;
});
// De-dupe by user id to get their most relevant level
$levelByUserId = [];
foreach ($watches as $watch) {
$levelByUserId[$watch->user_id] = $watch->level;
}
// Populate the class arrays
$this->watchers = array_keys(array_filter($levelByUserId, fn(int $level) => $level >= $this->watchLevel));
$this->ignorers = array_keys(array_filter($levelByUserId, fn(int $level) => $level === 0));
}
/**
* @return Watch[]
*/
protected function getRelevantWatches(): array
{
/** @var Entity[] $entitiesInvolved */
@ -49,7 +75,7 @@ class EntityWatchers
});
return $query->get([
'level', 'watchable_id', 'watchable_type', 'user_id'
'level', 'watchable_id', 'watchable_type', 'user_id'
])->all();
}
}

View File

@ -3,6 +3,7 @@
namespace BookStack\Entities\Controllers;
use BookStack\Activity\Models\View;
use BookStack\Activity\Tools\UserWatchOptions;
use BookStack\Entities\Models\Book;
use BookStack\Entities\Repos\ChapterRepo;
use BookStack\Entities\Tools\BookContents;
@ -81,6 +82,7 @@ class ChapterController extends Controller
'chapter' => $chapter,
'current' => $chapter,
'sidebarTree' => $sidebarTree,
'watchOptions' => new UserWatchOptions(user()),
'pages' => $pages,
'next' => $nextPreviousLocator->getNext(),
'previous' => $nextPreviousLocator->getPrevious(),

View File

@ -4,6 +4,7 @@ namespace BookStack\Entities\Controllers;
use BookStack\Activity\Models\View;
use BookStack\Activity\Tools\CommentTree;
use BookStack\Activity\Tools\UserWatchOptions;
use BookStack\Entities\Models\Page;
use BookStack\Entities\Repos\PageRepo;
use BookStack\Entities\Tools\BookContents;
@ -151,6 +152,7 @@ class PageController extends Controller
'sidebarTree' => $sidebarTree,
'commentTree' => $commentTree,
'pageNav' => $pageNav,
'watchOptions' => new UserWatchOptions(user()),
'next' => $nextPreviousLocator->getNext(),
'previous' => $nextPreviousLocator->getPrevious(),
'referenceCount' => $this->referenceFetcher->getPageReferenceCountToEntity($page),

View File

@ -139,12 +139,12 @@
<hr class="primary-background">
@if(signedInUser())
@include('entities.favourite-action', ['entity' => $book])
@endif
@if($watchOptions->canWatch() && !$watchOptions->isWatching($book))
@include('entities.watch-action', ['entity' => $book])
@endif
@if(signedInUser())
@include('entities.favourite-action', ['entity' => $book])
@endif
@if(userCan('content-export'))
@include('entities.export-menu', ['entity' => $book])
@endif

View File

@ -157,6 +157,9 @@
<hr class="primary-background"/>
@if($watchOptions->canWatch() && !$watchOptions->isWatching($chapter))
@include('entities.watch-action', ['entity' => $chapter])
@endif
@if(signedInUser())
@include('entities.favourite-action', ['entity' => $chapter])
@endif

View File

@ -5,7 +5,7 @@
<input type="hidden" name="id" value="{{ $entity->id }}">
<ul refs="dropdown@menu" class="dropdown-menu xl-limited anchor-left pb-none">
@foreach(\BookStack\Activity\WatchLevels::all() as $option)
@foreach(\BookStack\Activity\WatchLevels::all() as $option => $value)
<li>
<button name="level" value="{{ $option }}" class="icon-item">
@if($watchLevel === $option)

View File

@ -185,6 +185,9 @@
<hr class="primary-background"/>
@if($watchOptions->canWatch() && !$watchOptions->isWatching($page))
@include('entities.watch-action', ['entity' => $page])
@endif
@if(signedInUser())
@include('entities.favourite-action', ['entity' => $page])
@endif