diff --git a/src/Api/Jobs/AddPostJob.php b/src/Api/Jobs/AddPostJob.php index 23634ca1..e7952a79 100644 --- a/src/Api/Jobs/AddPostJob.php +++ b/src/Api/Jobs/AddPostJob.php @@ -38,7 +38,7 @@ class AddPostJob extends AbstractJob 'post' => TextHelper::reprPost($post), 'tags' => TextHelper::reprTags($post->getTags()), 'safety' => $post->getSafety()->toString(), - 'source' => $post->source]); + 'source' => $post->getSource()]); Logger::flush(); diff --git a/src/Api/Jobs/EditPostSourceJob.php b/src/Api/Jobs/EditPostSourceJob.php index a91bee85..d9133b4c 100644 --- a/src/Api/Jobs/EditPostSourceJob.php +++ b/src/Api/Jobs/EditPostSourceJob.php @@ -13,7 +13,7 @@ class EditPostSourceJob extends AbstractPostJob $post = $this->post; $newSource = $this->getArgument(self::SOURCE); - $oldSource = $post->source; + $oldSource = $post->getSource(); $post->setSource($newSource); if ($this->getContext() == self::CONTEXT_NORMAL) @@ -24,7 +24,7 @@ class EditPostSourceJob extends AbstractPostJob Logger::log('{user} changed source of {post} to {source}', [ 'user' => TextHelper::reprUser(Auth::getCurrentUser()), 'post' => TextHelper::reprPost($post), - 'source' => $post->source]); + 'source' => $post->getSource()]); } return $post; diff --git a/src/Models/Entities/PostEntity.php b/src/Models/Entities/PostEntity.php index 0cc0403c..9807102c 100644 --- a/src/Models/Entities/PostEntity.php +++ b/src/Models/Entities/PostEntity.php @@ -16,16 +16,23 @@ class PostEntity extends AbstractEntity implements IValidatable public $imageWidth; public $imageHeight; public $uploaderId; - public $source; + protected $source; public $commentCount = 0; public $favCount = 0; public $score = 0; public function validate() { - //todo if (empty($this->getType())) throw new SimpleException('No post type detected'); + + $this->getType()->validate(); + + $this->getSafety()->validate(); + + $maxSourceLength = getConfig()->posts->maxSourceLength; + if (strlen($this->getSource()) > $maxSourceLength) + throw new SimpleException('Source must have at most %d characters', $maxSourceLength); } public function getUploader() @@ -197,7 +204,6 @@ class PostEntity extends AbstractEntity implements IValidatable public function setType(PostType $type) { - $type->validate(); $this->type = $type; } @@ -208,13 +214,17 @@ class PostEntity extends AbstractEntity implements IValidatable public function setSafety(PostSafety $safety) { - $safety->validate(); $this->safety = $safety; } + public function getSource() + { + return $this->source; + } + public function setSource($source) { - $this->source = PostModel::validateSource($source); + $this->source = trim($source); } public function getThumbCustomPath($width = null, $height = null) @@ -390,14 +400,19 @@ class PostEntity extends AbstractEntity implements IValidatable public function getEditToken() { $x = []; + foreach ($this->getTags() as $tag) $x []= TextHelper::reprTag($tag->getName()); + foreach ($this->getRelations() as $relatedPost) $x []= TextHelper::reprPost($relatedPost); + $x []= $this->getSafety()->toInteger(); - $x []= $this->source; + $x []= $this->getSource(); $x []= $this->fileHash; + natcasesort($x); + $x = join(' ', $x); return md5($x); } diff --git a/src/Models/PostModel.php b/src/Models/PostModel.php index 42ddbfb9..57584610 100644 --- a/src/Models/PostModel.php +++ b/src/Models/PostModel.php @@ -57,7 +57,7 @@ class PostModel extends AbstractCrudModel 'image_width' => $post->imageWidth, 'image_height' => $post->imageHeight, 'uploader_id' => $post->uploaderId, - 'source' => $post->source, + 'source' => $post->getSource(), ]; $stmt = new Sql\UpdateStatement(); @@ -273,17 +273,6 @@ class PostModel extends AbstractCrudModel - public static function validateSource($source) - { - $source = trim($source); - - $maxLength = getConfig()->posts->maxSourceLength; - if (strlen($source) > $maxLength) - throw new SimpleException('Source must have at most %d characters', $maxLength); - - return $source; - } - public static function validateThumbSize($width, $height) { $width = $width === null ? getConfig()->browsing->thumbWidth : $width; diff --git a/src/Views/post-edit.phtml b/src/Views/post-edit.phtml index 6bbab574..f23753ee 100644 --- a/src/Views/post-edit.phtml +++ b/src/Views/post-edit.phtml @@ -62,7 +62,7 @@ + value="context->transport->post->getSource()) ?>"/> diff --git a/src/Views/post-view.phtml b/src/Views/post-view.phtml index eadc2139..c2654710 100644 --- a/src/Views/post-view.phtml +++ b/src/Views/post-view.phtml @@ -125,8 +125,8 @@ $canEditAnything = count(array_filter($editPostPrivileges)) > 0;
Source: - context->transport->post->source)): ?> + title="context->transport->post->getSource() ?: 'unknown') ?>"> + context->transport->post->getSource())): ?> diff --git a/tests/JobTests/EditPostSourceJobTest.php b/tests/JobTests/EditPostSourceJobTest.php index 492854b9..38b1deb8 100644 --- a/tests/JobTests/EditPostSourceJobTest.php +++ b/tests/JobTests/EditPostSourceJobTest.php @@ -10,7 +10,7 @@ class EditPostSourceJobTest extends AbstractTest return $this->runApi('a'); }); - $this->assert->areEqual('a', $post->source); + $this->assert->areEqual('a', $post->getSource()); $this->assert->doesNotThrow(function() use ($post) { PostModel::findById($post->getId());