Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a notification driver with the new channel extender #28

Merged
merged 4 commits into from
Nov 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
}
}