mirror of
https://github.com/flarum/core.git
synced 2025-08-08 09:26:34 +02:00
Initial commit
This commit is contained in:
29
extensions/pusher/src/Extension.php
Normal file
29
extensions/pusher/src/Extension.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php namespace Flarum\Pusher;
|
||||
|
||||
use Flarum\Support\Extension as BaseExtension;
|
||||
use Illuminate\Events\Dispatcher;
|
||||
|
||||
class Extension extends BaseExtension
|
||||
{
|
||||
/**
|
||||
* Bootstrap the application events.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function boot(Dispatcher $events)
|
||||
{
|
||||
$events->subscribe('Flarum\Pusher\Listeners\AddClientAssets');
|
||||
$events->subscribe('Flarum\Pusher\Listeners\PushNewPosts');
|
||||
$events->subscribe('Flarum\Pusher\Listeners\AddApiAttributes');
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the service provider.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
20
extensions/pusher/src/Listeners/AddApiAttributes.php
Executable file
20
extensions/pusher/src/Listeners/AddApiAttributes.php
Executable file
@@ -0,0 +1,20 @@
|
||||
<?php namespace Flarum\Pusher\Listeners;
|
||||
|
||||
use Flarum\Events\ApiAttributes;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
use Flarum\Api\Serializers\ForumSerializer;
|
||||
|
||||
class AddApiAttributes
|
||||
{
|
||||
public function subscribe(Dispatcher $events)
|
||||
{
|
||||
$events->listen(ApiAttributes::class, [$this, 'addAttributes']);
|
||||
}
|
||||
|
||||
public function addAttributes(ApiAttributes $event)
|
||||
{
|
||||
if ($event->serializer instanceof ForumSerializer) {
|
||||
$event->attributes['pusherKey'] = app('Flarum\Core\Settings\SettingsRepository')->get('pusher.app_key');
|
||||
}
|
||||
}
|
||||
}
|
44
extensions/pusher/src/Listeners/AddClientAssets.php
Normal file
44
extensions/pusher/src/Listeners/AddClientAssets.php
Normal file
@@ -0,0 +1,44 @@
|
||||
<?php namespace Flarum\Pusher\Listeners;
|
||||
|
||||
use Flarum\Events\RegisterLocales;
|
||||
use Flarum\Events\BuildClientView;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
|
||||
class AddClientAssets
|
||||
{
|
||||
public function subscribe(Dispatcher $events)
|
||||
{
|
||||
$events->listen(RegisterLocales::class, [$this, 'addLocale']);
|
||||
$events->listen(BuildClientView::class, [$this, 'addAssets']);
|
||||
}
|
||||
|
||||
public function addLocale(RegisterLocales $event)
|
||||
{
|
||||
$event->addTranslations('en', __DIR__.'/../../locale/en.yml');
|
||||
}
|
||||
|
||||
public function addAssets(BuildClientView $event)
|
||||
{
|
||||
$event->forumAssets([
|
||||
__DIR__.'/../../js/forum/dist/extension.js',
|
||||
__DIR__.'/../../less/forum/extension.less'
|
||||
]);
|
||||
|
||||
$event->forumBootstrapper('pusher/main');
|
||||
|
||||
$event->forumTranslations([
|
||||
'pusher.show_updated_discussions'
|
||||
]);
|
||||
|
||||
$event->adminAssets([
|
||||
__DIR__.'/../../js/admin/dist/extension.js',
|
||||
__DIR__.'/../../less/admin/extension.less'
|
||||
]);
|
||||
|
||||
$event->adminBootstrapper('pusher/main');
|
||||
|
||||
$event->adminTranslations([
|
||||
// 'pusher.hello_world'
|
||||
]);
|
||||
}
|
||||
}
|
47
extensions/pusher/src/Listeners/PushNewPosts.php
Normal file
47
extensions/pusher/src/Listeners/PushNewPosts.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php namespace Flarum\Pusher\Listeners;
|
||||
|
||||
use Flarum\Events\PostWasPosted;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
use Flarum\Core\Settings\SettingsRepository;
|
||||
use Flarum\Core\Users\Guest;
|
||||
use Flarum\Core\Discussions\Discussion;
|
||||
use Pusher;
|
||||
|
||||
class PushNewPosts
|
||||
{
|
||||
protected $settings;
|
||||
|
||||
public function __construct(SettingsRepository $settings)
|
||||
{
|
||||
$this->settings = $settings;
|
||||
}
|
||||
|
||||
public function subscribe(Dispatcher $events)
|
||||
{
|
||||
$events->listen(PostWasPosted::class, [$this, 'pushNewPost']);
|
||||
}
|
||||
|
||||
public function pushNewPost(PostWasPosted $event)
|
||||
{
|
||||
$guest = new Guest;
|
||||
$discussion = Discussion::whereVisibleTo($guest)->find($event->post->discussion_id);
|
||||
|
||||
if ($discussion) {
|
||||
$post = $discussion->postsVisibleTo($guest)->find($event->post->id);
|
||||
|
||||
if ($post) {
|
||||
$pusher = new Pusher(
|
||||
$this->settings->get('pusher.app_key'),
|
||||
$this->settings->get('pusher.app_secret'),
|
||||
$this->settings->get('pusher.app_id')
|
||||
);
|
||||
|
||||
$pusher->trigger('public', 'newPost', [
|
||||
'postId' => $post->id,
|
||||
'discussionId' => $discussion->id,
|
||||
'tagIds' => $discussion->tags()->lists('id')
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user