1
0
mirror of https://github.com/flarum/core.git synced 2025-10-12 15:34:26 +02:00
Files
php-flarum/src/Api/Serializer/PostSerializer.php
2018-05-14 13:49:52 +02:00

107 lines
2.4 KiB
PHP

<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Api\Serializer;
use Flarum\Post\CommentPost;
use Flarum\User\Gate;
class PostSerializer extends BasicPostSerializer
{
/**
* @var \Flarum\User\Gate
*/
protected $gate;
/**
* @param \Flarum\User\Gate $gate
*/
public function __construct(Gate $gate)
{
$this->gate = $gate;
}
/**
* {@inheritdoc}
*/
protected function getDefaultAttributes($post)
{
$attributes = parent::getDefaultAttributes($post);
unset($attributes['content']);
$gate = $this->gate->forUser($this->actor);
$canEdit = $gate->allows('edit', $post);
if ($post instanceof CommentPost) {
$attributes['contentHtml'] = $post->content_html;
if ($canEdit) {
$attributes['content'] = $post->content;
}
if ($gate->allows('viewIps', $post)) {
$attributes['ipAddress'] = $post->ip_address;
}
} else {
$attributes['content'] = $post->content;
}
if ($post->edited_at) {
$attributes['editTime'] = $this->formatDate($post->edited_at);
}
if ($post->hidden_at) {
$attributes['isHidden'] = true;
$attributes['hideTime'] = $this->formatDate($post->hidden_at);
}
$attributes += [
'canEdit' => $canEdit,
'canDelete' => $gate->allows('delete', $post)
];
return $attributes;
}
/**
* @return \Tobscure\JsonApi\Relationship
*/
protected function user($post)
{
return $this->hasOne($post, UserSerializer::class);
}
/**
* @return \Tobscure\JsonApi\Relationship
*/
protected function discussion($post)
{
return $this->hasOne($post, BasicDiscussionSerializer::class);
}
/**
* @return \Tobscure\JsonApi\Relationship
*/
protected function editUser($post)
{
return $this->hasOne($post, BasicUserSerializer::class);
}
/**
* @return \Tobscure\JsonApi\Relationship
*/
protected function hideUser($post)
{
return $this->hasOne($post, BasicUserSerializer::class);
}
}