mirror of
https://github.com/flarum/framework.git
synced 2024-12-04 16:23:37 +08:00
feat: refactor lock extension
This commit is contained in:
parent
14955bb6d5
commit
e19346efd3
|
@ -7,8 +7,9 @@
|
||||||
* LICENSE file that was distributed with this source code.
|
* LICENSE file that was distributed with this source code.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
use Flarum\Api\Serializer\BasicDiscussionSerializer;
|
use Flarum\Api\Context;
|
||||||
use Flarum\Api\Serializer\DiscussionSerializer;
|
use Flarum\Api\Resource;
|
||||||
|
use Flarum\Api\Schema;
|
||||||
use Flarum\Discussion\Discussion;
|
use Flarum\Discussion\Discussion;
|
||||||
use Flarum\Discussion\Event\Saving;
|
use Flarum\Discussion\Event\Saving;
|
||||||
use Flarum\Discussion\Search\DiscussionSearcher;
|
use Flarum\Discussion\Search\DiscussionSearcher;
|
||||||
|
@ -33,24 +34,38 @@ return [
|
||||||
new Extend\Locales(__DIR__.'/locale'),
|
new Extend\Locales(__DIR__.'/locale'),
|
||||||
|
|
||||||
(new Extend\Notification())
|
(new Extend\Notification())
|
||||||
->type(DiscussionLockedBlueprint::class, BasicDiscussionSerializer::class, ['alert']),
|
->type(DiscussionLockedBlueprint::class, ['alert']),
|
||||||
|
|
||||||
(new Extend\Model(Discussion::class))
|
(new Extend\Model(Discussion::class))
|
||||||
->cast('is_locked', 'bool'),
|
->cast('is_locked', 'bool'),
|
||||||
|
|
||||||
(new Extend\ApiSerializer(DiscussionSerializer::class))
|
(new Extend\ApiResource(Resource\DiscussionResource::class))
|
||||||
->attribute('isLocked', function (DiscussionSerializer $serializer, Discussion $discussion) {
|
->fields(fn () => [
|
||||||
return $discussion->is_locked;
|
Schema\Boolean::make('isLocked')
|
||||||
})
|
->writable(fn (Discussion $discussion, Context $context) => $context->getActor()->can('lock', $discussion))
|
||||||
->attribute('canLock', function (DiscussionSerializer $serializer, Discussion $discussion) {
|
->set(function (Discussion $discussion, bool $isLocked, Context $context) {
|
||||||
return $serializer->getActor()->can('lock', $discussion);
|
$actor = $context->getActor();
|
||||||
}),
|
|
||||||
|
if ($discussion->is_locked === $isLocked) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$discussion->is_locked = $isLocked;
|
||||||
|
|
||||||
|
$discussion->raise(
|
||||||
|
$discussion->is_locked
|
||||||
|
? new DiscussionWasLocked($discussion, $actor)
|
||||||
|
: new DiscussionWasUnlocked($discussion, $actor)
|
||||||
|
);
|
||||||
|
}),
|
||||||
|
Schema\Boolean::make('canLock')
|
||||||
|
->get(fn (Discussion $discussion, Context $context) => $context->getActor()->can('lock', $discussion)),
|
||||||
|
]),
|
||||||
|
|
||||||
(new Extend\Post())
|
(new Extend\Post())
|
||||||
->type(DiscussionLockedPost::class),
|
->type(DiscussionLockedPost::class),
|
||||||
|
|
||||||
(new Extend\Event())
|
(new Extend\Event())
|
||||||
->listen(Saving::class, Listener\SaveLockedToDatabase::class)
|
|
||||||
->listen(DiscussionWasLocked::class, Listener\CreatePostWhenDiscussionIsLocked::class)
|
->listen(DiscussionWasLocked::class, Listener\CreatePostWhenDiscussionIsLocked::class)
|
||||||
->listen(DiscussionWasUnlocked::class, Listener\CreatePostWhenDiscussionIsUnlocked::class),
|
->listen(DiscussionWasUnlocked::class, Listener\CreatePostWhenDiscussionIsUnlocked::class),
|
||||||
|
|
||||||
|
|
|
@ -1,40 +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\Lock\Listener;
|
|
||||||
|
|
||||||
use Flarum\Discussion\Event\Saving;
|
|
||||||
use Flarum\Lock\Event\DiscussionWasLocked;
|
|
||||||
use Flarum\Lock\Event\DiscussionWasUnlocked;
|
|
||||||
|
|
||||||
class SaveLockedToDatabase
|
|
||||||
{
|
|
||||||
public function handle(Saving $event): void
|
|
||||||
{
|
|
||||||
if (isset($event->data['attributes']['isLocked'])) {
|
|
||||||
$isLocked = (bool) $event->data['attributes']['isLocked'];
|
|
||||||
$discussion = $event->discussion;
|
|
||||||
$actor = $event->actor;
|
|
||||||
|
|
||||||
$actor->assertCan('lock', $discussion);
|
|
||||||
|
|
||||||
if ((bool) $discussion->is_locked === $isLocked) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
$discussion->is_locked = $isLocked;
|
|
||||||
|
|
||||||
$discussion->raise(
|
|
||||||
$discussion->is_locked
|
|
||||||
? new DiscussionWasLocked($discussion, $actor)
|
|
||||||
: new DiscussionWasUnlocked($discussion, $actor)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user