示例#1
0
 /**
  * Get the user results, ready to be serialized and assigned to the
  * document response.
  *
  * @param JsonApiRequest $request
  * @param Document $document
  * @return \Illuminate\Database\Eloquent\Collection
  */
 protected function data(JsonApiRequest $request, Document $document)
 {
     $criteria = new SearchCriteria($request->actor, $request->get('filter.q'), $request->sort);
     $results = $this->searcher->search($criteria, $request->limit, $request->offset, $request->include);
     $this->addPaginationLinks($document, $request, $this->url->toRoute('users.index'), $results->areMoreResults());
     return $results->getResults();
 }
示例#2
0
 /**
  * Get a single user, ready to be serialized and assigned to the JsonApi
  * response.
  *
  * @param \Flarum\Api\JsonApiRequest $request
  * @param \Flarum\Api\JsonApiResponse $response
  * @return \Flarum\Core\Models\Discussion
  */
 protected function data(JsonApiRequest $request, JsonApiResponse $response)
 {
     $id = $request->get('id');
     if (!is_numeric($id)) {
         $id = $this->users->getIdForUsername($id);
     }
     return $this->users->findOrFail($id, $request->actor->getUser());
 }
示例#3
0
 /**
  * Get a single user, ready to be serialized and assigned to the JsonApi
  * response.
  *
  * @param JsonApiRequest $request
  * @param Document $document
  * @return \Flarum\Core\Users\User
  */
 protected function data(JsonApiRequest $request, Document $document)
 {
     $id = $request->get('id');
     if (!is_numeric($id)) {
         $id = $this->users->getIdForUsername($id);
     }
     return $this->users->findOrFail($id, $request->actor);
 }
示例#4
0
 /**
  * Get the discussion results, ready to be serialized and assigned to the
  * document response.
  *
  * @param JsonApiRequest $request
  * @param Document $document
  * @return \Illuminate\Database\Eloquent\Collection
  */
 protected function data(JsonApiRequest $request, Document $document)
 {
     $criteria = new SearchCriteria($request->actor, $request->get('filter.q'), $request->sort);
     $load = array_merge($request->include, ['state']);
     $results = $this->searcher->search($criteria, $request->limit, $request->offset, $load);
     // TODO: add query params (filter, sort, include) to the pagination URLs
     $this->addPaginationLinks($document, $request, $request->http ? $this->url->toRoute('discussions.index') : '', $results->areMoreResults());
     return $results->getResults();
 }
示例#5
0
 /**
  * Get the user results, ready to be serialized and assigned to the
  * document response.
  *
  * @param \Flarum\Api\JsonApiRequest $request
  * @param \Flarum\Api\JsonApiResponse $response
  * @return \Illuminate\Database\Eloquent\Collection
  */
 protected function data(JsonApiRequest $request, JsonApiResponse $response)
 {
     $criteria = new UserSearchCriteria($request->actor->getUser(), $request->get('q'), $request->sort);
     $results = $this->searcher->search($criteria, $request->limit, $request->offset, $request->include);
     if (($total = $results->getTotal()) !== null) {
         $response->content->addMeta('total', $total);
     }
     static::addPaginationLinks($response, $request, route('flarum.api.users.index'), $total ?: $results->areMoreResults());
     return $results->getUsers();
 }
示例#6
0
 protected function getPosts(JsonApiRequest $request, array $where)
 {
     $user = $request->actor->getUser();
     if (isset($where['discussion_id']) && ($near = $request->get('near')) > 1) {
         $offset = $this->posts->getIndexForNumber($where['discussion_id'], $near, $user);
         $offset = max(0, $offset - $request->limit / 2);
     } else {
         $offset = 0;
     }
     return $this->posts->findWhere($where, $user, $request->sort, $request->limit, $offset);
 }
示例#7
0
 /**
  * Create a discussion according to input from the API request.
  *
  * @param JsonApiRequest $request
  * @return \Flarum\Core\Discussions\Discussion
  */
 protected function create(JsonApiRequest $request)
 {
     $actor = $request->actor;
     $discussion = $this->bus->dispatch(new StartDiscussion($actor, $request->get('data')));
     // After creating the discussion, we assume that the user has seen all
     // of the posts in the discussion; thus, we will mark the discussion
     // as read if they are logged in.
     if ($actor->exists) {
         $this->bus->dispatch(new ReadDiscussion($discussion->id, $actor, 1));
     }
     return $discussion;
 }
示例#8
0
 /**
  * Update a discussion according to input from the API request, and return
  * it ready to be serialized and assigned to the JsonApi response.
  *
  * @param \Flarum\Api\JsonApiRequest $request
  * @param \Flarum\Api\JsonApiResponse $response
  * @return \Flarum\Core\Models\Discussion
  */
 protected function data(JsonApiRequest $request, JsonApiResponse $response)
 {
     $user = $request->actor->getUser();
     $discussionId = $request->get('id');
     if ($data = array_except($request->get('data'), ['readNumber'])) {
         $discussion = $this->bus->dispatch(new EditDiscussionCommand($discussionId, $user, $data));
     }
     if ($readNumber = $request->get('data.readNumber')) {
         $state = $this->bus->dispatch(new ReadDiscussionCommand($discussionId, $user, $readNumber));
         $discussion = $state->discussion;
     }
     return $discussion;
 }
示例#9
0
 /**
  * Reply to a discussion according to input from the API request.
  *
  * @param \Flarum\Api\JsonApiRequest $request
  * @param \Flarum\Api\JsonApiResponse $response
  * @return \Flarum\Core\Models\Post
  */
 protected function create(JsonApiRequest $request, JsonApiResponse $response)
 {
     $user = $request->actor->getUser();
     $discussionId = $request->get('data.links.discussion.linkage.id');
     $post = $this->bus->dispatch(new PostReplyCommand($discussionId, $user, $request->get('data')));
     // After replying, we assume that the user has seen all of the posts
     // in the discussion; thus, we will mark the discussion as read if
     // they are logged in.
     if ($user->exists) {
         $this->bus->dispatch(new ReadDiscussionCommand($discussionId, $user, $post->number));
     }
     return $post;
 }
示例#10
0
 /**
  * Get the forum, ready to be serialized and assigned to the JsonApi
  * response.
  *
  * @param JsonApiRequest $request
  * @param Document $document
  * @return \Flarum\Core\Forum
  */
 protected function data(JsonApiRequest $request, Document $document)
 {
     if (!$request->actor->isAdmin()) {
         throw new PermissionDeniedException();
     }
     $config = $request->get('data.attributes.config');
     if (is_array($config)) {
         foreach ($config as $k => $v) {
             $this->settings->set($k, $v);
         }
     }
     return app('flarum.forum');
 }
示例#11
0
 /**
  * Get a single discussion, ready to be serialized and assigned to the
  * JsonApi response.
  *
  * @param \Flarum\Api\JsonApiRequest $request
  * @param \Flarum\Api\JsonApiResponse $response
  * @return \Flarum\Core\Models\Discussion
  */
 protected function data(JsonApiRequest $request, JsonApiResponse $response)
 {
     $user = $request->actor->getUser();
     $discussion = $this->discussions->findOrFail($request->get('id'), $user);
     $discussion->posts_ids = $discussion->posts()->whereCan($user, 'view')->get(['id'])->fetch('id')->all();
     if (in_array('posts', $request->include)) {
         $length = strlen($prefix = 'posts.');
         $relations = array_filter(array_map(function ($relationship) use($prefix, $length) {
             return substr($relationship, 0, $length) === $prefix ? substr($relationship, $length) : false;
         }, $request->include));
         $discussion->posts = $this->getPosts($request, ['discussion_id' => $discussion->id])->load($relations);
     }
     return $discussion;
 }
示例#12
0
 /**
  * Reply to a discussion according to input from the API request.
  *
  * @param JsonApiRequest $request
  * @return \Flarum\Core\Posts\Post
  */
 protected function create(JsonApiRequest $request)
 {
     $actor = $request->actor;
     $discussionId = $request->get('data.relationships.discussion.data.id');
     $post = $this->bus->dispatch(new PostReply($discussionId, $actor, $request->get('data')));
     // After replying, we assume that the user has seen all of the posts
     // in the discussion; thus, we will mark the discussion as read if
     // they are logged in.
     if ($actor->exists) {
         $this->bus->dispatch(new ReadDiscussion($discussionId, $actor, $post->number));
     }
     $discussion = $post->discussion;
     $discussion->posts_ids = $discussion->postsVisibleTo($actor)->orderBy('time')->lists('id');
     return $post;
 }
示例#13
0
 /**
  * @param JsonApiRequest $request
  * @param Document       $document
  *
  * @return \Flarum\Core\Users\User
  */
 protected function data(JsonApiRequest $request, Document $document)
 {
     // TODO: update settings namespace
     // should be loaded from config ... upload folder relative to the flarum base path
     $uploadDir = '/upload/';
     // list of allowed filetypes (empty if all are allowed)
     $allowedTypes = array_filter(explode(',', $this->settings->get('flamure.fileupload.allowed')));
     // list of blocked filetypes (empty if none are blocked)
     $blockedTypes = array_filter(explode(',', $this->settings->get('flamure.fileupload.blocked')));
     // end of config
     $uploadDir = getcwd() . $uploadDir;
     $file = $request->http->getUploadedFiles()['file'];
     $id = $request->get('id');
     $user = $request->actor;
     // check if this type of file is allowed
     $ext = explode('.', $file->getClientFilename());
     $ext = strtolower(end($ext));
     if (count($allowedTypes) > 0 && !in_array($ext, $allowedTypes) || in_array($ext, $blockedTypes)) {
         throw new RuntimeException('Filetype not allowed');
     }
     // generate correct directory
     // unique id is generated then folder structure is build based on it
     // upload/first_2_letters/second_2_letters/last_2_letters
     // to avoid the need to save in database to preserve filename
     // add _ to begining to make sure its valid folder name
     $uid = uniqid();
     $uid = ['_' . $uid[0] . $uid[1], '_' . $uid[2] . $uid[3], '_' . $uid[11] . $uid[12]];
     $currentPath = $uploadDir;
     if (!is_dir($currentPath)) {
         mkdir($currentPath, 0777, true);
     }
     foreach ($uid as $dir) {
         $currentPath .= "{$dir}/";
         if (!is_dir($currentPath)) {
             mkdir($currentPath, 0777, true);
         }
     }
     try {
         $file->moveTo($currentPath . $file->getClientFilename());
         $currentPath = str_replace($uploadDir, '', $currentPath);
         $response = '/upload/' . $currentPath . $file->getClientFilename();
         // send output (must be updated)
         return $response;
     } catch (Exception $e) {
         throw new RuntimeException('Upload failed');
     }
 }
示例#14
0
 /**
  * Get a single discussion, ready to be serialized and assigned to the
  * JsonApi response.
  *
  * @param JsonApiRequest $request
  * @param Document $document
  * @return \Flarum\Core\Discussions\Discussion
  */
 protected function data(JsonApiRequest $request, Document $document)
 {
     $discussionId = $request->get('id');
     $actor = $request->actor;
     $discussion = $this->discussions->findOrFail($discussionId, $actor);
     $discussion->posts_ids = $discussion->postsVisibleTo($actor)->orderBy('time')->lists('id');
     // TODO: Refactor to be simpler, and get posts straight from the
     // discussion's postsVisibleTo relation method.
     if (in_array('posts', $request->include)) {
         $prefixLength = strlen($prefix = 'posts.');
         $postRelations = array_filter(array_map(function ($relation) use($prefix, $prefixLength) {
             return substr($relation, 0, $prefixLength) === $prefix ? substr($relation, $prefixLength) : false;
         }, $request->include));
         $discussion->posts = $this->getPosts($request, ['discussion_id' => $discussion->id])->load($postRelations);
     }
     return $discussion;
 }
示例#15
0
 /**
  * Update a discussion according to input from the API request, and return
  * it ready to be serialized and assigned to the JsonApi response.
  *
  * @param JsonApiRequest $request
  * @param Document $document
  * @return \Flarum\Core\Discussions\Discussion
  */
 protected function data(JsonApiRequest $request, Document $document)
 {
     $actor = $request->actor;
     $discussionId = $request->get('id');
     $data = $request->get('data');
     $discussion = $this->bus->dispatch(new EditDiscussion($discussionId, $actor, $data));
     // TODO: Refactor the ReadDiscussion (state) command into EditDiscussion?
     // That's what extensions will do anyway.
     if ($readNumber = array_get($data, 'attributes.readNumber')) {
         $state = $this->bus->dispatch(new ReadDiscussion($discussionId, $actor, $readNumber));
         $discussion = $state->discussion;
     }
     if ($posts = $discussion->getModifiedPosts()) {
         $discussion->posts_ids = $discussion->postsVisibleTo($actor)->orderBy('time')->lists('id');
         $discussion->posts = array_filter($posts, function ($post) {
             return $post->exists;
         });
         $request->include = array_merge($request->include, ['posts']);
         $request->link = array_merge($request->include, ['posts', 'posts.discussion', 'posts.user']);
     }
     return $discussion;
 }
示例#16
0
 /**
  * Get the post results, ready to be serialized and assigned to the
  * document response.
  *
  * @param \Flarum\Api\JsonApiRequest $request
  * @param \Flarum\Api\JsonApiResponse $response
  * @return \Illuminate\Database\Eloquent\Collection
  */
 protected function data(JsonApiRequest $request, JsonApiResponse $response)
 {
     $postIds = (array) $request->get('ids');
     $user = $request->actor->getUser();
     if (count($postIds)) {
         $posts = $this->posts->findByIds($postIds, $user);
     } else {
         if ($discussionId = $request->get('discussions')) {
             $where['discussion_id'] = $discussionId;
         }
         if ($number = $request->get('number')) {
             $where['number'] = $number;
         }
         if ($userId = $request->get('users')) {
             $where['user_id'] = $userId;
         }
         $posts = $this->getPosts($request, $where);
     }
     return $posts;
 }
示例#17
0
 /**
  * Get the post results, ready to be serialized and assigned to the
  * document response.
  *
  * @param JsonApiRequest $request
  * @param Document $document
  * @return \Illuminate\Database\Eloquent\Collection
  */
 protected function data(JsonApiRequest $request, Document $document)
 {
     $postIds = (array) $request->get('ids');
     $actor = $request->actor;
     if (count($postIds)) {
         $posts = $this->posts->findByIds($postIds, $actor);
     } else {
         $where = [];
         if ($discussionId = $request->get('filter.discussion')) {
             $where['discussion_id'] = $discussionId;
         }
         if ($number = $request->get('filter.number')) {
             $where['number'] = $number;
         }
         if ($userId = $request->get('filter.user')) {
             $where['user_id'] = $userId;
         }
         if ($type = $request->get('filter.type')) {
             $where['type'] = $type;
         }
         $posts = $this->getPosts($request, $where);
     }
     return $posts;
 }
示例#18
0
 /**
  * Register a user according to input from the API request.
  *
  * @param JsonApiRequest $request
  * @return \Flarum\Core\Users\User
  */
 protected function create(JsonApiRequest $request)
 {
     $user = $this->bus->dispatch(new RegisterUser($request->actor, $request->get('data')));
     $request->actor = $user;
     return $user;
 }
示例#19
0
 /**
  * Delete a user's avatar, and return the user ready to be serialized and
  * assigned to the JsonApi response.
  *
  * @param \Flarum\Api\JsonApiRequest $request
  * @param \Flarum\Api\JsonApiResponse $response
  * @return \Flarum\Core\Models\User
  */
 protected function data(JsonApiRequest $request, JsonApiResponse $response)
 {
     return $this->bus->dispatch(new DeleteAvatarCommand($request->get('id'), $request->actor->getUser()));
 }
示例#20
0
 /**
  * Upload an avatar for a user, and return the user ready to be serialized
  * and assigned to the JsonApi response.
  *
  * @param JsonApiRequest $request
  * @param Document $document
  * @return \Flarum\Core\Users\User
  */
 protected function data(JsonApiRequest $request, Document $document)
 {
     return $this->bus->dispatch(new UploadAvatar($request->get('id'), $request->http->getUploadedFiles()['avatar'], $request->actor));
 }
示例#21
0
 /**
  * Delete a user's avatar, and return the user ready to be serialized and
  * assigned to the JsonApi response.
  *
  * @param JsonApiRequest $request
  * @param Document $document
  * @return \Flarum\Core\Users\User
  */
 protected function data(JsonApiRequest $request, Document $document)
 {
     return $this->bus->dispatch(new DeleteAvatar($request->get('id'), $request->actor));
 }
示例#22
0
 /**
  * Get the activity results, ready to be serialized and assigned to the
  * document response.
  *
  * @param \Flarum\Api\JsonApiRequest $request
  * @param \Flarum\Api\JsonApiResponse $response
  * @return \Illuminate\Database\Eloquent\Collection
  */
 protected function data(JsonApiRequest $request, JsonApiResponse $response)
 {
     $actor = $request->actor->getUser();
     $user = $this->users->findOrFail($request->get('users'), $actor);
     return $this->activity->findByUser($user->id, $actor, $request->limit, $request->offset, $request->get('type'))->load($request->include);
 }
示例#23
0
 /**
  * Mark a notification as read, and return it ready to be serialized and
  * assigned to the JsonApi response.
  *
  * @param JsonApiRequest $request
  * @param Document $document
  * @return \Flarum\Core\Notifications\Notification
  */
 protected function data(JsonApiRequest $request, Document $document)
 {
     return $this->bus->dispatch(new ReadNotification($request->get('id'), $request->actor));
 }
示例#24
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);
 }
示例#25
0
 /**
  * Upload an avatar for a user, and return the user ready to be serialized
  * and assigned to the JsonApi response.
  *
  * @param \Flarum\Api\JsonApiRequest $request
  * @param \Flarum\Api\JsonApiResponse $response
  * @return \Flarum\Core\Models\User
  */
 protected function data(JsonApiRequest $request, JsonApiResponse $response)
 {
     return $this->bus->dispatch(new UploadAvatarCommand($request->get('id'), $request->http->file('avatar'), $request->actor->getUser()));
 }
示例#26
0
 /**
  * Register a user according to input from the API request.
  *
  * @param \Flarum\Api\JsonApiRequest $request
  * @param \Flarum\Api\JsonApiResponse $response
  * @return \Flarum\Core\Models\User
  */
 protected function create(JsonApiRequest $request, JsonApiResponse $response)
 {
     return $this->bus->dispatch(new RegisterUserCommand($request->actor->getUser(), $this->forum, $request->get('data')));
 }
示例#27
0
 /**
  * Create a group according to input from the API request.
  *
  * @param JsonApiRequest $request
  * @return \Flarum\Core\Groups\Group
  */
 protected function create(JsonApiRequest $request)
 {
     return $this->bus->dispatch(new CreateGroup($request->actor, $request->get('data')));
 }
示例#28
0
 /**
  * Update a post according to input from the API request, and return it
  * ready to be serialized and assigned to the JsonApi response.
  *
  * @param \Flarum\Api\JsonApiRequest $request
  * @param \Flarum\Api\JsonApiResponse $response
  * @return \Flarum\Core\Models\Post
  */
 protected function data(JsonApiRequest $request, JsonApiResponse $response)
 {
     return $this->bus->dispatch(new EditPostCommand($request->get('id'), $request->actor->getUser(), $request->get('data')));
 }
示例#29
0
 /**
  * @param JsonApiRequest $request
  * @param Document $document
  * @return \Flarum\Core\Tags\Tag
  */
 protected function data(JsonApiRequest $request, Document $document)
 {
     return $this->bus->dispatch(new EditTag($request->get('id'), $request->actor, $request->get('data')));
 }
示例#30
0
 /**
  * Get a single post, ready to be serialized and assigned to the JsonApi
  * response.
  *
  * @param \Flarum\Api\JsonApiRequest $request
  * @param \Flarum\Api\JsonApiResponse $response
  * @return \Flarum\Core\Models\Discussion
  */
 protected function data(JsonApiRequest $request, JsonApiResponse $response)
 {
     return $this->posts->findOrFail($request->get('id'), $request->actor->getUser());
 }