mirror of
https://github.com/flarum/core.git
synced 2025-07-31 13:40:20 +02:00
Upgrade to JSON-API RC3 + latest version of tobscure/json-api
Note: npm source for ember-json-api changed to a fork, but I still had to apply a custom change to get polymorphic relationships to work (see https://github.com/kurko/ember-json-api/pull/71#issuecomment-85257281)
This commit is contained in:
@@ -40,7 +40,7 @@ class IndexAction extends BaseAction
|
||||
// Finally, we can set up the activity serializer and use it to create
|
||||
// a collection of activity results.
|
||||
$serializer = new ActivitySerializer(['sender', 'post', 'post.discussion', 'post.user', 'post.discussion.startUser', 'post.discussion.lastUser'], ['user']);
|
||||
$document = $this->document()->setPrimaryElement($serializer->collection($activity));
|
||||
$document = $this->document()->setData($serializer->collection($activity));
|
||||
|
||||
return $this->respondWithDocument($document);
|
||||
}
|
||||
|
@@ -23,7 +23,12 @@ abstract class BaseAction extends Action
|
||||
|
||||
public function handle(Request $request, $routeParams = [])
|
||||
{
|
||||
$params = array_merge($request->all(), $routeParams);
|
||||
if (str_contains($request->header('CONTENT_TYPE'), 'application/vnd.api+json')) {
|
||||
$input = $request->json();
|
||||
} else {
|
||||
$input = $request;
|
||||
}
|
||||
$params = array_merge($input->all(), $routeParams);
|
||||
|
||||
return $this->call($params);
|
||||
}
|
||||
|
@@ -18,8 +18,8 @@ class CreateAction extends BaseAction
|
||||
// By default, the only required attributes of a discussion are the
|
||||
// title and the content. We'll extract these from the rbaseequest data
|
||||
// and pass them through to the StartDiscussionCommand.
|
||||
$title = $params->get('discussions.title');
|
||||
$content = $params->get('discussions.content');
|
||||
$title = $params->get('data.title');
|
||||
$content = $params->get('data.content');
|
||||
$user = $this->actor->getUser();
|
||||
|
||||
$command = new StartDiscussionCommand($title, $content, $user, app('flarum.forum'));
|
||||
@@ -34,7 +34,7 @@ class CreateAction extends BaseAction
|
||||
}
|
||||
|
||||
$serializer = new DiscussionSerializer(['posts']);
|
||||
$document = $this->document()->setPrimaryElement($serializer->resource($discussion));
|
||||
$document = $this->document()->setData($serializer->resource($discussion));
|
||||
|
||||
return $this->respondWithDocument($document);
|
||||
}
|
||||
|
@@ -36,16 +36,14 @@ class IndexAction extends BaseAction
|
||||
{
|
||||
$query = $params->get('q');
|
||||
$start = $params->start();
|
||||
$include = $params->included(['startPost', 'lastPost', 'relevantPosts']);
|
||||
$include = $params->included(['startUser', 'lastUser', 'startPost', 'lastPost', 'relevantPosts']);
|
||||
$count = $params->count(20, 50);
|
||||
$sort = $params->sort(['', 'lastPost', 'replies', 'created']);
|
||||
|
||||
$relations = array_merge(['startUser', 'lastUser'], $include);
|
||||
|
||||
// Set up the discussion searcher with our search criteria, and get the
|
||||
// requested range of results with the necessary relations loaded.
|
||||
$criteria = new DiscussionSearchCriteria($this->actor->getUser(), $query, $sort['field'], $sort['order']);
|
||||
$load = array_merge($relations, ['state']);
|
||||
$load = array_merge($include, ['state']);
|
||||
|
||||
$results = $this->searcher->search($criteria, $count, $start, $load);
|
||||
|
||||
@@ -61,9 +59,8 @@ class IndexAction extends BaseAction
|
||||
// specified.
|
||||
if ($results->areMoreResults()) {
|
||||
$start += $count;
|
||||
$include = implode(',', $include);
|
||||
$sort = $sort['string'];
|
||||
$input = array_filter(compact('query', 'sort', 'start', 'count', 'include'));
|
||||
$input = array_filter(compact('query', 'sort', 'start', 'count')) + ['include' => implode(',', $include)];
|
||||
$moreUrl = $this->buildUrl('discussions.index', [], $input);
|
||||
} else {
|
||||
$moreUrl = '';
|
||||
@@ -72,8 +69,8 @@ class IndexAction extends BaseAction
|
||||
|
||||
// Finally, we can set up the discussion serializer and use it to create
|
||||
// a collection of discussion results.
|
||||
$serializer = new DiscussionSerializer($relations);
|
||||
$document->setPrimaryElement($serializer->collection($results->getDiscussions()));
|
||||
$serializer = new DiscussionSerializer($include);
|
||||
$document->setData($serializer->collection($results->getDiscussions()));
|
||||
|
||||
return $this->respondWithDocument($document);
|
||||
}
|
||||
|
@@ -49,6 +49,8 @@ class ShowAction extends BaseAction
|
||||
|
||||
$discussion = $this->discussions->findOrFail($params->get('id'), $this->actor->getUser());
|
||||
|
||||
$discussion->posts_ids = $discussion->posts()->get(['id'])->fetch('id')->all();
|
||||
|
||||
if (in_array('posts', $include)) {
|
||||
$relations = ['user', 'user.groups', 'editUser', 'hideUser'];
|
||||
$discussion->posts = $this->getPosts($params, ['discussion_id' => $discussion->id])->load($relations);
|
||||
@@ -63,7 +65,7 @@ class ShowAction extends BaseAction
|
||||
// relations, we will specify that we want the 'posts' relation to be
|
||||
// linked so that a list of post IDs will show up in the response.
|
||||
$serializer = new DiscussionSerializer($include, ['posts']);
|
||||
$document = $this->document()->setPrimaryElement($serializer->resource($discussion));
|
||||
$document = $this->document()->setData($serializer->resource($discussion));
|
||||
|
||||
return $this->respondWithDocument($document);
|
||||
}
|
||||
|
@@ -24,10 +24,10 @@ class UpdateAction extends BaseAction
|
||||
// discussion's direct properties; by default, this is just the title.
|
||||
// As usual, however, we will fire an event to allow plugins to update
|
||||
// additional properties.
|
||||
if ($data = array_except($params->get('discussions'), ['readNumber'])) {
|
||||
if ($data = array_except($params->get('data'), ['readNumber'])) {
|
||||
try {
|
||||
$command = new EditDiscussionCommand($discussionId, $user);
|
||||
$this->hydrate($command, $params->get('discussions'));
|
||||
$this->hydrate($command, $params->get('data'));
|
||||
$discussion = $this->dispatch($command, $params);
|
||||
} catch (PermissionDeniedException $e) {
|
||||
// Temporary fix. See @todo below
|
||||
@@ -43,7 +43,7 @@ class UpdateAction extends BaseAction
|
||||
// PermissionDeniedException is thrown by the
|
||||
// EditDiscussionCommand above. So this needs to be extracted into
|
||||
// its own endpoint.
|
||||
if ($readNumber = $params->get('discussions.readNumber')) {
|
||||
if ($readNumber = $params->get('data.readNumber')) {
|
||||
$command = new ReadDiscussionCommand($discussionId, $user, $readNumber);
|
||||
$this->dispatch($command, $params);
|
||||
}
|
||||
@@ -52,7 +52,7 @@ class UpdateAction extends BaseAction
|
||||
// handlers would have thrown an exception if not.) We set this
|
||||
// discussion as our document's primary element.
|
||||
$serializer = new DiscussionSerializer(['addedPosts', 'addedPosts.user']);
|
||||
$document = $this->document()->setPrimaryElement($serializer->resource($discussion));
|
||||
$document = $this->document()->setData($serializer->resource($discussion));
|
||||
|
||||
return $this->respondWithDocument($document);
|
||||
}
|
||||
|
@@ -11,7 +11,7 @@ class Index extends Base
|
||||
$groups = Group::get();
|
||||
|
||||
$serializer = new GroupSerializer;
|
||||
$this->document->setPrimaryElement($serializer->collection($groups));
|
||||
$this->document->setData($serializer->collection($groups));
|
||||
|
||||
return $this->respondWithDocument();
|
||||
}
|
||||
|
@@ -22,8 +22,8 @@ class CreateAction extends BaseAction
|
||||
// the post content, and the author's user account. Let's set up a
|
||||
// command with this information. We also fire an event to allow plugins
|
||||
// to add data to the command.
|
||||
$discussionId = $params->get('posts.links.discussion');
|
||||
$content = $params->get('posts.content');
|
||||
$discussionId = $params->get('data.links.discussion.linkage.id');
|
||||
$content = $params->get('data.content');
|
||||
|
||||
$command = new PostReplyCommand($discussionId, $content, $user);
|
||||
$post = $this->dispatch($command, $params);
|
||||
@@ -40,7 +40,7 @@ class CreateAction extends BaseAction
|
||||
// would have thrown an exception if not.) We set this post as our
|
||||
// document's primary element.
|
||||
$serializer = new PostSerializer;
|
||||
$document = $this->document()->setPrimaryElement($serializer->resource($post));
|
||||
$document = $this->document()->setData($serializer->resource($post));
|
||||
|
||||
return $this->respondWithDocument($document, 201);
|
||||
}
|
||||
|
@@ -60,7 +60,7 @@ class IndexAction extends BaseAction
|
||||
// a post resource or collection, depending on how many posts were
|
||||
// requested.
|
||||
$serializer = new PostSerializer($include);
|
||||
$document = $this->document()->setPrimaryElement($serializer->collection($posts->load($include)));
|
||||
$document = $this->document()->setData($serializer->collection($posts->load($include)));
|
||||
|
||||
return $this->respondWithDocument($document);
|
||||
}
|
||||
|
@@ -35,7 +35,7 @@ class ShowAction extends BaseAction
|
||||
// a post resource or collection, depending on how many posts were
|
||||
// requested.
|
||||
$serializer = new PostSerializer($relations);
|
||||
$document = $this->document()->setPrimaryElement($serializer->resource($posts->first()));
|
||||
$document = $this->document()->setData($serializer->resource($posts->first()));
|
||||
|
||||
return $this->respondWithDocument($document);
|
||||
}
|
||||
|
@@ -20,14 +20,14 @@ class UpdateAction extends BaseAction
|
||||
// updates (i.e. if we were to run one command and then another, if the
|
||||
// second one failed, the first one would still have succeeded.)
|
||||
$command = new EditPostCommand($postId, $this->actor->getUser());
|
||||
$this->hydrate($command, $params->get('posts'));
|
||||
$this->hydrate($command, $params->get('data'));
|
||||
$post = $this->dispatch($command, $params);
|
||||
|
||||
// Presumably, the post was updated successfully. (The command handler
|
||||
// would have thrown an exception if not.) We set this post as our
|
||||
// document's primary element.
|
||||
$serializer = new PostSerializer;
|
||||
$document = $this->document()->setPrimaryElement($serializer->resource($post));
|
||||
$document = $this->document()->setData($serializer->resource($post));
|
||||
|
||||
return $this->respondWithDocument($document);
|
||||
}
|
||||
|
@@ -18,9 +18,9 @@ class CreateAction extends BaseAction
|
||||
// required attributes of a user is the username, email, and password.
|
||||
// Let's set up a command with this information. We also fire an event
|
||||
// to allow plugins to add data to the command.
|
||||
$username = $params->get('users.username');
|
||||
$email = $params->get('users.email');
|
||||
$password = $params->get('users.password');
|
||||
$username = $params->get('data.username');
|
||||
$email = $params->get('data.email');
|
||||
$password = $params->get('data.password');
|
||||
|
||||
$command = new RegisterUserCommand($username, $email, $password, $this->actor->getUser(), app('flarum.forum'));
|
||||
$user = $this->dispatch($command, $params);
|
||||
@@ -29,7 +29,7 @@ class CreateAction extends BaseAction
|
||||
// would have thrown an exception if not.) We set this post as our
|
||||
// document's primary element.
|
||||
$serializer = new UserSerializer;
|
||||
$document = $this->document()->setPrimaryElement($serializer->resource($user));
|
||||
$document = $this->document()->setData($serializer->resource($user));
|
||||
|
||||
return $this->respondWithDocument($document, 201);
|
||||
}
|
||||
|
@@ -72,7 +72,7 @@ class IndexAction extends BaseAction
|
||||
// Finally, we can set up the discussion serializer and use it to create
|
||||
// a collection of discussion results.
|
||||
$serializer = new UserSerializer($relations);
|
||||
$document->setPrimaryElement($serializer->collection($results->getUsers()));
|
||||
$document->setData($serializer->collection($results->getUsers()));
|
||||
|
||||
return $this->respondWithDocument($document);
|
||||
}
|
||||
|
@@ -37,7 +37,7 @@ class ShowAction extends BaseAction
|
||||
// document's primary resource. We will specify that we want the
|
||||
// 'groups' relation to be included by default.
|
||||
$serializer = new UserSerializer(['groups']);
|
||||
$document = $this->document()->setPrimaryElement($serializer->resource($user));
|
||||
$document = $this->document()->setData($serializer->resource($user));
|
||||
|
||||
return $this->respondWithDocument($document);
|
||||
}
|
||||
|
@@ -21,14 +21,14 @@ class UpdateAction extends BaseAction
|
||||
// updates (i.e. if we were to run one command and then another, if the
|
||||
// second one failed, the first one would still have succeeded.)
|
||||
$command = new EditUserCommand($userId, $this->actor->getUser());
|
||||
$this->hydrate($command, $params->get('users'));
|
||||
$this->hydrate($command, $params->get('data'));
|
||||
$user = $this->dispatch($command, $params);
|
||||
|
||||
// Presumably, the user was updated successfully. (The command handler
|
||||
// would have thrown an exception if not.) We set this user as our
|
||||
// document's primary element.
|
||||
$serializer = new UserSerializer;
|
||||
$document = $this->document()->setPrimaryElement($serializer->resource($user));
|
||||
$document = $this->document()->setData($serializer->resource($user));
|
||||
|
||||
return $this->respondWithDocument($document);
|
||||
}
|
||||
|
@@ -4,20 +4,11 @@ class SerializeRelationship
|
||||
{
|
||||
public $serializer;
|
||||
|
||||
public $model;
|
||||
|
||||
public $type;
|
||||
|
||||
public $name;
|
||||
|
||||
public $relations;
|
||||
|
||||
public function __construct($serializer, $model, $type, $name, $relations)
|
||||
public function __construct($serializer, $name)
|
||||
{
|
||||
$this->serializer = $serializer;
|
||||
$this->model = $model;
|
||||
$this->type = $type;
|
||||
$this->name = $name;
|
||||
$this->relations = $relations;
|
||||
}
|
||||
}
|
||||
|
@@ -1,11 +1,10 @@
|
||||
<?php namespace Flarum\Api\Serializers;
|
||||
|
||||
use Flarum\Core\Models\Activity;
|
||||
|
||||
class ActivitySerializer extends BaseSerializer
|
||||
{
|
||||
/**
|
||||
* The resource type.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $type = 'activity';
|
||||
@@ -16,50 +15,30 @@ class ActivitySerializer extends BaseSerializer
|
||||
* @param Activity $activity The Activity model to serialize.
|
||||
* @return array
|
||||
*/
|
||||
protected function attributes(Activity $activity)
|
||||
protected function attributes($activity)
|
||||
{
|
||||
$attributes = [
|
||||
'id' => ((int) $activity->id) ?: str_random(5),
|
||||
'type' => $activity->type,
|
||||
'contentType' => $activity->type,
|
||||
'content' => json_encode($activity->data),
|
||||
'time' => $activity->time->toRFC3339String()
|
||||
];
|
||||
|
||||
return $this->attributesEvent($activity, $attributes);
|
||||
return $this->extendAttributes($activity, $attributes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a resource containing an activity's sender.
|
||||
*
|
||||
* @param Activity $activity
|
||||
* @return Tobscure\JsonApi\Resource
|
||||
*/
|
||||
public function linkUser(Activity $activity)
|
||||
public function user()
|
||||
{
|
||||
return (new UserBasicSerializer)->resource($activity->user_id);
|
||||
return $this->hasOne('Flarum\Api\Serializers\UserBasicSerializer');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a resource containing an activity's sender.
|
||||
*
|
||||
* @param Activity $activity
|
||||
* @param array $relations
|
||||
* @return Tobscure\JsonApi\Resource
|
||||
*/
|
||||
public function includeSender(Activity $activity, $relations)
|
||||
public function sender()
|
||||
{
|
||||
return (new UserBasicSerializer($relations))->resource($activity->sender);
|
||||
return $this->hasOne('Flarum\Api\Serializers\UserBasicSerializer');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a resource containing an activity's sender.
|
||||
*
|
||||
* @param Activity $activity
|
||||
* @param array $relations
|
||||
* @return Tobscure\JsonApi\Resource
|
||||
*/
|
||||
public function includePost(Activity $activity, $relations)
|
||||
public function post()
|
||||
{
|
||||
return (new PostSerializer($relations))->resource($activity->post);
|
||||
return $this->hasOne('Flarum\Api\Serializers\PostSerializer');
|
||||
}
|
||||
}
|
||||
|
@@ -4,6 +4,7 @@ use Tobscure\JsonApi\SerializerAbstract;
|
||||
use Flarum\Api\Events\SerializeAttributes;
|
||||
use Flarum\Api\Events\SerializeRelationship;
|
||||
use Flarum\Core\Support\Actor;
|
||||
use Closure;
|
||||
|
||||
/**
|
||||
* A base serializer to call Flarum events at common serialization points.
|
||||
@@ -35,13 +36,55 @@ abstract class BaseSerializer extends SerializerAbstract
|
||||
* @param array $attributes Attributes that have already been serialized.
|
||||
* @return array
|
||||
*/
|
||||
protected function attributesEvent($model, $attributes = [])
|
||||
protected function extendAttributes($model, &$attributes = [])
|
||||
{
|
||||
event(new SerializeAttributes($this, $model, $attributes));
|
||||
|
||||
return $attributes;
|
||||
}
|
||||
|
||||
protected function relationship($serializer, Closure $callback = null, $many = false)
|
||||
{
|
||||
// Get the relationship name from the stack trace.
|
||||
if (is_null($callback)) {
|
||||
list(, , $caller) = debug_backtrace(false, 3);
|
||||
$relation = $caller['function'];
|
||||
}
|
||||
|
||||
return function ($model, $include, $links) use ($serializer, $callback, $many, $relation) {
|
||||
if ($callback) {
|
||||
$data = $callback($model, $include);
|
||||
} else {
|
||||
if ($include) {
|
||||
$data = $model->$relation;
|
||||
} elseif ($many) {
|
||||
$relationIds = $relation.'_ids';
|
||||
$data = $model->$relationIds ?: $model->relation()->get(['id'])->fetch('id')->all();
|
||||
} else {
|
||||
$relationId = $relation.'_id';
|
||||
$data = $model->$relationId;
|
||||
}
|
||||
}
|
||||
|
||||
if (is_array($serializer)) {
|
||||
$class = get_class(is_object($data) ? $data : $model->$relation()->getRelated());
|
||||
$serializer = $serializer[$class];
|
||||
}
|
||||
$serializer = new $serializer($links);
|
||||
return $many ? $serializer->collection($data) : $serializer->resource($data);
|
||||
};
|
||||
}
|
||||
|
||||
public function hasOne($serializer, Closure $callback = null)
|
||||
{
|
||||
return $this->relationship($serializer, $callback);
|
||||
}
|
||||
|
||||
public function hasMany($serializer, Closure $callback = null)
|
||||
{
|
||||
return $this->relationship($serializer, $callback, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fire an event to allow for custom links and includes.
|
||||
*
|
||||
@@ -51,12 +94,6 @@ abstract class BaseSerializer extends SerializerAbstract
|
||||
*/
|
||||
public function __call($name, $arguments)
|
||||
{
|
||||
if ($link = starts_with($name, 'link') || starts_with($name, 'include')) {
|
||||
$model = isset($arguments[0]) ? $arguments[0] : null;
|
||||
$relations = isset($arguments[1]) ? $arguments[1] : null;
|
||||
$type = $link ? 'link' : 'include';
|
||||
$name = substr($name, strlen($type));
|
||||
return event(new SerializeRelationship($this, $model, $type, $name, $relations), null, true);
|
||||
}
|
||||
return event(new SerializeRelationship($this, $name), null, true);
|
||||
}
|
||||
}
|
||||
|
@@ -1,11 +1,10 @@
|
||||
<?php namespace Flarum\Api\Serializers;
|
||||
|
||||
use Flarum\Core\Models\Discussion;
|
||||
|
||||
class DiscussionBasicSerializer extends BaseSerializer
|
||||
{
|
||||
/**
|
||||
* The resource type.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $type = 'discussions';
|
||||
@@ -16,29 +15,47 @@ class DiscussionBasicSerializer extends BaseSerializer
|
||||
* @param Discussion $discussion The Discussion model to serialize.
|
||||
* @return array
|
||||
*/
|
||||
protected function attributes(Discussion $discussion)
|
||||
protected function attributes($discussion)
|
||||
{
|
||||
$attributes = [
|
||||
'id' => (int) $discussion->id,
|
||||
'title' => $discussion->title,
|
||||
'title' => $discussion->title
|
||||
];
|
||||
|
||||
return $this->attributesEvent($discussion, $attributes);
|
||||
return $this->extendAttributes($discussion, $attributes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the URL templates where this resource and its related resources can
|
||||
* be accessed.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function href()
|
||||
public function startUser()
|
||||
{
|
||||
$href = [
|
||||
'discussions' => $this->action('DiscussionsController@show', ['id' => '{discussions.id}']),
|
||||
'posts' => $this->action('PostsController@indexForDiscussion', ['id' => '{discussions.id}'])
|
||||
];
|
||||
return $this->hasOne('Flarum\Api\Serializers\UserBasicSerializer');
|
||||
}
|
||||
|
||||
return $this->hrefEvent($href);
|
||||
public function startPost()
|
||||
{
|
||||
return $this->hasOne('Flarum\Api\Serializers\PostBasicSerializer');
|
||||
}
|
||||
|
||||
public function lastUser()
|
||||
{
|
||||
return $this->hasOne('Flarum\Api\Serializers\UserBasicSerializer');
|
||||
}
|
||||
|
||||
public function lastPost()
|
||||
{
|
||||
return $this->hasOne('Flarum\Api\Serializers\PostBasicSerializer');
|
||||
}
|
||||
|
||||
public function posts()
|
||||
{
|
||||
return $this->hasMany('Flarum\Api\Serializers\PostSerializer');
|
||||
}
|
||||
|
||||
public function relevantPosts()
|
||||
{
|
||||
return $this->hasMany('Flarum\Api\Serializers\PostBasicSerializer');
|
||||
}
|
||||
|
||||
public function addedPosts()
|
||||
{
|
||||
return $this->hasMany('Flarum\Api\Serializers\PostBasicSerializer');
|
||||
}
|
||||
}
|
||||
|
@@ -1,12 +1,10 @@
|
||||
<?php namespace Flarum\Api\Serializers;
|
||||
|
||||
use Flarum\Core\Models\Discussion;
|
||||
use Flarum\Core\Models\DiscussionState;
|
||||
|
||||
class DiscussionSerializer extends DiscussionBasicSerializer
|
||||
{
|
||||
/**
|
||||
* Default relations to include.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $include = ['startUser', 'lastUser'];
|
||||
@@ -17,7 +15,7 @@ class DiscussionSerializer extends DiscussionBasicSerializer
|
||||
* @param Discussion $discussion The Discussion model to serialize.
|
||||
* @return array
|
||||
*/
|
||||
protected function attributes(Discussion $discussion)
|
||||
protected function attributes($discussion)
|
||||
{
|
||||
$attributes = parent::attributes($discussion);
|
||||
|
||||
@@ -37,109 +35,6 @@ class DiscussionSerializer extends DiscussionBasicSerializer
|
||||
'readNumber' => $state ? (int) $state->read_number : 0
|
||||
];
|
||||
|
||||
$this->attributesEvent($discussion, $attributes);
|
||||
|
||||
return $attributes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a collection containing a discussion's viewable post IDs.
|
||||
*
|
||||
* @param Discussion $discussion
|
||||
* @return Tobscure\JsonApi\Collection
|
||||
*/
|
||||
public function linkPosts(Discussion $discussion)
|
||||
{
|
||||
$user = static::$actor->getUser();
|
||||
|
||||
return (new PostBasicSerializer)->collection($discussion->posts()->whereCan($user, 'view')->orderBy('time', 'asc')->ids());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a collection containing a discussion's viewable posts. Assumes that
|
||||
* the discussion model's posts attributes has been filled.
|
||||
*
|
||||
* @param Discussion $discussion
|
||||
* @param array $relations
|
||||
* @return Tobscure\JsonApi\Collection
|
||||
*/
|
||||
public function includePosts(Discussion $discussion, $relations)
|
||||
{
|
||||
return (new PostSerializer($relations))->collection($discussion->posts);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a collection containing a discussion's relevant posts. Assumes that
|
||||
* the discussion model's relevantPosts attributes has been filled (this
|
||||
* happens in the DiscussionFinder.)
|
||||
*
|
||||
* @param Discussion $discussion
|
||||
* @param array $relations
|
||||
* @return Tobscure\JsonApi\Collection
|
||||
*/
|
||||
public function includeRelevantPosts(Discussion $discussion, $relations)
|
||||
{
|
||||
return (new PostBasicSerializer($relations))->collection($discussion->relevantPosts);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a resource containing a discussion's start user.
|
||||
*
|
||||
* @param Discussion $discussion
|
||||
* @param array $relations
|
||||
* @return Tobscure\JsonApi\Resource
|
||||
*/
|
||||
public function includeStartUser(Discussion $discussion, $relations)
|
||||
{
|
||||
return (new UserBasicSerializer($relations))->resource($discussion->startUser);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a resource containing a discussion's starting post.
|
||||
*
|
||||
* @param Discussion $discussion
|
||||
* @param array $relations
|
||||
* @return Tobscure\JsonApi\Resource
|
||||
*/
|
||||
public function includeStartPost(Discussion $discussion, $relations)
|
||||
{
|
||||
return (new PostBasicSerializer($relations))->resource($discussion->startPost);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a resource containing a discussion's last user.
|
||||
*
|
||||
* @param Discussion $discussion
|
||||
* @param array $relations
|
||||
* @return Tobscure\JsonApi\Resource
|
||||
*/
|
||||
public function includeLastUser(Discussion $discussion, $relations)
|
||||
{
|
||||
return (new UserBasicSerializer($relations))->resource($discussion->lastUser);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a resource containing a discussion's last post.
|
||||
*
|
||||
* @param Discussion $discussion
|
||||
* @param array $relations
|
||||
* @return Tobscure\JsonApi\Resource
|
||||
*/
|
||||
public function includeLastPost(Discussion $discussion, $relations)
|
||||
{
|
||||
return (new PostBasicSerializer($relations))->resource($discussion->lastPost);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a resource containing a discussion's list of posts that have been
|
||||
* added during this request.
|
||||
*
|
||||
* @param Discussion $discussion
|
||||
* @param array $relations
|
||||
* @return Tobscure\JsonApi\Collection
|
||||
*/
|
||||
public function includeAddedPosts(Discussion $discussion, $relations)
|
||||
{
|
||||
return (new PostBasicSerializer($relations))->collection($discussion->getAddedPosts());
|
||||
return $this->extendAttributes($discussion, $attributes);
|
||||
}
|
||||
}
|
||||
|
@@ -1,17 +1,10 @@
|
||||
<?php namespace Flarum\Api\Serializers;
|
||||
|
||||
use Flarum\Core\Models\Group;
|
||||
|
||||
class GroupSerializer extends BaseSerializer
|
||||
{
|
||||
/**
|
||||
* The name to use for Flarum events.
|
||||
* @var string
|
||||
*/
|
||||
protected static $eventName = 'Group';
|
||||
|
||||
/**
|
||||
* The resource type.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $type = 'groups';
|
||||
@@ -22,27 +15,13 @@ class GroupSerializer extends BaseSerializer
|
||||
* @param Group $group The Group model to serialize.
|
||||
* @return array
|
||||
*/
|
||||
protected function attributes(Group $group)
|
||||
protected function attributes($group)
|
||||
{
|
||||
$attributes = [
|
||||
'id' => (int) $group->id,
|
||||
'name' => $group->name
|
||||
];
|
||||
|
||||
return $this->attributesEvent($group, $attributes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the URL templates where this resource and its related resources can
|
||||
* be accessed.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function href()
|
||||
{
|
||||
return [
|
||||
'groups' => $this->action('GroupsController@show', ['id' => '{groups.id}']),
|
||||
'users' => $this->action('UsersController@indexForGroup', ['id' => '{groups.id}'])
|
||||
];
|
||||
return $this->extendAttributes($group, $attributes);
|
||||
}
|
||||
}
|
||||
|
@@ -1,29 +1,24 @@
|
||||
<?php namespace Flarum\Api\Serializers;
|
||||
|
||||
use Flarum\Core\Models\Post;
|
||||
|
||||
class PostBasicSerializer extends BaseSerializer
|
||||
{
|
||||
/**
|
||||
* The name to use for Flarum events.
|
||||
* @var string
|
||||
*/
|
||||
protected static $eventName = 'PostBasic';
|
||||
|
||||
/**
|
||||
* The resource type.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $type = 'posts';
|
||||
|
||||
/**
|
||||
* Default relations to link.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $link = ['discussion'];
|
||||
|
||||
/**
|
||||
* Default relations to include.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $include = ['user'];
|
||||
@@ -34,13 +29,13 @@ class PostBasicSerializer extends BaseSerializer
|
||||
* @param Post $post The Post model to serialize.
|
||||
* @return array
|
||||
*/
|
||||
protected function attributes(Post $post)
|
||||
protected function attributes($post)
|
||||
{
|
||||
$attributes = [
|
||||
'id' => (int) $post->id,
|
||||
'number' => (int) $post->number,
|
||||
'time' => $post->time->toRFC3339String(),
|
||||
'type' => $post->type
|
||||
'contentType' => $post->type
|
||||
];
|
||||
|
||||
if ($post->type === 'comment') {
|
||||
@@ -49,42 +44,16 @@ class PostBasicSerializer extends BaseSerializer
|
||||
$attributes['content'] = json_encode($post->content);
|
||||
}
|
||||
|
||||
return $this->attributesEvent($post, $attributes);
|
||||
return $this->extendAttributes($post, $attributes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the URL templates where this resource and its related resources can
|
||||
* be accessed.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function href()
|
||||
public function user()
|
||||
{
|
||||
return [
|
||||
'posts' => $this->action('PostsController@show', ['id' => '{posts.id}'])
|
||||
];
|
||||
return $this->hasOne('Flarum\Api\Serializers\UserBasicSerializer');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a resource containing a post's user.
|
||||
*
|
||||
* @param Post $post
|
||||
* @param array $relations
|
||||
* @return Tobscure\JsonApi\Resource
|
||||
*/
|
||||
public function includeUser(Post $post, $relations)
|
||||
public function discussion()
|
||||
{
|
||||
return (new UserBasicSerializer($relations))->resource($post->user);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a resource containing a post's discussion ID.
|
||||
*
|
||||
* @param Post $post
|
||||
* @return Tobscure\JsonApi\Resource
|
||||
*/
|
||||
public function linkDiscussion(Post $post)
|
||||
{
|
||||
return (new DiscussionBasicSerializer)->resource($post->discussion_id);
|
||||
return $this->hasOne('Flarum\Api\Serializers\DiscussionBasicSerializer');
|
||||
}
|
||||
}
|
||||
|
@@ -1,18 +1,10 @@
|
||||
<?php namespace Flarum\Api\Serializers;
|
||||
|
||||
use Flarum\Core\Models\Post;
|
||||
use Flarum\Core\Models\User;
|
||||
|
||||
class PostSerializer extends PostBasicSerializer
|
||||
{
|
||||
/**
|
||||
* Default relations to link.
|
||||
* @var array
|
||||
*/
|
||||
protected $link = ['discussion'];
|
||||
|
||||
/**
|
||||
* Default relations to include.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $include = ['user', 'editUser', 'hideUser'];
|
||||
@@ -23,7 +15,7 @@ class PostSerializer extends PostBasicSerializer
|
||||
* @param Post $post The Post model to serialize.
|
||||
* @return array
|
||||
*/
|
||||
protected function attributes(Post $post)
|
||||
protected function attributes($post)
|
||||
{
|
||||
$attributes = parent::attributes($post);
|
||||
$user = static::$actor->getUser();
|
||||
@@ -38,7 +30,7 @@ class PostSerializer extends PostBasicSerializer
|
||||
$attributes['content'] = $post->content;
|
||||
}
|
||||
} else {
|
||||
$attributes['content'] = json_encode($post->content);
|
||||
$attributes['content'] = $post->content;
|
||||
}
|
||||
|
||||
if ($post->edit_time) {
|
||||
@@ -55,54 +47,26 @@ class PostSerializer extends PostBasicSerializer
|
||||
'canDelete' => $post->can($user, 'delete')
|
||||
];
|
||||
|
||||
return $this->attributesEvent($post, $attributes);
|
||||
return $this->extendAttributes($post, $attributes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a resource containing a post's user.
|
||||
*
|
||||
* @param Post $post
|
||||
* @param array $relations
|
||||
* @return Tobscure\JsonApi\Resource
|
||||
*/
|
||||
public function includeUser(Post $post, $relations = [])
|
||||
public function user()
|
||||
{
|
||||
return (new UserSerializer($relations))->resource($post->user);
|
||||
return $this->hasOne('Flarum\Api\Serializers\UserSerializer');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a resource containing a post's discussion.
|
||||
*
|
||||
* @param Post $post
|
||||
* @param array $relations
|
||||
* @return Tobscure\JsonApi\Resource
|
||||
*/
|
||||
public function includeDiscussion(Post $post, $relations = [])
|
||||
public function discussion()
|
||||
{
|
||||
return (new DiscussionSerializer($relations))->resource($post->discussion);
|
||||
return $this->hasOne('Flarum\Api\Serializers\DiscussionSerializer');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a resource containing a post's edit user.
|
||||
*
|
||||
* @param Post $post
|
||||
* @param array $relations
|
||||
* @return Tobscure\JsonApi\Resource
|
||||
*/
|
||||
public function includeEditUser(Post $post, $relations = [])
|
||||
public function editUser()
|
||||
{
|
||||
return (new UserBasicSerializer($relations))->resource($post->editUser);
|
||||
return $this->hasOne('Flarum\Api\Serializers\UserSerializer');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a resource containing a post's hide user.
|
||||
*
|
||||
* @param Post $post
|
||||
* @param array $relations
|
||||
* @return Tobscure\JsonApi\Resource
|
||||
*/
|
||||
public function includeHideUser(Post $post, $relations = [])
|
||||
public function hideUser()
|
||||
{
|
||||
return (new UserBasicSerializer($relations))->resource($post->hideUser);
|
||||
return $this->hasOne('Flarum\Api\Serializers\UserSerializer');
|
||||
}
|
||||
}
|
||||
|
@@ -1,17 +1,10 @@
|
||||
<?php namespace Flarum\Api\Serializers;
|
||||
|
||||
use Flarum\Core\Models\User;
|
||||
|
||||
class UserBasicSerializer extends BaseSerializer
|
||||
{
|
||||
/**
|
||||
* The name to use for Flarum events.
|
||||
* @var string
|
||||
*/
|
||||
protected static $eventName = 'UserBasic';
|
||||
|
||||
/**
|
||||
* The resource type.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $type = 'users';
|
||||
@@ -22,29 +15,13 @@ class UserBasicSerializer extends BaseSerializer
|
||||
* @param User $user The User model to serialize.
|
||||
* @return array
|
||||
*/
|
||||
protected function attributes(User $user)
|
||||
protected function attributes($user)
|
||||
{
|
||||
$attributes = [
|
||||
'id' => (int) $user->id,
|
||||
'username' => $user->username,
|
||||
'avatarUrl' => $user->avatar_url
|
||||
];
|
||||
|
||||
return $this->attributesEvent($user, $attributes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the URL templates where this resource and its related resources can
|
||||
* be accessed.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function href()
|
||||
{
|
||||
$href = [
|
||||
'users' => $this->action('UsersController@show', ['id' => '{users.id}'])
|
||||
];
|
||||
|
||||
return $this->hrefEvent($href);
|
||||
return $this->extendAttributes($user, $attributes);
|
||||
}
|
||||
}
|
||||
|
@@ -1,17 +1,10 @@
|
||||
<?php namespace Flarum\Api\Serializers;
|
||||
|
||||
use Flarum\Core\Models\User;
|
||||
|
||||
class UserSerializer extends UserBasicSerializer
|
||||
{
|
||||
/**
|
||||
* The name to use for Flarum events.
|
||||
* @var string
|
||||
*/
|
||||
protected static $eventName = 'User';
|
||||
|
||||
/**
|
||||
* Default relations to include.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $include = ['groups'];
|
||||
@@ -22,7 +15,7 @@ class UserSerializer extends UserBasicSerializer
|
||||
* @param User $user The User model to serialize.
|
||||
* @return array
|
||||
*/
|
||||
protected function attributes(User $user)
|
||||
protected function attributes($user)
|
||||
{
|
||||
$attributes = parent::attributes($user);
|
||||
|
||||
@@ -51,21 +44,15 @@ class UserSerializer extends UserBasicSerializer
|
||||
if ($user->id === $actorUser->id) {
|
||||
$attributes += [
|
||||
'readTime' => $user->read_time ? $user->read_time->toRFC3339String() : null,
|
||||
'unreadNotificationsCount' => $user->getUnreadNotificationsCount()
|
||||
];
|
||||
}
|
||||
|
||||
return $this->attributesEvent($user, $attributes);
|
||||
return $this->extendAttributes($user, $attributes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a collection containing a user's groups.
|
||||
*
|
||||
* @param User $user
|
||||
* @param array $relations
|
||||
* @return Tobscure\JsonApi\Collection
|
||||
*/
|
||||
protected function includeGroups(User $user, $relations)
|
||||
protected function groups()
|
||||
{
|
||||
return (new GroupSerializer($relations))->collection($user->groups);
|
||||
return $this->hasMany('Flarum\Api\Serializers\GroupSerializer');
|
||||
}
|
||||
}
|
||||
|
@@ -47,7 +47,7 @@ class Discussion extends Model
|
||||
*
|
||||
* @var \Flarum\Core\Models\Post[]
|
||||
*/
|
||||
protected $addedPosts = [];
|
||||
public $addedPosts = [];
|
||||
|
||||
/**
|
||||
* The user for which the state relationship should be loaded.
|
||||
@@ -154,17 +154,6 @@ class Discussion extends Model
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of the posts that have been added to this discussion during
|
||||
* this request.
|
||||
*
|
||||
* @return \Flarum\Core\Models\Post[]
|
||||
*/
|
||||
public function getAddedPosts()
|
||||
{
|
||||
return $this->addedPosts;
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify that a post was added to this discussion during this request
|
||||
* for later retrieval.
|
||||
|
@@ -34,8 +34,7 @@ class IndexAction extends Action
|
||||
|
||||
$response = $this->callAction('Flarum\Api\Actions\Users\ShowAction', ['id' => $user->id]);
|
||||
|
||||
$data['users'] = [$response->getData()->users];
|
||||
$data['groups'] = $response->getData()->linked->groups;
|
||||
$data = array_merge([$response->getData()->data], $response->getData()->included);
|
||||
}
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user