1
0
mirror of https://github.com/flarum/core.git synced 2025-07-06 01:24:02 +02:00
Files
php-flarum/src/Api/Controller/CreateDiscussionController.php
2015-11-23 14:15:30 +10:30

73 lines
1.7 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\Controller;
use Flarum\Core\Command\StartDiscussion;
use Flarum\Core\Command\ReadDiscussion;
use Illuminate\Contracts\Bus\Dispatcher;
use Psr\Http\Message\ServerRequestInterface;
use Tobscure\JsonApi\Document;
class CreateDiscussionController extends AbstractCreateController
{
/**
* {@inheritdoc}
*/
public $serializer = 'Flarum\Api\Serializer\DiscussionSerializer';
/**
* {@inheritdoc}
*/
public $include = [
'posts',
'startUser',
'lastUser',
'startPost',
'lastPost'
];
/**
* @var Dispatcher
*/
protected $bus;
/**
* @param Dispatcher $bus
*/
public function __construct(Dispatcher $bus)
{
$this->bus = $bus;
}
/**
* {@inheritdoc}
*/
protected function data(ServerRequestInterface $request, Document $document)
{
$actor = $request->getAttribute('actor');
$discussion = $this->bus->dispatch(
new StartDiscussion($actor, array_get($request->getParsedBody(), 'data', []))
);
// 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 ($actor->exists) {
$this->bus->dispatch(
new ReadDiscussion($discussion->id, $actor, 1)
);
}
return $discussion;
}
}