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

Add Notification Type Extender #2424

Merged
merged 11 commits into from
Oct 31, 2020
15 changes: 10 additions & 5 deletions src/Api/ApiServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,20 @@ public function register()

return $pipe;
});

$this->app->singleton('flarum.api.notification_serializers', function () {
return [
'discussionRenamed' => BasicDiscussionSerializer::class
];
});
}

/**
* {@inheritdoc}
*/
public function boot()
{
$this->registerNotificationSerializers();
$this->setNotificationSerializers();

AbstractSerializeController::setContainer($this->app);
AbstractSerializeController::setEventDispatcher($events = $this->app->make('events'));
Expand All @@ -94,13 +100,12 @@ public function boot()
/**
* Register notification serializers.
*/
protected function registerNotificationSerializers()
protected function setNotificationSerializers()
{
$blueprints = [];
$serializers = [
'discussionRenamed' => BasicDiscussionSerializer::class
];
$serializers = $this->app->make('flarum.api.notification_serializers');
SychO9 marked this conversation as resolved.
Show resolved Hide resolved

// Deprecated in beta 15, remove in beta 16
SychO9 marked this conversation as resolved.
Show resolved Hide resolved
$this->app->make('events')->dispatch(
new ConfigureNotificationTypes($blueprints, $serializers)
);
Expand Down
3 changes: 3 additions & 0 deletions src/Event/ConfigureNotificationTypes.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
use InvalidArgumentException;
use ReflectionClass;

/**
* @deprecated in beta 15, removed in beta 16
*/
class ConfigureNotificationTypes
{
/**
Expand Down
38 changes: 38 additions & 0 deletions src/Extend/Notification.php
Original file line number Diff line number Diff line change
@@ -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);
});
}
}
56 changes: 36 additions & 20 deletions src/Notification/NotificationServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,46 +17,62 @@

class NotificationServiceProvider extends AbstractServiceProvider
{
/**
* {@inheritdoc}
*/
public function register()
{
$this->app->singleton('flarum.notification.blueprints', function () {
return [
DiscussionRenamedBlueprint::class => ['alert']
];
});
}

/**
* {@inheritdoc}
*/
public function boot()
{
$this->registerNotificationTypes();
$this->setNotificationTypes();
}

/**
* Register notification types.
*/
public function registerNotificationTypes()
protected function setNotificationTypes()
{
$blueprints = [
DiscussionRenamedBlueprint::class => ['alert']
];
$blueprints = $this->app->make('flarum.notification.blueprints');
SychO9 marked this conversation as resolved.
Show resolved Hide resolved

// Deprecated in beta 15, remove in beta 16
$this->app->make('events')->dispatch(
new ConfigureNotificationTypes($blueprints)
);

foreach ($blueprints as $blueprint => $enabled) {
Notification::setSubjectModel(
$type = $blueprint::getType(),
$blueprint::getSubjectModel()
);
foreach ($blueprints as $blueprint => $channelsEnabledByDefault) {
$this->addType($blueprint, $channelsEnabledByDefault);
}
}

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::getNotificationPreferenceKey($type, 'alert'),
User::getNotificationPreferenceKey($type, 'email'),
'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)
);
}
}
}
}
68 changes: 68 additions & 0 deletions tests/integration/extenders/NotificationTest.php
Original file line number Diff line number Diff line change
@@ -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()
askvortsov1 marked this conversation as resolved.
Show resolved Hide resolved
{
$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';
}
}