1
0
mirror of https://github.com/flarum/core.git synced 2025-10-13 16:05:05 +02:00

Add Notification Type Extender and Tests (#2424)

This commit is contained in:
Sami Mazouz
2020-10-31 22:17:14 +01:00
committed by GitHub
parent 0b2a5fa5b8
commit b311512502
5 changed files with 155 additions and 25 deletions

View File

@@ -17,46 +17,62 @@ use ReflectionClass;
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');
// 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)
);
}
}
}
}