Skip to content

Commit

Permalink
Queue 'new post' notification
Browse files Browse the repository at this point in the history
  • Loading branch information
dsevillamartin authored and luceos committed Apr 1, 2020
1 parent 4fd7fb4 commit 349102d
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 21 deletions.
53 changes: 53 additions & 0 deletions src/Job/SendReplyNotification.php
Original file line number Diff line number Diff line change
@@ -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()
);
}
}
29 changes: 8 additions & 21 deletions src/Listener/SendNotificationWhenReplyIsPosted.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
}
}

0 comments on commit 349102d

Please sign in to comment.