1
0
mirror of https://github.com/flarum/core.git synced 2025-10-23 20:56:05 +02:00
Files
php-flarum/src/Api/Actions/Discussions/CreateAction.php
Toby Zerner a62e04f956 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)
2015-03-24 15:04:24 +10:30

42 lines
1.5 KiB
PHP

<?php namespace Flarum\Api\Actions\Discussions;
use Flarum\Core\Commands\StartDiscussionCommand;
use Flarum\Core\Commands\ReadDiscussionCommand;
use Flarum\Api\Actions\BaseAction;
use Flarum\Api\Actions\ApiParams;
use Flarum\Api\Serializers\DiscussionSerializer;
class CreateAction extends BaseAction
{
/**
* Start a new discussion.
*
* @return Response
*/
protected function run(ApiParams $params)
{
// 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('data.title');
$content = $params->get('data.content');
$user = $this->actor->getUser();
$command = new StartDiscussionCommand($title, $content, $user, app('flarum.forum'));
$discussion = $this->dispatch($command, $params);
// After creating the discussion, we assume that the user has seen all
// of the posts in the discussion; thus, we will mark the discussion
// as read if they are logged in.
if ($user->exists) {
$command = new ReadDiscussionCommand($discussion->id, $user, 1);
$this->dispatch($command, $params);
}
$serializer = new DiscussionSerializer(['posts']);
$document = $this->document()->setData($serializer->resource($discussion));
return $this->respondWithDocument($document);
}
}