Skip to content

Commit

Permalink
Use a notification driver with the new channel extender (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
SychO9 authored Nov 5, 2020
1 parent 7bdddae commit 80ba5fe
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 39 deletions.
6 changes: 4 additions & 2 deletions extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@

use Flarum\Api\Event\Serializing;
use Flarum\Extend;
use Flarum\Notification\Event\Sending;
use Flarum\Post\Event\Posted;
use Flarum\Pusher\Api\Controller\AuthController;
use Flarum\Pusher\Listener;
use Flarum\Pusher\PusherNotificationDriver;
use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Events\Dispatcher;
Expand All @@ -30,6 +30,9 @@

new Extend\Locales(__DIR__.'/locale'),

(new Extend\Notification())
->driver('pusher', PusherNotificationDriver::class),

function (Dispatcher $events, Container $container) {
$container->bind(Pusher::class, function ($app) {
$settings = $app->make(SettingsRepositoryInterface::class);
Expand All @@ -49,7 +52,6 @@ function (Dispatcher $events, Container $container) {
});

$events->listen(Posted::class, Listener\PushNewPost::class);
$events->listen(Sending::class, Listener\PushNotification::class);
$events->listen(Serializing::class, Listener\AddPusherApi::class);
},
];
37 changes: 0 additions & 37 deletions src/Listener/PushNotification.php

This file was deleted.

45 changes: 45 additions & 0 deletions src/PusherNotificationDriver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/

namespace Flarum\Pusher;

use Flarum\Notification\Blueprint\BlueprintInterface;
use Flarum\Notification\Driver\NotificationDriverInterface;
use Illuminate\Contracts\Queue\Queue;

class PusherNotificationDriver implements NotificationDriverInterface
{
/**
* @var Queue
*/
protected $queue;

public function __construct(Queue $queue)
{
$this->queue = $queue;
}

/**
* {@inheritDoc}
*/
public function send(BlueprintInterface $blueprint, array $users): void
{
if (count($users)) {
$this->queue->push(new SendPusherNotificationsJob($blueprint, $users));
}
}

/**
* {@inheritDoc}
*/
public function registerType(string $blueprintClass, array $driversEnabledByDefault): void
{
// ...
}
}
43 changes: 43 additions & 0 deletions src/SendPusherNotificationsJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/

namespace Flarum\Pusher;

use Flarum\Notification\Blueprint\BlueprintInterface;
use Flarum\Queue\AbstractJob;
use Flarum\User\User;
use Pusher;

class SendPusherNotificationsJob extends AbstractJob
{
/**
* @var BlueprintInterface
*/
private $blueprint;

/**
* @var User[]
*/
private $recipients;

public function __construct(BlueprintInterface $blueprint, array $recipients)
{
$this->blueprint = $blueprint;
$this->recipients = $recipients;
}

public function handle(Pusher $pusher)
{
foreach ($this->recipients as $user) {
if ($user->shouldAlert($this->blueprint::getType())) {
$pusher->trigger('private-user'.$user->id, 'notification', null);
}
}
}
}

0 comments on commit 80ba5fe

Please sign in to comment.