From cf3e0cc5bcb154d73ff613bff26f615975011204 Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Fri, 27 Mar 2020 15:50:02 +0100 Subject: [PATCH] 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. --- .../Blueprint/AbstractBlueprint.php | 23 ------------ .../Blueprint/BlueprintInterface.php | 11 ++++++ .../core/src/Notification/BlueprintBC.php | 36 +++++++++++++++++++ .../Notification/Job/SendNotificationsJob.php | 3 +- .../src/Notification/NotificationSyncer.php | 6 ++-- 5 files changed, 52 insertions(+), 27 deletions(-) delete mode 100644 framework/core/src/Notification/Blueprint/AbstractBlueprint.php create mode 100644 framework/core/src/Notification/BlueprintBC.php diff --git a/framework/core/src/Notification/Blueprint/AbstractBlueprint.php b/framework/core/src/Notification/Blueprint/AbstractBlueprint.php deleted file mode 100644 index 23eb893d5..000000000 --- a/framework/core/src/Notification/Blueprint/AbstractBlueprint.php +++ /dev/null @@ -1,23 +0,0 @@ - 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 - ]; - } -} diff --git a/framework/core/src/Notification/Blueprint/BlueprintInterface.php b/framework/core/src/Notification/Blueprint/BlueprintInterface.php index 38f5ed060..422ba1784 100644 --- a/framework/core/src/Notification/Blueprint/BlueprintInterface.php +++ b/framework/core/src/Notification/Blueprint/BlueprintInterface.php @@ -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. * diff --git a/framework/core/src/Notification/BlueprintBC.php b/framework/core/src/Notification/BlueprintBC.php new file mode 100644 index 000000000..b3a4209d0 --- /dev/null +++ b/framework/core/src/Notification/BlueprintBC.php @@ -0,0 +1,36 @@ +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 + ]; + } + } +} diff --git a/framework/core/src/Notification/Job/SendNotificationsJob.php b/framework/core/src/Notification/Job/SendNotificationsJob.php index f3e8d0277..93a08b8fd 100644 --- a/framework/core/src/Notification/Job/SendNotificationsJob.php +++ b/framework/core/src/Notification/Job/SendNotificationsJob.php @@ -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) { diff --git a/framework/core/src/Notification/NotificationSyncer.php b/framework/core/src/Notification/NotificationSyncer.php index 94d154893..9ae014588 100644 --- a/framework/core/src/Notification/NotificationSyncer.php +++ b/framework/core/src/Notification/NotificationSyncer.php @@ -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]); } /**