1
0
mirror of https://github.com/flarum/core.git synced 2025-08-20 15:21:49 +02:00

Update for composer branch

This commit is contained in:
Toby Zerner
2015-10-11 17:30:11 +10:30
parent 0746d0aee3
commit fde7921487
27 changed files with 731 additions and 290 deletions

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\Subscriptions;
use Flarum\Support\Extension as BaseExtension;
use Illuminate\Events\Dispatcher;
class Extension extends BaseExtension
{
public function listen(Dispatcher $events)
{
$events->subscribe('Flarum\Subscriptions\Listeners\AddClientAssets');
$events->subscribe('Flarum\Subscriptions\Listeners\AddApiAttributes');
$events->subscribe('Flarum\Subscriptions\Listeners\PersistSubscriptionData');
$events->subscribe('Flarum\Subscriptions\Listeners\NotifyNewPosts');
$events->subscribe('Flarum\Subscriptions\Listeners\HideIgnoredDiscussions');
}
public function boot()
{
$this->loadViewsFrom(__DIR__.'/../views', 'subscriptions');
}
}

View File

@@ -8,17 +8,23 @@
* file that was distributed with this source code.
*/
namespace Flarum\Subscriptions\Gambits;
namespace Flarum\Subscriptions\Gambit;
use Flarum\Core\Search\Search;
use Flarum\Core\Search\RegexGambit;
use Flarum\Core\Search\AbstractRegexGambit;
use Flarum\Core\Search\AbstractSearch;
use Illuminate\Database\Query\Expression;
class SubscriptionGambit extends RegexGambit
class SubscriptionGambit extends AbstractRegexGambit
{
/**
* {@inheritdoc}
*/
protected $pattern = 'is:(follow|ignor)(?:ing|ed)';
protected function conditions(Search $search, array $matches, $negate)
/**
* {@inheritdoc}
*/
protected function conditions(AbstractSearch $search, array $matches, $negate)
{
$actor = $search->getActor();

View File

@@ -0,0 +1,40 @@
<?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\Subscriptions\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/subscriptions/main');
$event->addTranslations('flarum-subscriptions.forum');
}
}
}

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\Subscriptions\Listener;
use Flarum\Api\Serializer\DiscussionSerializer;
use Flarum\Event\PrepareApiAttributes;
use Illuminate\Contracts\Events\Dispatcher;
class AddDiscussionSubscriptionAttribute
{
/**
* @param Dispatcher $events
*/
public function subscribe(Dispatcher $events)
{
$events->listen(PrepareApiAttributes::class, [$this, 'addAttributes']);
}
/**
* @param PrepareApiAttributes $event
*/
public function addAttributes(PrepareApiAttributes $event)
{
if ($event->isSerializer(DiscussionSerializer::class)
&& ($state = $event->model->state)) {
$event->attributes['subscription'] = $state->subscription ?: false;
}
}
}

View File

@@ -0,0 +1,65 @@
<?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\Subscriptions\Listener;
use Flarum\Event\ConfigureDiscussionGambits;
use Flarum\Event\ConfigureDiscussionSearch;
use Flarum\Event\ConfigureForumRoutes;
use Flarum\Subscriptions\Gambit\SubscriptionGambit;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Database\Query\Expression;
class FilterDiscussionListBySubscription
{
/**
* @param Dispatcher $events
*/
public function subscribe(Dispatcher $events)
{
$events->listen(ConfigureDiscussionGambits::class, [$this, 'addGambit']);
$events->listen(ConfigureDiscussionSearch::class, [$this, 'filterIgnored']);
$events->listen(ConfigureForumRoutes::class, [$this, 'addRoutes']);
}
/**
* @param ConfigureDiscussionGambits $event
*/
public function addGambit(ConfigureDiscussionGambits $event)
{
$event->gambits->add(SubscriptionGambit::class);
}
/**
* @param ConfigureDiscussionSearch $event
*/
public function filterIgnored(ConfigureDiscussionSearch $event)
{
if (! $event->criteria->query) {
// might be better as `id IN (subquery)`?
$actor = $event->search->getActor();
$event->search->getQuery()->whereNotExists(function ($query) use ($actor) {
$query->selectRaw(1)
->from('users_discussions')
->where('discussions.id', new Expression('discussion_id'))
->where('user_id', $actor->id)
->where('subscription', 'ignore');
});
}
}
/**
* @param ConfigureForumRoutes $event
*/
public function addRoutes(ConfigureForumRoutes $event)
{
$event->get('/following', 'following');
}
}

View File

@@ -8,18 +8,27 @@
* file that was distributed with this source code.
*/
namespace Flarum\Subscriptions\Listeners;
namespace Flarum\Subscriptions\Listener;
use Flarum\Events\DiscussionWillBeSaved;
use Flarum\Core\Exceptions\PermissionDeniedException;
use Flarum\Core\Access\AssertPermissionTrait;
use Flarum\Event\DiscussionWillBeSaved;
use Illuminate\Contracts\Events\Dispatcher;
class PersistSubscriptionData
class SaveSubscriptionToDatabase
{
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)
{
$discussion = $event->discussion;
@@ -29,9 +38,7 @@ class PersistSubscriptionData
$actor = $event->actor;
$subscription = $data['attributes']['subscription'];
if (! $actor->exists) {
throw new PermissionDeniedException;
}
$this->assertRegistered($actor);
$state = $discussion->stateFor($actor);

View File

@@ -8,29 +8,40 @@
* file that was distributed with this source code.
*/
namespace Flarum\Subscriptions\Listeners;
namespace Flarum\Subscriptions\Listener;
use Flarum\Subscriptions\Notifications\NewPostBlueprint;
use Flarum\Events\RegisterNotificationTypes;
use Flarum\Events\PostWasPosted;
use Flarum\Events\PostWasHidden;
use Flarum\Events\PostWasRestored;
use Flarum\Events\PostWasDeleted;
use Flarum\Core\Notifications\NotificationSyncer;
use Flarum\Api\Serializer\DiscussionBasicSerializer;
use Flarum\Core\Notification\NotificationSyncer;
use Flarum\Core\Post;
use Flarum\Event\ConfigureNotificationTypes;
use Flarum\Event\PostWasDeleted;
use Flarum\Event\PostWasHidden;
use Flarum\Event\PostWasPosted;
use Flarum\Event\PostWasRestored;
use Flarum\Subscriptions\Notification\NewPostBlueprint;
use Illuminate\Contracts\Events\Dispatcher;
class NotifyNewPosts
class SendNotificationWhenReplyIsPosted
{
/**
* @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(RegisterNotificationTypes::class, [$this, 'addNotificationType']);
$events->listen(ConfigureNotificationTypes::class, [$this, 'addNotificationType']);
// Register with '1' as priority so this runs before discussion metadata
// is updated, as we need to compare the user's last read number to that
@@ -41,15 +52,17 @@ class NotifyNewPosts
$events->listen(PostWasDeleted::class, [$this, 'whenPostWasDeleted']);
}
public function addNotificationType(RegisterNotificationTypes $event)
/**
* @param ConfigureNotificationTypes $event
*/
public function addNotificationType(ConfigureNotificationTypes $event)
{
$event->register(
NewPostBlueprint::class,
'Flarum\Api\Serializers\DiscussionBasicSerializer',
['alert', 'email']
);
$event->add(NewPostBlueprint::class, DiscussionBasicSerializer::class, ['alert', 'email']);
}
/**
* @param PostWasPosted $event
*/
public function whenPostWasPosted(PostWasPosted $event)
{
$post = $event->post;
@@ -67,22 +80,35 @@ class NotifyNewPosts
);
}
/**
* @param PostWasHidden $event
*/
public function whenPostWasHidden(PostWasHidden $event)
{
$this->notifications->delete($this->getNotification($event->post));
}
/**
* @param PostWasRestored $event
*/
public function whenPostWasRestored(PostWasRestored $event)
{
$this->notifications->restore($this->getNotification($event->post));
}
/**
* @param PostWasDeleted $event
*/
public function whenPostWasDeleted(PostWasDeleted $event)
{
$this->notifications->delete($this->getNotification($event->post));
}
protected function getNotification($post)
/**
* @param Post $post
* @return NewPostBlueprint
*/
protected function getNotification(Post $post)
{
return new NewPostBlueprint($post);
}

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\Subscriptions\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 &&
($state = $event->model->state)) {
$event->attributes['subscription'] = $state->subscription ?: false;
}
}
}

View File

@@ -1,61 +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\Subscriptions\Listeners;
use Flarum\Events\RegisterLocales;
use Flarum\Events\BuildClientView;
use Flarum\Events\RegisterForumRoutes;
use Illuminate\Contracts\Events\Dispatcher;
class AddClientAssets
{
public function subscribe(Dispatcher $events)
{
$events->listen(RegisterLocales::class, [$this, 'addLocale']);
$events->listen(BuildClientView::class, [$this, 'addAssets']);
$events->listen(RegisterForumRoutes::class, [$this, 'addRoutes']);
}
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('subscriptions/main');
$event->forumTranslations([
'subscriptions.following',
'subscriptions.ignoring',
'subscriptions.follow',
'subscriptions.unfollow',
'subscriptions.ignore',
'subscriptions.notify_new_post',
'subscriptions.new_post_notification',
'subscriptions.not_following',
'subscriptions.not_following_description',
'subscriptions.following_description',
'subscriptions.ignoring_description',
'subscriptions.unignore'
]);
}
public function addRoutes(RegisterForumRoutes $event)
{
$event->get('/following', 'flarum.forum.following');
}
}

View File

@@ -1,44 +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\Subscriptions\Listeners;
use Flarum\Events\RegisterDiscussionGambits;
use Flarum\Events\DiscussionSearchWillBePerformed;
use Illuminate\Database\Query\Expression;
class HideIgnoredDiscussions
{
public function subscribe($events)
{
$events->listen(RegisterDiscussionGambits::class, [$this, 'addGambit']);
$events->listen(DiscussionSearchWillBePerformed::class, [$this, 'filterIgnored']);
}
public function addGambit(RegisterDiscussionGambits $event)
{
$event->gambits->add('Flarum\Subscriptions\Gambits\SubscriptionGambit');
}
public function filterIgnored(DiscussionSearchWillBePerformed $event)
{
if (! $event->criteria->query) {
// might be better as `id IN (subquery)`?
$actor = $event->search->getActor();
$event->search->getQuery()->whereNotExists(function ($query) use ($actor) {
$query->selectRaw(1)
->from('users_discussions')
->where('discussions.id', new Expression('discussion_id'))
->where('user_id', $actor->id)
->where('subscription', 'ignore');
});
}
}
}

View File

@@ -8,54 +8,81 @@
* file that was distributed with this source code.
*/
namespace Flarum\Subscriptions\Notifications;
namespace Flarum\Subscriptions\Notification;
use Flarum\Core\Posts\Post;
use Flarum\Core\Users\User;
use Flarum\Core\Notifications\Blueprint;
use Flarum\Core\Notifications\MailableBlueprint;
use Flarum\Core\Discussion;
use Flarum\Core\Post;
use Flarum\Core\Notification\BlueprintInterface;
use Flarum\Core\Notification\MailableInterface;
class NewPostBlueprint implements Blueprint, MailableBlueprint
class NewPostBlueprint implements BlueprintInterface, MailableInterface
{
/**
* @var Post
*/
public $post;
/**
* @param Post $post
*/
public function __construct(Post $post)
{
$this->post = $post;
}
/**
* {@inheritdoc}
*/
public function getSubject()
{
return $this->post->discussion;
}
/**
* {@inheritdoc}
*/
public function getSender()
{
return $this->post->user;
}
/**
* {@inheritdoc}
*/
public function getData()
{
return ['postNumber' => (int) $this->post->number];
}
/**
* {@inheritdoc}
*/
public function getEmailView()
{
return ['text' => 'subscriptions::emails.newPost'];
return ['text' => 'flarum-subscriptions::emails.newPost'];
}
/**
* {@inheritdoc}
*/
public function getEmailSubject()
{
return '[New Post] '.$this->post->discussion->title;
}
/**
* {@inheritdoc}
*/
public static function getType()
{
return 'newPost';
}
/**
* {@inheritdoc}
*/
public static function getSubjectModel()
{
return 'Flarum\Core\Discussions\Discussion';
return Discussion::class;
}
}