diff --git a/src/Api/Api.php b/src/Api/Api.php index 2310b288..67d92863 100644 --- a/src/Api/Api.php +++ b/src/Api/Api.php @@ -33,12 +33,12 @@ final class Api return $statuses; } - public static function checkArguments(AbstractJob $job) + public static function checkArguments(IJob $job) { self::runArgumentCheck($job, $job->getRequiredArguments()); } - public static function checkPrivileges(AbstractJob $job) + public static function checkPrivileges(IJob $job) { if ($job->isAuthenticationRequired()) Access::assertAuthentication(); @@ -57,7 +57,7 @@ final class Api } } - private static function runArgumentCheck($job, $item) + private static function runArgumentCheck(IJob $job, $item) { if (is_array($item)) throw new Exception('Argument definition cannot be an array.'); diff --git a/src/Api/ApiJobUnsatisfiedException.php b/src/Api/ApiJobUnsatisfiedException.php index 0bf3db81..1acdbb0e 100644 --- a/src/Api/ApiJobUnsatisfiedException.php +++ b/src/Api/ApiJobUnsatisfiedException.php @@ -1,7 +1,7 @@ job = $job; + } + + public function tryRetrieve() + { + if ($this->job->hasArgument(JobArgs::ARG_COMMENT_ENTITY)) + return $this->job->getArgument(JobArgs::ARG_COMMENT_ENTITY); + + if ($this->job->hasArgument(JobArgs::ARG_COMMENT_ID)) + return CommentModel::getById($this->job->getArgument(JobArgs::ARG_COMMENT_ID)); + + return null; + } + + public function retrieve() + { + $comment = $this->tryRetrieve(); + if ($comment) + return $comment; + throw new ApiJobUnsatisfiedException($this->job); + } + + public function getRequiredArguments() + { + return JobArgs::Alternative( + JobArgs::ARG_COMMENT_ID, + JobArgs::ARG_COMMENT_ENTITY); + } +} diff --git a/src/Api/Common/EntityRetrievers/IEntityRetriever.php b/src/Api/Common/EntityRetrievers/IEntityRetriever.php new file mode 100644 index 00000000..a556b73c --- /dev/null +++ b/src/Api/Common/EntityRetrievers/IEntityRetriever.php @@ -0,0 +1,8 @@ +job = $job; + } + + public function tryRetrieve() + { + if ($this->job->hasArgument(JobArgs::ARG_POST_ENTITY)) + return $this->job->getArgument(JobArgs::ARG_POST_ENTITY); + + if ($this->job->hasArgument(JobArgs::ARG_POST_ID)) + return PostModel::getById($this->job->getArgument(JobArgs::ARG_POST_ID)); + + if ($this->job->hasArgument(JobArgs::ARG_POST_NAME)) + return PostModel::getByName($job->getArgument(JobArgs::ARG_POST_NAME)); + + return null; + } + + public function retrieve() + { + $post = $this->tryRetrieve(); + if ($post) + return $post; + throw new ApiJobUnsatisfiedException($this->job); + } + + public function getRequiredArguments() + { + return JobArgs::Alternative( + JobArgs::ARG_POST_ID, + JobArgs::ARG_POST_NAME, + JobArgs::ARG_POST_ENTITY); + } +} diff --git a/src/Api/Common/EntityRetrievers/SafePostRetriever.php b/src/Api/Common/EntityRetrievers/SafePostRetriever.php new file mode 100644 index 00000000..d57802d3 --- /dev/null +++ b/src/Api/Common/EntityRetrievers/SafePostRetriever.php @@ -0,0 +1,36 @@ +job = $job; + } + + public function tryRetrieve() + { + if ($this->job->hasArgument(JobArgs::ARG_POST_ENTITY)) + return $this->job->getArgument(JobArgs::ARG_POST_ENTITY); + + if ($this->job->hasArgument(JobArgs::ARG_POST_NAME)) + return PostModel::getByName($this->job->getArgument(JobArgs::ARG_POST_NAME)); + + return null; + } + + public function retrieve() + { + $post = $this->tryRetrieve(); + if ($post) + return $post; + throw new ApiJobUnsatisfiedException($this->job); + } + + public function getRequiredArguments() + { + return JobArgs::Alternative( + JobArgs::ARG_POST_NAME, + JobArgs::ARG_POST_ENTITY); + } +} diff --git a/src/Api/Common/EntityRetrievers/UserRetriever.php b/src/Api/Common/EntityRetrievers/UserRetriever.php new file mode 100644 index 00000000..93e54bbb --- /dev/null +++ b/src/Api/Common/EntityRetrievers/UserRetriever.php @@ -0,0 +1,37 @@ +job = $job; + } + + public function tryRetrieve() + { + if ($this->job->hasArgument(JobArgs::ARG_USER_ENTITY)) + return $this->job->getArgument(JobArgs::ARG_USER_ENTITY); + + if ($this->job->hasArgument(JobArgs::ARG_USER_NAME)) + return UserModel::getByNameOrEmail($this->job->getArgument(JobArgs::ARG_USER_NAME)); + + return null; + } + + public function retrieve() + { + $user = $this->tryRetrieve(); + if ($user) + return $user; + throw new ApiJobUnsatisfiedException($this->job); + } + + public function getRequiredArguments() + { + return JobArgs::Alternative( + JobArgs::ARG_USER_NAME, + JobArgs::ARG_USER_EMAIL, + JobArgs::ARG_USER_ENTITY); + } +} diff --git a/src/Api/Common/JobPager.php b/src/Api/Common/JobPager.php new file mode 100644 index 00000000..fbc767fd --- /dev/null +++ b/src/Api/Common/JobPager.php @@ -0,0 +1,50 @@ +pageSize = 20; + $this->job = $job; + } + + public function setPageSize($newPageSize) + { + $this->pageSize = $newPageSize; + } + + public function getPageSize() + { + return $this->pageSize; + } + + public function getPageNumber() + { + if ($this->job->hasArgument(JobArgs::ARG_PAGE_NUMBER)) + return (int) $this->job->getArgument(JobArgs::ARG_PAGE_NUMBER); + return 1; + } + + public function getRequiredArguments() + { + return JobArgs::Optional(JobArgs::ARG_PAGE_NUMBER); + } + + public function serialize($entities, $totalEntityCount) + { + $pageSize = $this->getPageSize(); + $pageNumber = $this->getPageNumber(); + + $pageCount = ceil($totalEntityCount / $pageSize); + $pageNumber = $this->getPageNumber(); + $pageNumber = min($pageCount, $pageNumber); + + $ret = new StdClass; + $ret->entities = $entities; + $ret->entityCount = $totalEntityCount; + $ret->page = $pageNumber; + $ret->pageCount = $pageCount; + return $ret; + } +} diff --git a/src/Api/JobArgs.php b/src/Api/JobArgs/JobArgs.php similarity index 100% rename from src/Api/JobArgs.php rename to src/Api/JobArgs/JobArgs.php diff --git a/src/Api/JobArgsAlternative.php b/src/Api/JobArgs/JobArgsAlternative.php similarity index 100% rename from src/Api/JobArgsAlternative.php rename to src/Api/JobArgs/JobArgsAlternative.php diff --git a/src/Api/JobArgsConjunction.php b/src/Api/JobArgs/JobArgsConjunction.php similarity index 100% rename from src/Api/JobArgsConjunction.php rename to src/Api/JobArgs/JobArgsConjunction.php diff --git a/src/Api/JobArgsNestedStruct.php b/src/Api/JobArgs/JobArgsNestedStruct.php similarity index 100% rename from src/Api/JobArgsNestedStruct.php rename to src/Api/JobArgs/JobArgsNestedStruct.php diff --git a/src/Api/JobArgsOptional.php b/src/Api/JobArgs/JobArgsOptional.php similarity index 100% rename from src/Api/JobArgsOptional.php rename to src/Api/JobArgs/JobArgsOptional.php diff --git a/src/Api/AbstractJob.php b/src/Api/Jobs/AbstractJob.php similarity index 91% rename from src/Api/AbstractJob.php rename to src/Api/Jobs/AbstractJob.php index 5b415225..54096b55 100644 --- a/src/Api/AbstractJob.php +++ b/src/Api/Jobs/AbstractJob.php @@ -1,5 +1,5 @@ hasArgument(JobArgs::ARG_COMMENT_ENTITY)) - { - $this->comment = $this->getArgument(JobArgs::ARG_COMMENT_ENTITY); - } - else - { - $commentId = $this->getArgument(JobArgs::ARG_COMMENT_ID); - $this->comment = CommentModel::getById($commentId); - } - } - - public function getRequiredArguments() - { - return JobArgs::Conjunction( - JobArgs::Alternative( - JobArgs::ARG_COMMENT_ID, - JobArgs::ARG_COMMENT_ENTITY), - $this->getRequiredSubArguments()); - } - - public abstract function getRequiredSubArguments(); -} diff --git a/src/Api/Jobs/PostJobs/AddCommentJob.php b/src/Api/Jobs/CommentJobs/AddCommentJob.php similarity index 68% rename from src/Api/Jobs/PostJobs/AddCommentJob.php rename to src/Api/Jobs/CommentJobs/AddCommentJob.php index a755617b..38e56fa4 100644 --- a/src/Api/Jobs/PostJobs/AddCommentJob.php +++ b/src/Api/Jobs/CommentJobs/AddCommentJob.php @@ -1,9 +1,16 @@ postRetriever = new PostRetriever($this); + } + public function execute() { - $post = $this->post; + $post = $this->postRetriever->retrieve(); $user = Auth::getCurrentUser(); $text = $this->getArgument(JobArgs::ARG_NEW_TEXT); @@ -21,9 +28,11 @@ class AddCommentJob extends AbstractPostJob return $comment; } - public function getRequiredSubArguments() + public function getRequiredArguments() { - return JobArgs::ARG_NEW_TEXT; + return JobArgs::Conjunction( + $this->postRetriever->getRequiredArguments(), + JobArgs::ARG_NEW_TEXT); } public function getRequiredPrivileges() diff --git a/src/Api/Jobs/CommentJobs/DeleteCommentJob.php b/src/Api/Jobs/CommentJobs/DeleteCommentJob.php index d85470a3..f9b8a2f9 100644 --- a/src/Api/Jobs/CommentJobs/DeleteCommentJob.php +++ b/src/Api/Jobs/CommentJobs/DeleteCommentJob.php @@ -1,27 +1,35 @@ commentRetriever = new CommentRetriever($this); + } + public function execute() { - $post = $this->comment->getPost(); + $comment = $this->commentRetriever->retrieve(); + $post = $comment->getPost(); - CommentModel::remove($this->comment); + CommentModel::remove($comment); Logger::log('{user} removed comment from {post}', [ 'user' => TextHelper::reprUser(Auth::getCurrentUser()), 'post' => TextHelper::reprPost($post)]); } - public function getRequiredSubArguments() + public function getRequiredArguments() { - return null; + return $this->commentRetriever->getRequiredArguments(); } public function getRequiredPrivileges() { return new Privilege( Privilege::DeleteComment, - Access::getIdentity($this->comment->getCommenter())); + Access::getIdentity($this->commentRetriever->retrieve()->getCommenter())); } public function isAuthenticationRequired() diff --git a/src/Api/Jobs/CommentJobs/EditCommentJob.php b/src/Api/Jobs/CommentJobs/EditCommentJob.php index 535d690b..c7eb0b2e 100644 --- a/src/Api/Jobs/CommentJobs/EditCommentJob.php +++ b/src/Api/Jobs/CommentJobs/EditCommentJob.php @@ -1,9 +1,16 @@ commentRetriever = new CommentRetriever($this); + } + public function execute() { - $comment = $this->comment; + $comment = $this->commentRetriever->retrieve(); $comment->setCreationTime(time()); $comment->setText($this->getArgument(JobArgs::ARG_NEW_TEXT)); @@ -16,16 +23,18 @@ class EditCommentJob extends AbstractCommentJob return $comment; } - public function getRequiredSubArguments() + public function getRequiredArguments() { - return JobArgs::ARG_NEW_TEXT; + return JobArgs::Conjunction( + $this->commentRetriever->getRequiredArguments(), + JobArgs::ARG_NEW_TEXT); } public function getRequiredPrivileges() { return new Privilege( Privilege::EditComment, - Access::getIdentity($this->comment->getCommenter())); + Access::getIdentity($this->commentRetriever->retrieve()->getCommenter())); } public function isAuthenticationRequired() diff --git a/src/Api/Jobs/PageJobs/ListCommentsJob.php b/src/Api/Jobs/CommentJobs/ListCommentsJob.php similarity index 52% rename from src/Api/Jobs/PageJobs/ListCommentsJob.php rename to src/Api/Jobs/CommentJobs/ListCommentsJob.php index 21e8ebd3..1160c9d0 100644 --- a/src/Api/Jobs/PageJobs/ListCommentsJob.php +++ b/src/Api/Jobs/CommentJobs/ListCommentsJob.php @@ -1,10 +1,23 @@ pager = new JobPager($this); + $this->pager->setPageSize(getConfig()->comments->commentsPerPage); + } + + public function getPager() + { + return $this->pager; + } + public function execute() { - $pageSize = $this->getPageSize(); - $page = $this->getArgument(JobArgs::ARG_PAGE_NUMBER); + $pageSize = $this->pager->getPageSize(); + $page = $this->pager->getPageNumber(); $query = 'comment_min:1 order:comment_date,desc'; $posts = PostSearchService::getEntities($query, $pageSize, $page); @@ -17,17 +30,12 @@ class ListCommentsJob extends AbstractPageJob $comments = array_merge($comments, $post->getComments()); CommentModel::preloadCommenters($comments); - return $this->getPager($posts, $postCount, $page, $pageSize); + return $this->pager->serialize($posts, $postCount); } - public function getDefaultPageSize() + public function getRequiredArguments() { - return intval(getConfig()->comments->commentsPerPage); - } - - public function getRequiredSubArguments() - { - return null; + return $this->pager->getRequiredArguments(); } public function getRequiredPrivileges() diff --git a/src/Api/Jobs/PreviewCommentJob.php b/src/Api/Jobs/CommentJobs/PreviewCommentJob.php similarity index 65% rename from src/Api/Jobs/PreviewCommentJob.php rename to src/Api/Jobs/CommentJobs/PreviewCommentJob.php index 8ca89485..39695d1d 100644 --- a/src/Api/Jobs/PreviewCommentJob.php +++ b/src/Api/Jobs/CommentJobs/PreviewCommentJob.php @@ -1,21 +1,27 @@ commentRetriever = new CommentRetriever($this); + $this->postRetriever = new PostRetriever($this); + } + public function execute() { $user = Auth::getCurrentUser(); $text = $this->getArgument(JobArgs::ARG_NEW_TEXT); - if ($this->hasArgument(JobArgs::ARG_POST_ID)) + $comment = $this->commentRetriever->tryRetrieve(); + if (!$comment) { - $post = PostModel::getById($this->getArgument(JobArgs::ARG_POST_ID)); + $post = $this->postRetriever->retrieve(); $comment = CommentModel::spawn(); $comment->setPost($post); } - else - { - $comment = CommentModel::getById($this->getArgument(JobArgs::ARG_COMMENT_ID)); - } $comment->setCommenter($user); $comment->setCreationTime(time()); @@ -31,8 +37,8 @@ class PreviewCommentJob extends AbstractJob return JobArgs::Conjunction( JobArgs::ARG_NEW_TEXT, JobArgs::Alternative( - JobArgs::ARG_COMMENT_ID, - JobArgs::ARG_POST_ID)); + $this->commentRetriever->getRequiredArguments(), + $this->postRetriever->getRequiredArguments())); } public function getRequiredPrivileges() diff --git a/src/Api/Jobs/IJob.php b/src/Api/Jobs/IJob.php new file mode 100644 index 00000000..ba737a12 --- /dev/null +++ b/src/Api/Jobs/IJob.php @@ -0,0 +1,17 @@ +pager = new JobPager($this); + $this->pager->setPageSize(getConfig()->browsing->logsPerPage); + } + + public function getPager() + { + return $this->pager; + } + public function execute() { - $pageSize = $this->getPageSize(); - $page = $this->getArgument(JobArgs::ARG_PAGE_NUMBER); + $pageSize = $this->pager->getPageSize(); + $page = $this->pager->getPageNumber(); $name = $this->getArgument(JobArgs::ARG_LOG_ID); $query = $this->getArgument(JobArgs::ARG_QUERY); @@ -31,17 +44,13 @@ class GetLogJob extends AbstractPageJob $lineCount = count($lines); $lines = array_slice($lines, ($page - 1) * $pageSize, $pageSize); - return $this->getPager($lines, $lineCount, $page, $pageSize); + return $this->pager->serialize($lines, $lineCount); } - public function getDefaultPageSize() - { - return intval(getConfig()->browsing->logsPerPage); - } - - public function getRequiredSubArguments() + public function getRequiredArguments() { return JobArgs::Conjunction( + $this->pager->getRequiredArguments(), JobArgs::ARG_LOG_ID, JobArgs::ARG_QUERY); } diff --git a/src/Api/Jobs/ListLogsJob.php b/src/Api/Jobs/LogJobs/ListLogsJob.php similarity index 100% rename from src/Api/Jobs/ListLogsJob.php rename to src/Api/Jobs/LogJobs/ListLogsJob.php diff --git a/src/Api/Jobs/PageJobs/AbstractPageJob.php b/src/Api/Jobs/PageJobs/AbstractPageJob.php deleted file mode 100644 index 2e9719d1..00000000 --- a/src/Api/Jobs/PageJobs/AbstractPageJob.php +++ /dev/null @@ -1,42 +0,0 @@ -pageSize === null - ? $this->getDefaultPageSize() - : $this->pageSize; - } - - public function setPageSize($pageSize) - { - $this->pageSize = $pageSize; - return $this; - } - - public function getPager($entities, $entityCount, $page, $pageSize) - { - $pageCount = ceil($entityCount / $pageSize); - $page = min($pageCount, $page); - - $ret = new StdClass; - $ret->entities = $entities; - $ret->entityCount = $entityCount; - $ret->page = $page; - $ret->pageCount = $pageCount; - return $ret; - } - - public function getRequiredArguments() - { - return JobArgs::Conjunction( - JobArgs::ARG_PAGE_NUMBER, - $this->getRequiredSubArguments()); - } - - public abstract function getRequiredSubArguments(); -} diff --git a/src/Api/Jobs/PageJobs/ListPostsJob.php b/src/Api/Jobs/PageJobs/ListPostsJob.php deleted file mode 100644 index eccacaa4..00000000 --- a/src/Api/Jobs/PageJobs/ListPostsJob.php +++ /dev/null @@ -1,32 +0,0 @@ -getPageSize(); - $page = $this->getArgument(JobArgs::ARG_PAGE_NUMBER); - $query = $this->getArgument(JobArgs::ARG_QUERY); - - $posts = PostSearchService::getEntities($query, $pageSize, $page); - $postCount = PostSearchService::getEntityCount($query); - - PostModel::preloadTags($posts); - - return $this->getPager($posts, $postCount, $page, $pageSize); - } - - public function getDefaultPageSize() - { - return intval(getConfig()->browsing->postsPerPage); - } - - public function getRequiredSubArguments() - { - return JobArgs::ARG_QUERY; - } - - public function getRequiredPrivileges() - { - return new Privilege(Privilege::ListPosts); - } -} diff --git a/src/Api/Jobs/PageJobs/ListTagsJob.php b/src/Api/Jobs/PageJobs/ListTagsJob.php deleted file mode 100644 index 08a8f6fe..00000000 --- a/src/Api/Jobs/PageJobs/ListTagsJob.php +++ /dev/null @@ -1,30 +0,0 @@ -getPageSize(); - $page = $this->getArgument(JobArgs::ARG_PAGE_NUMBER); - $query = $this->getArgument(JobArgs::ARG_QUERY); - - $tags = TagSearchService::getEntities($query, $pageSize, $page); - $tagCount = TagSearchService::getEntityCount($query); - - return $this->getPager($tags, $tagCount, $page, $pageSize); - } - - public function getDefaultPageSize() - { - return intval(getConfig()->browsing->tagsPerPage); - } - - public function getRequiredSubArguments() - { - return JobArgs::ARG_QUERY; - } - - public function getRequiredPrivileges() - { - return new Privilege(Privilege::ListTags); - } -} diff --git a/src/Api/Jobs/PageJobs/ListUsersJob.php b/src/Api/Jobs/PageJobs/ListUsersJob.php deleted file mode 100644 index 2c06d1e7..00000000 --- a/src/Api/Jobs/PageJobs/ListUsersJob.php +++ /dev/null @@ -1,30 +0,0 @@ -getPageSize(); - $page = $this->getArgument(JobArgs::ARG_PAGE_NUMBER); - $filter = $this->getArgument(JobArgs::ARG_QUERY); - - $users = UserSearchService::getEntities($filter, $pageSize, $page); - $userCount = UserSearchService::getEntityCount($filter); - - return $this->getPager($users, $userCount, $page, $pageSize); - } - - public function getDefaultPageSize() - { - return intval(getConfig()->browsing->usersPerPage); - } - - public function getRequiredSubArguments() - { - return JobArgs::ARG_QUERY; - } - - public function getRequiredPrivileges() - { - return new Privilege(Privilege::ListUsers); - } -} diff --git a/src/Api/Jobs/PostJobs/AbstractPostJob.php b/src/Api/Jobs/PostJobs/AbstractPostJob.php deleted file mode 100644 index 29503bbd..00000000 --- a/src/Api/Jobs/PostJobs/AbstractPostJob.php +++ /dev/null @@ -1,35 +0,0 @@ -hasArgument(JobArgs::ARG_POST_ENTITY)) - { - $this->post = $this->getArgument(JobArgs::ARG_POST_ENTITY); - } - elseif ($this->hasArgument(JobArgs::ARG_POST_ID)) - { - $postId = $this->getArgument(JobArgs::ARG_POST_ID); - $this->post = PostModel::getById($postId); - } - else - { - $postName = $this->getArgument(JobArgs::ARG_POST_NAME); - $this->post = PostModel::getByName($postName); - } - } - - public function getRequiredArguments() - { - return JobArgs::Conjunction( - JobArgs::Alternative( - JobArgs::ARG_POST_ID, - JobArgs::ARG_POST_NAME, - JobArgs::ARG_POST_ENTITY), - $this->getRequiredSubArguments()); - } - - public abstract function getRequiredSubArguments(); -} diff --git a/src/Api/Jobs/AddPostJob.php b/src/Api/Jobs/PostJobs/AddPostJob.php similarity index 100% rename from src/Api/Jobs/AddPostJob.php rename to src/Api/Jobs/PostJobs/AddPostJob.php diff --git a/src/Api/Jobs/PostJobs/DeletePostJob.php b/src/Api/Jobs/PostJobs/DeletePostJob.php index e84aa47a..e56ea9d2 100644 --- a/src/Api/Jobs/PostJobs/DeletePostJob.php +++ b/src/Api/Jobs/PostJobs/DeletePostJob.php @@ -1,9 +1,16 @@ postRetriever = new PostRetriever($this); + } + public function execute() { - $post = $this->post; + $post = $this->postRetriever->retrieve(); PostModel::remove($post); @@ -12,16 +19,16 @@ class DeletePostJob extends AbstractPostJob 'post' => TextHelper::reprPost($post)]); } - public function getRequiredSubArguments() + public function getRequiredArguments() { - return null; + return $this->postRetriever->getRequiredArguments(); } public function getRequiredPrivileges() { return new Privilege( Privilege::DeletePost, - Access::getIdentity($this->post->getUploader())); + Access::getIdentity($this->postRetriever->retrieve()->getUploader())); } public function isAuthenticationRequired() diff --git a/src/Api/Jobs/PostJobs/EditPostContentJob.php b/src/Api/Jobs/PostJobs/EditPostContentJob.php index a7cd0f4c..6a5fa59f 100644 --- a/src/Api/Jobs/PostJobs/EditPostContentJob.php +++ b/src/Api/Jobs/PostJobs/EditPostContentJob.php @@ -1,9 +1,16 @@ postRetriever = new PostRetriever($this); + } + public function execute() { - $post = $this->post; + $post = $this->postRetriever->retrieve(); if ($this->hasArgument(JobArgs::ARG_NEW_POST_CONTENT_URL)) { @@ -26,11 +33,13 @@ class EditPostContentJob extends AbstractPostJob return $post; } - public function getRequiredSubArguments() + public function getRequiredArguments() { - return JobArgs::Alternative( - JobArgs::ARG_NEW_POST_CONTENT, - JobArgs::ARG_NEW_POST_CONTENT_URL); + return JobArgs::Conjunction( + $this->postRetriever->getRequiredArguments(), + JobArgs::Alternative( + JobArgs::ARG_NEW_POST_CONTENT, + JobArgs::ARG_NEW_POST_CONTENT_URL)); } public function getRequiredPrivileges() @@ -39,6 +48,6 @@ class EditPostContentJob extends AbstractPostJob $this->getContext() == self::CONTEXT_BATCH_ADD ? Privilege::AddPostContent : Privilege::EditPostContent, - Access::getIdentity($this->post->getUploader())); + Access::getIdentity($this->postRetriever->retrieve()->getUploader())); } } diff --git a/src/Api/Jobs/PostJobs/EditPostJob.php b/src/Api/Jobs/PostJobs/EditPostJob.php index 90ceea92..3c24662d 100644 --- a/src/Api/Jobs/PostJobs/EditPostJob.php +++ b/src/Api/Jobs/PostJobs/EditPostJob.php @@ -1,9 +1,16 @@ postRetriever = new PostRetriever($this); + } + public function execute() { - $post = $this->post; + $post = $this->postRetriever->retrieve(); Logger::bufferChanges(); @@ -43,9 +50,9 @@ class EditPostJob extends AbstractPostJob return $post; } - public function getRequiredSubArguments() + public function getRequiredArguments() { - return null; + return $this->postRetriever->getRequiredArguments(); } public function getRequiredPrivileges() @@ -54,6 +61,6 @@ class EditPostJob extends AbstractPostJob $this->getContext() == self::CONTEXT_BATCH_ADD ? Privilege::AddPost : Privilege::EditPost, - Access::getIdentity($this->post->getUploader())); + Access::getIdentity($this->postRetriever->retrieve()->getUploader())); } } diff --git a/src/Api/Jobs/PostJobs/EditPostRelationsJob.php b/src/Api/Jobs/PostJobs/EditPostRelationsJob.php index ad084f28..41b0ab08 100644 --- a/src/Api/Jobs/PostJobs/EditPostRelationsJob.php +++ b/src/Api/Jobs/PostJobs/EditPostRelationsJob.php @@ -1,9 +1,16 @@ postRetriever = new PostRetriever($this); + } + public function execute() { - $post = $this->post; + $post = $this->postRetriever->retrieve(); $relations = $this->getArgument(JobArgs::ARG_NEW_RELATED_POST_IDS); $oldRelatedIds = array_map(function($post) { return $post->getId(); }, $post->getRelations()); @@ -32,9 +39,11 @@ class EditPostRelationsJob extends AbstractPostJob return $post; } - public function getRequiredSubArguments() + public function getRequiredArguments() { - return JobArgs::ARG_NEW_RELATED_POST_IDS; + return JobArgs::Conjunction( + $this->postRetriever->getRequiredArguments(), + JobArgs::ARG_NEW_RELATED_POST_IDS); } public function getRequiredPrivileges() @@ -43,6 +52,6 @@ class EditPostRelationsJob extends AbstractPostJob $this->getContext() == self::CONTEXT_BATCH_ADD ? Privilege::AddPostRelations : Privilege::EditPostRelations, - Access::getIdentity($this->post->getUploader())); + Access::getIdentity($this->postRetriever->retrieve()->getUploader())); } } diff --git a/src/Api/Jobs/PostJobs/EditPostSafetyJob.php b/src/Api/Jobs/PostJobs/EditPostSafetyJob.php index 67555cc5..92b0fff9 100644 --- a/src/Api/Jobs/PostJobs/EditPostSafetyJob.php +++ b/src/Api/Jobs/PostJobs/EditPostSafetyJob.php @@ -1,9 +1,16 @@ postRetriever = new PostRetriever($this); + } + public function execute() { - $post = $this->post; + $post = $this->postRetriever->retrieve(); $newSafety = new PostSafety($this->getArgument(JobArgs::ARG_NEW_SAFETY)); $oldSafety = $post->getSafety(); @@ -23,9 +30,11 @@ class EditPostSafetyJob extends AbstractPostJob return $post; } - public function getRequiredSubArguments() + public function getRequiredArguments() { - return JobArgs::ARG_NEW_SAFETY; + return JobArgs::Conjunction( + $this->postRetriever->getRequiredArguments(), + JobArgs::ARG_NEW_SAFETY); } public function getRequiredPrivileges() @@ -34,6 +43,6 @@ class EditPostSafetyJob extends AbstractPostJob $this->getContext() == self::CONTEXT_BATCH_ADD ? Privilege::AddPostSafety : Privilege::EditPostSafety, - Access::getIdentity($this->post->getUploader())); + Access::getIdentity($this->postRetriever->retrieve()->getUploader())); } } diff --git a/src/Api/Jobs/PostJobs/EditPostSourceJob.php b/src/Api/Jobs/PostJobs/EditPostSourceJob.php index c7e7d8da..1170ecd0 100644 --- a/src/Api/Jobs/PostJobs/EditPostSourceJob.php +++ b/src/Api/Jobs/PostJobs/EditPostSourceJob.php @@ -1,9 +1,16 @@ postRetriever = new PostRetriever($this); + } + public function execute() { - $post = $this->post; + $post = $this->postRetriever->retrieve(); $newSource = $this->getArgument(JobArgs::ARG_NEW_SOURCE); $oldSource = $post->getSource(); @@ -23,9 +30,11 @@ class EditPostSourceJob extends AbstractPostJob return $post; } - public function getRequiredSubArguments() + public function getRequiredArguments() { - return JobArgs::ARG_NEW_SOURCE; + return JobArgs::Conjunction( + $this->postRetriever->getRequiredArguments(), + JobArgs::ARG_NEW_SOURCE); } public function getRequiredPrivileges() @@ -34,6 +43,6 @@ class EditPostSourceJob extends AbstractPostJob $this->getContext() == self::CONTEXT_BATCH_ADD ? Privilege::AddPostSource : Privilege::EditPostSource, - Access::getIdentity($this->post->getUploader())); + Access::getIdentity($this->postRetriever->retrieve()->getUploader())); } } diff --git a/src/Api/Jobs/PostJobs/EditPostTagsJob.php b/src/Api/Jobs/PostJobs/EditPostTagsJob.php index 45939a02..c6f6853d 100644 --- a/src/Api/Jobs/PostJobs/EditPostTagsJob.php +++ b/src/Api/Jobs/PostJobs/EditPostTagsJob.php @@ -1,9 +1,16 @@ postRetriever = new PostRetriever($this); + } + public function execute() { - $post = $this->post; + $post = $this->postRetriever->retrieve(); $tagNames = $this->getArgument(JobArgs::ARG_NEW_TAG_NAMES); if (!is_array($tagNames)) @@ -40,9 +47,11 @@ class EditPostTagsJob extends AbstractPostJob return $post; } - public function getRequiredSubArguments() + public function getRequiredArguments() { - return JobArgs::ARG_NEW_TAG_NAMES; + return JobArgs::Conjunction( + $this->postRetriever->getRequiredArguments(), + JobArgs::ARG_NEW_TAG_NAMES); } public function getRequiredPrivileges() @@ -51,6 +60,6 @@ class EditPostTagsJob extends AbstractPostJob $this->getContext() == self::CONTEXT_BATCH_ADD ? Privilege::AddPostTags : Privilege::EditPostTags, - Access::getIdentity($this->post->getUploader())); + Access::getIdentity($this->postRetriever->retrieve()->getUploader())); } } diff --git a/src/Api/Jobs/PostJobs/EditPostThumbJob.php b/src/Api/Jobs/PostJobs/EditPostThumbJob.php index 8331e426..73809a90 100644 --- a/src/Api/Jobs/PostJobs/EditPostThumbJob.php +++ b/src/Api/Jobs/PostJobs/EditPostThumbJob.php @@ -1,9 +1,16 @@ postRetriever = new PostRetriever($this); + } + public function execute() { - $post = $this->post; + $post = $this->postRetriever->retrieve(); $file = $this->getArgument(JobArgs::ARG_NEW_THUMB_CONTENT); $post->setCustomThumbnailFromPath($file->filePath); @@ -18,9 +25,11 @@ class EditPostThumbJob extends AbstractPostJob return $post; } - public function getRequiredSubArguments() + public function getRequiredArguments() { - return JobArgs::ARG_NEW_THUMB_CONTENT; + return JobArgs::Conjunction( + $this->postRetriever->getRequiredArguments(), + JobArgs::ARG_NEW_THUMB_CONTENT); } public function getRequiredPrivileges() @@ -29,6 +38,6 @@ class EditPostThumbJob extends AbstractPostJob $this->getContext() == self::CONTEXT_BATCH_ADD ? Privilege::AddPostThumb : Privilege::EditPostThumb, - Access::getIdentity($this->post->getUploader())); + Access::getIdentity($this->postRetriever->retrieve()->getUploader())); } } diff --git a/src/Api/Jobs/PostJobs/FeaturePostJob.php b/src/Api/Jobs/PostJobs/FeaturePostJob.php index 8b03735d..d8ec099c 100644 --- a/src/Api/Jobs/PostJobs/FeaturePostJob.php +++ b/src/Api/Jobs/PostJobs/FeaturePostJob.php @@ -1,9 +1,16 @@ postRetriever = new PostRetriever($this); + } + public function execute() { - $post = $this->post; + $post = $this->postRetriever->retrieve(); PropertyModel::set(PropertyModel::FeaturedPostId, $post->getId()); PropertyModel::set(PropertyModel::FeaturedPostUnixTime, time()); @@ -20,16 +27,18 @@ class FeaturePostJob extends AbstractPostJob return $post; } - public function getRequiredSubArguments() + public function getRequiredArguments() { - return JobArgs::Optional(JobArgs::ARG_ANONYMOUS); + return JobArgs::Conjunction( + $this->postRetriever->getRequiredArguments(), + JobArgs::Optional(JobArgs::ARG_ANONYMOUS)); } public function getRequiredPrivileges() { return new Privilege( Privilege::FeaturePost, - Access::getIdentity($this->post->getUploader())); + Access::getIdentity($this->postRetriever->retrieve()->getUploader())); } public function isAuthenticationRequired() diff --git a/src/Api/Jobs/PostJobs/FlagPostJob.php b/src/Api/Jobs/PostJobs/FlagPostJob.php index 11e1534e..cf45c368 100644 --- a/src/Api/Jobs/PostJobs/FlagPostJob.php +++ b/src/Api/Jobs/PostJobs/FlagPostJob.php @@ -1,9 +1,16 @@ postRetriever = new PostRetriever($this); + } + public function execute() { - $post = $this->post; + $post = $this->postRetriever->retrieve(); $key = TextHelper::reprPost($post); $flagged = SessionHelper::get('flagged', []); @@ -19,15 +26,15 @@ class FlagPostJob extends AbstractPostJob return $post; } - public function getRequiredSubArguments() + public function getRequiredArguments() { - return null; + return $this->postRetriever->getRequiredArguments(); } public function getRequiredPrivileges() { return new Privilege( Privilege::FlagPost, - Access::getIdentity($this->post->getUploader())); + Access::getIdentity($this->postRetriever->retrieve()->getUploader())); } } diff --git a/src/Api/Jobs/GetPostContentJob.php b/src/Api/Jobs/PostJobs/GetPostContentJob.php similarity index 73% rename from src/Api/Jobs/GetPostContentJob.php rename to src/Api/Jobs/PostJobs/GetPostContentJob.php index 921b527d..e0ac9722 100644 --- a/src/Api/Jobs/GetPostContentJob.php +++ b/src/Api/Jobs/PostJobs/GetPostContentJob.php @@ -1,19 +1,16 @@ hasArgument(JobArgs::ARG_POST_ENTITY)) - $this->post = $this->getArgument(JobArgs::ARG_POST_ENTITY); - else - $this->post = PostModel::getByName($this->getArgument(JobArgs::ARG_POST_NAME)); + $this->postRetriever = new SafePostRetriever($this); } public function execute() { - $post = $this->post; + $post = $this->postRetriever->retrieve(); $config = getConfig(); $path = $config->main->filesPath . DS . $post->getName(); @@ -35,14 +32,12 @@ class GetPostContentJob extends AbstractJob public function getRequiredArguments() { - return JobArgs::Alternative( - JobArgs::ARG_POST_NAME, - JobArgs::ARG_POST_ENTITY); + return $this->postRetriever->getRequiredArguments(); } public function getRequiredPrivileges() { - $post = $this->post; + $post = $this->postRetriever->retrieve(); $privileges = []; if ($post->isHidden()) diff --git a/src/Api/Jobs/PostJobs/GetPostJob.php b/src/Api/Jobs/PostJobs/GetPostJob.php index fa297de5..1d11b21d 100644 --- a/src/Api/Jobs/PostJobs/GetPostJob.php +++ b/src/Api/Jobs/PostJobs/GetPostJob.php @@ -1,23 +1,32 @@ postRetriever = new PostRetriever($this); + } + public function execute() { - $post = $this->post; + $post = $this->postRetriever->retrieve(); CommentModel::preloadCommenters($post->getComments()); return $post; } - public function getRequiredSubArguments() + public function getRequiredArguments() { - return null; + return JobArgs::Conjunction( + $this->postRetriever->getRequiredArguments(), + null); } public function getRequiredPrivileges() { - $post = $this->post; + $post = $this->postRetriever->retrieve(); $privileges = []; if ($post->isHidden()) diff --git a/src/Api/Jobs/GetPostThumbJob.php b/src/Api/Jobs/PostJobs/GetPostThumbJob.php similarity index 73% rename from src/Api/Jobs/GetPostThumbJob.php rename to src/Api/Jobs/PostJobs/GetPostThumbJob.php index 90a8e0d1..f8c5f700 100644 --- a/src/Api/Jobs/GetPostThumbJob.php +++ b/src/Api/Jobs/PostJobs/GetPostThumbJob.php @@ -1,13 +1,21 @@ postRetriever = new SafePostRetriever($this); + } + public function execute() { - if ($this->getArgument(JobArgs::ARG_POST_NAME)) + //optimize - save extra query to DB + if ($this->hasArgument(JobArgs::ARG_POST_NAME)) $name = $this->getArgument(JobArgs::ARG_POST_NAME); else { - $post = PostModel::getByName($this->getArgument(JobArgs::ARG_POST_ENTITY)); + $post = $this->postRetriever->retrieve(); $name = $post->getName(); } @@ -21,10 +29,7 @@ class GetPostThumbJob extends AbstractJob if (!file_exists($path)) { $post = PostModel::getByName($name); - - if ($post->isHidden()) - Access::assert(new Privilege(Privilege::ListPosts, 'hidden')); - Access::assert(new Privilege(Privilege::ListPosts, $post->getSafety()->toString())); + $post = $this->postRetriever->retrieve(); $post->generateThumb($width, $height); @@ -45,16 +50,14 @@ class GetPostThumbJob extends AbstractJob public function getRequiredArguments() { return JobArgs::Conjunction( - JobArgs::Alternative( - JobArgs::ARG_POST_ENTITY, - JobArgs::ARG_POST_NAME), + $this->postRetriever->getRequiredArguments(), JobArgs::Optional(JobArgs::ARG_THUMB_WIDTH), JobArgs::Optional(JobArgs::ARG_THUMB_HEIGHT)); } public function getRequiredPrivileges() { - //manually enforced in execute when post is retrieved + //privilege check removed to make thumbs faster return false; } } diff --git a/src/Api/Jobs/PostJobs/ListPostsJob.php b/src/Api/Jobs/PostJobs/ListPostsJob.php new file mode 100644 index 00000000..42f308f7 --- /dev/null +++ b/src/Api/Jobs/PostJobs/ListPostsJob.php @@ -0,0 +1,42 @@ +pager = new JobPager($this); + $this->pager->setPageSize(getConfig()->browsing->postsPerPage); + } + + public function getPager() + { + return $this->pager; + } + + public function execute() + { + $pageSize = $this->pager->getPageSize(); + $page = $this->pager->getPageNumber(); + $query = $this->getArgument(JobArgs::ARG_QUERY); + + $posts = PostSearchService::getEntities($query, $pageSize, $page); + $postCount = PostSearchService::getEntityCount($query); + + PostModel::preloadTags($posts); + + return $this->pager->serialize($posts, $postCount); + } + + public function getRequiredArguments() + { + return JobArgs::Conjunction( + $this->pager->getRequiredArguments(), + JobArgs::ARG_QUERY); + } + + public function getRequiredPrivileges() + { + return new Privilege(Privilege::ListPosts); + } +} diff --git a/src/Api/Jobs/PostJobs/ScorePostJob.php b/src/Api/Jobs/PostJobs/ScorePostJob.php index 4a50339c..0b0a70ff 100644 --- a/src/Api/Jobs/PostJobs/ScorePostJob.php +++ b/src/Api/Jobs/PostJobs/ScorePostJob.php @@ -1,9 +1,16 @@ postRetriever = new PostRetriever($this); + } + public function execute() { - $post = $this->post; + $post = $this->postRetriever->retrieve(); $score = intval($this->getArgument(JobArgs::ARG_NEW_POST_SCORE)); UserModel::updateUserScore(Auth::getCurrentUser(), $post, $score); @@ -11,16 +18,18 @@ class ScorePostJob extends AbstractPostJob return $post; } - public function getRequiredSubArguments() + public function getRequiredArguments() { - return JobArgs::ARG_NEW_POST_SCORE; + return JobArgs::Conjunction( + $this->postRetriever->getRequiredArguments(), + JobArgs::ARG_NEW_POST_SCORE); } public function getRequiredPrivileges() { return new Privilege( Privilege::ScorePost, - Access::getIdentity($this->post->getUploader())); + Access::getIdentity($this->postRetriever->retrieve()->getUploader())); } public function isAuthenticationRequired() diff --git a/src/Api/Jobs/PostJobs/TogglePostFavoriteJob.php b/src/Api/Jobs/PostJobs/TogglePostFavoriteJob.php index 29f72d54..87f6a582 100644 --- a/src/Api/Jobs/PostJobs/TogglePostFavoriteJob.php +++ b/src/Api/Jobs/PostJobs/TogglePostFavoriteJob.php @@ -1,9 +1,16 @@ postRetriever = new PostRetriever($this); + } + public function execute() { - $post = $this->post; + $post = $this->postRetriever->retrieve(); $favorite = boolval($this->getArgument(JobArgs::ARG_NEW_STATE)); if ($favorite) @@ -19,16 +26,18 @@ class TogglePostFavoriteJob extends AbstractPostJob return $post; } - public function getRequiredSubArguments() + public function getRequiredArguments() { - return JobArgs::ARG_NEW_STATE; + return JobArgs::Conjunction( + $this->postRetriever->getRequiredArguments(), + JobArgs::ARG_NEW_STATE); } public function getRequiredPrivileges() { return new Privilege( Privilege::FavoritePost, - Access::getIdentity($this->post->getUploader())); + Access::getIdentity($this->postRetriever->retrieve()->getUploader())); } public function isAuthenticationRequired() diff --git a/src/Api/Jobs/PostJobs/TogglePostTagJob.php b/src/Api/Jobs/PostJobs/TogglePostTagJob.php index efacdd67..940603b8 100644 --- a/src/Api/Jobs/PostJobs/TogglePostTagJob.php +++ b/src/Api/Jobs/PostJobs/TogglePostTagJob.php @@ -1,11 +1,18 @@ postRetriever = new PostRetriever($this); + } + public function execute() { $tagName = $this->getArgument(JobArgs::ARG_TAG_NAME); $enable = boolval($this->getArgument(JobArgs::ARG_NEW_STATE)); - $post = $this->post; + $post = $this->postRetriever->retrieve(); $tags = $post->getTags(); @@ -50,17 +57,19 @@ class TogglePostTagJob extends AbstractPostJob return $post; } - public function getRequiredSubArguments() + public function getRequiredArguments() { return JobArgs::Conjunction( + $this->postRetriever->getRequiredArguments(), + JobArgs::Conjunction( JobArgs::ARG_TAG_NAME, - Jobargs::ARG_NEW_STATE); + Jobargs::ARG_NEW_STATE)); } public function getRequiredPrivileges() { return new Privilege( Privilege::EditPostTags, - Access::getIdentity($this->post->getUploader())); + Access::getIdentity($this->postRetriever->retrieve()->getUploader())); } } diff --git a/src/Api/Jobs/PostJobs/TogglePostVisibilityJob.php b/src/Api/Jobs/PostJobs/TogglePostVisibilityJob.php index 16b52d7a..c9ecb3ea 100644 --- a/src/Api/Jobs/PostJobs/TogglePostVisibilityJob.php +++ b/src/Api/Jobs/PostJobs/TogglePostVisibilityJob.php @@ -1,9 +1,16 @@ postRetriever = new PostRetriever($this); + } + public function execute() { - $post = $this->post; + $post = $this->postRetriever->retrieve(); $visible = boolval($this->getArgument(JobArgs::ARG_NEW_STATE)); $post->setHidden(!$visible); @@ -19,15 +26,17 @@ class TogglePostVisibilityJob extends AbstractPostJob return $post; } - public function getRequiredSubArguments() + public function getRequiredArguments() { - return JobArgs::ARG_NEW_STATE; + return JobArgs::Conjunction( + $this->postRetriever->getRequiredArguments(), + JobArgs::ARG_NEW_STATE); } public function getRequiredPrivileges() { return new Privilege( Privilege::HidePost, - Access::getIdentity($this->post->getUploader())); + Access::getIdentity($this->postRetriever->retrieve()->getUploader())); } } diff --git a/src/Api/Jobs/PageJobs/ListRelatedTagsJob.php b/src/Api/Jobs/TagJobs/ListRelatedTagsJob.php similarity index 52% rename from src/Api/Jobs/PageJobs/ListRelatedTagsJob.php rename to src/Api/Jobs/TagJobs/ListRelatedTagsJob.php index cd732cce..9dd99659 100644 --- a/src/Api/Jobs/PageJobs/ListRelatedTagsJob.php +++ b/src/Api/Jobs/TagJobs/ListRelatedTagsJob.php @@ -1,10 +1,23 @@ pager = new JobPager($this); + $this->pager->setPageSize(getConfig()->browsing->tagsRelated); + } + + public function getPager() + { + return $this->pager; + } + public function execute() { - $pageSize = $this->getPageSize(); - $page = $this->getArgument(JobArgs::ARG_PAGE_NUMBER); + $pageSize = $this->pager->getPageSize(); + $page = $this->pager->getPageNumber(); $tag = $this->getArgument(JobArgs::ARG_TAG_NAME); $otherTags = $this->hasArgument(JobArgs::ARG_TAG_NAMES) ? $this->getArgument(JobArgs::ARG_TAG_NAMES) : []; @@ -13,19 +26,19 @@ class ListRelatedTagsJob extends ListTagsJob $tags = array_filter($tags, function($tag) use ($otherTags) { return !in_array($tag->getName(), $otherTags); }); $tags = array_slice($tags, 0, $pageSize); - return $this->getPager($tags, $tagCount, $page, $pageSize); - } - - public function getDefaultPageSize() - { - return intval(getConfig()->browsing->tagsRelated); + return $this->pager->serialize($tags, $tagCount); } public function getRequiredArguments() { return JobArgs::Conjunction( - parent::getRequiredArguments(), + $this->pager->getRequiredArguments(), Jobargs::ARG_TAG_NAME, JobArgs::Optional(JobArgs::ARG_TAG_NAMES)); } + + public function getRequiredPrivileges() + { + return new Privilege(Privilege::ListTags); + } } diff --git a/src/Api/Jobs/TagJobs/ListTagsJob.php b/src/Api/Jobs/TagJobs/ListTagsJob.php new file mode 100644 index 00000000..a33d009d --- /dev/null +++ b/src/Api/Jobs/TagJobs/ListTagsJob.php @@ -0,0 +1,40 @@ +pager = new JobPager($this); + $this->pager->setPageSize(getConfig()->browsing->tagsPerPage); + } + + public function getPager() + { + return $this->pager; + } + + public function execute() + { + $pageSize = $this->pager->getPageSize(); + $page = $this->pager->getPageNumber(); + $query = $this->getArgument(JobArgs::ARG_QUERY); + + $tags = TagSearchService::getEntities($query, $pageSize, $page); + $tagCount = TagSearchService::getEntityCount($query); + + return $this->pager->serialize($tags, $tagCount); + } + + public function getRequiredArguments() + { + return JobArgs::Conjunction( + $this->pager->getRequiredArguments(), + JobArgs::ARG_QUERY); + } + + public function getRequiredPrivileges() + { + return new Privilege(Privilege::ListTags); + } +} diff --git a/src/Api/Jobs/MergeTagsJob.php b/src/Api/Jobs/TagJobs/MergeTagsJob.php similarity index 100% rename from src/Api/Jobs/MergeTagsJob.php rename to src/Api/Jobs/TagJobs/MergeTagsJob.php diff --git a/src/Api/Jobs/RenameTagsJob.php b/src/Api/Jobs/TagJobs/RenameTagsJob.php similarity index 100% rename from src/Api/Jobs/RenameTagsJob.php rename to src/Api/Jobs/TagJobs/RenameTagsJob.php diff --git a/src/Api/Jobs/UserJobs/AbstractUserJob.php b/src/Api/Jobs/UserJobs/AbstractUserJob.php deleted file mode 100644 index 5bf5cfc6..00000000 --- a/src/Api/Jobs/UserJobs/AbstractUserJob.php +++ /dev/null @@ -1,30 +0,0 @@ -hasArgument(JobArgs::ARG_USER_ENTITY)) - { - $this->user = $this->getArgument(JobArgs::ARG_USER_ENTITY); - } - else - { - $userName = $this->getArgument(JobArgs::ARG_USER_NAME); - $this->user = UserModel::getByNameOrEmail($userName); - } - } - - public function getRequiredArguments() - { - return JobArgs::Conjunction( - JobArgs::Alternative( - JobArgs::ARG_USER_NAME, - JobArgs::ARG_USER_EMAIL, - JobArgs::ARG_USER_ENTITY), - $this->getRequiredSubArguments()); - } - - public abstract function getRequiredSubArguments(); -} diff --git a/src/Api/Jobs/UserJobs/AcceptUserRegistrationJob.php b/src/Api/Jobs/UserJobs/AcceptUserRegistrationJob.php index 3fdc2ee4..110a5142 100644 --- a/src/Api/Jobs/UserJobs/AcceptUserRegistrationJob.php +++ b/src/Api/Jobs/UserJobs/AcceptUserRegistrationJob.php @@ -1,9 +1,16 @@ userRetriever = new UserRetriever($this); + } + public function execute() { - $user = $this->user; + $user = $this->userRetriever->getRequiredArguments(); $user->setStaffConfirmed(true); UserModel::save($user); @@ -13,9 +20,9 @@ class AcceptUserRegistrationJob extends AbstractUserJob 'subject' => TextHelper::reprUser($user)]); } - public function getRequiredSubArguments() + public function getRequiredArguments() { - return null; + return $this->userRetriever->getRequiredArguments(); } public function getRequiredPrivileges() diff --git a/src/Api/Jobs/ActivateUserEmailJob.php b/src/Api/Jobs/UserJobs/ActivateUserEmailJob.php similarity index 78% rename from src/Api/Jobs/ActivateUserEmailJob.php rename to src/Api/Jobs/UserJobs/ActivateUserEmailJob.php index b7cf5c09..f1746c11 100644 --- a/src/Api/Jobs/ActivateUserEmailJob.php +++ b/src/Api/Jobs/UserJobs/ActivateUserEmailJob.php @@ -1,16 +1,18 @@ userRetriever = new UserRetriever($this); + } + public function execute() { if (!$this->hasArgument(JobArgs::ARG_TOKEN)) { - if ($this->hasArgument(JobArgs::ARG_USER_ENTITY)) - $user = $this->getArgument(JobArgs::ARG_USER_ENTITY); - elseif ($this->hasArgument(JobArgs::ARG_USER_NAME)) - $user = UserModel::getByName($this->getArgument(JobArgs::ARG_USER_NAME)); - else - $user = UserModel::getByEmail($this->getArgument(JobArgs::ARG_USER_EMAIL)); + $user = $this->userRetriever->retrieve(); if (empty($user->getUnconfirmedEmail())) { @@ -47,10 +49,7 @@ class ActivateUserEmailJob extends AbstractJob { return JobArgs::Alternative( JobArgs::ARG_TOKEN, - JobArgs::Alternative( - JobArgs::ARG_USER_ENTITY, - JobArgs::ARG_USER_EMAIL, - JobArgs::ARG_USER_NAME)); + $this->userRetriever->getRequiredArguments()); } public static function sendEmail($user) diff --git a/src/Api/Jobs/AddUserJob.php b/src/Api/Jobs/UserJobs/AddUserJob.php similarity index 100% rename from src/Api/Jobs/AddUserJob.php rename to src/Api/Jobs/UserJobs/AddUserJob.php diff --git a/src/Api/Jobs/UserJobs/DeleteUserJob.php b/src/Api/Jobs/UserJobs/DeleteUserJob.php index 60b59a87..f92759e2 100644 --- a/src/Api/Jobs/UserJobs/DeleteUserJob.php +++ b/src/Api/Jobs/UserJobs/DeleteUserJob.php @@ -1,9 +1,16 @@ userRetriever = new UserRetriever($this); + } + public function execute() { - $user = $this->user; + $user = $this->userRetriever->retrieve(); $name = $user->getName(); UserModel::remove($user); @@ -13,15 +20,15 @@ class DeleteUserJob extends AbstractUserJob 'subject' => TextHelper::reprUser($name)]); } - public function getRequiredSubArguments() + public function getRequiredArguments() { - return null; + return $this->userRetriever->getRequiredArguments(); } public function getRequiredPrivileges() { return new Privilege( Privilege::DeleteUser, - Access::getIdentity($this->user)); + Access::getIdentity($this->userRetriever->retrieve())); } } diff --git a/src/Api/Jobs/UserJobs/EditUserAccessRankJob.php b/src/Api/Jobs/UserJobs/EditUserAccessRankJob.php index 755eeee0..82584b2b 100644 --- a/src/Api/Jobs/UserJobs/EditUserAccessRankJob.php +++ b/src/Api/Jobs/UserJobs/EditUserAccessRankJob.php @@ -1,9 +1,16 @@ userRetriever = new UserRetriever($this); + } + public function execute() { - $user = $this->user; + $user = $this->userRetriever->retrieve(); $newAccessRank = new AccessRank($this->getArgument(JobArgs::ARG_NEW_ACCESS_RANK)); $oldAccessRank = $user->getAccessRank(); @@ -23,15 +30,17 @@ class EditUserAccessRankJob extends AbstractUserJob return $user; } - public function getRequiredSubArguments() + public function getRequiredArguments() { - return JobArgs::ARG_NEW_ACCESS_RANK; + return JobArgs::Conjunction( + $this->userRetriever->getRequiredArguments(), + JobArgs::ARG_NEW_ACCESS_RANK); } public function getRequiredPrivileges() { return new Privilege( Privilege::ChangeUserAccessRank, - Access::getIdentity($this->user)); + Access::getIdentity($this->userRetriever->retrieve())); } } diff --git a/src/Api/Jobs/UserJobs/EditUserEmailJob.php b/src/Api/Jobs/UserJobs/EditUserEmailJob.php index aecd3120..e1929eba 100644 --- a/src/Api/Jobs/UserJobs/EditUserEmailJob.php +++ b/src/Api/Jobs/UserJobs/EditUserEmailJob.php @@ -1,13 +1,20 @@ userRetriever = new UserRetriever($this); + } + public function execute() { if (getConfig()->registration->needEmailForRegistering) if (!$this->hasArgument(JobArgs::ARG_NEW_EMAIL) or empty($this->getArgument(JobArgs::ARG_NEW_EMAIL))) throw new SimpleException('E-mail address is required - you will be sent confirmation e-mail.'); - $user = $this->user; + $user = $this->userRetriever->retrieve(); $newEmail = $this->getArgument(JobArgs::ARG_NEW_EMAIL); $oldEmail = $user->getConfirmedEmail(); @@ -44,9 +51,11 @@ class EditUserEmailJob extends AbstractUserJob } } - public function getRequiredSubArguments() + public function getRequiredArguments() { - return JobArgs::ARG_NEW_EMAIL; + return JobArgs::Conjunction( + $this->userRetriever->getRequiredArguments(), + JobArgs::ARG_NEW_EMAIL); } public function getRequiredPrivileges() @@ -55,6 +64,6 @@ class EditUserEmailJob extends AbstractUserJob $this->getContext() == self::CONTEXT_BATCH_ADD ? Privilege::RegisterAccount : Privilege::ChangeUserEmail, - Access::getIdentity($this->user)); + Access::getIdentity($this->userRetriever->retrieve())); } } diff --git a/src/Api/Jobs/UserJobs/EditUserJob.php b/src/Api/Jobs/UserJobs/EditUserJob.php index 3110583a..bdc8727b 100644 --- a/src/Api/Jobs/UserJobs/EditUserJob.php +++ b/src/Api/Jobs/UserJobs/EditUserJob.php @@ -1,10 +1,12 @@ userRetriever = new UserRetriever($this); $this->subJobs = [ new EditUserAccessRankJob(), @@ -34,7 +36,7 @@ class EditUserJob extends AbstractUserJob public function execute() { - $user = $this->user; + $user = $this->userRetriever->retrieve(); Logger::bufferChanges(); @@ -65,9 +67,9 @@ class EditUserJob extends AbstractUserJob return $user; } - public function getRequiredSubArguments() + public function getRequiredArguments() { - return null; + return $this->userRetriever->getRequiredArguments(); } public function getRequiredPrivileges() diff --git a/src/Api/Jobs/UserJobs/EditUserNameJob.php b/src/Api/Jobs/UserJobs/EditUserNameJob.php index 9a92b212..6149e7b5 100644 --- a/src/Api/Jobs/UserJobs/EditUserNameJob.php +++ b/src/Api/Jobs/UserJobs/EditUserNameJob.php @@ -1,9 +1,16 @@ userRetriever = new UserRetriever($this); + } + public function execute() { - $user = $this->user; + $user = $this->userRetriever->retrieve(); $newName = $this->getArgument(JobArgs::ARG_NEW_USER_NAME); $oldName = $user->getName(); @@ -23,9 +30,11 @@ class EditUserNameJob extends AbstractUserJob return $user; } - public function getRequiredSubArguments() + public function getRequiredArguments() { - return JobArgs::ARG_NEW_USER_NAME; + return JobArgs::Conjunction( + $this->userRetriever->getRequiredArguments(), + JobArgs::ARG_NEW_USER_NAME); } public function getRequiredPrivileges() @@ -34,6 +43,6 @@ class EditUserNameJob extends AbstractUserJob $this->getContext() == self::CONTEXT_BATCH_ADD ? Privilege::RegisterAccount : Privilege::ChangeUserName, - Access::getIdentity($this->user)); + Access::getIdentity($this->userRetriever->retrieve())); } } diff --git a/src/Api/Jobs/UserJobs/EditUserPasswordJob.php b/src/Api/Jobs/UserJobs/EditUserPasswordJob.php index c3ab4687..06f35224 100644 --- a/src/Api/Jobs/UserJobs/EditUserPasswordJob.php +++ b/src/Api/Jobs/UserJobs/EditUserPasswordJob.php @@ -1,9 +1,16 @@ userRetriever = new UserRetriever($this); + } + public function execute() { - $user = $this->user; + $user = $this->userRetriever->retrieve(); $newPassword = $this->getArgument(JobArgs::ARG_NEW_PASSWORD); $oldPasswordHash = $user->getPasswordHash(); @@ -22,9 +29,11 @@ class EditUserPasswordJob extends AbstractUserJob return $user; } - public function getRequiredSubArguments() + public function getRequiredArguments() { - return JobArgs::ARG_NEW_PASSWORD; + return JobArgs::Conjunction( + $this->userRetriever->getRequiredArguments(), + JobArgs::ARG_NEW_PASSWORD); } public function getRequiredPrivileges() @@ -33,6 +42,6 @@ class EditUserPasswordJob extends AbstractUserJob $this->getContext() == self::CONTEXT_BATCH_ADD ? Privilege::RegisterAccount : Privilege::ChangeUserPassword, - Access::getIdentity($this->user)); + Access::getIdentity($this->userRetriever->retrieve())); } } diff --git a/src/Api/Jobs/UserJobs/FlagUserJob.php b/src/Api/Jobs/UserJobs/FlagUserJob.php index 3911fdbc..95250ac6 100644 --- a/src/Api/Jobs/UserJobs/FlagUserJob.php +++ b/src/Api/Jobs/UserJobs/FlagUserJob.php @@ -1,9 +1,16 @@ userRetriever = new UserRetriever($this); + } + public function execute() { - $user = $this->user; + $user = $this->userRetriever->retrieve(); $key = TextHelper::reprUser($user); $flagged = SessionHelper::get('flagged', []); @@ -19,15 +26,15 @@ class FlagUserJob extends AbstractUserJob return $user; } - public function getRequiredSubArguments() + public function getRequiredArguments() { - return null; + return $this->userRetriever->getRequiredArguments(); } public function getRequiredPrivileges() { return new Privilege( Privilege::FlagUser, - Access::getIdentity($this->user)); + Access::getIdentity($this->userRetriever->retrieve())); } } diff --git a/src/Api/Jobs/UserJobs/GetUserJob.php b/src/Api/Jobs/UserJobs/GetUserJob.php index 9f3e5e6d..b02a48e3 100644 --- a/src/Api/Jobs/UserJobs/GetUserJob.php +++ b/src/Api/Jobs/UserJobs/GetUserJob.php @@ -1,20 +1,27 @@ user; + $this->userRetriever = new UserRetriever($this); } - public function getRequiredSubArguments() + public function execute() { - return null; + return $this->userRetriever->retrieve(); + } + + public function getRequiredArguments() + { + return $this->userRetriever->getRequiredArguments(); } public function getRequiredPrivileges() { return new Privilege( Privilege::ViewUser, - Access::getIdentity($this->user)); + Access::getIdentity($this->userRetriever->retrieve())); } } diff --git a/src/Api/Jobs/UserJobs/ListUsersJob.php b/src/Api/Jobs/UserJobs/ListUsersJob.php new file mode 100644 index 00000000..cc700558 --- /dev/null +++ b/src/Api/Jobs/UserJobs/ListUsersJob.php @@ -0,0 +1,40 @@ +pager = new JobPager($this); + $this->pager->setPageSize(getConfig()->browsing->usersPerPage); + } + + public function getPager() + { + return $this->pager; + } + + public function execute() + { + $pageSize = $this->pager->getPageSize(); + $page = $this->pager->getPageNumber(); + $filter = $this->getArgument(JobArgs::ARG_QUERY); + + $users = UserSearchService::getEntities($filter, $pageSize, $page); + $userCount = UserSearchService::getEntityCount($filter); + + return $this->pager->serialize($users, $userCount); + } + + public function getRequiredArguments() + { + return JobArgs::Conjunction( + $this->pager->getRequiredArguments(), + JobArgs::ARG_QUERY); + } + + public function getRequiredPrivileges() + { + return new Privilege(Privilege::ListUsers); + } +} diff --git a/src/Api/Jobs/PasswordResetJob.php b/src/Api/Jobs/UserJobs/PasswordResetJob.php similarity index 79% rename from src/Api/Jobs/PasswordResetJob.php rename to src/Api/Jobs/UserJobs/PasswordResetJob.php index d75a4b49..251fb5fd 100644 --- a/src/Api/Jobs/PasswordResetJob.php +++ b/src/Api/Jobs/UserJobs/PasswordResetJob.php @@ -1,16 +1,18 @@ userRetriever = new UserRetriever($this); + } + public function execute() { if (!$this->hasArgument(JobArgs::ARG_TOKEN)) { - if ($this->hasArgument(JobArgs::ARG_USER_NAME)) - $user = UserModel::getByNameOrEmail($this->getArgument(JobArgs::ARG_USER_NAME)); - elseif ($this->hasArgument(JobArgs::ARG_USER_EMAIL)) - $user = UserModel::getByNameOrEmail($this->getArgument(JobArgs::ARG_USER_EMAIL)); - else - $user = $this->getArgument(JobArgs::ARG_USER_ENTITTY); + $user = $this->userRetriever->retrieve(); if (empty($user->getConfirmedEmail())) throw new SimpleException('This user has no e-mail confirmed; password reset cannot proceed'); @@ -50,9 +52,7 @@ class PasswordResetJob extends AbstractJob public function getRequiredArguments() { return JobArgs::Alternative( - JobArgs::ARG_USER_NAME, - JobArgs::ARG_USER_EMAIL, - JobArgs::ARG_USER_ENTITY, + $this->userRetriever->getRequiredArguments(), JobArgs::ARG_TOKEN); } diff --git a/src/Api/Jobs/UserJobs/ToggleUserBanJob.php b/src/Api/Jobs/UserJobs/ToggleUserBanJob.php index 76c2897e..2e7f02d5 100644 --- a/src/Api/Jobs/UserJobs/ToggleUserBanJob.php +++ b/src/Api/Jobs/UserJobs/ToggleUserBanJob.php @@ -1,9 +1,16 @@ userRetriever = new UserRetriever($this); + } + public function execute() { - $user = $this->user; + $user = $this->userRetriever->retrieve(); $banned = boolval($this->getArgument(JobArgs::ARG_NEW_STATE)); if ($banned) @@ -22,15 +29,17 @@ class ToggleUserBanJob extends AbstractUserJob return $user; } - public function getRequiredSubArguments() + public function getRequiredArguments() { - return JobArgs::ARG_NEW_STATE; + return JobArgs::Conjunction( + $this->userRetriever->getRequiredArguments(), + JobArgs::ARG_NEW_STATE); } public function getRequiredPrivileges() { return new Privilege( Privilege::BanUser, - Access::getIdentity($this->user)); + Access::getIdentity($this->userRetriever->retrieve())); } } diff --git a/tests/Api/ApiArgumentTest.php b/tests/Api/ApiArgumentTest.php index 55847140..6c6616b6 100644 --- a/tests/Api/ApiArgumentTest.php +++ b/tests/Api/ApiArgumentTest.php @@ -44,9 +44,7 @@ class ApiArgumentTest extends AbstractFullApiTest public function testDeleteCommentJob() { $this->testArguments(new DeleteCommentJob(), - JobArgs::Alternative( - JobArgs::ARG_COMMENT_ENTITY, - JobArgs::ARG_COMMENT_ID)); + $this->getCommentSelector()); } public function testDeleteUserJob() @@ -60,9 +58,7 @@ class ApiArgumentTest extends AbstractFullApiTest $this->testArguments(new EditCommentJob(), JobArgs::Conjunction( JobArgs::ARG_NEW_TEXT, - JobArgs::Alternative( - JobArgs::ARG_COMMENT_ENTITY, - JobArgs::ARG_COMMENT_ID))); + $this->getCommentSelector())); } public function testEditPostContentJob() @@ -185,7 +181,7 @@ class ApiArgumentTest extends AbstractFullApiTest JobArgs::Conjunction( JobArgs::ARG_QUERY, JobArgs::ARG_LOG_ID, - JobArgs::ARG_PAGE_NUMBER)); + JobArgs::Optional(JobArgs::ARG_PAGE_NUMBER))); } public function testGetPostContentJob() @@ -218,7 +214,7 @@ class ApiArgumentTest extends AbstractFullApiTest public function testListCommentsJob() { $this->testArguments(new ListCommentsJob(), - JobArgs::ARG_PAGE_NUMBER); + JobArgs::Optional(JobArgs::ARG_PAGE_NUMBER)); } public function testListLogsJob() @@ -232,7 +228,7 @@ class ApiArgumentTest extends AbstractFullApiTest $this->testArguments(new ListPostsJob(), JobArgs::Conjunction( JobArgs::ARG_QUERY, - JobArgs::ARG_PAGE_NUMBER)); + JobArgs::Optional(JobArgs::ARG_PAGE_NUMBER))); } public function testListRelatedTagsJob() @@ -241,8 +237,7 @@ class ApiArgumentTest extends AbstractFullApiTest JobArgs::Conjunction( JobArgs::Optional(JobArgs::ARG_TAG_NAMES), JobArgs::ARG_TAG_NAME, - JobArgs::ARG_QUERY, - JobArgs::ARG_PAGE_NUMBER)); + JobArgs::Optional(JobArgs::ARG_PAGE_NUMBER))); } public function testListTagsJob() @@ -250,7 +245,7 @@ class ApiArgumentTest extends AbstractFullApiTest $this->testArguments(new ListTagsJob(), JobArgs::Conjunction( JobArgs::ARG_QUERY, - JobArgs::ARG_PAGE_NUMBER)); + JobArgs::Optional(JobArgs::ARG_PAGE_NUMBER))); } public function testListUsersJob() @@ -258,7 +253,7 @@ class ApiArgumentTest extends AbstractFullApiTest $this->testArguments(new ListUsersJob(), JobArgs::Conjunction( JobArgs::ARG_QUERY, - JobArgs::ARG_PAGE_NUMBER)); + JobArgs::Optional(JobArgs::ARG_PAGE_NUMBER))); } public function testMergeTagsJob() @@ -290,8 +285,8 @@ class ApiArgumentTest extends AbstractFullApiTest $this->testArguments(new PreviewCommentJob(), JobArgs::Conjunction( JobArgs::Alternative( - JobArgs::ARG_POST_ID, - JobArgs::ARG_COMMENT_ID), + $this->getPostSelector(), + $this->getCommentSelector()), JobArgs::ARG_NEW_TEXT)); } @@ -357,6 +352,13 @@ class ApiArgumentTest extends AbstractFullApiTest JobArgs::ARG_POST_ENTITY); } + protected function getCommentSelector() + { + return JobArgs::Alternative( + JobArgs::ARG_COMMENT_ENTITY, + JobArgs::ARG_COMMENT_ID); + } + protected function getUserSelector() { return JobArgs::Alternative(