mirror of
https://github.com/flarum/core.git
synced 2025-08-08 17:36:38 +02:00
Implement push notifications
This commit is contained in:
37
extensions/pusher/src/Api/AuthAction.php
Normal file
37
extensions/pusher/src/Api/AuthAction.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php namespace Flarum\Pusher\Api;
|
||||
|
||||
use Flarum\Api\Actions\JsonApiAction;
|
||||
use Flarum\Api\Request;
|
||||
use Flarum\Core\Settings\SettingsRepository;
|
||||
use Zend\Diactoros\Response\JsonResponse;
|
||||
use Zend\Diactoros\Response\EmptyResponse;
|
||||
use Pusher;
|
||||
|
||||
class AuthAction extends JsonApiAction
|
||||
{
|
||||
protected $settings;
|
||||
|
||||
public function __construct(SettingsRepository $settings)
|
||||
{
|
||||
$this->settings = $settings;
|
||||
}
|
||||
|
||||
protected function respond(Request $request)
|
||||
{
|
||||
$userChannel = 'private-user' . $request->actor->id;
|
||||
|
||||
if ($request->get('channel_name') === $userChannel) {
|
||||
$pusher = new Pusher(
|
||||
$this->settings->get('pusher.app_key'),
|
||||
$this->settings->get('pusher.app_secret'),
|
||||
$this->settings->get('pusher.app_id')
|
||||
);
|
||||
|
||||
$payload = json_decode($pusher->socket_auth($userChannel, $request->get('socket_id')), true);
|
||||
|
||||
return new JsonResponse($payload);
|
||||
}
|
||||
|
||||
return new EmptyResponse(403);
|
||||
}
|
||||
}
|
@@ -1,6 +1,7 @@
|
||||
<?php namespace Flarum\Pusher\Listeners;
|
||||
|
||||
use Flarum\Events\ApiAttributes;
|
||||
use Flarum\Events\RegisterApiRoutes;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
use Flarum\Api\Serializers\ForumSerializer;
|
||||
|
||||
@@ -9,6 +10,7 @@ class AddApiAttributes
|
||||
public function subscribe(Dispatcher $events)
|
||||
{
|
||||
$events->listen(ApiAttributes::class, [$this, 'addAttributes']);
|
||||
$events->listen(RegisterApiRoutes::class, [$this, 'addRoutes']);
|
||||
}
|
||||
|
||||
public function addAttributes(ApiAttributes $event)
|
||||
@@ -17,4 +19,9 @@ class AddApiAttributes
|
||||
$event->attributes['pusherKey'] = app('Flarum\Core\Settings\SettingsRepository')->get('pusher.app_key');
|
||||
}
|
||||
}
|
||||
|
||||
public function addRoutes(RegisterApiRoutes $event)
|
||||
{
|
||||
$event->post('/pusher/auth', 'pusher.auth', 'Flarum\Pusher\Api\AuthAction');
|
||||
}
|
||||
}
|
||||
|
@@ -1,6 +1,7 @@
|
||||
<?php namespace Flarum\Pusher\Listeners;
|
||||
|
||||
use Flarum\Events\PostWasPosted;
|
||||
use Flarum\Events\NotificationWillBeSent;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
use Flarum\Core\Settings\SettingsRepository;
|
||||
use Flarum\Core\Users\Guest;
|
||||
@@ -19,16 +20,13 @@ class PushNewPosts
|
||||
public function subscribe(Dispatcher $events)
|
||||
{
|
||||
$events->listen(PostWasPosted::class, [$this, 'pushNewPost']);
|
||||
$events->listen(NotificationWillBeSent::class, [$this, 'pushNotification']);
|
||||
}
|
||||
|
||||
public function pushNewPost(PostWasPosted $event)
|
||||
{
|
||||
if ($event->post->isVisibleTo(new Guest)) {
|
||||
$pusher = new Pusher(
|
||||
$this->settings->get('pusher.app_key'),
|
||||
$this->settings->get('pusher.app_secret'),
|
||||
$this->settings->get('pusher.app_id')
|
||||
);
|
||||
$pusher = $this->getPusher();
|
||||
|
||||
$pusher->trigger('public', 'newPost', [
|
||||
'postId' => $event->post->id,
|
||||
@@ -37,4 +35,25 @@ class PushNewPosts
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
public function pushNotification(NotificationWillBeSent $event)
|
||||
{
|
||||
$pusher = $this->getPusher();
|
||||
$blueprint = $event->blueprint;
|
||||
|
||||
foreach ($event->users as $user) {
|
||||
if ($user->shouldAlert($blueprint::getType())) {
|
||||
$pusher->trigger('private-user' . $user->id, 'notification', null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected function getPusher()
|
||||
{
|
||||
return new Pusher(
|
||||
$this->settings->get('pusher.app_key'),
|
||||
$this->settings->get('pusher.app_secret'),
|
||||
$this->settings->get('pusher.app_id')
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user