Fix notifications

This commit is contained in:
Toby Zerner 2015-07-17 14:48:20 +09:30
parent 54ab536f96
commit 1cde3d7242
6 changed files with 97 additions and 7 deletions

View File

@ -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;
}
}

View File

@ -54,6 +54,6 @@ class DiscussionRenamedBlueprint implements Blueprint
*/
public static function getSubjectModel()
{
return 'Flarum\Core\Models\Discussion';
return 'Flarum\Core\Discussions\Discussion';
}
}

View File

@ -1,6 +1,7 @@
<?php namespace Flarum\Core\Notifications;
use Flarum\Core\Model;
use Flarum\Core\Support\MappedMorphToTrait;
/**
* Models a notification record in the database.
@ -20,6 +21,8 @@ use Flarum\Core\Model;
*/
class Notification extends Model
{
use MappedMorphToTrait;
/**
* {@inheritdoc}
*/
@ -111,7 +114,7 @@ class Notification extends Model
*/
public function subject()
{
return $this->morphTo('subject', 'subjectModel', 'subject_id');
return $this->mappedMorphTo(static::$subjectModels, 'subject', 'type', 'subject_id');
}
/**

View File

@ -0,0 +1,43 @@
<?php namespace Flarum\Core\Support;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
class MappedMorphTo extends MorphTo
{
/**
*
* @var string
*/
protected $map;
/**
* Create a new morph to relationship instance.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param \Illuminate\Database\Eloquent\Model $parent
* @param string $foreignKey
* @param string $otherKey
* @param string $type
* @param string $relation
* @return void
*/
public function __construct(Builder $query, Model $parent, $foreignKey, $otherKey, $type, $relation, $map)
{
$this->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];
}
}

View File

@ -0,0 +1,48 @@
<?php namespace Flarum\Core\Support;
trait MappedMorphToTrait
{
public function mappedMorphTo($classes, $name = null, $type = null, $id = null)
{
// If no name is provided, we will use the backtrace to get the function name
// since that is most likely the name of the polymorphic interface. We can
// use that to get both the class and foreign key that will be utilized.
if (is_null($name)) {
list(, $caller) = debug_backtrace(false, 2);
$name = snake_case($caller['function']);
}
list($type, $id) = $this->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
);
}
}
}

View File

@ -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));