1
0
mirror of https://github.com/flarum/core.git synced 2025-10-19 18:56:44 +02:00

Simplify and improve notifications API.

It turns out that the idea of “sending” a notification is flawed. (What
happens if the notification subject is deleted shortly after? The
notified user would end up with a dud notification which would be
confusing. What about if a post is edited to mention an extra user? If
you sent out notifications, the users who’ve already been mentioned
would get a duplicate notification.)

Instead, I’ve introduced the idea of notification “syncing”. Whenever a
change is made to a piece of data (e.g. a post is created, edited, or
deleted), you make a common notification and “sync” it to a set of
users. The users who’ve received this notification before won’t get it
again. It will be sent out to new users, and hidden from users who’ve
received it before but are no longer recipients (e.g. users who’ve been
“unmentioned” in a post).

To keep track of this, we use the existing notifications database
table, with an added `is_deleted` column. The syncing/diffing is
handled all behind the scenes; the API is extremely simple (see
Core\Notifications\DiscussionRenamedNotification +
Core\Events\Handlers\DiscussionRenamedNotifier)
This commit is contained in:
Toby Zerner
2015-05-20 12:24:01 +09:30
parent f2d1100ec3
commit 98b3d0f89e
25 changed files with 334 additions and 313 deletions

View File

@@ -73,6 +73,6 @@ class IndexAction extends SerializeCollectionAction
$user->markNotificationsAsRead()->save();
return $this->notifications->findByUser($user->id, $request->limit, $request->offset);
return $this->notifications->findByUser($user, $request->limit, $request->offset);
}
}

View File

@@ -64,9 +64,8 @@ abstract class BaseSerializer extends SerializerAbstract
}
}
if (is_array($serializer)) {
$class = get_class(is_object($data) ? $data : $model->$relation()->getRelated());
$serializer = $serializer[$class];
if ($serializer instanceof Closure) {
$serializer = $serializer($model, $data);
}
$serializer = new $serializer($this->actor, $links);
return $many ? $serializer->collection($data) : $serializer->resource($data);

View File

@@ -4,10 +4,21 @@ class NotificationSerializer extends BaseSerializer
{
/**
* The resource type.
*
* @var string
*/
protected $type = 'notifications';
/**
* A map of notification types (key) to the serializer that should be used
* to output the notification's subject (value).
*
* @var array
*/
public static $subjects = [
'discussionRenamed' => 'Flarum\Api\Serializers\DiscussionBasicSerializer'
];
/**
* Serialize attributes of an notification model for JSON output.
*
@@ -40,9 +51,8 @@ class NotificationSerializer extends BaseSerializer
public function subject()
{
return $this->hasOne([
'Flarum\Core\Models\Discussion' => 'Flarum\Api\Serializers\DiscussionSerializer',
'Flarum\Core\Models\CommentPost' => 'Flarum\Api\Serializers\PostSerializer'
]);
return $this->hasOne(function ($notification) {
return static::$subjects[$notification->type];
});
}
}