From 977989ffedc52bfa116bef7f8e7bf525a9f56f61 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Sun, 4 May 2014 17:53:40 +0200 Subject: [PATCH] Added one-time save to posts/users adding/editing --- src/Api/AbstractJob.php | 10 ++++++++-- src/Api/AbstractPostJob.php | 11 +++++++++-- src/Api/AbstractUserJob.php | 11 +++++++++-- src/Api/Jobs/AbstractPostEditJob.php | 11 +++++++++++ src/Api/Jobs/AbstractUserEditJob.php | 11 +++++++++++ src/Api/Jobs/AddPostJob.php | 13 ++++++------- src/Api/Jobs/AddUserJob.php | 7 ++----- src/Api/Jobs/EditPostContentJob.php | 6 ++++-- src/Api/Jobs/EditPostJob.php | 10 ++++++++-- src/Api/Jobs/EditPostRelationsJob.php | 5 +++-- src/Api/Jobs/EditPostSafetyJob.php | 5 +++-- src/Api/Jobs/EditPostSourceJob.php | 5 +++-- src/Api/Jobs/EditPostTagsJob.php | 9 ++++++--- src/Api/Jobs/EditPostThumbJob.php | 5 +++-- src/Api/Jobs/EditPostUrlJob.php | 5 +++-- src/Api/Jobs/EditUserAccessRankJob.php | 5 +++-- src/Api/Jobs/EditUserEmailJob.php | 5 +++-- src/Api/Jobs/EditUserJob.php | 12 +++++++++--- src/Api/Jobs/EditUserNameJob.php | 5 +++-- src/Api/Jobs/EditUserPasswordJob.php | 5 +++-- 20 files changed, 110 insertions(+), 46 deletions(-) create mode 100644 src/Api/Jobs/AbstractPostEditJob.php create mode 100644 src/Api/Jobs/AbstractUserEditJob.php diff --git a/src/Api/AbstractJob.php b/src/Api/AbstractJob.php index a71997a9..a4a683f8 100644 --- a/src/Api/AbstractJob.php +++ b/src/Api/AbstractJob.php @@ -2,16 +2,22 @@ abstract class AbstractJob { const COMMENT_ID = 'comment-id'; + const LOG_ID = 'log-id'; + + const POST_ENTITY = 'post'; const POST_ID = 'post-id'; const POST_NAME = 'post-name'; + const TAG_NAME = 'tag-name'; const TAG_NAMES = 'tags'; + + const USER_ENTITY = 'user'; const USER_ID = 'user-id'; const USER_NAME = 'user-name'; - const TEXT = 'text'; + const PAGE_NUMBER = 'page-number'; + const TEXT = 'text'; const QUERY = 'query'; - const LOG_ID = 'log-id'; const STATE = 'state'; protected $arguments; diff --git a/src/Api/AbstractPostJob.php b/src/Api/AbstractPostJob.php index d886a103..470a2187 100644 --- a/src/Api/AbstractPostJob.php +++ b/src/Api/AbstractPostJob.php @@ -5,7 +5,14 @@ abstract class AbstractPostJob extends AbstractJob public function prepare() { - $postId = $this->getArgument(self::POST_ID); - $this->post = PostModel::findByIdOrName($postId); + if ($this->hasArgument(self::POST_ENTITY)) + { + $this->post = $this->getArgument(self::POST_ENTITY); + } + else + { + $postId = $this->getArgument(self::POST_ID); + $this->post = PostModel::findByIdOrName($postId); + } } } diff --git a/src/Api/AbstractUserJob.php b/src/Api/AbstractUserJob.php index 00dab341..1b2acf11 100644 --- a/src/Api/AbstractUserJob.php +++ b/src/Api/AbstractUserJob.php @@ -5,7 +5,14 @@ abstract class AbstractUserJob extends AbstractJob public function prepare() { - $userName = $this->getArgument(self::USER_NAME); - $this->user = UserModel::findByNameOrEmail($userName); + if ($this->hasArgument(self::USER_ENTITY)) + { + $this->user = $this->getArgument(self::USER_ENTITY); + } + else + { + $userName = $this->getArgument(self::USER_NAME); + $this->user = UserModel::findByNameOrEmail($userName); + } } } diff --git a/src/Api/Jobs/AbstractPostEditJob.php b/src/Api/Jobs/AbstractPostEditJob.php new file mode 100644 index 00000000..5d1ce361 --- /dev/null +++ b/src/Api/Jobs/AbstractPostEditJob.php @@ -0,0 +1,11 @@ +skipSaving = true; + return $this; + } +} diff --git a/src/Api/Jobs/AbstractUserEditJob.php b/src/Api/Jobs/AbstractUserEditJob.php new file mode 100644 index 00000000..c72f4639 --- /dev/null +++ b/src/Api/Jobs/AbstractUserEditJob.php @@ -0,0 +1,11 @@ +skipSaving = true; + return $this; + } +} diff --git a/src/Api/Jobs/AddPostJob.php b/src/Api/Jobs/AddPostJob.php index f0a86fe7..05a0cbaa 100644 --- a/src/Api/Jobs/AddPostJob.php +++ b/src/Api/Jobs/AddPostJob.php @@ -17,13 +17,10 @@ class AddPostJob extends AbstractJob PostModel::forgeId($post); //do the edits - //warning: it uses the same privileges as post editing internally + //warning: it uses internally the same privileges as post editing $arguments = $this->getArguments(); - $arguments[EditPostJob::POST_ID] = $post->id; - Api::run(new EditPostJob(), $arguments); - - //load the post after edits - $post = PostModel::findById($post->id); + $arguments[EditPostJob::POST_ENTITY] = $post; + Api::run((new EditPostJob)->skipSaving(), $arguments); // basically means that user didn't specify file nor url //todo: @@ -33,6 +30,9 @@ class AddPostJob extends AbstractJob if (empty($post->type)) throw new SimpleException('No post type detected; upload faled'); + //save to db + PostModel::save($post); + //clean edit log LogHelper::setBuffer([]); @@ -48,7 +48,6 @@ class AddPostJob extends AbstractJob //finish LogHelper::flush(); - PostModel::save($post); return $post; } diff --git a/src/Api/Jobs/AddUserJob.php b/src/Api/Jobs/AddUserJob.php index 46450322..688e56da 100644 --- a/src/Api/Jobs/AddUserJob.php +++ b/src/Api/Jobs/AddUserJob.php @@ -12,7 +12,7 @@ class AddUserJob extends AbstractJob UserModel::forgeId($user); $arguments = $this->getArguments(); - $arguments[EditUserJob::USER_NAME] = $user->name; + $arguments[EditUserJob::USER_ENTITY] = $user; $arguments[EditUserAccessRankJob::NEW_ACCESS_RANK] = $firstUser ? AccessRank::Admin @@ -20,16 +20,13 @@ class AddUserJob extends AbstractJob LogHelper::bufferChanges(); Api::disablePrivilegeChecking(); - Api::run(new EditUserJob(), $arguments); + Api::run((new EditUserJob)->skipSaving(), $arguments); Api::enablePrivilegeChecking(); LogHelper::setBuffer([]); if ($firstUser) $user->confirmEmail(); - //load the user after edits - $user = UserModel::findById($user->id); - //save the user to db if everything went okay UserModel::save($user); diff --git a/src/Api/Jobs/EditPostContentJob.php b/src/Api/Jobs/EditPostContentJob.php index a4d1ff57..46e2c056 100644 --- a/src/Api/Jobs/EditPostContentJob.php +++ b/src/Api/Jobs/EditPostContentJob.php @@ -1,5 +1,5 @@ setContentFromPath($file->filePath, $file->fileName); - PostModel::save($post); + if (!$this->skipSaving) + PostModel::save($post); + LogHelper::log('{user} changed contents of {post}', [ 'user' => TextHelper::reprUser(Auth::getCurrentUser()), 'post' => TextHelper::reprPost($post)]); diff --git a/src/Api/Jobs/EditPostJob.php b/src/Api/Jobs/EditPostJob.php index b08ca696..dc8230de 100644 --- a/src/Api/Jobs/EditPostJob.php +++ b/src/Api/Jobs/EditPostJob.php @@ -1,5 +1,5 @@ skipSaving) + $subJob->skipSaving(); + $args = $this->getArguments(); - $args[self::POST_ID] = $post->id; + $args[self::POST_ENTITY] = $post; try { Api::run($subJob, $args); @@ -31,6 +34,9 @@ class EditPostJob extends AbstractPostJob } } + if (!$this->skipSaving) + PostModel::save($post); + LogHelper::flush(); return $post; } diff --git a/src/Api/Jobs/EditPostRelationsJob.php b/src/Api/Jobs/EditPostRelationsJob.php index 06a999fe..a8134aa9 100644 --- a/src/Api/Jobs/EditPostRelationsJob.php +++ b/src/Api/Jobs/EditPostRelationsJob.php @@ -1,5 +1,5 @@ setRelationsFromText($relations); $newRelatedIds = array_map(function($post) { return $post->id; }, $post->getRelations()); - PostModel::save($post); + if (!$this->skipSaving) + PostModel::save($post); foreach (array_diff($oldRelatedIds, $newRelatedIds) as $post2id) { diff --git a/src/Api/Jobs/EditPostSafetyJob.php b/src/Api/Jobs/EditPostSafetyJob.php index 5fe25803..6ed468ef 100644 --- a/src/Api/Jobs/EditPostSafetyJob.php +++ b/src/Api/Jobs/EditPostSafetyJob.php @@ -1,5 +1,5 @@ safety; $post->setSafety($newSafety); - PostModel::save($post); + if (!$this->skipSaving) + PostModel::save($post); if ($oldSafety != $newSafety) { diff --git a/src/Api/Jobs/EditPostSourceJob.php b/src/Api/Jobs/EditPostSourceJob.php index 7dc6233c..14ca0037 100644 --- a/src/Api/Jobs/EditPostSourceJob.php +++ b/src/Api/Jobs/EditPostSourceJob.php @@ -1,5 +1,5 @@ source; $post->setSource($newSource); - PostModel::save($post); + if (!$this->skipSaving) + PostModel::save($post); if ($oldSource != $newSource) { diff --git a/src/Api/Jobs/EditPostTagsJob.php b/src/Api/Jobs/EditPostTagsJob.php index b82482f2..f9994329 100644 --- a/src/Api/Jobs/EditPostTagsJob.php +++ b/src/Api/Jobs/EditPostTagsJob.php @@ -1,5 +1,5 @@ setTagsFromText($tags); $newTags = array_map(function($tag) { return $tag->name; }, $post->getTags()); - PostModel::save($post); - TagModel::removeUnused(); + if (!$this->skipSaving) + { + PostModel::save($post); + TagModel::removeUnused(); + } foreach (array_diff($oldTags, $newTags) as $tag) { diff --git a/src/Api/Jobs/EditPostThumbJob.php b/src/Api/Jobs/EditPostThumbJob.php index 9f83f4f3..af2cf9a1 100644 --- a/src/Api/Jobs/EditPostThumbJob.php +++ b/src/Api/Jobs/EditPostThumbJob.php @@ -1,5 +1,5 @@ setCustomThumbnailFromPath($file->filePath); - PostModel::save($post); + if (!$this->skipSaving) + PostModel::save($post); LogHelper::log('{user} changed thumb of {post}', [ 'user' => TextHelper::reprUser(Auth::getCurrentUser()), diff --git a/src/Api/Jobs/EditPostUrlJob.php b/src/Api/Jobs/EditPostUrlJob.php index a357cf4f..0817152f 100644 --- a/src/Api/Jobs/EditPostUrlJob.php +++ b/src/Api/Jobs/EditPostUrlJob.php @@ -1,5 +1,5 @@ setContentFromUrl($url); - PostModel::save($post); + if (!$this->skipSaving) + PostModel::save($post); LogHelper::log('{user} changed contents of {post}', [ 'user' => TextHelper::reprUser(Auth::getCurrentUser()), diff --git a/src/Api/Jobs/EditUserAccessRankJob.php b/src/Api/Jobs/EditUserAccessRankJob.php index 5b3de9b1..b62f070c 100644 --- a/src/Api/Jobs/EditUserAccessRankJob.php +++ b/src/Api/Jobs/EditUserAccessRankJob.php @@ -1,5 +1,5 @@ accessRank = $newAccessRank; - UserModel::save($user); + if (!$this->skipSaving) + UserModel::save($user); LogHelper::log('{user} changed {subject}\'s access rank to {rank}', [ 'user' => TextHelper::reprUser(Auth::getCurrentUser()), diff --git a/src/Api/Jobs/EditUserEmailJob.php b/src/Api/Jobs/EditUserEmailJob.php index 5c5f56be..ad49331d 100644 --- a/src/Api/Jobs/EditUserEmailJob.php +++ b/src/Api/Jobs/EditUserEmailJob.php @@ -1,5 +1,5 @@ confirmEmail(); } - UserModel::save($user); + if (!$this->skipSaving) + UserModel::save($user); LogHelper::log('{user} changed {subject}\'s e-mail to {mail}', [ 'user' => TextHelper::reprUser(Auth::getCurrentUser()), diff --git a/src/Api/Jobs/EditUserJob.php b/src/Api/Jobs/EditUserJob.php index 418dece3..b778ba9a 100644 --- a/src/Api/Jobs/EditUserJob.php +++ b/src/Api/Jobs/EditUserJob.php @@ -1,5 +1,5 @@ subJobs as $subJob) { + if ($this->skipSaving) + $subJob->skipSaving(); + $args = $this->getArguments(); - $args[self::USER_NAME] = $user->name; + $args[self::USER_ENTITY] = $user; try { Api::run($subJob, $args); @@ -51,6 +54,9 @@ class EditUserJob extends AbstractUserJob } } + if (!$this->skipSaving) + UserModel::save($user); + LogHelper::flush(); return $user; } diff --git a/src/Api/Jobs/EditUserNameJob.php b/src/Api/Jobs/EditUserNameJob.php index 56342c83..5c23ef7a 100644 --- a/src/Api/Jobs/EditUserNameJob.php +++ b/src/Api/Jobs/EditUserNameJob.php @@ -1,5 +1,5 @@ name = $newName; UserModel::validateUserName($user); - UserModel::save($user); + if (!$this->skipSaving) + UserModel::save($user); LogHelper::log('{user} renamed {old} to {new}', [ 'user' => TextHelper::reprUser(Auth::getCurrentUser()), diff --git a/src/Api/Jobs/EditUserPasswordJob.php b/src/Api/Jobs/EditUserPasswordJob.php index 9e5964be..3958a14a 100644 --- a/src/Api/Jobs/EditUserPasswordJob.php +++ b/src/Api/Jobs/EditUserPasswordJob.php @@ -1,5 +1,5 @@ passHash = $newPasswordHash; - UserModel::save($user); + if (!$this->skipSaving) + UserModel::save($user); LogHelper::log('{user} changed {subject}\'s password', [ 'user' => TextHelper::reprUser(Auth::getCurrentUser()),