1
0
mirror of https://github.com/flarum/core.git synced 2025-10-09 22:16:51 +02:00

Extract current user attributes into a separate serializer

This prevents the unread notifications count query being run for every
post by the currently authenticated user
This commit is contained in:
Toby Zerner
2015-06-01 12:25:40 +09:30
parent 0f9549f4b9
commit bb1491e19e
3 changed files with 25 additions and 12 deletions

View File

@@ -0,0 +1,21 @@
<?php namespace Flarum\Api\Serializers;
class CurrentUserSerializer extends UserSerializer
{
protected function attributes($user)
{
$attributes = parent::attributes($user);
$actingUser = $this->actor->getUser();
if ($user->id === $actingUser->id) {
$attributes += [
'readTime' => $user->read_time ? $user->read_time->toRFC3339String() : null,
'unreadNotificationsCount' => $user->getUnreadNotificationsCount(),
'preferences' => $user->preferences
];
}
return $this->extendAttributes($user, $attributes);
}
}