Fixed issues with variable types
- False booleans were serialized as NULLs, which lead to problems with queries like 'SELECT ... WHERE NOT x' - Fixed anonymous uploads - More robust integer and boolean parsing in jobs
This commit is contained in:
parent
2a6f047c28
commit
c501ccdff1
13 changed files with 38 additions and 14 deletions
|
@ -5,8 +5,10 @@ class AddPostJob extends AbstractJob
|
|||
{
|
||||
$post = PostModel::spawn();
|
||||
|
||||
$anonymous = $this->hasArgument(JobArgs::ARG_ANONYMOUS)
|
||||
and $this->getArgument(JobArgs::ARG_ANONYMOUS);
|
||||
$anonymous = false;
|
||||
if ($this->hasArgument(JobArgs::ARG_ANONYMOUS))
|
||||
$anonymous = TextHelper::toBoolean($this->getArgument(JobArgs::ARG_ANONYMOUS));
|
||||
|
||||
if (Auth::isLoggedIn() and !$anonymous)
|
||||
$post->setUploader(Auth::getCurrentUser());
|
||||
|
||||
|
|
|
@ -15,8 +15,12 @@ class FeaturePostJob extends AbstractJob
|
|||
PropertyModel::set(PropertyModel::FeaturedPostId, $post->getId());
|
||||
PropertyModel::set(PropertyModel::FeaturedPostUnixTime, time());
|
||||
|
||||
$anonymous = false;
|
||||
if ($this->hasArgument(JobArgs::ARG_ANONYMOUS))
|
||||
$anonymous = TextHelper::toBoolean($this->getArgument(JobArgs::ARG_ANONYMOUS));
|
||||
|
||||
PropertyModel::set(PropertyModel::FeaturedPostUserName,
|
||||
($this->hasArgument(JobArgs::ARG_ANONYMOUS) and $this->getArgument(JobArgs::ARG_ANONYMOUS))
|
||||
$anonymous
|
||||
? null
|
||||
: Auth::getCurrentUser()->getName());
|
||||
|
||||
|
|
|
@ -11,7 +11,7 @@ class ScorePostJob extends AbstractJob
|
|||
public function execute()
|
||||
{
|
||||
$post = $this->postRetriever->retrieve();
|
||||
$score = intval($this->getArgument(JobArgs::ARG_NEW_POST_SCORE));
|
||||
$score = TextHelper::toInteger($this->getArgument(JobArgs::ARG_NEW_POST_SCORE));
|
||||
|
||||
UserModel::updateUserScore(Auth::getCurrentUser(), $post, $score);
|
||||
|
||||
|
|
|
@ -11,7 +11,7 @@ class TogglePostFavoriteJob extends AbstractJob
|
|||
public function execute()
|
||||
{
|
||||
$post = $this->postRetriever->retrieve();
|
||||
$favorite = boolval($this->getArgument(JobArgs::ARG_NEW_STATE));
|
||||
$favorite = TextHelper::toBoolean($this->getArgument(JobArgs::ARG_NEW_STATE));
|
||||
|
||||
if ($favorite)
|
||||
{
|
||||
|
|
|
@ -11,7 +11,7 @@ class TogglePostTagJob extends AbstractJob
|
|||
public function execute()
|
||||
{
|
||||
$tagName = $this->getArgument(JobArgs::ARG_TAG_NAME);
|
||||
$enable = boolval($this->getArgument(JobArgs::ARG_NEW_STATE));
|
||||
$enable = TextHelper::toBoolean($this->getArgument(JobArgs::ARG_NEW_STATE));
|
||||
$post = $this->postRetriever->retrieve();
|
||||
|
||||
$tags = $post->getTags();
|
||||
|
|
|
@ -11,7 +11,7 @@ class TogglePostVisibilityJob extends AbstractJob
|
|||
public function execute()
|
||||
{
|
||||
$post = $this->postRetriever->retrieve();
|
||||
$visible = boolval($this->getArgument(JobArgs::ARG_NEW_STATE));
|
||||
$visible = TextHelper::toBoolean($this->getArgument(JobArgs::ARG_NEW_STATE));
|
||||
|
||||
$post->setHidden(!$visible);
|
||||
PostModel::save($post);
|
||||
|
|
|
@ -11,7 +11,7 @@ class ToggleUserBanJob extends AbstractJob
|
|||
public function execute()
|
||||
{
|
||||
$user = $this->userRetriever->retrieve();
|
||||
$banned = boolval($this->getArgument(JobArgs::ARG_NEW_STATE));
|
||||
$banned = TextHelper::toBoolean($this->getArgument(JobArgs::ARG_NEW_STATE));
|
||||
|
||||
if ($banned)
|
||||
$user->ban();
|
||||
|
|
|
@ -7,6 +7,14 @@ class TextHelper
|
|||
return preg_match($emailRegex, $email);
|
||||
}
|
||||
|
||||
public static function toInteger($x)
|
||||
{
|
||||
$y = self::toIntegerOrNull($x);
|
||||
if ($y === null)
|
||||
return 0;
|
||||
return $y;
|
||||
}
|
||||
|
||||
public static function toIntegerOrNull($x)
|
||||
{
|
||||
if ($x === true or $x === false)
|
||||
|
@ -28,6 +36,14 @@ class TextHelper
|
|||
return null;
|
||||
}
|
||||
|
||||
public static function toBoolean($x)
|
||||
{
|
||||
$y = self::toBooleanOrNull($x);
|
||||
if ($y === null)
|
||||
return false;
|
||||
return $y;
|
||||
}
|
||||
|
||||
public static function toBooleanOrNull($x)
|
||||
{
|
||||
switch (strtolower($x))
|
||||
|
|
|
@ -25,7 +25,7 @@ final class PostModel extends AbstractCrudModel
|
|||
'file_size' => $post->getFileSize(),
|
||||
'mime_type' => $post->getMimeType(),
|
||||
'safety' => $post->getSafety()->toInteger(),
|
||||
'hidden' => $post->isHidden(),
|
||||
'hidden' => $post->isHidden() ? 1 : 0,
|
||||
'upload_date' => $post->getCreationTime(),
|
||||
'image_width' => $post->getImageWidth(),
|
||||
'image_height' => $post->getImageHeight(),
|
||||
|
|
|
@ -20,7 +20,7 @@ final class TokenModel extends AbstractCrudModel
|
|||
$bindings = [
|
||||
'user_id' => $token->getUserId(),
|
||||
'token' => $token->getText(),
|
||||
'used' => $token->isUsed(),
|
||||
'used' => $token->isUsed() ? 1 : 0,
|
||||
'expires' => $token->getExpirationTime(),
|
||||
];
|
||||
|
||||
|
|
|
@ -21,14 +21,14 @@ final class UserModel extends AbstractCrudModel
|
|||
'name' => $user->getName(),
|
||||
'pass_salt' => $user->getPasswordSalt(),
|
||||
'pass_hash' => $user->getPasswordHash(),
|
||||
'staff_confirmed' => $user->isStaffConfirmed(),
|
||||
'staff_confirmed' => $user->isStaffConfirmed() ? 1 : 0,
|
||||
'email_unconfirmed' => $user->getUnconfirmedEmail(),
|
||||
'email_confirmed' => $user->getConfirmedEmail(),
|
||||
'join_date' => $user->getJoinTime(),
|
||||
'last_login_date' => $user->getLastLoginTime(),
|
||||
'access_rank' => $user->getAccessRank()->toInteger(),
|
||||
'settings' => $user->getSettings()->getAllAsSerializedString(),
|
||||
'banned' => $user->isBanned(),
|
||||
'banned' => $user->isBanned() ? 1 : 0,
|
||||
];
|
||||
|
||||
$stmt = (new Sql\UpdateStatement)
|
||||
|
|
|
@ -18,6 +18,7 @@ class AddPostJobTest extends AbstractTest
|
|||
return Api::run(
|
||||
new AddPostJob(),
|
||||
[
|
||||
JobArgs::ARG_ANONYMOUS => '0',
|
||||
JobArgs::ARG_NEW_SAFETY => PostSafety::Safe,
|
||||
JobArgs::ARG_NEW_SOURCE => '',
|
||||
JobArgs::ARG_NEW_TAG_NAMES => ['kamen', 'raider'],
|
||||
|
@ -30,6 +31,7 @@ class AddPostJobTest extends AbstractTest
|
|||
file_get_contents($post->getFullPath()),
|
||||
file_get_contents($this->testSupport->getPath('image.jpg')));
|
||||
$this->assert->areEqual(Auth::getCurrentUser()->getId(), $post->getUploaderId());
|
||||
$this->assert->isNotNull($post->getUploaderId());
|
||||
}
|
||||
|
||||
public function testAnonymousUploads()
|
||||
|
@ -47,7 +49,7 @@ class AddPostJobTest extends AbstractTest
|
|||
return Api::run(
|
||||
new AddPostJob(),
|
||||
[
|
||||
JobArgs::ARG_ANONYMOUS => true,
|
||||
JobArgs::ARG_ANONYMOUS => '1',
|
||||
JobArgs::ARG_NEW_TAG_NAMES => ['kamen', 'raider'],
|
||||
JobArgs::ARG_NEW_POST_CONTENT =>
|
||||
new ApiFileInput($this->testSupport->getPath('image.jpg'), 'test.jpg'),
|
||||
|
|
|
@ -36,7 +36,7 @@ class FeaturePostJobTest extends AbstractTest
|
|||
new FeaturePostJob(),
|
||||
[
|
||||
JobArgs::ARG_POST_ID => $posts[1]->getId(),
|
||||
JobArgs::ARG_ANONYMOUS => true,
|
||||
JobArgs::ARG_ANONYMOUS => '1',
|
||||
]);
|
||||
});
|
||||
|
||||
|
|
Loading…
Reference in a new issue