/** * @param EditPost $command * @return \Flarum\Core\Post * @throws \Flarum\Core\Exception\PermissionDeniedException */ public function handle(EditPost $command) { $actor = $command->actor; $data = $command->data; $post = $this->posts->findOrFail($command->postId, $actor); if ($post instanceof CommentPost) { $attributes = array_get($data, 'attributes', []); if (isset($attributes['content'])) { $this->assertCan($actor, 'edit', $post); $post->revise($attributes['content'], $actor); } if (isset($attributes['isHidden'])) { $this->assertCan($actor, 'edit', $post); if ($attributes['isHidden']) { $post->hide($actor); } else { $post->restore(); } } } $this->events->fire(new PostWillBeSaved($post, $actor, $data)); $this->validator->assertValid($post->getDirty()); $post->save(); $this->dispatchEventsFor($post, $actor); return $post; }
/** * @param PostReply $command * @return CommentPost * @throws \Flarum\Core\Exception\PermissionDeniedException */ public function handle(PostReply $command) { $actor = $command->actor; // Make sure the user has permission to reply to this discussion. First, // make sure the discussion exists and that the user has permission to // view it; if not, fail with a ModelNotFound exception so we don't give // away the existence of the discussion. If the user is allowed to view // it, check if they have permission to reply. $discussion = $this->discussions->findOrFail($command->discussionId, $actor); $this->assertCan($actor, 'reply', $discussion); // Create a new Post entity, persist it, and dispatch domain events. // Before persistence, though, fire an event to give plugins an // opportunity to alter the post entity based on data in the command. $post = CommentPost::reply($discussion->id, array_get($command->data, 'attributes.content'), $actor->id, $command->ipAddress); if ($actor->isAdmin() && ($time = array_get($command->data, 'attributes.time'))) { $post->time = new DateTime($time); } $this->events->fire(new PostWillBeSaved($post, $actor, $command->data)); $this->validator->assertValid($post->getAttributes()); $post->save(); $this->notifications->onePerUser(function () use($post, $actor) { $this->dispatchEventsFor($post, $actor); }); return $post; }