mirror of
https://github.com/flarum/core.git
synced 2025-08-21 07:41:49 +02:00
Initial commit
This commit is contained in:
72
extensions/subscriptions/src/Handlers/NewPostNotifier.php
Executable file
72
extensions/subscriptions/src/Handlers/NewPostNotifier.php
Executable file
@@ -0,0 +1,72 @@
|
||||
<?php namespace Flarum\Subscriptions\Handlers;
|
||||
|
||||
use Flarum\Subscriptions\NewPostNotification;
|
||||
use Flarum\Core\Events\PostWasPosted;
|
||||
use Flarum\Core\Events\PostWasHidden;
|
||||
use Flarum\Core\Events\PostWasRestored;
|
||||
use Flarum\Core\Events\PostWasDeleted;
|
||||
use Flarum\Core\Notifications\NotificationSyncer;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
|
||||
class NewPostNotifier
|
||||
{
|
||||
protected $notifications;
|
||||
|
||||
public function __construct(NotificationSyncer $notifications)
|
||||
{
|
||||
$this->notifications = $notifications;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the listeners for the subscriber.
|
||||
*
|
||||
* @param \Illuminate\Contracts\Events\Dispatcher $events
|
||||
*/
|
||||
public function subscribe(Dispatcher $events)
|
||||
{
|
||||
// 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
|
||||
// of the previous post.
|
||||
$events->listen('Flarum\Core\Events\PostWasPosted', __CLASS__.'@whenPostWasPosted', 1);
|
||||
$events->listen('Flarum\Core\Events\PostWasHidden', __CLASS__.'@whenPostWasHidden');
|
||||
$events->listen('Flarum\Core\Events\PostWasRestored', __CLASS__.'@whenPostWasRestored');
|
||||
$events->listen('Flarum\Core\Events\PostWasDeleted', __CLASS__.'@whenPostWasDeleted');
|
||||
}
|
||||
|
||||
public function whenPostWasPosted(PostWasPosted $event)
|
||||
{
|
||||
$post = $event->post;
|
||||
$discussion = $post->discussion;
|
||||
|
||||
$notify = $discussion->readers()
|
||||
->where('users.id', '!=', $post->user_id)
|
||||
->where('users_discussions.subscription', 'follow')
|
||||
->where('users_discussions.read_number', $discussion->last_post_number)
|
||||
->get();
|
||||
|
||||
$this->notifications->sync(
|
||||
$this->getNotification($event->post),
|
||||
$notify->all()
|
||||
);
|
||||
}
|
||||
|
||||
public function whenPostWasHidden(PostWasHidden $event)
|
||||
{
|
||||
$this->notifications->delete($this->getNotification($event->post));
|
||||
}
|
||||
|
||||
public function whenPostWasRestored(PostWasRestored $event)
|
||||
{
|
||||
$this->notifications->restore($this->getNotification($event->post));
|
||||
}
|
||||
|
||||
public function whenPostWasDeleted(PostWasDeleted $event)
|
||||
{
|
||||
$this->notifications->delete($this->getNotification($event->post));
|
||||
}
|
||||
|
||||
protected function getNotification($post)
|
||||
{
|
||||
return new NewPostNotification($post);
|
||||
}
|
||||
}
|
31
extensions/subscriptions/src/Handlers/SubscriptionSaver.php
Executable file
31
extensions/subscriptions/src/Handlers/SubscriptionSaver.php
Executable file
@@ -0,0 +1,31 @@
|
||||
<?php namespace Flarum\Subscriptions\Handlers;
|
||||
|
||||
use Flarum\Core\Events\DiscussionWillBeSaved;
|
||||
|
||||
class SubscriptionSaver
|
||||
{
|
||||
public function subscribe($events)
|
||||
{
|
||||
$events->listen('Flarum\Core\Events\DiscussionWillBeSaved', __CLASS__.'@whenDiscussionWillBeSaved');
|
||||
}
|
||||
|
||||
public function whenDiscussionWillBeSaved(DiscussionWillBeSaved $event)
|
||||
{
|
||||
$discussion = $event->discussion;
|
||||
$data = $event->command->data;
|
||||
|
||||
if (isset($data['subscription'])) {
|
||||
$user = $event->command->user;
|
||||
$subscription = $data['subscription'];
|
||||
|
||||
$state = $discussion->stateFor($user);
|
||||
|
||||
if (! in_array($subscription, ['follow', 'ignore'])) {
|
||||
$subscription = null;
|
||||
}
|
||||
|
||||
$state->subscription = $subscription;
|
||||
$state->save();
|
||||
}
|
||||
}
|
||||
}
|
26
extensions/subscriptions/src/Handlers/SubscriptionSearchModifier.php
Executable file
26
extensions/subscriptions/src/Handlers/SubscriptionSearchModifier.php
Executable file
@@ -0,0 +1,26 @@
|
||||
<?php namespace Flarum\Subscriptions\Handlers;
|
||||
|
||||
use Flarum\Core\Events\DiscussionSearchWillBePerformed;
|
||||
|
||||
class SubscriptionSearchModifier
|
||||
{
|
||||
public function subscribe($events)
|
||||
{
|
||||
$events->listen('Flarum\Core\Events\DiscussionSearchWillBePerformed', __CLASS__.'@filterIgnored');
|
||||
}
|
||||
|
||||
public function filterIgnored(DiscussionSearchWillBePerformed $event)
|
||||
{
|
||||
if (! $event->criteria->query) {
|
||||
// might be better as `id IN (subquery)`?
|
||||
$user = $event->criteria->user;
|
||||
$event->searcher->getQuery()->whereNotExists(function ($query) use ($user) {
|
||||
$query->select(app('db')->raw(1))
|
||||
->from('users_discussions')
|
||||
->whereRaw('discussion_id = discussions.id')
|
||||
->where('user_id', $user->id)
|
||||
->where('subscription', 'ignore');
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
55
extensions/subscriptions/src/NewPostNotification.php
Normal file
55
extensions/subscriptions/src/NewPostNotification.php
Normal file
@@ -0,0 +1,55 @@
|
||||
<?php namespace Flarum\Subscriptions;
|
||||
|
||||
use Flarum\Core\Models\Post;
|
||||
use Flarum\Core\Models\User;
|
||||
use Flarum\Core\Notifications\NotificationAbstract;
|
||||
|
||||
class NewPostNotification extends NotificationAbstract
|
||||
{
|
||||
public $post;
|
||||
|
||||
public function __construct(Post $post)
|
||||
{
|
||||
$this->post = $post;
|
||||
}
|
||||
|
||||
public function getSubject()
|
||||
{
|
||||
return $this->post->discussion;
|
||||
}
|
||||
|
||||
public function getSender()
|
||||
{
|
||||
return $this->post->user;
|
||||
}
|
||||
|
||||
public function getData()
|
||||
{
|
||||
return ['postNumber' => (int) $this->post->number];
|
||||
}
|
||||
|
||||
public function getEmailView()
|
||||
{
|
||||
return ['text' => 'flarum-subscriptions::emails.newPost'];
|
||||
}
|
||||
|
||||
public function getEmailSubject()
|
||||
{
|
||||
return '[New Post] '.$this->post->discussion->title;
|
||||
}
|
||||
|
||||
public static function getType()
|
||||
{
|
||||
return 'newPost';
|
||||
}
|
||||
|
||||
public static function getSubjectModel()
|
||||
{
|
||||
return 'Flarum\Core\Models\Discussion';
|
||||
}
|
||||
|
||||
public static function isEmailable()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
37
extensions/subscriptions/src/SubscriptionGambit.php
Normal file
37
extensions/subscriptions/src/SubscriptionGambit.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php namespace Flarum\Subscriptions;
|
||||
|
||||
use Flarum\Core\Search\SearcherInterface;
|
||||
use Flarum\Core\Search\GambitAbstract;
|
||||
|
||||
class SubscriptionGambit extends GambitAbstract
|
||||
{
|
||||
/**
|
||||
* The gambit's regex pattern.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $pattern = 'is:(follow|ignor)(?:ing|ed)';
|
||||
|
||||
/**
|
||||
* Apply conditions to the searcher, given matches from the gambit's
|
||||
* regex.
|
||||
*
|
||||
* @param array $matches The matches from the gambit's regex.
|
||||
* @param \Flarum\Core\Search\SearcherInterface $searcher
|
||||
* @return void
|
||||
*/
|
||||
protected function conditions(SearcherInterface $searcher, array $matches, $negate)
|
||||
{
|
||||
$user = $searcher->getUser();
|
||||
|
||||
// might be better as `id IN (subquery)`?
|
||||
$method = $negate ? 'whereNotExists' : 'whereExists';
|
||||
$searcher->getQuery()->$method(function ($query) use ($user, $matches) {
|
||||
$query->select(app('db')->raw(1))
|
||||
->from('users_discussions')
|
||||
->whereRaw('discussion_id = discussions.id')
|
||||
->where('user_id', $user->id)
|
||||
->where('subscription', $matches[1] === 'follow' ? 'follow' : 'ignore');
|
||||
});
|
||||
}
|
||||
}
|
@@ -0,0 +1,59 @@
|
||||
<?php namespace Flarum\Subscriptions;
|
||||
|
||||
use Flarum\Support\ServiceProvider;
|
||||
use Flarum\Extend;
|
||||
|
||||
class SubscriptionsServiceProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
* Bootstrap the application events.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function boot()
|
||||
{
|
||||
$this->loadViewsFrom(__DIR__.'/../views', 'flarum-subscriptions');
|
||||
|
||||
$this->extend([
|
||||
(new Extend\Locale('en'))->translations(__DIR__.'/../locale/en.yml'),
|
||||
|
||||
(new Extend\ForumClient())
|
||||
->assets([
|
||||
__DIR__.'/../js/dist/extension.js',
|
||||
__DIR__.'/../less/extension.less'
|
||||
])
|
||||
->translations([
|
||||
// Add the keys of translations you would like to be available
|
||||
// for use by the JS client application.
|
||||
])
|
||||
->route('get', '/following', 'flarum.forum.following'),
|
||||
|
||||
(new Extend\ApiSerializer('Flarum\Api\Serializers\DiscussionSerializer'))
|
||||
->attributes(function (&$attributes, $discussion, $user) {
|
||||
if ($state = $discussion->stateFor($user)) {
|
||||
$attributes['subscription'] = $state->subscription ?: false;
|
||||
}
|
||||
}),
|
||||
|
||||
new Extend\EventSubscriber('Flarum\Subscriptions\Handlers\SubscriptionSaver'),
|
||||
new Extend\EventSubscriber('Flarum\Subscriptions\Handlers\SubscriptionSearchModifier'),
|
||||
new Extend\EventSubscriber('Flarum\Subscriptions\Handlers\NewPostNotifier'),
|
||||
|
||||
new Extend\DiscussionGambit('Flarum\Subscriptions\SubscriptionGambit'),
|
||||
|
||||
(new Extend\NotificationType('Flarum\Subscriptions\NewPostNotification', 'Flarum\Api\Serializers\DiscussionBasicSerializer'))
|
||||
->enableByDefault('alert')
|
||||
->enableByDefault('email')
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the service provider.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user