Esempio n. 1
0
 /**
  * @param SearchCriteria $criteria
  * @param int|null $limit
  * @param int $offset
  * @param array $load An array of relationships to load on the results.
  * @return SearchResults
  */
 public function search(SearchCriteria $criteria, $limit = null, $offset = 0, array $load = [])
 {
     $actor = $criteria->actor;
     $query = $this->discussions->query()->whereVisibleTo($actor);
     // Construct an object which represents this search for discussions.
     // Apply gambits to it, sort, and paging criteria. Also give extensions
     // an opportunity to modify it.
     $search = new DiscussionSearch($query->getQuery(), $actor);
     $this->gambits->apply($search, $criteria->query);
     $this->applySort($search, $criteria->sort);
     $this->applyOffset($search, $offset);
     $this->applyLimit($search, $limit + 1);
     // TODO: inject dispatcher
     event(new ConfigureDiscussionSearch($search, $criteria));
     // Execute the search query and retrieve the results. We get one more
     // results than the user asked for, so that we can say if there are more
     // results. If there are, we will get rid of that extra result.
     $discussions = $query->get();
     $areMoreResults = $limit > 0 && $discussions->count() > $limit;
     if ($areMoreResults) {
         $discussions->pop();
     }
     // The relevant posts relationship isn't a typical Eloquent
     // relationship; rather, we need to extract that information from our
     // search object. We will delegate that task and prevent Eloquent
     // from trying to load it.
     if (in_array('relevantPosts', $load)) {
         $this->loadRelevantPosts($discussions, $search);
         $load = array_diff($load, ['relevantPosts', 'relevantPosts.discussion', 'relevantPosts.user']);
     }
     Discussion::setStateUser($actor);
     $discussions->load($load);
     return new SearchResults($discussions, $areMoreResults);
 }
Esempio n. 2
0
 /**
  * @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;
 }
Esempio n. 3
0
 /**
  * @param DeleteDiscussion $command
  * @return \Flarum\Core\Discussion
  * @throws PermissionDeniedException
  */
 public function handle(DeleteDiscussion $command)
 {
     $actor = $command->actor;
     $discussion = $this->discussions->findOrFail($command->discussionId, $actor);
     $this->assertCan($actor, 'delete', $discussion);
     $this->events->fire(new DiscussionWillBeDeleted($discussion, $actor, $command->data));
     $discussion->delete();
     $this->dispatchEventsFor($discussion, $actor);
     return $discussion;
 }
Esempio n. 4
0
 /**
  * {@inheritdoc}
  */
 protected function data(ServerRequestInterface $request, Document $document)
 {
     $discussionId = array_get($request->getQueryParams(), 'id');
     $actor = $request->getAttribute('actor');
     $include = $this->extractInclude($request);
     $discussion = $this->discussions->findOrFail($discussionId, $actor);
     if (in_array('posts', $include)) {
         $postRelationships = $this->getPostRelationships($include);
         $this->includePosts($discussion, $request, $postRelationships);
     }
     return $discussion;
 }
Esempio n. 5
0
 /**
  * @param ReadDiscussion $command
  * @return \Flarum\Core\DiscussionState
  * @throws \Flarum\Core\Exception\PermissionDeniedException
  */
 public function handle(ReadDiscussion $command)
 {
     $actor = $command->actor;
     $this->assertRegistered($actor);
     $discussion = $this->discussions->findOrFail($command->discussionId, $actor);
     $state = $discussion->stateFor($actor);
     $state->read($command->readNumber);
     $this->events->fire(new DiscussionStateWillBeSaved($state));
     $state->save();
     $this->dispatchEventsFor($state);
     return $state;
 }
Esempio n. 6
0
 /**
  * {@inheritdoc}
  */
 protected function conditions(AbstractSearch $search, array $matches, $negate)
 {
     if (!$search instanceof DiscussionSearch) {
         throw new LogicException('This gambit can only be applied on a DiscussionSearch');
     }
     $actor = $search->getActor();
     if ($actor->exists) {
         $readIds = $this->discussions->getReadIds($actor);
         $search->getQuery()->where(function ($query) use($readIds, $negate, $actor) {
             if (!$negate) {
                 $query->whereNotIn('id', $readIds)->where('last_time', '>', $actor->read_time ?: 0);
             } else {
                 $query->whereIn('id', $readIds)->orWhere('last_time', '<=', $actor->read_time ?: 0);
             }
         });
     }
 }
Esempio n. 7
0
 /**
  * @param EditDiscussion $command
  * @return \Flarum\Core\Discussion
  * @throws PermissionDeniedException
  */
 public function handle(EditDiscussion $command)
 {
     $actor = $command->actor;
     $data = $command->data;
     $attributes = array_get($data, 'attributes', []);
     $discussion = $this->discussions->findOrFail($command->discussionId, $actor);
     if (isset($attributes['title'])) {
         $this->assertCan($actor, 'rename', $discussion);
         $discussion->rename($attributes['title']);
     }
     if (isset($attributes['isHidden'])) {
         $this->assertCan($actor, 'hide', $discussion);
         if ($attributes['isHidden']) {
             $discussion->hide($actor);
         } else {
             $discussion->restore();
         }
     }
     $this->events->fire(new DiscussionWillBeSaved($discussion, $actor, $data));
     $this->validator->assertValid($discussion->getDirty());
     $discussion->save();
     $this->dispatchEventsFor($discussion, $actor);
     return $discussion;
 }