diff --git a/framework/core/src/Notification/Blueprint/BlueprintInterface.php b/framework/core/src/Notification/Blueprint/BlueprintInterface.php index 422ba1784..38f5ed060 100644 --- a/framework/core/src/Notification/Blueprint/BlueprintInterface.php +++ b/framework/core/src/Notification/Blueprint/BlueprintInterface.php @@ -22,7 +22,6 @@ interface BlueprintInterface /** * Get the user that sent the notification. * - * @deprecated Will be removed for beta.14 * @return User|null */ public function getFromUser(); @@ -30,7 +29,6 @@ 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(); @@ -38,19 +36,10 @@ 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 deleted file mode 100644 index b3a4209d0..000000000 --- a/framework/core/src/Notification/BlueprintBC.php +++ /dev/null @@ -1,36 +0,0 @@ -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 93a08b8fd..5ffb70f2b 100644 --- a/framework/core/src/Notification/Job/SendNotificationsJob.php +++ b/framework/core/src/Notification/Job/SendNotificationsJob.php @@ -9,9 +9,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; @@ -37,19 +35,8 @@ class SendNotificationsJob extends AbstractJob public function handle() { - $now = Carbon::now('utc')->toDateTimeString(); - event(new Sending($this->blueprint, $this->recipients)); - $attributes = BlueprintBC::getAttributes($this->blueprint); - - Notification::insert( - array_map(function (User $user) use ($attributes, $now) { - return $attributes + [ - 'user_id' => $user->id, - 'created_at' => $now - ]; - }, $this->recipients) - ); + Notification::notify($this->recipients, $this->blueprint); } } diff --git a/framework/core/src/Notification/Notification.php b/framework/core/src/Notification/Notification.php index 2e11b1dec..94782dddf 100644 --- a/framework/core/src/Notification/Notification.php +++ b/framework/core/src/Notification/Notification.php @@ -12,6 +12,7 @@ namespace Flarum\Notification; use Carbon\Carbon; use Flarum\Database\AbstractModel; use Flarum\Event\ScopeModelVisibility; +use Flarum\Notification\Blueprint\BlueprintInterface; use Flarum\User\User; use Illuminate\Database\Eloquent\Builder; use Illuminate\Support\Arr; @@ -141,10 +142,11 @@ class Notification extends AbstractModel * to the given user. * * @param Builder $query + * @return Builder */ public function scopeWhereSubjectVisibleTo(Builder $query, User $actor) { - $query->where(function ($query) use ($actor) { + return $query->where(function ($query) use ($actor) { $classes = []; foreach (static::$subjectModels as $type => $class) { @@ -174,10 +176,11 @@ class Notification extends AbstractModel * * @param Builder $query * @param object $model + * @return Builder */ public function scopeWhereSubject(Builder $query, $model) { - $query->whereSubjectModel(get_class($model)) + return $query->whereSubjectModel(get_class($model)) ->where('subject_id', $model->id); } @@ -187,6 +190,7 @@ class Notification extends AbstractModel * * @param Builder $query * @param string $class + * @return Builder */ public function scopeWhereSubjectModel(Builder $query, string $class) { @@ -194,7 +198,40 @@ class Notification extends AbstractModel return $modelClass === $class or is_subclass_of($class, $modelClass); }); - $query->whereIn('type', array_keys($notificationTypes)); + return $query->whereIn('type', array_keys($notificationTypes)); + } + + /** + * Scope the query to find all records matching the given blueprint. + * + * @param Builder $query + * @param BlueprintInterface $blueprint + * @return Builder + */ + public function scopeMatchingBlueprint(Builder $query, BlueprintInterface $blueprint) + { + return $query->where(static::getBlueprintAttributes($blueprint)); + } + + /** + * Send notifications to the given recipients. + * + * @param User[] $recipients + * @param BlueprintInterface $blueprint + */ + public static function notify(array $recipients, BlueprintInterface $blueprint) + { + $attributes = static::getBlueprintAttributes($blueprint); + $now = Carbon::now('utc')->toDateTimeString(); + + static::insert( + array_map(function (User $user) use ($attributes, $now) { + return $attributes + [ + 'user_id' => $user->id, + 'created_at' => $now + ]; + }, $recipients) + ); } /** @@ -219,4 +256,14 @@ class Notification extends AbstractModel { static::$subjectModels[$type] = $subjectModel; } + + private static function getBlueprintAttributes(BlueprintInterface $blueprint): array + { + 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/NotificationSyncer.php b/framework/core/src/Notification/NotificationSyncer.php index 9ae014588..1bcc6ebb7 100644 --- a/framework/core/src/Notification/NotificationSyncer.php +++ b/framework/core/src/Notification/NotificationSyncer.php @@ -58,12 +58,10 @@ class NotificationSyncer */ public function sync(Blueprint\BlueprintInterface $blueprint, array $users) { - $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 // deleted in order to match the provided list of users. - $toDelete = Notification::where($attributes)->get(); + $toDelete = Notification::matchingBlueprint($blueprint)->get(); $toUndelete = []; $newRecipients = []; @@ -121,7 +119,7 @@ class NotificationSyncer */ public function delete(BlueprintInterface $blueprint) { - Notification::where(BlueprintBC::getAttributes($blueprint))->update(['is_deleted' => true]); + Notification::matchingBlueprint($blueprint)->update(['is_deleted' => true]); } /** @@ -132,7 +130,7 @@ class NotificationSyncer */ public function restore(BlueprintInterface $blueprint) { - Notification::where(BlueprintBC::getAttributes($blueprint))->update(['is_deleted' => false]); + Notification::matchingBlueprint($blueprint)->update(['is_deleted' => false]); } /**