Added one-time save to posts/users adding/editing

This commit is contained in:
Marcin Kurczewski 2014-05-04 17:53:40 +02:00
parent b02c55e52c
commit 977989ffed
20 changed files with 110 additions and 46 deletions

View file

@ -2,16 +2,22 @@
abstract class AbstractJob abstract class AbstractJob
{ {
const COMMENT_ID = 'comment-id'; const COMMENT_ID = 'comment-id';
const LOG_ID = 'log-id';
const POST_ENTITY = 'post';
const POST_ID = 'post-id'; const POST_ID = 'post-id';
const POST_NAME = 'post-name'; const POST_NAME = 'post-name';
const TAG_NAME = 'tag-name'; const TAG_NAME = 'tag-name';
const TAG_NAMES = 'tags'; const TAG_NAMES = 'tags';
const USER_ENTITY = 'user';
const USER_ID = 'user-id'; const USER_ID = 'user-id';
const USER_NAME = 'user-name'; const USER_NAME = 'user-name';
const TEXT = 'text';
const PAGE_NUMBER = 'page-number'; const PAGE_NUMBER = 'page-number';
const TEXT = 'text';
const QUERY = 'query'; const QUERY = 'query';
const LOG_ID = 'log-id';
const STATE = 'state'; const STATE = 'state';
protected $arguments; protected $arguments;

View file

@ -4,8 +4,15 @@ abstract class AbstractPostJob extends AbstractJob
protected $post; protected $post;
public function prepare() public function prepare()
{
if ($this->hasArgument(self::POST_ENTITY))
{
$this->post = $this->getArgument(self::POST_ENTITY);
}
else
{ {
$postId = $this->getArgument(self::POST_ID); $postId = $this->getArgument(self::POST_ID);
$this->post = PostModel::findByIdOrName($postId); $this->post = PostModel::findByIdOrName($postId);
} }
}
} }

View file

@ -4,8 +4,15 @@ abstract class AbstractUserJob extends AbstractJob
protected $user; protected $user;
public function prepare() public function prepare()
{
if ($this->hasArgument(self::USER_ENTITY))
{
$this->user = $this->getArgument(self::USER_ENTITY);
}
else
{ {
$userName = $this->getArgument(self::USER_NAME); $userName = $this->getArgument(self::USER_NAME);
$this->user = UserModel::findByNameOrEmail($userName); $this->user = UserModel::findByNameOrEmail($userName);
} }
}
} }

View file

@ -0,0 +1,11 @@
<?php
abstract class AbstractPostEditJob extends AbstractPostJob
{
protected $skipSaving = false;
public function skipSaving()
{
$this->skipSaving = true;
return $this;
}
}

View file

@ -0,0 +1,11 @@
<?php
abstract class AbstractUserEditJob extends AbstractUserJob
{
protected $skipSaving = false;
public function skipSaving()
{
$this->skipSaving = true;
return $this;
}
}

View file

@ -17,13 +17,10 @@ class AddPostJob extends AbstractJob
PostModel::forgeId($post); PostModel::forgeId($post);
//do the edits //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 = $this->getArguments();
$arguments[EditPostJob::POST_ID] = $post->id; $arguments[EditPostJob::POST_ENTITY] = $post;
Api::run(new EditPostJob(), $arguments); Api::run((new EditPostJob)->skipSaving(), $arguments);
//load the post after edits
$post = PostModel::findById($post->id);
// basically means that user didn't specify file nor url // basically means that user didn't specify file nor url
//todo: //todo:
@ -33,6 +30,9 @@ class AddPostJob extends AbstractJob
if (empty($post->type)) if (empty($post->type))
throw new SimpleException('No post type detected; upload faled'); throw new SimpleException('No post type detected; upload faled');
//save to db
PostModel::save($post);
//clean edit log //clean edit log
LogHelper::setBuffer([]); LogHelper::setBuffer([]);
@ -48,7 +48,6 @@ class AddPostJob extends AbstractJob
//finish //finish
LogHelper::flush(); LogHelper::flush();
PostModel::save($post);
return $post; return $post;
} }

View file

@ -12,7 +12,7 @@ class AddUserJob extends AbstractJob
UserModel::forgeId($user); UserModel::forgeId($user);
$arguments = $this->getArguments(); $arguments = $this->getArguments();
$arguments[EditUserJob::USER_NAME] = $user->name; $arguments[EditUserJob::USER_ENTITY] = $user;
$arguments[EditUserAccessRankJob::NEW_ACCESS_RANK] = $firstUser $arguments[EditUserAccessRankJob::NEW_ACCESS_RANK] = $firstUser
? AccessRank::Admin ? AccessRank::Admin
@ -20,16 +20,13 @@ class AddUserJob extends AbstractJob
LogHelper::bufferChanges(); LogHelper::bufferChanges();
Api::disablePrivilegeChecking(); Api::disablePrivilegeChecking();
Api::run(new EditUserJob(), $arguments); Api::run((new EditUserJob)->skipSaving(), $arguments);
Api::enablePrivilegeChecking(); Api::enablePrivilegeChecking();
LogHelper::setBuffer([]); LogHelper::setBuffer([]);
if ($firstUser) if ($firstUser)
$user->confirmEmail(); $user->confirmEmail();
//load the user after edits
$user = UserModel::findById($user->id);
//save the user to db if everything went okay //save the user to db if everything went okay
UserModel::save($user); UserModel::save($user);

View file

@ -1,5 +1,5 @@
<?php <?php
class EditPostContentJob extends AbstractPostJob class EditPostContentJob extends AbstractPostEditJob
{ {
const POST_CONTENT = 'post-content'; const POST_CONTENT = 'post-content';
@ -10,7 +10,9 @@ class EditPostContentJob extends AbstractPostJob
$post->setContentFromPath($file->filePath, $file->fileName); $post->setContentFromPath($file->filePath, $file->fileName);
if (!$this->skipSaving)
PostModel::save($post); PostModel::save($post);
LogHelper::log('{user} changed contents of {post}', [ LogHelper::log('{user} changed contents of {post}', [
'user' => TextHelper::reprUser(Auth::getCurrentUser()), 'user' => TextHelper::reprUser(Auth::getCurrentUser()),
'post' => TextHelper::reprPost($post)]); 'post' => TextHelper::reprPost($post)]);

View file

@ -1,5 +1,5 @@
<?php <?php
class EditPostJob extends AbstractPostJob class EditPostJob extends AbstractPostEditJob
{ {
public function execute() public function execute()
{ {
@ -20,8 +20,11 @@ class EditPostJob extends AbstractPostJob
foreach ($subJobs as $subJob) foreach ($subJobs as $subJob)
{ {
if ($this->skipSaving)
$subJob->skipSaving();
$args = $this->getArguments(); $args = $this->getArguments();
$args[self::POST_ID] = $post->id; $args[self::POST_ENTITY] = $post;
try try
{ {
Api::run($subJob, $args); Api::run($subJob, $args);
@ -31,6 +34,9 @@ class EditPostJob extends AbstractPostJob
} }
} }
if (!$this->skipSaving)
PostModel::save($post);
LogHelper::flush(); LogHelper::flush();
return $post; return $post;
} }

View file

@ -1,5 +1,5 @@
<?php <?php
class EditPostRelationsJob extends AbstractPostJob class EditPostRelationsJob extends AbstractPostEditJob
{ {
const RELATED_POST_IDS = 'related-post-ids'; const RELATED_POST_IDS = 'related-post-ids';
@ -12,6 +12,7 @@ class EditPostRelationsJob extends AbstractPostJob
$post->setRelationsFromText($relations); $post->setRelationsFromText($relations);
$newRelatedIds = array_map(function($post) { return $post->id; }, $post->getRelations()); $newRelatedIds = array_map(function($post) { return $post->id; }, $post->getRelations());
if (!$this->skipSaving)
PostModel::save($post); PostModel::save($post);
foreach (array_diff($oldRelatedIds, $newRelatedIds) as $post2id) foreach (array_diff($oldRelatedIds, $newRelatedIds) as $post2id)

View file

@ -1,5 +1,5 @@
<?php <?php
class EditPostSafetyJob extends AbstractPostJob class EditPostSafetyJob extends AbstractPostEditJob
{ {
const SAFETY = 'safety'; const SAFETY = 'safety';
@ -11,6 +11,7 @@ class EditPostSafetyJob extends AbstractPostJob
$oldSafety = $post->safety; $oldSafety = $post->safety;
$post->setSafety($newSafety); $post->setSafety($newSafety);
if (!$this->skipSaving)
PostModel::save($post); PostModel::save($post);
if ($oldSafety != $newSafety) if ($oldSafety != $newSafety)

View file

@ -1,5 +1,5 @@
<?php <?php
class EditPostSourceJob extends AbstractPostJob class EditPostSourceJob extends AbstractPostEditJob
{ {
const SOURCE = 'source'; const SOURCE = 'source';
@ -11,6 +11,7 @@ class EditPostSourceJob extends AbstractPostJob
$oldSource = $post->source; $oldSource = $post->source;
$post->setSource($newSource); $post->setSource($newSource);
if (!$this->skipSaving)
PostModel::save($post); PostModel::save($post);
if ($oldSource != $newSource) if ($oldSource != $newSource)

View file

@ -1,5 +1,5 @@
<?php <?php
class EditPostTagsJob extends AbstractPostJob class EditPostTagsJob extends AbstractPostEditJob
{ {
public function execute() public function execute()
{ {
@ -10,8 +10,11 @@ class EditPostTagsJob extends AbstractPostJob
$post->setTagsFromText($tags); $post->setTagsFromText($tags);
$newTags = array_map(function($tag) { return $tag->name; }, $post->getTags()); $newTags = array_map(function($tag) { return $tag->name; }, $post->getTags());
if (!$this->skipSaving)
{
PostModel::save($post); PostModel::save($post);
TagModel::removeUnused(); TagModel::removeUnused();
}
foreach (array_diff($oldTags, $newTags) as $tag) foreach (array_diff($oldTags, $newTags) as $tag)
{ {

View file

@ -1,5 +1,5 @@
<?php <?php
class EditPostThumbJob extends AbstractPostJob class EditPostThumbJob extends AbstractPostEditJob
{ {
const THUMB_CONTENT = 'thumb-content'; const THUMB_CONTENT = 'thumb-content';
@ -10,6 +10,7 @@ class EditPostThumbJob extends AbstractPostJob
$post->setCustomThumbnailFromPath($file->filePath); $post->setCustomThumbnailFromPath($file->filePath);
if (!$this->skipSaving)
PostModel::save($post); PostModel::save($post);
LogHelper::log('{user} changed thumb of {post}', [ LogHelper::log('{user} changed thumb of {post}', [

View file

@ -1,5 +1,5 @@
<?php <?php
class EditPostUrlJob extends AbstractPostJob class EditPostUrlJob extends AbstractPostEditJob
{ {
const POST_CONTENT_URL = 'post-content-url'; const POST_CONTENT_URL = 'post-content-url';
@ -10,6 +10,7 @@ class EditPostUrlJob extends AbstractPostJob
$post->setContentFromUrl($url); $post->setContentFromUrl($url);
if (!$this->skipSaving)
PostModel::save($post); PostModel::save($post);
LogHelper::log('{user} changed contents of {post}', [ LogHelper::log('{user} changed contents of {post}', [

View file

@ -1,5 +1,5 @@
<?php <?php
class EditUserAccessRankJob extends AbstractUserJob class EditUserAccessRankJob extends AbstractUserEditJob
{ {
const NEW_ACCESS_RANK = 'new-access-rank'; const NEW_ACCESS_RANK = 'new-access-rank';
@ -14,6 +14,7 @@ class EditUserAccessRankJob extends AbstractUserJob
$user->accessRank = $newAccessRank; $user->accessRank = $newAccessRank;
if (!$this->skipSaving)
UserModel::save($user); UserModel::save($user);
LogHelper::log('{user} changed {subject}\'s access rank to {rank}', [ LogHelper::log('{user} changed {subject}\'s access rank to {rank}', [

View file

@ -1,5 +1,5 @@
<?php <?php
class EditUserEmailJob extends AbstractUserJob class EditUserEmailJob extends AbstractUserEditJob
{ {
const NEW_EMAIL = 'new-email'; const NEW_EMAIL = 'new-email';
@ -29,6 +29,7 @@ class EditUserEmailJob extends AbstractUserJob
$user->confirmEmail(); $user->confirmEmail();
} }
if (!$this->skipSaving)
UserModel::save($user); UserModel::save($user);
LogHelper::log('{user} changed {subject}\'s e-mail to {mail}', [ LogHelper::log('{user} changed {subject}\'s e-mail to {mail}', [

View file

@ -1,5 +1,5 @@
<?php <?php
class EditUserJob extends AbstractUserJob class EditUserJob extends AbstractUserEditJob
{ {
protected $subJobs; protected $subJobs;
@ -38,10 +38,13 @@ class EditUserJob extends AbstractUserJob
LogHelper::bufferChanges(); LogHelper::bufferChanges();
foreach ($subJobs as $subJob) foreach ($this->subJobs as $subJob)
{ {
if ($this->skipSaving)
$subJob->skipSaving();
$args = $this->getArguments(); $args = $this->getArguments();
$args[self::USER_NAME] = $user->name; $args[self::USER_ENTITY] = $user;
try try
{ {
Api::run($subJob, $args); Api::run($subJob, $args);
@ -51,6 +54,9 @@ class EditUserJob extends AbstractUserJob
} }
} }
if (!$this->skipSaving)
UserModel::save($user);
LogHelper::flush(); LogHelper::flush();
return $user; return $user;
} }

View file

@ -1,5 +1,5 @@
<?php <?php
class EditUserNameJob extends AbstractUserJob class EditUserNameJob extends AbstractUserEditJob
{ {
const NEW_USER_NAME = 'new-user-name'; const NEW_USER_NAME = 'new-user-name';
@ -15,6 +15,7 @@ class EditUserNameJob extends AbstractUserJob
$user->name = $newName; $user->name = $newName;
UserModel::validateUserName($user); UserModel::validateUserName($user);
if (!$this->skipSaving)
UserModel::save($user); UserModel::save($user);
LogHelper::log('{user} renamed {old} to {new}', [ LogHelper::log('{user} renamed {old} to {new}', [

View file

@ -1,5 +1,5 @@
<?php <?php
class EditUserPasswordJob extends AbstractUserJob class EditUserPasswordJob extends AbstractUserEditJob
{ {
const NEW_PASSWORD = 'new-password'; const NEW_PASSWORD = 'new-password';
@ -15,6 +15,7 @@ class EditUserPasswordJob extends AbstractUserJob
$user->passHash = $newPasswordHash; $user->passHash = $newPasswordHash;
if (!$this->skipSaving)
UserModel::save($user); UserModel::save($user);
LogHelper::log('{user} changed {subject}\'s password', [ LogHelper::log('{user} changed {subject}\'s password', [