-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Notifications: added user preference UI & logic
Includes testing to cover. Also added file missing from previous commit.
- Loading branch information
1 parent
45e75ed
commit 634e74e
Showing
8 changed files
with
211 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
namespace BookStack\Activity\Notifications; | ||
|
||
use BookStack\Activity\ActivityType; | ||
use BookStack\Activity\Models\Loggable; | ||
use BookStack\Activity\Notifications\Handlers\CommentCreationNotificationHandler; | ||
use BookStack\Activity\Notifications\Handlers\NotificationHandler; | ||
use BookStack\Activity\Notifications\Handlers\PageCreationNotificationHandler; | ||
use BookStack\Activity\Notifications\Handlers\PageUpdateNotificationHandler; | ||
|
||
class NotificationManager | ||
{ | ||
/** | ||
* @var class-string<NotificationHandler>[] | ||
*/ | ||
protected array $handlers = []; | ||
|
||
public function handle(string $activityType, string|Loggable $detail): void | ||
{ | ||
$handlersToRun = $this->handlers[$activityType] ?? []; | ||
foreach ($handlersToRun as $handlerClass) { | ||
/** @var NotificationHandler $handler */ | ||
$handler = app()->make($handlerClass); | ||
$handler->handle($activityType, $detail); | ||
} | ||
} | ||
|
||
/** | ||
* @param class-string<NotificationHandler> $handlerClass | ||
*/ | ||
public function registerHandler(string $activityType, string $handlerClass): void | ||
{ | ||
if (!isset($this->handlers[$activityType])) { | ||
$this->handlers[$activityType] = []; | ||
} | ||
|
||
if (!in_array($handlerClass, $this->handlers[$activityType])) { | ||
$this->handlers[$activityType][] = $handlerClass; | ||
} | ||
} | ||
|
||
public function loadDefaultHandlers(): void | ||
{ | ||
$this->registerHandler(ActivityType::PAGE_CREATE, PageCreationNotificationHandler::class); | ||
$this->registerHandler(ActivityType::PAGE_UPDATE, PageUpdateNotificationHandler::class); | ||
$this->registerHandler(ActivityType::COMMENT_CREATE, CommentCreationNotificationHandler::class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
namespace BookStack\Settings; | ||
|
||
use BookStack\Users\Models\User; | ||
|
||
class UserNotificationPreferences | ||
{ | ||
public function __construct( | ||
protected User $user | ||
) { | ||
} | ||
|
||
public function notifyOnOwnPageChanges(): bool | ||
{ | ||
return $this->getNotificationSetting('own-page-changes'); | ||
} | ||
|
||
public function notifyOnOwnPageComments(): bool | ||
{ | ||
return $this->getNotificationSetting('own-page-comments'); | ||
} | ||
|
||
public function notifyOnCommentReplies(): bool | ||
{ | ||
return $this->getNotificationSetting('comment-replies'); | ||
} | ||
|
||
public function updateFromSettingsArray(array $settings) | ||
{ | ||
$allowList = ['own-page-changes', 'own-page-comments', 'comment-replies']; | ||
foreach ($settings as $setting => $status) { | ||
if (!in_array($setting, $allowList)) { | ||
continue; | ||
} | ||
|
||
$value = $status === 'true' ? 'true' : 'false'; | ||
setting()->putUser($this->user, 'notifications#' . $setting, $value); | ||
} | ||
} | ||
|
||
protected function getNotificationSetting(string $key): bool | ||
{ | ||
return setting()->getUser($this->user, 'notifications#' . $key); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
@extends('layouts.simple') | ||
|
||
@section('body') | ||
<div class="container small my-xl"> | ||
|
||
<section class="card content-wrap auto-height"> | ||
<form action="{{ url('/preferences/notifications') }}" method="post"> | ||
{{ method_field('put') }} | ||
{{ csrf_field() }} | ||
|
||
<h1 class="list-heading">{{ trans('preferences.notifications') }}</h1> | ||
<p class="text-small text-muted">{{ trans('preferences.notifications_desc') }}</p> | ||
|
||
<div class="toggle-switch-list"> | ||
<div> | ||
@include('form.toggle-switch', [ | ||
'name' => 'preferences[own-page-changes]', | ||
'value' => $preferences->notifyOnOwnPageChanges(), | ||
'label' => trans('preferences.notifications_opt_own_page_changes'), | ||
]) | ||
</div> | ||
<div> | ||
@include('form.toggle-switch', [ | ||
'name' => 'preferences[own-page-comments]', | ||
'value' => $preferences->notifyOnOwnPageComments(), | ||
'label' => trans('preferences.notifications_opt_own_page_comments'), | ||
]) | ||
</div> | ||
<div> | ||
@include('form.toggle-switch', [ | ||
'name' => 'preferences[comment-replies]', | ||
'value' => $preferences->notifyOnCommentReplies(), | ||
'label' => trans('preferences.notifications_opt_comment_replies'), | ||
]) | ||
</div> | ||
</div> | ||
|
||
<div class="form-group text-right"> | ||
<button class="button">{{ trans('preferences.notifications_save') }}</button> | ||
</div> | ||
</form> | ||
</section> | ||
|
||
</div> | ||
@stop |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters