Add BC layer for notification blueprints

This gives extension authors time to add the new `getAttributes()`
method to their `BlueprintInterface` implementations.

The layer itself is easy to remove in beta.14.
This commit is contained in:
Franz Liedke 2020-03-27 15:50:02 +01:00
parent 310065fb1c
commit bbe7e97ba1
No known key found for this signature in database
GPG Key ID: 9A0231A879B055F4
5 changed files with 52 additions and 27 deletions

View File

@ -1,23 +0,0 @@
<?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\Notification\Blueprint;
abstract class AbstractBlueprint implements BlueprintInterface
{
public function getAttributes(): array
{
return [
'type' => static::getType(),
'from_user_id' => ($fromUser = $this->getFromUser()) ? $fromUser->id : null,
'subject_id' => ($subject = $this->getSubject()) ? $subject->getKey() : null,
'data' => ($data = $this->getData()) ? json_encode($data) : null
];
}
}

View File

@ -22,6 +22,7 @@ interface BlueprintInterface
/**
* Get the user that sent the notification.
*
* @deprecated Will be removed for beta.14
* @return User|null
*/
public function getFromUser();
@ -29,6 +30,7 @@ interface BlueprintInterface
/**
* Get the model that is the subject of this activity.
*
* @deprecated Will be removed for beta.14
* @return AbstractModel|null
*/
public function getSubject();
@ -36,10 +38,19 @@ interface BlueprintInterface
/**
* Get the data to be stored in the notification.
*
* @deprecated Will be removed for beta.14
* @return array|null
*/
public function getData();
/**
* Get the attributes that uniquely identify a notification, plus metadata.
* TODO: Uncomment this for beta.14.
*
* @return array
*/
//public function getAttributes(): array;
/**
* Get the serialized type of this activity.
*

View File

@ -0,0 +1,36 @@
<?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\Notification;
use Flarum\Notification\Blueprint\BlueprintInterface;
/**
* A backwards compatibility layer for notification blueprints.
*
* Will be removed for Beta 14 in favor of BlueprintInterface::getAttributes().
*
* @deprecated
*/
class BlueprintBC
{
public static function getAttributes(BlueprintInterface $blueprint): array
{
if (method_exists($blueprint, 'getAttributes')) {
return $blueprint->getAttributes();
} else {
return [
'type' => $blueprint::getType(),
'from_user_id' => ($fromUser = $blueprint->getFromUser()) ? $fromUser->id : null,
'subject_id' => ($subject = $blueprint->getSubject()) ? $subject->id : null,
'data' => ($data = $blueprint->getData()) ? json_encode($data) : null
];
}
}
}

View File

@ -11,6 +11,7 @@ namespace Flarum\Notification\Job;
use Carbon\Carbon;
use Flarum\Notification\Blueprint\BlueprintInterface;
use Flarum\Notification\BlueprintBC;
use Flarum\Notification\Event\Sending;
use Flarum\Notification\Notification;
use Flarum\Queue\AbstractJob;
@ -40,7 +41,7 @@ class SendNotificationsJob extends AbstractJob
event(new Sending($this->blueprint, $this->recipients));
$attributes = $this->blueprint->getAttributes();
$attributes = BlueprintBC::getAttributes($this->blueprint);
Notification::insert(
array_map(function (User $user) use ($attributes, $now) {

View File

@ -58,7 +58,7 @@ class NotificationSyncer
*/
public function sync(Blueprint\BlueprintInterface $blueprint, array $users)
{
$attributes = $blueprint->getAttributes();
$attributes = BlueprintBC::getAttributes($blueprint);
// Find all existing notification records in the database matching this
// blueprint. We will begin by assuming that they all need to be
@ -121,7 +121,7 @@ class NotificationSyncer
*/
public function delete(BlueprintInterface $blueprint)
{
Notification::where($blueprint->getAttributes())->update(['is_deleted' => true]);
Notification::where(BlueprintBC::getAttributes($blueprint))->update(['is_deleted' => true]);
}
/**
@ -132,7 +132,7 @@ class NotificationSyncer
*/
public function restore(BlueprintInterface $blueprint)
{
Notification::where($blueprint->getAttributes())->update(['is_deleted' => false]);
Notification::where(BlueprintBC::getAttributes($blueprint))->update(['is_deleted' => false]);
}
/**