Mark a discussion as read when it is created

This commit is contained in:
Toby Zerner 2015-02-12 15:18:45 +10:30
parent b0f6d7f1f8
commit 72485d534e

View File

@ -3,6 +3,7 @@
use Event;
use Flarum\Core\Discussions\Commands\StartDiscussionCommand;
use Flarum\Core\Discussions\Commands\ReadDiscussionCommand;
use Flarum\Core\Users\User;
use Flarum\Api\Actions\Base;
use Flarum\Api\Serializers\DiscussionSerializer;
@ -21,12 +22,21 @@ class Create extends Base
// and pass them through to the StartDiscussionCommand.
$title = $this->input('discussions.title');
$content = $this->input('discussions.content');
$command = new StartDiscussionCommand($title, $content, User::current());
$user = User::current();
$command = new StartDiscussionCommand($title, $content, $user);
Event::fire('Flarum.Api.Actions.Discussions.Create.WillExecuteCommand', [$command, $this->document]);
$discussion = $this->commandBus->execute($command);
// 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->commandBus->execute($command);
}
$serializer = new DiscussionSerializer(['posts']);
$this->document->setPrimaryElement($serializer->resource($discussion));