1
0
mirror of https://github.com/flarum/core.git synced 2025-10-19 02:36:08 +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:
Toby Zerner
2015-03-24 15:04:24 +10:30
parent 536281e273
commit a62e04f956
36 changed files with 342 additions and 503 deletions

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -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();
}

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -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);
}