mirror of
https://github.com/flarum/framework.git
synced 2025-03-10 04:05:30 +08:00
Add Notification Type Extender and Tests (#2424)
This commit is contained in:
parent
a7d7150b68
commit
10db7aad62
@ -75,6 +75,12 @@ class ApiServiceProvider extends AbstractServiceProvider
|
|||||||
|
|
||||||
return $pipe;
|
return $pipe;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
$this->app->singleton('flarum.api.notification_serializers', function () {
|
||||||
|
return [
|
||||||
|
'discussionRenamed' => BasicDiscussionSerializer::class
|
||||||
|
];
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -82,7 +88,7 @@ class ApiServiceProvider extends AbstractServiceProvider
|
|||||||
*/
|
*/
|
||||||
public function boot()
|
public function boot()
|
||||||
{
|
{
|
||||||
$this->registerNotificationSerializers();
|
$this->setNotificationSerializers();
|
||||||
|
|
||||||
AbstractSerializeController::setContainer($this->app);
|
AbstractSerializeController::setContainer($this->app);
|
||||||
AbstractSerializeController::setEventDispatcher($events = $this->app->make('events'));
|
AbstractSerializeController::setEventDispatcher($events = $this->app->make('events'));
|
||||||
@ -94,13 +100,12 @@ class ApiServiceProvider extends AbstractServiceProvider
|
|||||||
/**
|
/**
|
||||||
* Register notification serializers.
|
* Register notification serializers.
|
||||||
*/
|
*/
|
||||||
protected function registerNotificationSerializers()
|
protected function setNotificationSerializers()
|
||||||
{
|
{
|
||||||
$blueprints = [];
|
$blueprints = [];
|
||||||
$serializers = [
|
$serializers = $this->app->make('flarum.api.notification_serializers');
|
||||||
'discussionRenamed' => BasicDiscussionSerializer::class
|
|
||||||
];
|
|
||||||
|
|
||||||
|
// Deprecated in beta 15, remove in beta 16
|
||||||
$this->app->make('events')->dispatch(
|
$this->app->make('events')->dispatch(
|
||||||
new ConfigureNotificationTypes($blueprints, $serializers)
|
new ConfigureNotificationTypes($blueprints, $serializers)
|
||||||
);
|
);
|
||||||
|
@ -13,6 +13,9 @@ use Flarum\Notification\Blueprint\BlueprintInterface;
|
|||||||
use InvalidArgumentException;
|
use InvalidArgumentException;
|
||||||
use ReflectionClass;
|
use ReflectionClass;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated in beta 15, removed in beta 16
|
||||||
|
*/
|
||||||
class ConfigureNotificationTypes
|
class ConfigureNotificationTypes
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
|
38
framework/core/src/Extend/Notification.php
Normal file
38
framework/core/src/Extend/Notification.php
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
<?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\Extend;
|
||||||
|
|
||||||
|
use Flarum\Extension\Extension;
|
||||||
|
use Illuminate\Contracts\Container\Container;
|
||||||
|
|
||||||
|
class Notification implements ExtenderInterface
|
||||||
|
{
|
||||||
|
private $blueprints = [];
|
||||||
|
private $serializers = [];
|
||||||
|
|
||||||
|
public function type(string $blueprint, string $serializer, array $channelsEnabledByDefault = [])
|
||||||
|
{
|
||||||
|
$this->blueprints[$blueprint] = $channelsEnabledByDefault;
|
||||||
|
$this->serializers[$blueprint::getType()] = $serializer;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function extend(Container $container, Extension $extension = null)
|
||||||
|
{
|
||||||
|
$container->extend('flarum.notification.blueprints', function ($existingBlueprints) {
|
||||||
|
return array_merge($existingBlueprints, $this->blueprints);
|
||||||
|
});
|
||||||
|
|
||||||
|
$container->extend('flarum.api.notification_serializers', function ($existingSerializers) {
|
||||||
|
return array_merge($existingSerializers, $this->serializers);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
@ -17,46 +17,62 @@ use ReflectionClass;
|
|||||||
|
|
||||||
class NotificationServiceProvider extends AbstractServiceProvider
|
class NotificationServiceProvider extends AbstractServiceProvider
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function register()
|
||||||
|
{
|
||||||
|
$this->app->singleton('flarum.notification.blueprints', function () {
|
||||||
|
return [
|
||||||
|
DiscussionRenamedBlueprint::class => ['alert']
|
||||||
|
];
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public function boot()
|
public function boot()
|
||||||
{
|
{
|
||||||
$this->registerNotificationTypes();
|
$this->setNotificationTypes();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Register notification types.
|
* Register notification types.
|
||||||
*/
|
*/
|
||||||
public function registerNotificationTypes()
|
protected function setNotificationTypes()
|
||||||
{
|
{
|
||||||
$blueprints = [
|
$blueprints = $this->app->make('flarum.notification.blueprints');
|
||||||
DiscussionRenamedBlueprint::class => ['alert']
|
|
||||||
];
|
|
||||||
|
|
||||||
|
// Deprecated in beta 15, remove in beta 16
|
||||||
$this->app->make('events')->dispatch(
|
$this->app->make('events')->dispatch(
|
||||||
new ConfigureNotificationTypes($blueprints)
|
new ConfigureNotificationTypes($blueprints)
|
||||||
);
|
);
|
||||||
|
|
||||||
foreach ($blueprints as $blueprint => $enabled) {
|
foreach ($blueprints as $blueprint => $channelsEnabledByDefault) {
|
||||||
Notification::setSubjectModel(
|
$this->addType($blueprint, $channelsEnabledByDefault);
|
||||||
$type = $blueprint::getType(),
|
}
|
||||||
$blueprint::getSubjectModel()
|
}
|
||||||
);
|
|
||||||
|
|
||||||
|
protected function addType(string $blueprint, array $channelsEnabledByDefault)
|
||||||
|
{
|
||||||
|
Notification::setSubjectModel(
|
||||||
|
$type = $blueprint::getType(),
|
||||||
|
$blueprint::getSubjectModel()
|
||||||
|
);
|
||||||
|
|
||||||
|
User::addPreference(
|
||||||
|
User::getNotificationPreferenceKey($type, 'alert'),
|
||||||
|
'boolval',
|
||||||
|
in_array('alert', $channelsEnabledByDefault)
|
||||||
|
);
|
||||||
|
|
||||||
|
if ((new ReflectionClass($blueprint))->implementsInterface(MailableInterface::class)) {
|
||||||
User::addPreference(
|
User::addPreference(
|
||||||
User::getNotificationPreferenceKey($type, 'alert'),
|
User::getNotificationPreferenceKey($type, 'email'),
|
||||||
'boolval',
|
'boolval',
|
||||||
in_array('alert', $enabled)
|
in_array('email', $channelsEnabledByDefault)
|
||||||
);
|
);
|
||||||
|
|
||||||
if ((new ReflectionClass($blueprint))->implementsInterface(MailableInterface::class)) {
|
|
||||||
User::addPreference(
|
|
||||||
User::getNotificationPreferenceKey($type, 'email'),
|
|
||||||
'boolval',
|
|
||||||
in_array('email', $enabled)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,68 @@
|
|||||||
|
<?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\Tests\integration\extenders;
|
||||||
|
|
||||||
|
use Flarum\Extend;
|
||||||
|
use Flarum\Notification\Blueprint\BlueprintInterface;
|
||||||
|
use Flarum\Notification\Notification;
|
||||||
|
|
||||||
|
class NotificationTest extends \Flarum\Tests\integration\TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function notification_type_doesnt_exist_by_default()
|
||||||
|
{
|
||||||
|
$this->assertArrayNotHasKey('customNotificationType', Notification::getSubjectModels());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function notification_type_exists_if_added()
|
||||||
|
{
|
||||||
|
$this->extend((new Extend\Notification)->type(
|
||||||
|
CustomNotificationType::class,
|
||||||
|
'customNotificationTypeSerializer'
|
||||||
|
));
|
||||||
|
|
||||||
|
$this->app();
|
||||||
|
|
||||||
|
$this->assertArrayHasKey('customNotificationType', Notification::getSubjectModels());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class CustomNotificationType implements BlueprintInterface
|
||||||
|
{
|
||||||
|
public function getFromUser()
|
||||||
|
{
|
||||||
|
// ...
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getSubject()
|
||||||
|
{
|
||||||
|
// ...
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getData()
|
||||||
|
{
|
||||||
|
// ...
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getType()
|
||||||
|
{
|
||||||
|
return 'customNotificationType';
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getSubjectModel()
|
||||||
|
{
|
||||||
|
return 'customNotificationTypeSubjectModel';
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user