1
0
mirror of https://github.com/flarum/core.git synced 2025-07-19 07:41:22 +02:00

Mark discussion as read upon reply

This commit is contained in:
Toby Zerner
2015-02-03 18:41:15 +10:30
parent 2f6fd26363
commit f847fce76a

View File

@@ -2,6 +2,7 @@
use Event; use Event;
use Flarum\Core\Posts\Commands\PostReplyCommand; use Flarum\Core\Posts\Commands\PostReplyCommand;
use Flarum\Core\Discussions\Commands\ReadDiscussionCommand;
use Flarum\Core\Users\User; use Flarum\Core\Users\User;
use Flarum\Api\Actions\Base; use Flarum\Api\Actions\Base;
use Flarum\Api\Serializers\PostSerializer; use Flarum\Api\Serializers\PostSerializer;
@@ -15,6 +16,8 @@ class Create extends Base
*/ */
protected function run() protected function run()
{ {
$user = User::current();
// We've received a request to post a reply. By default, the only // We've received a request to post a reply. By default, the only
// required attributes of a post is the ID of the discussion to post in, // required attributes of a post is the ID of the discussion to post in,
// the post content, and the author's user account. Let's set up a // the post content, and the author's user account. Let's set up a
@@ -22,12 +25,20 @@ class Create extends Base
// to add data to the command. // to add data to the command.
$discussionId = $this->input('posts.links.discussion'); $discussionId = $this->input('posts.links.discussion');
$content = $this->input('posts.content'); $content = $this->input('posts.content');
$command = new PostReplyCommand($discussionId, $content, User::current()); $command = new PostReplyCommand($discussionId, $content, $user);
Event::fire('Flarum.Api.Actions.Posts.Create.WillExecuteCommand', [$command]); Event::fire('Flarum.Api.Actions.Posts.Create.WillExecuteCommand', [$command]);
$post = $this->commandBus->execute($command); $post = $this->commandBus->execute($command);
// After replying, 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($discussionId, $user, $post->number);
$this->commandBus->execute($command);
}
// Presumably, the post was created successfully. (The command handler // Presumably, the post was created successfully. (The command handler
// would have thrown an exception if not.) We set this post as our // would have thrown an exception if not.) We set this post as our
// document's primary element. // document's primary element.