/** * @param EditPost $command * @return \Flarum\Core\Posts\Post * @throws \Flarum\Core\Exceptions\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'])) { $post->assertCan($actor, 'edit'); $post->revise($attributes['content'], $actor); } if (isset($attributes['isHidden'])) { $post->assertCan($actor, 'edit'); if ($attributes['isHidden']) { $post->hide($actor); } else { $post->restore(); } } } event(new PostWillBeSaved($post, $actor, $data)); $post->save(); $this->dispatchEventsFor($post); return $post; }
/** * @param DeletePost $command * @return \Flarum\Core\Posts\Post */ public function handle(DeletePost $command) { $actor = $command->actor; $post = $this->posts->findOrFail($command->postId, $actor); $post->assertCan($actor, 'delete'); event(new PostWillBeDeleted($post, $actor, $command->data)); $post->delete(); $this->dispatchEventsFor($post); return $post; }
/** * @param JsonApiRequest $request * @param array $where * @return \Illuminate\Database\Eloquent\Collection */ protected function getPosts(JsonApiRequest $request, array $where) { $actor = $request->actor; if (isset($where['discussion_id']) && ($near = $request->get('page.near')) > 1) { $offset = $this->posts->getIndexForNumber($where['discussion_id'], $near, $actor); $offset = max(0, $offset - $request->limit / 2); } else { $offset = $request->offset; } return $this->posts->findWhere($where, $actor, $request->sort, $request->limit, $offset); }
/** * Load relevant posts onto each discussion using information from the * search. * * @param Collection $discussions * @param DiscussionSearch $search */ protected function loadRelevantPosts(Collection $discussions, DiscussionSearch $search) { $postIds = []; foreach ($search->getRelevantPostIds() as $relevantPostIds) { $postIds = array_merge($postIds, array_slice($relevantPostIds, 0, 2)); } $posts = $postIds ? $this->posts->findByIds($postIds, $search->getActor())->load('user')->all() : []; foreach ($discussions as $discussion) { $discussion->relevantPosts = array_filter($posts, function ($post) use($discussion) { return $post->discussion_id == $discussion->id; }); } }
/** * Get a single post, ready to be serialized and assigned to the JsonApi * response. * * @param JsonApiRequest $request * @param Document $document * @return \Flarum\Core\Posts\Post */ protected function data(JsonApiRequest $request, Document $document) { return $this->posts->findOrFail($request->get('id'), $request->actor); }