Queue 'new post' notification

Fixes flarum/core#1869
This commit is contained in:
David Sevilla Martin 2019-10-05 16:16:41 -04:00 committed by Daniël Klabbers
parent 82978c57ce
commit 9f3d6a9a1f
2 changed files with 61 additions and 21 deletions

View File

@ -0,0 +1,53 @@
<?php
namespace Flarum\Subscriptions\Job;
use Flarum\Notification\NotificationSyncer;
use Flarum\Post\Post;
use Flarum\Subscriptions\Notification\NewPostBlueprint;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\SerializesModels;
class SendReplyNotification implements ShouldQueue
{
use Queueable, SerializesModels;
/**
* @var Post
*/
protected $post;
/**
* @var int
*/
protected $lastPostNumber;
/**
* @param Post $post
* @param int|null $lastPostNumber
*/
public function __construct(Post $post, $lastPostNumber)
{
$this->post = $post;
$this->lastPostNumber = $lastPostNumber;
}
public function handle(NotificationSyncer $notifications) {
$post = $this->post;
$discussion = $post->discussion;
$notify = $discussion->readers()
->where('users.id', '!=', $post->user_id)
->where('discussion_user.subscription', 'follow')
->where('discussion_user.last_read_post_number', $this->lastPostNumber)
->get();
$notifications->sync(
new NewPostBlueprint($post),
$notify->all()
);
}
}

View File

@ -9,39 +9,26 @@
namespace Flarum\Subscriptions\Listener;
use Flarum\Notification\NotificationSyncer;
use Flarum\Post\Event\Posted;
use Flarum\Subscriptions\Notification\NewPostBlueprint;
use Flarum\Subscriptions\Job\SendReplyNotification;
use Illuminate\Contracts\Queue\Queue;
class SendNotificationWhenReplyIsPosted
{
/**
* @var NotificationSyncer
* @var Queue
*/
protected $notifications;
protected $queue;
/**
* @param NotificationSyncer $notifications
*/
public function __construct(NotificationSyncer $notifications)
public function __construct(Queue $queue)
{
$this->notifications = $notifications;
$this->queue = $queue;
}
public function handle(Posted $event)
{
$post = $event->post;
$discussion = $post->discussion;
$notify = $discussion->readers()
->where('users.id', '!=', $post->user_id)
->where('discussion_user.subscription', 'follow')
->where('discussion_user.last_read_post_number', $discussion->last_post_number)
->get();
$this->notifications->sync(
new NewPostBlueprint($event->post),
$notify->all()
$this->queue->push(
new SendReplyNotification($event->post, $event->post->discussion->last_post_number)
);
}
}