From 4e3b5f7c2cf32edd460e547a393c9980767e8c38 Mon Sep 17 00:00:00 2001 From: Sami Mazouz Date: Sat, 10 Feb 2024 16:05:31 +0100 Subject: [PATCH] chore: delete dead code --- .../Controller/CreateDiscussionController.php | 62 ------ .../Api/Controller/CreatePostController.php | 66 ------- .../Api/Controller/CreateUserController.php | 36 ---- .../Controller/DeleteDiscussionController.php | 35 ---- .../Api/Controller/DeletePostController.php | 31 --- .../Api/Controller/DeleteUserController.php | 31 --- .../Controller/ListDiscussionsController.php | 99 ---------- .../ListNotificationsController.php | 100 ---------- .../Api/Controller/ListPostsController.php | 124 ------------ .../Api/Controller/ListUsersController.php | 81 -------- .../Controller/ShowDiscussionController.php | 178 ------------------ .../Api/Controller/ShowForumController.php | 32 ---- .../src/Api/Controller/ShowPostController.php | 48 ----- .../src/Api/Controller/ShowUserController.php | 51 ----- .../UpdateNotificationController.php | 39 ---- .../Api/Controller/UpdatePostController.php | 49 ----- .../Api/Controller/UpdateUserController.php | 58 ------ .../Discussion/Command/DeleteDiscussion.php | 22 --- .../Command/DeleteDiscussionHandler.php | 46 ----- .../src/Discussion/Command/EditDiscussion.php | 22 --- .../Command/EditDiscussionHandler.php | 67 ------- .../Discussion/Command/StartDiscussion.php | 22 --- .../Command/StartDiscussionHandler.php | 83 -------- .../core/src/Group/Command/CreateGroup.php | 21 --- .../src/Group/Command/CreateGroupHandler.php | 57 ------ .../core/src/Group/Command/DeleteGroup.php | 22 --- .../src/Group/Command/DeleteGroupHandler.php | 46 ----- .../core/src/Group/Command/EditGroup.php | 22 --- .../src/Group/Command/EditGroupHandler.php | 70 ------- .../core/src/Post/Command/DeletePost.php | 22 --- .../src/Post/Command/DeletePostHandler.php | 46 ----- framework/core/src/Post/Command/EditPost.php | 22 --- .../core/src/Post/Command/EditPostHandler.php | 71 ------- framework/core/src/Post/Command/PostReply.php | 24 --- .../src/Post/Command/PostReplyHandler.php | 80 -------- .../core/src/User/Command/DeleteUser.php | 22 --- .../src/User/Command/DeleteUserHandler.php | 49 ----- framework/core/src/User/Command/EditUser.php | 22 --- .../core/src/User/Command/EditUserHandler.php | 128 ------------- .../core/src/User/Command/RegisterUser.php | 21 --- .../src/User/Command/RegisterUserHandler.php | 154 --------------- 41 files changed, 2281 deletions(-) delete mode 100644 framework/core/src/Api/Controller/CreateDiscussionController.php delete mode 100644 framework/core/src/Api/Controller/CreatePostController.php delete mode 100644 framework/core/src/Api/Controller/CreateUserController.php delete mode 100644 framework/core/src/Api/Controller/DeleteDiscussionController.php delete mode 100644 framework/core/src/Api/Controller/DeletePostController.php delete mode 100644 framework/core/src/Api/Controller/DeleteUserController.php delete mode 100644 framework/core/src/Api/Controller/ListDiscussionsController.php delete mode 100644 framework/core/src/Api/Controller/ListNotificationsController.php delete mode 100644 framework/core/src/Api/Controller/ListPostsController.php delete mode 100644 framework/core/src/Api/Controller/ListUsersController.php delete mode 100644 framework/core/src/Api/Controller/ShowDiscussionController.php delete mode 100644 framework/core/src/Api/Controller/ShowForumController.php delete mode 100644 framework/core/src/Api/Controller/ShowPostController.php delete mode 100644 framework/core/src/Api/Controller/ShowUserController.php delete mode 100644 framework/core/src/Api/Controller/UpdateNotificationController.php delete mode 100644 framework/core/src/Api/Controller/UpdatePostController.php delete mode 100644 framework/core/src/Api/Controller/UpdateUserController.php delete mode 100644 framework/core/src/Discussion/Command/DeleteDiscussion.php delete mode 100644 framework/core/src/Discussion/Command/DeleteDiscussionHandler.php delete mode 100644 framework/core/src/Discussion/Command/EditDiscussion.php delete mode 100644 framework/core/src/Discussion/Command/EditDiscussionHandler.php delete mode 100644 framework/core/src/Discussion/Command/StartDiscussion.php delete mode 100644 framework/core/src/Discussion/Command/StartDiscussionHandler.php delete mode 100644 framework/core/src/Group/Command/CreateGroup.php delete mode 100644 framework/core/src/Group/Command/CreateGroupHandler.php delete mode 100644 framework/core/src/Group/Command/DeleteGroup.php delete mode 100644 framework/core/src/Group/Command/DeleteGroupHandler.php delete mode 100644 framework/core/src/Group/Command/EditGroup.php delete mode 100644 framework/core/src/Group/Command/EditGroupHandler.php delete mode 100644 framework/core/src/Post/Command/DeletePost.php delete mode 100644 framework/core/src/Post/Command/DeletePostHandler.php delete mode 100644 framework/core/src/Post/Command/EditPost.php delete mode 100644 framework/core/src/Post/Command/EditPostHandler.php delete mode 100644 framework/core/src/Post/Command/PostReply.php delete mode 100644 framework/core/src/Post/Command/PostReplyHandler.php delete mode 100644 framework/core/src/User/Command/DeleteUser.php delete mode 100644 framework/core/src/User/Command/DeleteUserHandler.php delete mode 100644 framework/core/src/User/Command/EditUser.php delete mode 100644 framework/core/src/User/Command/EditUserHandler.php delete mode 100644 framework/core/src/User/Command/RegisterUser.php delete mode 100644 framework/core/src/User/Command/RegisterUserHandler.php diff --git a/framework/core/src/Api/Controller/CreateDiscussionController.php b/framework/core/src/Api/Controller/CreateDiscussionController.php deleted file mode 100644 index fe5a28ee6..000000000 --- a/framework/core/src/Api/Controller/CreateDiscussionController.php +++ /dev/null @@ -1,62 +0,0 @@ -getAttribute('ipAddress'); - - $discussion = $this->bus->dispatch( - new StartDiscussion($actor, Arr::get($request->getParsedBody(), 'data', []), $ipAddress) - ); - - // After creating the discussion, we assume that the user has seen all - // 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) - ); - } - - $this->loadRelations(new Collection([$discussion]), $this->extractInclude($request), $request); - - return $discussion; - } -} diff --git a/framework/core/src/Api/Controller/CreatePostController.php b/framework/core/src/Api/Controller/CreatePostController.php deleted file mode 100644 index 717b5ab4d..000000000 --- a/framework/core/src/Api/Controller/CreatePostController.php +++ /dev/null @@ -1,66 +0,0 @@ -getParsedBody(), 'data', []); - $discussionId = (int) Arr::get($data, 'relationships.discussion.data.id'); - $ipAddress = $request->getAttribute('ipAddress'); - - /** @var CommentPost $post */ - $post = $this->bus->dispatch( - new PostReply($discussionId, $actor, $data, $ipAddress) - ); - - // 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->setRelation('posts', $discussion->posts()->whereVisibleTo($actor)->orderBy('created_at')->pluck('id')); - - $this->loadRelations($post->newCollection([$post]), $this->extractInclude($request), $request); - - return $post; - } -} diff --git a/framework/core/src/Api/Controller/CreateUserController.php b/framework/core/src/Api/Controller/CreateUserController.php deleted file mode 100644 index 03c091607..000000000 --- a/framework/core/src/Api/Controller/CreateUserController.php +++ /dev/null @@ -1,36 +0,0 @@ -bus->dispatch( - new RegisterUser(RequestUtil::getActor($request), Arr::get($request->getParsedBody(), 'data', [])) - ); - } -} diff --git a/framework/core/src/Api/Controller/DeleteDiscussionController.php b/framework/core/src/Api/Controller/DeleteDiscussionController.php deleted file mode 100644 index 99319e4bd..000000000 --- a/framework/core/src/Api/Controller/DeleteDiscussionController.php +++ /dev/null @@ -1,35 +0,0 @@ -getQueryParams(), 'id'); - $actor = RequestUtil::getActor($request); - $input = $request->getParsedBody(); - - $this->bus->dispatch( - new DeleteDiscussion($id, $actor, $input) - ); - } -} diff --git a/framework/core/src/Api/Controller/DeletePostController.php b/framework/core/src/Api/Controller/DeletePostController.php deleted file mode 100644 index 3e61c55f8..000000000 --- a/framework/core/src/Api/Controller/DeletePostController.php +++ /dev/null @@ -1,31 +0,0 @@ -bus->dispatch( - new DeletePost(Arr::get($request->getQueryParams(), 'id'), RequestUtil::getActor($request)) - ); - } -} diff --git a/framework/core/src/Api/Controller/DeleteUserController.php b/framework/core/src/Api/Controller/DeleteUserController.php deleted file mode 100644 index 07329b6bc..000000000 --- a/framework/core/src/Api/Controller/DeleteUserController.php +++ /dev/null @@ -1,31 +0,0 @@ -bus->dispatch( - new DeleteUser(Arr::get($request->getQueryParams(), 'id'), RequestUtil::getActor($request)) - ); - } -} diff --git a/framework/core/src/Api/Controller/ListDiscussionsController.php b/framework/core/src/Api/Controller/ListDiscussionsController.php deleted file mode 100644 index 208f656ae..000000000 --- a/framework/core/src/Api/Controller/ListDiscussionsController.php +++ /dev/null @@ -1,99 +0,0 @@ - 'desc']; - - public array $sortFields = ['lastPostedAt', 'commentCount', 'createdAt']; - - public function __construct( - protected SearchManager $search, - protected UrlGenerator $url - ) { - } - - protected function data(ServerRequestInterface $request, Document $document): iterable - { - $actor = RequestUtil::getActor($request); - $filters = $this->extractFilter($request); - $sort = $this->extractSort($request); - $sortIsDefault = $this->sortIsDefault($request); - - $limit = $this->extractLimit($request); - $offset = $this->extractOffset($request); - $include = array_merge($this->extractInclude($request), ['state']); - - $results = $this->search->query( - Discussion::class, - new SearchCriteria($actor, $filters, $limit, $offset, $sort, $sortIsDefault) - ); - - $this->addPaginationData( - $document, - $request, - $this->url->to('api')->route('discussions.index'), - $results->areMoreResults() ? null : 0 - ); - - Discussion::setStateUser($actor); - - // Eager load groups for use in the policies (isAdmin check) - if (in_array('mostRelevantPost.user', $include)) { - $include[] = 'mostRelevantPost.user.groups'; - - // If the first level of the relationship wasn't explicitly included, - // add it so the code below can look for it - if (! in_array('mostRelevantPost', $include)) { - $include[] = 'mostRelevantPost'; - } - } - - $results = $results->getResults(); - - $this->loadRelations($results, $include, $request); - - if ($relations = array_intersect($include, ['firstPost', 'lastPost', 'mostRelevantPost'])) { - foreach ($results as $discussion) { - foreach ($relations as $relation) { - if ($discussion->$relation) { - $discussion->$relation->discussion = $discussion; - } - } - } - } - - return $results; - } -} diff --git a/framework/core/src/Api/Controller/ListNotificationsController.php b/framework/core/src/Api/Controller/ListNotificationsController.php deleted file mode 100644 index 6481274d1..000000000 --- a/framework/core/src/Api/Controller/ListNotificationsController.php +++ /dev/null @@ -1,100 +0,0 @@ -assertRegistered(); - - $actor->markNotificationsAsRead()->save(); - - $limit = $this->extractLimit($request); - $offset = $this->extractOffset($request); - $include = $this->extractInclude($request); - - if (! in_array('subject', $include)) { - $include[] = 'subject'; - } - - $notifications = $this->notifications->findByUser($actor, $limit + 1, $offset); - - $this->loadRelations($notifications, array_diff($include, ['subject.discussion']), $request); - - $notifications = $notifications->all(); - - $areMoreResults = false; - - if (count($notifications) > $limit) { - array_pop($notifications); - $areMoreResults = true; - } - - $this->addPaginationData( - $document, - $request, - $this->url->to('api')->route('notifications.index'), - $areMoreResults ? null : 0 - ); - - if (in_array('subject.discussion', $include)) { - $this->loadSubjectDiscussions($notifications); - } - - return $notifications; - } - - /** - * @param \Flarum\Notification\Notification[] $notifications - */ - private function loadSubjectDiscussions(array $notifications): void - { - $ids = []; - - foreach ($notifications as $notification) { - if ($notification->subject && ($discussionId = $notification->subject->getAttribute('discussion_id'))) { - $ids[] = $discussionId; - } - } - - $discussions = Discussion::query()->find(array_unique($ids)); - - foreach ($notifications as $notification) { - if ($notification->subject && ($discussionId = $notification->subject->getAttribute('discussion_id'))) { - $notification->subject->setRelation('discussion', $discussions->find($discussionId)); - } - } - } -} diff --git a/framework/core/src/Api/Controller/ListPostsController.php b/framework/core/src/Api/Controller/ListPostsController.php deleted file mode 100644 index 4c419bb64..000000000 --- a/framework/core/src/Api/Controller/ListPostsController.php +++ /dev/null @@ -1,124 +0,0 @@ -extractFilter($request); - $sort = $this->extractSort($request); - $sortIsDefault = $this->sortIsDefault($request); - - $limit = $this->extractLimit($request); - $offset = $this->extractOffset($request); - $include = $this->extractInclude($request); - - $results = $this->search->query( - Post::class, - new SearchCriteria($actor, $filters, $limit, $offset, $sort, $sortIsDefault) - ); - - $document->addPaginationLinks( - $this->url->to('api')->route('posts.index'), - $request->getQueryParams(), - $offset, - $limit, - $results->areMoreResults() ? null : 0 - ); - - // Eager load discussion for use in the policies, - // eager loading does not affect the JSON response, - // the response only includes relations included in the request. - if (! in_array('discussion', $include)) { - $include[] = 'discussion'; - } - - if (in_array('user', $include)) { - $include[] = 'user.groups'; - } - - $results = $results->getResults(); - - $this->loadRelations($results, $include, $request); - - return $results; - } - - /** - * @link https://github.com/flarum/framework/pull/3506 - */ - protected function extractSort(ServerRequestInterface $request): ?array - { - $sort = []; - - foreach ((parent::extractSort($request) ?: []) as $field => $direction) { - $sort["posts.$field"] = $direction; - } - - return $sort; - } - - protected function extractOffset(ServerRequestInterface $request): int - { - $actor = RequestUtil::getActor($request); - $queryParams = $request->getQueryParams(); - $sort = $this->extractSort($request); - $limit = $this->extractLimit($request); - $filter = $this->extractFilter($request); - - if (($near = Arr::get($queryParams, 'page.near')) > 1) { - if (count($filter) > 1 || ! isset($filter['discussion']) || $sort) { - throw new InvalidParameterException( - 'You can only use page[near] with filter[discussion] and the default sort order' - ); - } - - $offset = $this->posts->getIndexForNumber((int) $filter['discussion'], $near, $actor); - - return max(0, $offset - $limit / 2); - } - - return parent::extractOffset($request); - } -} diff --git a/framework/core/src/Api/Controller/ListUsersController.php b/framework/core/src/Api/Controller/ListUsersController.php deleted file mode 100644 index 0547afc71..000000000 --- a/framework/core/src/Api/Controller/ListUsersController.php +++ /dev/null @@ -1,81 +0,0 @@ -assertCan('searchUsers'); - - if (! $actor->hasPermission('user.viewLastSeenAt')) { - // If a user cannot see everyone's last online date, we prevent them from sorting by it - // Otherwise this sort field would defeat the privacy setting discloseOnline - // We use remove instead of add so that extensions can still completely disable the sort using the extender - $this->removeSortField('lastSeenAt'); - } - - $filters = $this->extractFilter($request); - $sort = $this->extractSort($request); - $sortIsDefault = $this->sortIsDefault($request); - - $limit = $this->extractLimit($request); - $offset = $this->extractOffset($request); - $include = $this->extractInclude($request); - - $results = $this->search->query( - User::class, - new SearchCriteria($actor, $filters, $limit, $offset, $sort, $sortIsDefault) - ); - - $document->addPaginationLinks( - $this->url->to('api')->route('users.index'), - $request->getQueryParams(), - $offset, - $limit, - $results->areMoreResults() ? null : 0 - ); - - $results = $results->getResults(); - - $this->loadRelations($results, $include, $request); - - return $results; - } -} diff --git a/framework/core/src/Api/Controller/ShowDiscussionController.php b/framework/core/src/Api/Controller/ShowDiscussionController.php deleted file mode 100644 index cabfe79cb..000000000 --- a/framework/core/src/Api/Controller/ShowDiscussionController.php +++ /dev/null @@ -1,178 +0,0 @@ -getQueryParams(), 'id'); - $actor = RequestUtil::getActor($request); - $include = $this->extractInclude($request); - - if (Arr::get($request->getQueryParams(), 'bySlug', false)) { - $discussion = $this->slugManager->forResource(Discussion::class)->fromSlug($discussionId, $actor); - } else { - $discussion = $this->discussions->findOrFail($discussionId, $actor); - } - - // If posts is included or a sub relation of post is included. - if (in_array('posts', $include) || Str::contains(implode(',', $include), 'posts.')) { - $postRelationships = $this->getPostRelationships($include); - - $this->includePosts($discussion, $request, $postRelationships); - } - - $this->loadRelations(new Collection([$discussion]), array_filter($include, function ($relationship) { - return ! Str::startsWith($relationship, 'posts'); - }), $request); - - return $discussion; - } - - private function includePosts(Discussion $discussion, ServerRequestInterface $request, array $include): void - { - $actor = RequestUtil::getActor($request); - $limit = $this->extractLimit($request); - $offset = $this->getPostsOffset($request, $discussion, $limit); - - $allPosts = $this->loadPostIds($discussion, $actor); - $loadedPosts = $this->loadPosts($discussion, $actor, $offset, $limit, $include, $request); - - array_splice($allPosts, $offset, $limit, $loadedPosts); - - $discussion->setRelation('posts', (new Post)->newCollection($allPosts)); - } - - private function loadPostIds(Discussion $discussion, User $actor): array - { - return $discussion->posts()->whereVisibleTo($actor)->orderBy('number')->pluck('id')->all(); - } - - private function getPostRelationships(array $include): array - { - $prefixLength = strlen($prefix = 'posts.'); - $relationships = []; - - foreach ($include as $relationship) { - if (substr($relationship, 0, $prefixLength) === $prefix) { - $relationships[] = substr($relationship, $prefixLength); - } - } - - return $relationships; - } - - private function getPostsOffset(ServerRequestInterface $request, Discussion $discussion, int $limit): int - { - $queryParams = $request->getQueryParams(); - $actor = RequestUtil::getActor($request); - - if (($near = Arr::get($queryParams, 'page.near')) > 1) { - $offset = $this->posts->getIndexForNumber($discussion->id, $near, $actor); - $offset = max(0, $offset - $limit / 2); - } else { - $offset = $this->extractOffset($request); - } - - return $offset; - } - - private function loadPosts(Discussion $discussion, User $actor, int $offset, int $limit, array $include, ServerRequestInterface $request): array - { - /** @var Builder $query */ - $query = $discussion->posts()->whereVisibleTo($actor); - - $query->orderBy('number')->skip($offset)->take($limit); - - $posts = $query->get(); - - /** @var Post $post */ - foreach ($posts as $post) { - $post->setRelation('discussion', $discussion); - } - - $this->loadRelations($posts, $include, $request); - - return $posts->all(); - } - - protected function getRelationsToLoad(Collection $models): array - { - $addedRelations = parent::getRelationsToLoad($models); - - if ($models->first() instanceof Discussion) { - return $addedRelations; - } - - return $this->getPostRelationships($addedRelations); - } - - protected function getRelationCallablesToLoad(Collection $models): array - { - $addedCallableRelations = parent::getRelationCallablesToLoad($models); - - if ($models->first() instanceof Discussion) { - return $addedCallableRelations; - } - - $postCallableRelationships = $this->getPostRelationships(array_keys($addedCallableRelations)); - - $relationCallables = array_intersect_key($addedCallableRelations, array_flip(array_map(function ($relation) { - return "posts.$relation"; - }, $postCallableRelationships))); - - // remove posts. prefix from keys - return array_combine(array_map(function ($relation) { - return substr($relation, 6); - }, array_keys($relationCallables)), array_values($relationCallables)); - } -} diff --git a/framework/core/src/Api/Controller/ShowForumController.php b/framework/core/src/Api/Controller/ShowForumController.php deleted file mode 100644 index 677c9b18e..000000000 --- a/framework/core/src/Api/Controller/ShowForumController.php +++ /dev/null @@ -1,32 +0,0 @@ -api - ->forResource(ForumResource::class) - ->forEndpoint(Show::class) - ->handle($request); - } -} diff --git a/framework/core/src/Api/Controller/ShowPostController.php b/framework/core/src/Api/Controller/ShowPostController.php deleted file mode 100644 index 25f7715b0..000000000 --- a/framework/core/src/Api/Controller/ShowPostController.php +++ /dev/null @@ -1,48 +0,0 @@ -posts->findOrFail(Arr::get($request->getQueryParams(), 'id'), RequestUtil::getActor($request)); - - $include = $this->extractInclude($request); - - $this->loadRelations(new Collection([$post]), $include, $request); - - return $post; - } -} diff --git a/framework/core/src/Api/Controller/ShowUserController.php b/framework/core/src/Api/Controller/ShowUserController.php deleted file mode 100644 index 779d5e5f0..000000000 --- a/framework/core/src/Api/Controller/ShowUserController.php +++ /dev/null @@ -1,51 +0,0 @@ -getQueryParams(), 'id'); - $actor = RequestUtil::getActor($request); - - if (Arr::get($request->getQueryParams(), 'bySlug', false)) { - $user = $this->slugManager->forResource(User::class)->fromSlug($id, $actor); - } else { - $user = $this->users->findOrFail($id, $actor); - } - - if ($actor->id === $user->id) { - $this->serializer = CurrentUserSerializer::class; - } - - return $user; - } -} diff --git a/framework/core/src/Api/Controller/UpdateNotificationController.php b/framework/core/src/Api/Controller/UpdateNotificationController.php deleted file mode 100644 index 153f37ced..000000000 --- a/framework/core/src/Api/Controller/UpdateNotificationController.php +++ /dev/null @@ -1,39 +0,0 @@ -getQueryParams(), 'id'); - $actor = RequestUtil::getActor($request); - - return $this->bus->dispatch( - new ReadNotification($id, $actor) - ); - } -} diff --git a/framework/core/src/Api/Controller/UpdatePostController.php b/framework/core/src/Api/Controller/UpdatePostController.php deleted file mode 100644 index 0935836b9..000000000 --- a/framework/core/src/Api/Controller/UpdatePostController.php +++ /dev/null @@ -1,49 +0,0 @@ -getQueryParams(), 'id'); - $actor = RequestUtil::getActor($request); - $data = Arr::get($request->getParsedBody(), 'data', []); - - $post = $this->bus->dispatch( - new EditPost($id, $actor, $data) - ); - - $this->loadRelations($post->newCollection([$post]), $this->extractInclude($request), $request); - - return $post; - } -} diff --git a/framework/core/src/Api/Controller/UpdateUserController.php b/framework/core/src/Api/Controller/UpdateUserController.php deleted file mode 100644 index 1651880fc..000000000 --- a/framework/core/src/Api/Controller/UpdateUserController.php +++ /dev/null @@ -1,58 +0,0 @@ -getQueryParams(), 'id'); - $actor = RequestUtil::getActor($request); - $data = Arr::get($request->getParsedBody(), 'data', []); - - if ($actor->id == $id) { - $this->serializer = CurrentUserSerializer::class; - } - - // Require the user's current password if they are attempting to change - // their own email address. - if (isset($data['attributes']['email']) && $actor->id == $id) { - $password = (string) Arr::get($request->getParsedBody(), 'meta.password'); - - if (! $actor->checkPassword($password)) { - throw new NotAuthenticatedException; - } - } - - return $this->bus->dispatch( - new EditUser($id, $actor, $data) - ); - } -} diff --git a/framework/core/src/Discussion/Command/DeleteDiscussion.php b/framework/core/src/Discussion/Command/DeleteDiscussion.php deleted file mode 100644 index 805a63fec..000000000 --- a/framework/core/src/Discussion/Command/DeleteDiscussion.php +++ /dev/null @@ -1,22 +0,0 @@ -actor; - - $discussion = $this->discussions->findOrFail($command->discussionId, $actor); - - $actor->assertCan('delete', $discussion); - - $this->events->dispatch( - new Deleting($discussion, $actor, $command->data) - ); - - $discussion->delete(); - - $this->dispatchEventsFor($discussion, $actor); - - return $discussion; - } -} diff --git a/framework/core/src/Discussion/Command/EditDiscussion.php b/framework/core/src/Discussion/Command/EditDiscussion.php deleted file mode 100644 index e49a1bd51..000000000 --- a/framework/core/src/Discussion/Command/EditDiscussion.php +++ /dev/null @@ -1,22 +0,0 @@ -actor; - $data = $command->data; - $attributes = Arr::get($data, 'attributes', []); - - $discussion = $this->discussions->findOrFail($command->discussionId, $actor); - - if (isset($attributes['title'])) { - $actor->assertCan('rename', $discussion); - - $discussion->rename($attributes['title']); - } - - if (isset($attributes['isHidden'])) { - $actor->assertCan('hide', $discussion); - - if ($attributes['isHidden']) { - $discussion->hide($actor); - } else { - $discussion->restore(); - } - } - - $this->events->dispatch( - new Saving($discussion, $actor, $data) - ); - - $this->validator->assertValid($discussion->getDirty()); - - $discussion->save(); - - $this->dispatchEventsFor($discussion, $actor); - - return $discussion; - } -} diff --git a/framework/core/src/Discussion/Command/StartDiscussion.php b/framework/core/src/Discussion/Command/StartDiscussion.php deleted file mode 100644 index e3d8756bf..000000000 --- a/framework/core/src/Discussion/Command/StartDiscussion.php +++ /dev/null @@ -1,22 +0,0 @@ -actor; - $data = $command->data; - $ipAddress = $command->ipAddress; - - $actor->assertCan('startDiscussion'); - - // Create a new Discussion entity, persist it, and dispatch domain - // events. Before persistence, though, fire an event to give plugins - // an opportunity to alter the discussion entity based on data in the - // command they may have passed through in the controller. - $discussion = Discussion::start( - Arr::get($data, 'attributes.title'), - $actor - ); - - $this->events->dispatch( - new Saving($discussion, $actor, $data) - ); - - $this->validator->assertValid($discussion->getAttributes()); - - $discussion->save(); - - // Now that the discussion has been created, we can add the first post. - // We will do this by running the PostReply command. - try { - $post = $this->bus->dispatch( - new PostReply($discussion->id, $actor, $data, $ipAddress, true) - ); - } catch (Exception $e) { - $discussion->delete(); - - throw $e; - } - - // Before we dispatch events, refresh our discussion instance's - // attributes as posting the reply will have changed some of them (e.g. - // last_time.) - $discussion->setRawAttributes($post->discussion->getAttributes(), true); - $discussion->setFirstPost($post); - $discussion->setLastPost($post); - - $this->dispatchEventsFor($discussion, $actor); - - $discussion->save(); - - return $discussion; - } -} diff --git a/framework/core/src/Group/Command/CreateGroup.php b/framework/core/src/Group/Command/CreateGroup.php deleted file mode 100644 index 247d23578..000000000 --- a/framework/core/src/Group/Command/CreateGroup.php +++ /dev/null @@ -1,21 +0,0 @@ -actor; - $data = $command->data; - - $actor->assertRegistered(); - $actor->assertCan('createGroup'); - - $group = Group::build( - Arr::get($data, 'attributes.nameSingular'), - Arr::get($data, 'attributes.namePlural'), - Arr::get($data, 'attributes.color'), - Arr::get($data, 'attributes.icon'), - Arr::get($data, 'attributes.isHidden', false) - ); - - $this->events->dispatch( - new Saving($group, $actor, $data) - ); - - $this->validator->assertValid($group->getAttributes()); - - $group->save(); - - $this->dispatchEventsFor($group, $actor); - - return $group; - } -} diff --git a/framework/core/src/Group/Command/DeleteGroup.php b/framework/core/src/Group/Command/DeleteGroup.php deleted file mode 100644 index 7c1aadfad..000000000 --- a/framework/core/src/Group/Command/DeleteGroup.php +++ /dev/null @@ -1,22 +0,0 @@ -actor; - - $group = $this->groups->findOrFail($command->groupId, $actor); - - $actor->assertCan('delete', $group); - - $this->events->dispatch( - new Deleting($group, $actor, $command->data) - ); - - $group->delete(); - - $this->dispatchEventsFor($group, $actor); - - return $group; - } -} diff --git a/framework/core/src/Group/Command/EditGroup.php b/framework/core/src/Group/Command/EditGroup.php deleted file mode 100644 index 929107ee1..000000000 --- a/framework/core/src/Group/Command/EditGroup.php +++ /dev/null @@ -1,22 +0,0 @@ -actor; - $data = $command->data; - - $group = $this->groups->findOrFail($command->groupId, $actor); - - $actor->assertCan('edit', $group); - - $attributes = Arr::get($data, 'attributes', []); - - if (isset($attributes['nameSingular']) && isset($attributes['namePlural'])) { - $group->rename($attributes['nameSingular'], $attributes['namePlural']); - } - - if (isset($attributes['color'])) { - $group->color = $attributes['color']; - } - - if (isset($attributes['icon'])) { - $group->icon = $attributes['icon']; - } - - if (isset($attributes['isHidden'])) { - $group->is_hidden = $attributes['isHidden']; - } - - $this->events->dispatch( - new Saving($group, $actor, $data) - ); - - $this->validator->assertValid($group->getDirty()); - - $group->save(); - - $this->dispatchEventsFor($group, $actor); - - return $group; - } -} diff --git a/framework/core/src/Post/Command/DeletePost.php b/framework/core/src/Post/Command/DeletePost.php deleted file mode 100644 index 9841de522..000000000 --- a/framework/core/src/Post/Command/DeletePost.php +++ /dev/null @@ -1,22 +0,0 @@ -actor; - - $post = $this->posts->findOrFail($command->postId, $actor); - - $actor->assertCan('delete', $post); - - $this->events->dispatch( - new Deleting($post, $actor, $command->data) - ); - - $post->delete(); - - $this->dispatchEventsFor($post, $actor); - - return $post; - } -} diff --git a/framework/core/src/Post/Command/EditPost.php b/framework/core/src/Post/Command/EditPost.php deleted file mode 100644 index 591ea5b11..000000000 --- a/framework/core/src/Post/Command/EditPost.php +++ /dev/null @@ -1,22 +0,0 @@ -actor; - $data = $command->data; - - $post = $this->posts->findOrFail($command->postId, $actor); - - if ($post instanceof CommentPost) { - $attributes = Arr::get($data, 'attributes', []); - - if (isset($attributes['content'])) { - $actor->assertCan('edit', $post); - - $post->revise($attributes['content'], $actor); - } - - if (isset($attributes['isHidden'])) { - $actor->assertCan('hide', $post); - - if ($attributes['isHidden']) { - $post->hide($actor); - } else { - $post->restore(); - } - } - } - - $this->events->dispatch( - new Saving($post, $actor, $data) - ); - - $this->validator->assertValid($post->getDirty()); - - $post->save(); - - $this->dispatchEventsFor($post, $actor); - - return $post; - } -} diff --git a/framework/core/src/Post/Command/PostReply.php b/framework/core/src/Post/Command/PostReply.php deleted file mode 100644 index d1e09b43d..000000000 --- a/framework/core/src/Post/Command/PostReply.php +++ /dev/null @@ -1,24 +0,0 @@ -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); - - // If this is the first post in the discussion, it's technically not a - // "reply", so we won't check for that permission. - if (! $command->isFirstPost) { - $actor->assertCan('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, - Arr::get($command->data, 'attributes.content'), - $actor->id, - $command->ipAddress, - $command->actor, - ); - - if ($actor->isAdmin() && ($time = Arr::get($command->data, 'attributes.createdAt'))) { - $post->created_at = new Carbon($time); - } - - $this->events->dispatch( - new Saving($post, $actor, $command->data) - ); - - $this->validator->assertValid($post->getAttributes()); - - $post->save(); - - $this->notifications->onePerUser(function () use ($post, $actor) { - $this->dispatchEventsFor($post, $actor); - }); - - return $post; - } -} diff --git a/framework/core/src/User/Command/DeleteUser.php b/framework/core/src/User/Command/DeleteUser.php deleted file mode 100644 index 0fad3270f..000000000 --- a/framework/core/src/User/Command/DeleteUser.php +++ /dev/null @@ -1,22 +0,0 @@ -actor; - $user = $this->users->findOrFail($command->userId, $actor); - - $actor->assertCan('delete', $user); - - $this->events->dispatch( - new Deleting($user, $actor, $command->data) - ); - - $user->delete(); - - $this->dispatchEventsFor($user, $actor); - - return $user; - } -} diff --git a/framework/core/src/User/Command/EditUser.php b/framework/core/src/User/Command/EditUser.php deleted file mode 100644 index 44cb8c176..000000000 --- a/framework/core/src/User/Command/EditUser.php +++ /dev/null @@ -1,22 +0,0 @@ -actor; - $data = $command->data; - - $user = $this->users->findOrFail($command->userId, $actor); - - $isSelf = $actor->id === $user->id; - - $attributes = Arr::get($data, 'attributes', []); - $relationships = Arr::get($data, 'relationships', []); - $validate = []; - - if (isset($attributes['username'])) { - $actor->assertCan('editCredentials', $user); - $user->rename($attributes['username']); - } - - if (isset($attributes['email'])) { - if ($isSelf) { - $user->requestEmailChange($attributes['email']); - - if ($attributes['email'] !== $user->email) { - $validate['email'] = $attributes['email']; - } - } else { - $actor->assertCan('editCredentials', $user); - $user->changeEmail($attributes['email']); - } - } - - if (! empty($attributes['isEmailConfirmed'])) { - $actor->assertAdmin(); - $user->activate(); - } - - if (isset($attributes['password'])) { - $actor->assertCan('editCredentials', $user); - $user->changePassword($attributes['password']); - - $validate['password'] = $attributes['password']; - } - - if (! empty($attributes['markedAllAsReadAt'])) { - $actor->assertPermission($isSelf); - $user->markAllAsRead(); - } - - if (! empty($attributes['preferences'])) { - $actor->assertPermission($isSelf); - - foreach ($attributes['preferences'] as $k => $v) { - $user->setPreference($k, $v); - } - } - - if (isset($relationships['groups']['data']) && is_array($relationships['groups']['data'])) { - $actor->assertCan('editGroups', $user); - - $oldGroups = $user->groups()->get()->all(); - $oldGroupIds = Arr::pluck($oldGroups, 'id'); - - $newGroupIds = []; - foreach ($relationships['groups']['data'] as $group) { - if ($id = Arr::get($group, 'id')) { - $newGroupIds[] = $id; - } - } - - // Ensure non-admins aren't adding/removing admins - $adminChanged = in_array('1', array_diff($oldGroupIds, $newGroupIds)) || in_array('1', array_diff($newGroupIds, $oldGroupIds)); - $actor->assertPermission(! $adminChanged || $actor->isAdmin()); - - $user->raise( - new GroupsChanged($user, $oldGroups) - ); - - $user->afterSave(function (User $user) use ($newGroupIds) { - $user->groups()->sync($newGroupIds); - $user->unsetRelation('groups'); - }); - } - - $this->events->dispatch( - new Saving($user, $actor, $data) - ); - - $this->validator->setUser($user); - $this->validator->assertValid(array_merge($user->getDirty(), $validate)); - - $user->save(); - - $this->dispatchEventsFor($user, $actor); - - return $user; - } -} diff --git a/framework/core/src/User/Command/RegisterUser.php b/framework/core/src/User/Command/RegisterUser.php deleted file mode 100644 index a2c306782..000000000 --- a/framework/core/src/User/Command/RegisterUser.php +++ /dev/null @@ -1,21 +0,0 @@ -actor; - $data = $command->data; - - if (! $this->settings->get('allow_sign_up')) { - $actor->assertAdmin(); - } - - $password = Arr::get($data, 'attributes.password'); - - // If a valid authentication token was provided as an attribute, - // then we won't require the user to choose a password. - if (isset($data['attributes']['token'])) { - /** @var RegistrationToken $token */ - $token = RegistrationToken::validOrFail($data['attributes']['token']); - - $password = $password ?: Str::random(20); - } - - $user = User::register( - Arr::get($data, 'attributes.username'), - Arr::get($data, 'attributes.email'), - $password - ); - - if (isset($token)) { - $this->applyToken($user, $token); - } - - if ($actor->isAdmin() && Arr::get($data, 'attributes.isEmailConfirmed')) { - $user->activate(); - } - - $this->events->dispatch( - new Saving($user, $actor, $data) - ); - - $this->userValidator->assertValid(array_merge($user->getAttributes(), compact('password'))); - - $user->save(); - - if (isset($token)) { - $this->fulfillToken($user, $token); - } - - $this->dispatchEventsFor($user, $actor); - - return $user; - } - - private function applyToken(User $user, RegistrationToken $token): void - { - foreach ($token->user_attributes as $k => $v) { - if ($k === 'avatar_url') { - $this->uploadAvatarFromUrl($user, $v); - continue; - } - - $user->$k = $v; - - if ($k === 'email') { - $user->activate(); - } - } - - $this->events->dispatch( - new RegisteringFromProvider($user, $token->provider, $token->payload) - ); - } - - /** - * @throws InvalidArgumentException - */ - private function uploadAvatarFromUrl(User $user, string $url): void - { - $urlValidator = $this->validator->make(compact('url'), [ - 'url' => 'required|active_url', - ]); - - if ($urlValidator->fails()) { - throw new InvalidArgumentException('Provided avatar URL must be a valid URI.', 503); - } - - $scheme = parse_url($url, PHP_URL_SCHEME); - - if (! in_array($scheme, ['http', 'https'])) { - throw new InvalidArgumentException("Provided avatar URL must have scheme http or https. Scheme provided was $scheme.", 503); - } - - $image = $this->imageManager->make($url); - - $this->avatarUploader->upload($user, $image); - } - - private function fulfillToken(User $user, RegistrationToken $token): void - { - $token->delete(); - - if ($token->provider && $token->identifier) { - $user->loginProviders()->create([ - 'provider' => $token->provider, - 'identifier' => $token->identifier - ]); - } - } -}