Find a post by ID, optionally making sure it is visible to a certain
user, or throw an exception.
public findOrFail ( integer $id, |
||
$id | integer | |
$actor | ||
return |
/** * @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 DeleteModeratorNotes $command * * @return ModeratorNotes */ public function handle(DeleteModeratorNotes $command) { $actor = $command->actor; $post = $this->posts->findOrFail($command->postId, $actor); $this->assertCan($actor, 'viewModeratorNotes', $post->discussion); $this->events->fire(new ModeratorNotesWillBeDeleted($post, $actor, $command->data)); $post->moderatorNotes()->delete(); return $post; }
/** * @param DeletePost $command * @return \Flarum\Core\Post * @throws PermissionDeniedException */ public function handle(DeletePost $command) { $actor = $command->actor; $post = $this->posts->findOrFail($command->postId, $actor); $this->assertCan($actor, 'delete', $post); $this->events->fire(new PostWillBeDeleted($post, $actor, $command->data)); $post->delete(); $this->dispatchEventsFor($post, $actor); return $post; }
/** * @param CreateFlag $command * @return Flag * @throws InvalidParameterException */ public function handle(CreateFlag $command) { $actor = $command->actor; $data = $command->data; $postId = array_get($data, 'relationships.post.data.id'); $post = $this->posts->findOrFail($postId, $actor); if (!$post instanceof CommentPost) { throw new InvalidParameterException(); } $this->assertCan($actor, 'flag', $post); Flag::unguard(); $flag = Flag::firstOrNew(['post_id' => $post->id, 'user_id' => $actor->id]); $flag->post_id = $post->id; $flag->user_id = $actor->id; $flag->type = 'user'; $flag->reason = array_get($data, 'attributes.reason'); $flag->reason_detail = array_get($data, 'attributes.reasonDetail'); $flag->time = time(); $flag->save(); return $flag; }
/** * {@inheritdoc} */ protected function data(ServerRequestInterface $request, Document $document) { return $this->posts->findOrFail(array_get($request->getQueryParams(), 'id'), $request->getAttribute('actor')); }