diff --git a/src/Api/Jobs/PostJobs/AddPostJob.php b/src/Api/Jobs/PostJobs/AddPostJob.php index 7234bc1f..70013921 100644 --- a/src/Api/Jobs/PostJobs/AddPostJob.php +++ b/src/Api/Jobs/PostJobs/AddPostJob.php @@ -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()); diff --git a/src/Api/Jobs/PostJobs/FeaturePostJob.php b/src/Api/Jobs/PostJobs/FeaturePostJob.php index d8ec099c..e0d1351b 100644 --- a/src/Api/Jobs/PostJobs/FeaturePostJob.php +++ b/src/Api/Jobs/PostJobs/FeaturePostJob.php @@ -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()); diff --git a/src/Api/Jobs/PostJobs/ScorePostJob.php b/src/Api/Jobs/PostJobs/ScorePostJob.php index 0b0a70ff..a1c967fb 100644 --- a/src/Api/Jobs/PostJobs/ScorePostJob.php +++ b/src/Api/Jobs/PostJobs/ScorePostJob.php @@ -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); diff --git a/src/Api/Jobs/PostJobs/TogglePostFavoriteJob.php b/src/Api/Jobs/PostJobs/TogglePostFavoriteJob.php index 87f6a582..9590fef1 100644 --- a/src/Api/Jobs/PostJobs/TogglePostFavoriteJob.php +++ b/src/Api/Jobs/PostJobs/TogglePostFavoriteJob.php @@ -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) { diff --git a/src/Api/Jobs/PostJobs/TogglePostTagJob.php b/src/Api/Jobs/PostJobs/TogglePostTagJob.php index 940603b8..6d448894 100644 --- a/src/Api/Jobs/PostJobs/TogglePostTagJob.php +++ b/src/Api/Jobs/PostJobs/TogglePostTagJob.php @@ -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(); diff --git a/src/Api/Jobs/PostJobs/TogglePostVisibilityJob.php b/src/Api/Jobs/PostJobs/TogglePostVisibilityJob.php index c9ecb3ea..0faf890c 100644 --- a/src/Api/Jobs/PostJobs/TogglePostVisibilityJob.php +++ b/src/Api/Jobs/PostJobs/TogglePostVisibilityJob.php @@ -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); diff --git a/src/Api/Jobs/UserJobs/ToggleUserBanJob.php b/src/Api/Jobs/UserJobs/ToggleUserBanJob.php index 2e7f02d5..48385f6d 100644 --- a/src/Api/Jobs/UserJobs/ToggleUserBanJob.php +++ b/src/Api/Jobs/UserJobs/ToggleUserBanJob.php @@ -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(); diff --git a/src/Helpers/TextHelper.php b/src/Helpers/TextHelper.php index 760cd7a9..a5832a88 100644 --- a/src/Helpers/TextHelper.php +++ b/src/Helpers/TextHelper.php @@ -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)) diff --git a/src/Models/PostModel.php b/src/Models/PostModel.php index a0d0d940..719c1481 100644 --- a/src/Models/PostModel.php +++ b/src/Models/PostModel.php @@ -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(), diff --git a/src/Models/TokenModel.php b/src/Models/TokenModel.php index 88f9bc09..97a3d762 100644 --- a/src/Models/TokenModel.php +++ b/src/Models/TokenModel.php @@ -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(), ]; diff --git a/src/Models/UserModel.php b/src/Models/UserModel.php index 00dbccd9..c2a823d1 100644 --- a/src/Models/UserModel.php +++ b/src/Models/UserModel.php @@ -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) diff --git a/tests/Tests/JobTests/AddPostJobTest.php b/tests/Tests/JobTests/AddPostJobTest.php index 53745252..069f1eee 100644 --- a/tests/Tests/JobTests/AddPostJobTest.php +++ b/tests/Tests/JobTests/AddPostJobTest.php @@ -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'), diff --git a/tests/Tests/JobTests/FeaturePostJobTest.php b/tests/Tests/JobTests/FeaturePostJobTest.php index 359a5332..60e0a1cf 100644 --- a/tests/Tests/JobTests/FeaturePostJobTest.php +++ b/tests/Tests/JobTests/FeaturePostJobTest.php @@ -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', ]); });