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:
21
src/Api/Serializers/CurrentUserSerializer.php
Normal file
21
src/Api/Serializers/CurrentUserSerializer.php
Normal 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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user