1
0
mirror of https://github.com/flarum/core.git synced 2025-08-13 11:54:32 +02:00

Update for composer branch

This commit is contained in:
Toby Zerner
2015-10-11 16:15:07 +10:30
parent 61c9471e50
commit fca470604e
33 changed files with 644 additions and 331 deletions

View File

@@ -0,0 +1,36 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Lock\Access;
use Flarum\Core\Access\AbstractPolicy;
use Flarum\Core\Discussion;
use Flarum\Core\User;
use Illuminate\Contracts\Events\Dispatcher;
class DiscussionPolicy extends AbstractPolicy
{
/**
* {@inheritdoc}
*/
protected $model = Discussion::class;
/**
* @param User $actor
* @param Discussion $discussion
* @return bool
*/
public function reply(User $actor, Discussion $discussion)
{
if ($discussion->is_locked && $actor->cannot('lock', $discussion)) {
return false;
}
}
}

View File

@@ -8,10 +8,10 @@
* file that was distributed with this source code.
*/
namespace Flarum\Lock\Events;
namespace Flarum\Lock\Event;
use Flarum\Core\Discussions\Discussion;
use Flarum\Core\Users\User;
use Flarum\Core\Discussion;
use Flarum\Core\User;
class DiscussionWasLocked
{

View File

@@ -8,10 +8,10 @@
* file that was distributed with this source code.
*/
namespace Flarum\Lock\Events;
namespace Flarum\Lock\Event;
use Flarum\Core\Discussions\Discussion;
use Flarum\Core\Users\User;
use Flarum\Core\Discussion;
use Flarum\Core\User;
class DiscussionWasUnlocked
{

View File

@@ -1,26 +0,0 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Lock;
use Flarum\Support\Extension as BaseExtension;
use Illuminate\Events\Dispatcher;
class Extension extends BaseExtension
{
public function listen(Dispatcher $events)
{
$events->subscribe('Flarum\Lock\Listeners\AddClientAssets');
$events->subscribe('Flarum\Lock\Listeners\AddApiAttributes');
$events->subscribe('Flarum\Lock\Listeners\PersistData');
$events->subscribe('Flarum\Lock\Listeners\NotifyDiscussionLocked');
$events->subscribe('Flarum\Lock\Listeners\ConfigurePermissions');
}
}

View File

@@ -8,16 +8,22 @@
* file that was distributed with this source code.
*/
namespace Flarum\Lock\Gambits;
namespace Flarum\Lock\Gambit;
use Flarum\Core\Search\Search;
use Flarum\Core\Search\RegexGambit;
use Flarum\Core\Search\AbstractRegexGambit;
use Flarum\Core\Search\AbstractSearch;
class LockGambit extends RegexGambit
class LockedGambit extends AbstractRegexGambit
{
/**
* {@inheritdoc}
*/
protected $pattern = 'is:locked';
protected function conditions(Search $search, array $matches, $negate)
/**
* {@inheritdoc}
*/
protected function conditions(AbstractSearch $search, array $matches, $negate)
{
$search->getQuery()->where('is_locked', ! $negate);
}

View File

@@ -0,0 +1,48 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Lock\Listener;
use Flarum\Event\ConfigureClientView;
use Illuminate\Contracts\Events\Dispatcher;
class AddClientAssets
{
/**
* @param Dispatcher $events
*/
public function subscribe(Dispatcher $events)
{
$events->listen(ConfigureClientView::class, [$this, 'addAssets']);
}
/**
* @param ConfigureClientView $event
*/
public function addAssets(ConfigureClientView $event)
{
if ($event->isForum()) {
$event->addAssets([
__DIR__.'/../../js/forum/dist/extension.js',
__DIR__.'/../../less/forum/extension.less'
]);
$event->addBootstrapper('flarum/lock/main');
$event->addTranslations('flarum-lock.forum');
}
if ($event->isAdmin()) {
$event->addAssets([
__DIR__.'/../../js/admin/dist/extension.js'
]);
$event->addBootstrapper('flarum/lock/main');
$event->addTranslations('flarum-lock.admin');
}
}
}

View File

@@ -0,0 +1,37 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Lock\Listener;
use Flarum\Api\Serializer\DiscussionSerializer;
use Flarum\Event\PrepareApiAttributes;
use Illuminate\Contracts\Events\Dispatcher;
class AddDiscussionLockedAttributes
{
/**
* @param Dispatcher $events
*/
public function subscribe(Dispatcher $events)
{
$events->listen(PrepareApiAttributes::class, [$this, 'prepareApiAttributes']);
}
/**
* @param PrepareApiAttributes $event
*/
public function prepareApiAttributes(PrepareApiAttributes $event)
{
if ($event->isSerializer(DiscussionSerializer::class)) {
$event->attributes['isLocked'] = (bool) $event->model->is_locked;
$event->attributes['canLock'] = (bool) $event->actor->can('lock', $event->model);
}
}
}

View File

@@ -0,0 +1,34 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Lock\Listener;
use Flarum\Event\ConfigureDiscussionGambits;
use Flarum\Lock\Gambit\LockedGambit;
use Illuminate\Contracts\Events\Dispatcher;
class AddLockedGambit
{
/**
* @param Dispatcher $events
*/
public function subscribe(Dispatcher $events)
{
$events->listen(ConfigureDiscussionGambits::class, [$this, 'configureDiscussionGambits']);
}
/**
* @param ConfigureDiscussionGambits $event
*/
public function configureDiscussionGambits(ConfigureDiscussionGambits $event)
{
$event->gambits->add(LockedGambit::class);
}
}

View File

@@ -0,0 +1,104 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Lock\Listener;
use Flarum\Api\Serializer\DiscussionBasicSerializer;
use Flarum\Core\Discussion;
use Flarum\Core\Notification\NotificationSyncer;
use Flarum\Core\User;
use Flarum\Event\ConfigureNotificationTypes;
use Flarum\Event\ConfigurePostTypes;
use Flarum\Lock\Event\DiscussionWasLocked;
use Flarum\Lock\Event\DiscussionWasUnlocked;
use Flarum\Lock\Notification\DiscussionLockedBlueprint;
use Flarum\Lock\Post\DiscussionLockedPost;
use Illuminate\Contracts\Events\Dispatcher;
class CreatePostWhenDiscussionIsLocked
{
/**
* @var NotificationSyncer
*/
protected $notifications;
/**
* @param NotificationSyncer $notifications
*/
public function __construct(NotificationSyncer $notifications)
{
$this->notifications = $notifications;
}
/**
* @param Dispatcher $events
*/
public function subscribe(Dispatcher $events)
{
$events->listen(ConfigurePostTypes::class, [$this, 'addPostType']);
$events->listen(ConfigureNotificationTypes::class, [$this, 'addNotificationType']);
$events->listen(DiscussionWasLocked::class, [$this, 'whenDiscussionWasLocked']);
$events->listen(DiscussionWasUnlocked::class, [$this, 'whenDiscussionWasUnlocked']);
}
/**
* @param ConfigurePostTypes $event
*/
public function addPostType(ConfigurePostTypes $event)
{
$event->add(DiscussionLockedPost::class);
}
/**
* @param ConfigureNotificationTypes $event
*/
public function addNotificationType(ConfigureNotificationTypes $event)
{
$event->add(DiscussionLockedBlueprint::class, DiscussionBasicSerializer::class, ['alert']);
}
/**
* @param DiscussionWasLocked $event
*/
public function whenDiscussionWasLocked(DiscussionWasLocked $event)
{
$this->lockedChanged($event->discussion, $event->user, true);
}
/**
* @param DiscussionWasUnlocked $event
*/
public function whenDiscussionWasUnlocked(DiscussionWasUnlocked $event)
{
$this->lockedChanged($event->discussion, $event->user, false);
}
/**
* @param Discussion $discussion
* @param User $user
* @param $isLocked
*/
protected function lockedChanged(Discussion $discussion, User $user, $isLocked)
{
$post = DiscussionLockedPost::reply(
$discussion->id,
$user->id,
$isLocked
);
$post = $discussion->mergePost($post);
if ($discussion->start_user_id !== $user->id) {
$notification = new DiscussionLockedBlueprint($post);
$this->notifications->sync($notification, $post->exists ? [$discussion->startUser] : []);
}
}
}

View File

@@ -8,19 +8,29 @@
* file that was distributed with this source code.
*/
namespace Flarum\Lock\Listeners;
namespace Flarum\Lock\Listener;
use Flarum\Lock\Events\DiscussionWasLocked;
use Flarum\Lock\Events\DiscussionWasUnlocked;
use Flarum\Events\DiscussionWillBeSaved;
use Flarum\Core\Access\AssertPermissionTrait;
use Flarum\Event\DiscussionWillBeSaved;
use Flarum\Lock\Event\DiscussionWasLocked;
use Flarum\Lock\Event\DiscussionWasUnlocked;
use Illuminate\Contracts\Events\Dispatcher;
class PersistData
class SaveLockedToDatabase
{
public function subscribe($events)
use AssertPermissionTrait;
/**
* @param Dispatcher $events
*/
public function subscribe(Dispatcher $events)
{
$events->listen(DiscussionWillBeSaved::class, [$this, 'whenDiscussionWillBeSaved']);
}
/**
* @param DiscussionWillBeSaved $event
*/
public function whenDiscussionWillBeSaved(DiscussionWillBeSaved $event)
{
if (isset($event->data['attributes']['isLocked'])) {
@@ -28,7 +38,7 @@ class PersistData
$discussion = $event->discussion;
$actor = $event->actor;
$discussion->assertCan($actor, 'lock');
$this->assertCan($actor, 'lock', $discussion);
if ((bool) $discussion->is_locked === $isLocked) {
return;

View File

@@ -1,31 +0,0 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Lock\Listeners;
use Flarum\Events\ApiAttributes;
use Illuminate\Contracts\Events\Dispatcher;
use Flarum\Api\Serializers\DiscussionSerializer;
class AddApiAttributes
{
public function subscribe(Dispatcher $events)
{
$events->listen(ApiAttributes::class, [$this, 'addAttributes']);
}
public function addAttributes(ApiAttributes $event)
{
if ($event->serializer instanceof DiscussionSerializer) {
$event->attributes['isLocked'] = (bool) $event->model->is_locked;
$event->attributes['canLock'] = (bool) $event->model->can($event->actor, 'lock');
}
}
}

View File

@@ -1,55 +0,0 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Lock\Listeners;
use Flarum\Events\RegisterLocales;
use Flarum\Events\BuildClientView;
use Illuminate\Contracts\Events\Dispatcher;
class AddClientAssets
{
public function subscribe(Dispatcher $events)
{
$events->listen(RegisterLocales::class, [$this, 'addLocale']);
$events->listen(BuildClientView::class, [$this, 'addAssets']);
}
public function addLocale(RegisterLocales $event)
{
$event->addTranslations('en', __DIR__.'/../../locale/en.yml');
}
public function addAssets(BuildClientView $event)
{
$event->forumAssets([
__DIR__.'/../../js/forum/dist/extension.js',
__DIR__.'/../../less/forum/extension.less'
]);
$event->forumBootstrapper('lock/main');
$event->forumTranslations([
'lock.discussion_locked_notification',
'lock.discussion_locked_post',
'lock.discussion_unlocked_post',
'lock.notify_discussion_locked',
'lock.locked',
'lock.lock',
'lock.unlock'
]);
$event->adminAssets([
__DIR__.'/../../js/admin/dist/extension.js'
]);
$event->adminBootstrapper('lock/main');
}
}

View File

@@ -1,33 +0,0 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Lock\Listeners;
use Flarum\Events\ModelAllow;
use Flarum\Core\Discussions\Discussion;
class ConfigurePermissions
{
public function subscribe($events)
{
$events->listen(ModelAllow::class, [$this, 'allowDiscussionPermissions'], 10);
}
public function allowDiscussionPermissions(ModelAllow $event)
{
if ($event->model instanceof Discussion &&
$event->model->is_locked &&
$event->action === 'reply') {
if (! $event->model->can($event->actor, 'lock')) {
return false;
}
}
}
}

View File

@@ -1,81 +0,0 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Lock\Listeners;
use Flarum\Events\RegisterPostTypes;
use Flarum\Events\RegisterNotificationTypes;
use Flarum\Lock\Posts\DiscussionLockedPost;
use Flarum\Lock\Notifications\DiscussionLockedBlueprint;
use Flarum\Lock\Events\DiscussionWasLocked;
use Flarum\Lock\Events\DiscussionWasUnlocked;
use Flarum\Core\Notifications\NotificationSyncer;
use Flarum\Core\Discussions\Discussion;
use Flarum\Core\Users\User;
use Illuminate\Contracts\Events\Dispatcher;
class NotifyDiscussionLocked
{
protected $notifications;
public function __construct(NotificationSyncer $notifications)
{
$this->notifications = $notifications;
}
public function subscribe(Dispatcher $events)
{
$events->listen(RegisterPostTypes::class, [$this, 'registerPostType']);
$events->listen(RegisterNotificationTypes::class, [$this, 'registerNotificationType']);
$events->listen(DiscussionWasLocked::class, [$this, 'whenDiscussionWasLocked']);
$events->listen(DiscussionWasUnlocked::class, [$this, 'whenDiscussionWasUnlocked']);
}
public function registerPostType(RegisterPostTypes $event)
{
$event->register('Flarum\Lock\Posts\DiscussionLockedPost');
}
public function registerNotificationType(RegisterNotificationTypes $event)
{
$event->register(
'Flarum\Lock\Notifications\DiscussionLockedBlueprint',
'Flarum\Api\Serializers\DiscussionBasicSerializer',
['alert']
);
}
public function whenDiscussionWasLocked(DiscussionWasLocked $event)
{
$this->stickyChanged($event->discussion, $event->user, true);
}
public function whenDiscussionWasUnlocked(DiscussionWasUnlocked $event)
{
$this->stickyChanged($event->discussion, $event->user, false);
}
protected function stickyChanged(Discussion $discussion, User $user, $isLocked)
{
$post = DiscussionLockedPost::reply(
$discussion->id,
$user->id,
$isLocked
);
$post = $discussion->mergePost($post);
if ($discussion->start_user_id !== $user->id) {
$notification = new DiscussionLockedBlueprint($post);
$this->notifications->sync($notification, $post->exists ? [$discussion->startUser] : []);
}
}
}

View File

@@ -8,42 +8,64 @@
* file that was distributed with this source code.
*/
namespace Flarum\Lock\Notifications;
namespace Flarum\Lock\Notification;
use Flarum\Lock\Posts\DiscussionLockedPost;
use Flarum\Core\Notifications\Blueprint;
use Flarum\Core\Discussion;
use Flarum\Core\Notification\BlueprintInterface;
use Flarum\Lock\Post\DiscussionLockedPost;
class DiscussionLockedBlueprint implements Blueprint
class DiscussionLockedBlueprint implements BlueprintInterface
{
/**
* @var DiscussionLockedPost
*/
protected $post;
/**
* @param DiscussionLockedPost $post
*/
public function __construct(DiscussionLockedPost $post)
{
$this->post = $post;
}
/**
* {@inheritdoc}
*/
public function getSender()
{
return $this->post->user;
}
/**
* {@inheritdoc}
*/
public function getSubject()
{
return $this->post->discussion;
}
/**
* {@inheritdoc}
*/
public function getData()
{
return ['postNumber' => (int) $this->post->number];
}
/**
* {@inheritdoc}
*/
public static function getType()
{
return 'discussionLocked';
}
/**
* {@inheritdoc}
*/
public static function getSubjectModel()
{
return 'Flarum\Core\Discussions\Discussion';
return Discussion::class;
}
}

View File

@@ -8,16 +8,22 @@
* file that was distributed with this source code.
*/
namespace Flarum\Lock\Posts;
namespace Flarum\Lock\Post;
use Flarum\Core\Posts\Post;
use Flarum\Core\Posts\EventPost;
use Flarum\Core\Posts\MergeablePost;
use Flarum\Core\Post;
use Flarum\Core\Post\AbstractEventPost;
use Flarum\Core\Post\MergeableInterface;
class DiscussionLockedPost extends EventPost implements MergeablePost
class DiscussionLockedPost extends AbstractEventPost implements MergeableInterface
{
/**
* {@inheritdoc}
*/
public static $type = 'discussionLocked';
/**
* {@inheritdoc}
*/
public function saveAfter(Post $previous)
{
// If the previous post is another 'discussion locked' post, and it's