Ejemplo n.º 1
0
 /**
  * @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;
 }
Ejemplo n.º 2
0
 /**
  * @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;
 }
Ejemplo n.º 3
0
 /**
  * @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);
 }
Ejemplo n.º 4
0
 /**
  * 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;
         });
     }
 }
Ejemplo n.º 5
0
 /**
  * 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);
 }