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
{
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;

View file

@ -4,8 +4,15 @@ abstract class AbstractPostJob extends AbstractJob
protected $post;
public function prepare()
{
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);
}
}
}

View file

@ -4,8 +4,15 @@ abstract class AbstractUserJob extends AbstractJob
protected $user;
public function prepare()
{
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);
}
}
}

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);
//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;
}

View file

@ -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);

View file

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

View file

@ -1,5 +1,5 @@
<?php
class EditPostJob extends AbstractPostJob
class EditPostJob extends AbstractPostEditJob
{
public function execute()
{
@ -20,8 +20,11 @@ class EditPostJob extends AbstractPostJob
foreach ($subJobs as $subJob)
{
if ($this->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;
}

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -1,5 +1,5 @@
<?php
class EditUserJob extends AbstractUserJob
class EditUserJob extends AbstractUserEditJob
{
protected $subJobs;
@ -38,10 +38,13 @@ class EditUserJob extends AbstractUserJob
LogHelper::bufferChanges();
foreach ($subJobs as $subJob)
foreach ($this->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;
}

View file

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

View file

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