mirror of
https://github.com/flarum/framework.git
synced 2025-02-06 12:44:58 +08:00
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:
parent
4c363fe76c
commit
cf3e0cc5bc
|
@ -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
|
|
||||||
];
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -22,6 +22,7 @@ interface BlueprintInterface
|
||||||
/**
|
/**
|
||||||
* Get the user that sent the notification.
|
* Get the user that sent the notification.
|
||||||
*
|
*
|
||||||
|
* @deprecated Will be removed for beta.14
|
||||||
* @return User|null
|
* @return User|null
|
||||||
*/
|
*/
|
||||||
public function getFromUser();
|
public function getFromUser();
|
||||||
|
@ -29,6 +30,7 @@ interface BlueprintInterface
|
||||||
/**
|
/**
|
||||||
* Get the model that is the subject of this activity.
|
* Get the model that is the subject of this activity.
|
||||||
*
|
*
|
||||||
|
* @deprecated Will be removed for beta.14
|
||||||
* @return AbstractModel|null
|
* @return AbstractModel|null
|
||||||
*/
|
*/
|
||||||
public function getSubject();
|
public function getSubject();
|
||||||
|
@ -36,10 +38,19 @@ interface BlueprintInterface
|
||||||
/**
|
/**
|
||||||
* Get the data to be stored in the notification.
|
* Get the data to be stored in the notification.
|
||||||
*
|
*
|
||||||
|
* @deprecated Will be removed for beta.14
|
||||||
* @return array|null
|
* @return array|null
|
||||||
*/
|
*/
|
||||||
public function getData();
|
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.
|
* Get the serialized type of this activity.
|
||||||
*
|
*
|
||||||
|
|
36
framework/core/src/Notification/BlueprintBC.php
Normal file
36
framework/core/src/Notification/BlueprintBC.php
Normal 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
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -11,6 +11,7 @@ namespace Flarum\Notification\Job;
|
||||||
|
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
use Flarum\Notification\Blueprint\BlueprintInterface;
|
use Flarum\Notification\Blueprint\BlueprintInterface;
|
||||||
|
use Flarum\Notification\BlueprintBC;
|
||||||
use Flarum\Notification\Event\Sending;
|
use Flarum\Notification\Event\Sending;
|
||||||
use Flarum\Notification\Notification;
|
use Flarum\Notification\Notification;
|
||||||
use Flarum\Queue\AbstractJob;
|
use Flarum\Queue\AbstractJob;
|
||||||
|
@ -40,7 +41,7 @@ class SendNotificationsJob extends AbstractJob
|
||||||
|
|
||||||
event(new Sending($this->blueprint, $this->recipients));
|
event(new Sending($this->blueprint, $this->recipients));
|
||||||
|
|
||||||
$attributes = $this->blueprint->getAttributes();
|
$attributes = BlueprintBC::getAttributes($this->blueprint);
|
||||||
|
|
||||||
Notification::insert(
|
Notification::insert(
|
||||||
array_map(function (User $user) use ($attributes, $now) {
|
array_map(function (User $user) use ($attributes, $now) {
|
||||||
|
|
|
@ -58,7 +58,7 @@ class NotificationSyncer
|
||||||
*/
|
*/
|
||||||
public function sync(Blueprint\BlueprintInterface $blueprint, array $users)
|
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
|
// Find all existing notification records in the database matching this
|
||||||
// blueprint. We will begin by assuming that they all need to be
|
// blueprint. We will begin by assuming that they all need to be
|
||||||
|
@ -121,7 +121,7 @@ class NotificationSyncer
|
||||||
*/
|
*/
|
||||||
public function delete(BlueprintInterface $blueprint)
|
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)
|
public function restore(BlueprintInterface $blueprint)
|
||||||
{
|
{
|
||||||
Notification::where($blueprint->getAttributes())->update(['is_deleted' => false]);
|
Notification::where(BlueprintBC::getAttributes($blueprint))->update(['is_deleted' => false]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue
Block a user