1
0
mirror of https://github.com/flarum/core.git synced 2025-07-31 13:40:20 +02:00

Use a notification driver with the new channel extender (#28)

This commit is contained in:
Sami Mazouz
2020-11-05 18:09:13 +01:00
committed by GitHub
parent 1c63326ad5
commit bf6dd3083d
4 changed files with 92 additions and 39 deletions

View File

@@ -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;
@@ -30,6 +30,9 @@ return [
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);
@@ -49,7 +52,6 @@ return [
});
$events->listen(Posted::class, Listener\PushNewPost::class);
$events->listen(Sending::class, Listener\PushNotification::class);
$events->listen(Serializing::class, Listener\AddPusherApi::class);
},
];

View File

@@ -1,37 +0,0 @@
<?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\Listener;
use Flarum\Notification\Event\Sending;
use Pusher;
class PushNotification
{
/**
* @var Pusher
*/
protected $pusher;
public function __construct(Pusher $pusher)
{
$this->pusher = $pusher;
}
public function handle(Sending $event)
{
$blueprint = $event->blueprint;
foreach ($event->users as $user) {
if ($user->shouldAlert($blueprint::getType())) {
$this->pusher->trigger('private-user'.$user->id, 'notification', null);
}
}
}
}

View File

@@ -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
{
// ...
}
}

View File

@@ -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);
}
}
}
}