Skip to content

Commit

Permalink
Performance: Load only basic information about terminal/relevant posts
Browse files Browse the repository at this point in the history
  • Loading branch information
tobyzerner committed Nov 11, 2017
1 parent 9cc67fe commit dedcbae
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
14 changes: 11 additions & 3 deletions src/Api/Serializer/DiscussionBasicSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ protected function startUser($discussion)
*/
protected function startPost($discussion)
{
return $this->hasOne($discussion, 'Flarum\Api\Serializer\PostSerializer');
return $this->hasOne($discussion, 'Flarum\Api\Serializer\PostBasicSerializer');
}

/**
Expand All @@ -70,7 +70,7 @@ protected function lastUser($discussion)
*/
protected function lastPost($discussion)
{
return $this->hasOne($discussion, 'Flarum\Api\Serializer\PostSerializer');
return $this->hasOne($discussion, 'Flarum\Api\Serializer\PostBasicSerializer');
}

/**
Expand All @@ -86,6 +86,14 @@ protected function posts($discussion)
*/
protected function relevantPosts($discussion)
{
return $this->hasMany($discussion, 'Flarum\Api\Serializer\PostSerializer');
return $this->hasMany($discussion, 'Flarum\Api\Serializer\PostBasicSerializer');
}

/**
* @return \Tobscure\JsonApi\Relationship
*/
protected function hideUser($discussion)
{
return $this->hasOne($discussion, 'Flarum\Api\Serializer\UserBasicSerializer');
}
}
8 changes: 0 additions & 8 deletions src/Api/Serializer/DiscussionSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,4 @@ protected function getDefaultAttributes($discussion)

return $attributes;
}

/**
* @return \Tobscure\JsonApi\Relationship
*/
protected function hideUser($discussion)
{
return $this->hasOne($discussion, 'Flarum\Api\Serializer\UserSerializer');
}
}
7 changes: 7 additions & 0 deletions src/Api/Serializer/PostBasicSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Flarum\Api\Serializer;

use Flarum\Core\Post;
use Flarum\Core\Post\CommentPost;
use InvalidArgumentException;

class PostBasicSerializer extends AbstractSerializer
Expand Down Expand Up @@ -42,6 +43,12 @@ protected function getDefaultAttributes($post)
'contentType' => $post->type
];

if ($post instanceof CommentPost) {
$attributes['contentHtml'] = $post->content_html;
} else {
$attributes['content'] = $post->content;
}

return $attributes;
}

Expand Down

0 comments on commit dedcbae

Please sign in to comment.