feat: refactor suspend extension

This commit is contained in:
Sami Mazouz 2024-02-26 14:46:34 +01:00
parent 8b7f3c3d6d
commit 6e753b4def
No known key found for this signature in database
6 changed files with 79 additions and 127 deletions

View File

@ -7,13 +7,13 @@
* LICENSE file that was distributed with this source code.
*/
use Flarum\Api\Serializer\BasicUserSerializer;
use Flarum\Api\Serializer\ForumSerializer;
use Flarum\Api\Serializer\UserSerializer;
use Flarum\Api\Context;
use Flarum\Api\Schema;
use Flarum\Api\Resource;
use Flarum\Extend;
use Flarum\Search\Database\DatabaseSearchDriver;
use Flarum\Suspend\Access\UserPolicy;
use Flarum\Suspend\AddUserSuspendAttributes;
use Flarum\Suspend\Api\UserResourceFields;
use Flarum\Suspend\Event\Suspended;
use Flarum\Suspend\Event\Unsuspended;
use Flarum\Suspend\Listener;
@ -39,22 +39,23 @@ return [
->cast('suspend_reason', 'string')
->cast('suspend_message', 'string'),
(new Extend\ApiSerializer(UserSerializer::class))
->attributes(AddUserSuspendAttributes::class),
(new Extend\ApiResource(Resource\UserResource::class))
->fields(UserResourceFields::class),
(new Extend\ApiSerializer(ForumSerializer::class))
->attribute('canSuspendUsers', function (ForumSerializer $serializer) {
return $serializer->getActor()->hasPermission('user.suspend');
}),
(new Extend\ApiResource(Resource\ForumResource::class))
->fields(fn () => [
Schema\Boolean::make('canSuspendUsers')
->get(fn (object $model, Context $context) => $context->getActor()->hasPermission('user.suspend')),
]),
new Extend\Locales(__DIR__.'/locale'),
(new Extend\Notification())
->type(UserSuspendedBlueprint::class, BasicUserSerializer::class, ['alert', 'email'])
->type(UserUnsuspendedBlueprint::class, BasicUserSerializer::class, ['alert', 'email']),
->type(UserSuspendedBlueprint::class, ['alert', 'email'])
->type(UserUnsuspendedBlueprint::class, ['alert', 'email']),
(new Extend\Event())
->listen(Saving::class, Listener\SaveSuspensionToDatabase::class)
->listen(Saving::class, Listener\SavingUser::class)
->listen(Suspended::class, Listener\SendNotificationWhenUserIsSuspended::class)
->listen(Unsuspended::class, Listener\SendNotificationWhenUserIsUnsuspended::class),

View File

@ -1,35 +0,0 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Suspend;
use Flarum\Api\Serializer\UserSerializer;
use Flarum\User\User;
class AddUserSuspendAttributes
{
public function __invoke(UserSerializer $serializer, User $user): array
{
$attributes = [];
$canSuspend = $serializer->getActor()->can('suspend', $user);
if ($canSuspend) {
$attributes['suspendReason'] = $user->suspend_reason;
}
if ($serializer->getActor()->id === $user->id || $canSuspend) {
$attributes['suspendMessage'] = $user->suspend_message;
$attributes['suspendedUntil'] = $serializer->formatDate($user->suspended_until);
}
$attributes['canSuspend'] = $canSuspend;
return $attributes;
}
}

View File

@ -0,0 +1,28 @@
<?php
namespace Flarum\Suspend\Api;
use Flarum\Api\Context;
use Flarum\Api\Schema;
use Flarum\User\User;
class UserResourceFields
{
public function __invoke(): array
{
return [
Schema\Boolean::make('canSuspend')
->get($canSuspend = fn (User $user, Context $context) => $context->getActor()->can('suspend', $user)),
Schema\Str::make('suspendReason')
->writable($canSuspend)
->visible($canSuspend),
Schema\Str::make('suspendMessage')
->writable($canSuspend)
->visible(fn (User $user, Context $context) => $context->getActor()->id === $user->id || $canSuspend($user, $context)),
Schema\Date::make('suspendedUntil')
->writable($canSuspend)
->visible(fn (User $user, Context $context) => $context->getActor()->id === $user->id || $canSuspend($user, $context))
->nullable(),
];
}
}

View File

@ -1,60 +0,0 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Suspend\Listener;
use Carbon\Carbon;
use DateTime;
use Flarum\Suspend\Event\Suspended;
use Flarum\Suspend\Event\Unsuspended;
use Flarum\Suspend\SuspendValidator;
use Flarum\User\Event\Saving;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Support\Arr;
class SaveSuspensionToDatabase
{
public function __construct(
protected SuspendValidator $validator,
protected Dispatcher $events
) {
}
public function handle(Saving $event): void
{
$attributes = Arr::get($event->data, 'attributes', []);
if (array_key_exists('suspendedUntil', $attributes)) {
$this->validator->assertValid($attributes);
$user = $event->user;
$actor = $event->actor;
$actor->assertCan('suspend', $user);
if ($attributes['suspendedUntil']) {
$user->suspended_until = Carbon::createFromTimestamp((new DateTime($attributes['suspendedUntil']))->getTimestamp());
$user->suspend_reason = empty($attributes['suspendReason']) ? null : $attributes['suspendReason'];
$user->suspend_message = empty($attributes['suspendMessage']) ? null : $attributes['suspendMessage'];
} else {
$user->suspended_until = null;
$user->suspend_reason = null;
$user->suspend_message = null;
}
if ($user->isDirty(['suspended_until', 'suspend_reason', 'suspend_message'])) {
$this->events->dispatch(
$user->suspended_until === null ?
new Unsuspended($user, $actor) :
new Suspended($user, $actor)
);
}
}
}
}

View File

@ -0,0 +1,37 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Suspend\Listener;
use Flarum\Suspend\Event\Suspended;
use Flarum\Suspend\Event\Unsuspended;
use Flarum\User\Event\Saving;
use Illuminate\Contracts\Events\Dispatcher;
class SavingUser
{
public function __construct(
protected Dispatcher $events
) {
}
public function handle(Saving $event): void
{
$user = $event->user;
$actor = $event->actor;
if ($user->isDirty(['suspended_until', 'suspend_reason', 'suspend_message'])) {
$this->events->dispatch(
$user->suspended_until === null ?
new Unsuspended($user, $actor) :
new Suspended($user, $actor)
);
}
}
}

View File

@ -1,19 +0,0 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Suspend;
use Flarum\Foundation\AbstractValidator;
class SuspendValidator extends AbstractValidator
{
protected array $rules = [
'suspendedUntil' => ['nullable', 'date'],
];
}