mirror of
https://github.com/flarum/framework.git
synced 2024-12-12 14:13:37 +08:00
Fix notifications
This commit is contained in:
parent
54ab536f96
commit
1cde3d7242
|
@ -6,8 +6,6 @@ use Flarum\Core\Support\DispatchesEvents;
|
||||||
|
|
||||||
class ReadNotificationHandler
|
class ReadNotificationHandler
|
||||||
{
|
{
|
||||||
use DispatchesEvents;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param ReadNotification $command
|
* @param ReadNotification $command
|
||||||
* @return Notification
|
* @return Notification
|
||||||
|
@ -26,8 +24,6 @@ class ReadNotificationHandler
|
||||||
$notification->read();
|
$notification->read();
|
||||||
$notification->save();
|
$notification->save();
|
||||||
|
|
||||||
$this->dispatchEventsFor($notification);
|
|
||||||
|
|
||||||
return $notification;
|
return $notification;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -54,6 +54,6 @@ class DiscussionRenamedBlueprint implements Blueprint
|
||||||
*/
|
*/
|
||||||
public static function getSubjectModel()
|
public static function getSubjectModel()
|
||||||
{
|
{
|
||||||
return 'Flarum\Core\Models\Discussion';
|
return 'Flarum\Core\Discussions\Discussion';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
<?php namespace Flarum\Core\Notifications;
|
<?php namespace Flarum\Core\Notifications;
|
||||||
|
|
||||||
use Flarum\Core\Model;
|
use Flarum\Core\Model;
|
||||||
|
use Flarum\Core\Support\MappedMorphToTrait;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Models a notification record in the database.
|
* Models a notification record in the database.
|
||||||
|
@ -20,6 +21,8 @@ use Flarum\Core\Model;
|
||||||
*/
|
*/
|
||||||
class Notification extends Model
|
class Notification extends Model
|
||||||
{
|
{
|
||||||
|
use MappedMorphToTrait;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
|
@ -111,7 +114,7 @@ class Notification extends Model
|
||||||
*/
|
*/
|
||||||
public function subject()
|
public function subject()
|
||||||
{
|
{
|
||||||
return $this->morphTo('subject', 'subjectModel', 'subject_id');
|
return $this->mappedMorphTo(static::$subjectModels, 'subject', 'type', 'subject_id');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
43
framework/core/src/Core/Support/MappedMorphTo.php
Normal file
43
framework/core/src/Core/Support/MappedMorphTo.php
Normal 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];
|
||||||
|
}
|
||||||
|
}
|
48
framework/core/src/Core/Support/MappedMorphToTrait.php
Normal file
48
framework/core/src/Core/Support/MappedMorphToTrait.php
Normal 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
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -38,7 +38,7 @@ class NotificationType implements ExtenderInterface
|
||||||
$class = $this->class;
|
$class = $this->class;
|
||||||
$type = $class::getType();
|
$type = $class::getType();
|
||||||
|
|
||||||
Notification::setSubjectModel($type, $class);
|
Notification::setSubjectModel($type, $class::getSubjectModel());
|
||||||
|
|
||||||
User::addPreference(User::getNotificationPreferenceKey($type, 'alert'), 'boolval', in_array('alert', $this->enabled));
|
User::addPreference(User::getNotificationPreferenceKey($type, 'alert'), 'boolval', in_array('alert', $this->enabled));
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user