From 1cde3d72424fcfff2ad4bbef418c79f5201cbe52 Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Fri, 17 Jul 2015 14:48:20 +0930 Subject: [PATCH] Fix notifications --- .../Commands/ReadNotificationHandler.php | 4 -- .../DiscussionRenamedBlueprint.php | 2 +- .../src/Core/Notifications/Notification.php | 5 +- .../core/src/Core/Support/MappedMorphTo.php | 43 +++++++++++++++++ .../src/Core/Support/MappedMorphToTrait.php | 48 +++++++++++++++++++ .../core/src/Extend/NotificationType.php | 2 +- 6 files changed, 97 insertions(+), 7 deletions(-) create mode 100644 framework/core/src/Core/Support/MappedMorphTo.php create mode 100644 framework/core/src/Core/Support/MappedMorphToTrait.php diff --git a/framework/core/src/Core/Notifications/Commands/ReadNotificationHandler.php b/framework/core/src/Core/Notifications/Commands/ReadNotificationHandler.php index 4747da536..7b401a9f3 100644 --- a/framework/core/src/Core/Notifications/Commands/ReadNotificationHandler.php +++ b/framework/core/src/Core/Notifications/Commands/ReadNotificationHandler.php @@ -6,8 +6,6 @@ use Flarum\Core\Support\DispatchesEvents; class ReadNotificationHandler { - use DispatchesEvents; - /** * @param ReadNotification $command * @return Notification @@ -26,8 +24,6 @@ class ReadNotificationHandler $notification->read(); $notification->save(); - $this->dispatchEventsFor($notification); - return $notification; } } diff --git a/framework/core/src/Core/Notifications/DiscussionRenamedBlueprint.php b/framework/core/src/Core/Notifications/DiscussionRenamedBlueprint.php index db6cd7e67..ca732204d 100644 --- a/framework/core/src/Core/Notifications/DiscussionRenamedBlueprint.php +++ b/framework/core/src/Core/Notifications/DiscussionRenamedBlueprint.php @@ -54,6 +54,6 @@ class DiscussionRenamedBlueprint implements Blueprint */ public static function getSubjectModel() { - return 'Flarum\Core\Models\Discussion'; + return 'Flarum\Core\Discussions\Discussion'; } } diff --git a/framework/core/src/Core/Notifications/Notification.php b/framework/core/src/Core/Notifications/Notification.php index baae5147b..18a5449a4 100644 --- a/framework/core/src/Core/Notifications/Notification.php +++ b/framework/core/src/Core/Notifications/Notification.php @@ -1,6 +1,7 @@ morphTo('subject', 'subjectModel', 'subject_id'); + return $this->mappedMorphTo(static::$subjectModels, 'subject', 'type', 'subject_id'); } /** diff --git a/framework/core/src/Core/Support/MappedMorphTo.php b/framework/core/src/Core/Support/MappedMorphTo.php new file mode 100644 index 000000000..d9579631a --- /dev/null +++ b/framework/core/src/Core/Support/MappedMorphTo.php @@ -0,0 +1,43 @@ +map = $map; + + parent::__construct($query, $parent, $foreignKey, $otherKey, $type, $relation); + } + + /** + * Create a new model instance by type. + * + * @param string $type + * @return \Illuminate\Database\Eloquent\Model + */ + public function createModelByType($type) + { + return new $this->map[$type]; + } +} diff --git a/framework/core/src/Core/Support/MappedMorphToTrait.php b/framework/core/src/Core/Support/MappedMorphToTrait.php new file mode 100644 index 000000000..3b21c0320 --- /dev/null +++ b/framework/core/src/Core/Support/MappedMorphToTrait.php @@ -0,0 +1,48 @@ +getMorphs($name, $type, $id); + + // If the type value is null it is probably safe to assume we're eager loading + // the relationship. When that is the case we will pass in a dummy query as + // there are multiple types in the morph and we can't use single queries. + if (is_null($typeValue = $this->$type)) { + return new MappedMorphTo( + $this->newQuery(), + $this, + $id, + null, + $type, + $name, + $classes + ); + } else { + // If we are not eager loading the relationship we will essentially treat this + // as a belongs-to style relationship since morph-to extends that class and + // we will pass in the appropriate values so that it behaves as expected. + $class = $classes[$typeValue]; + $instance = new $class; + + return new MappedMorphTo( + $instance->newQuery(), + $this, + $id, + $instance->getKeyName(), + $type, + $name, + $classes + ); + } + } +} diff --git a/framework/core/src/Extend/NotificationType.php b/framework/core/src/Extend/NotificationType.php index 2380d93b4..6517b86ce 100644 --- a/framework/core/src/Extend/NotificationType.php +++ b/framework/core/src/Extend/NotificationType.php @@ -38,7 +38,7 @@ class NotificationType implements ExtenderInterface $class = $this->class; $type = $class::getType(); - Notification::setSubjectModel($type, $class); + Notification::setSubjectModel($type, $class::getSubjectModel()); User::addPreference(User::getNotificationPreferenceKey($type, 'alert'), 'boolval', in_array('alert', $this->enabled));