diff --git a/src/Controllers/PostController.php b/src/Controllers/PostController.php index 61445dcd0..fc29bc7be 100644 --- a/src/Controllers/PostController.php +++ b/src/Controllers/PostController.php @@ -157,8 +157,8 @@ public function editAction($identifier) { $post = PostModel::getByIdOrName($identifier); - $editToken = InputHelper::get('edit-token'); - if ($editToken != $post->getEditToken()) + $revision = InputHelper::get('revision'); + if ($revision != $post->getRevision()) throw new SimpleException('This post was already edited by someone else in the meantime'); $jobArgs = diff --git a/src/Models/Entities/PostEntity.php b/src/Models/Entities/PostEntity.php index 1ca0d1c1a..680da76f7 100644 --- a/src/Models/Entities/PostEntity.php +++ b/src/Models/Entities/PostEntity.php @@ -47,6 +47,7 @@ public function fillFromDatabase($row) $this->setCache('comment_count', $row['comment_count']); $this->setCache('fav_count', $row['fav_count']); $this->setCache('score', $row['score']); + $this->setCache('revision', $row['revision']); $this->setType(new PostType($row['type'])); $this->setSafety(new PostSafety($row['safety'])); } @@ -70,6 +71,7 @@ public function serializeToArray() 'tags' => array_map(function($tag) { return $tag->getName(); }, $this->getTags()), 'type' => $this->getType()->toInteger(), 'safety' => $this->getSafety()->toInteger(), + 'revision' => $this->getRevision(), ]; } @@ -145,6 +147,11 @@ public function getFavorites() return $favorites; } + public function getRevision() + { + return (int) $this->getColumnWithCache('revision'); + } + public function getScore() { return (int) $this->getColumnWithCache('score'); @@ -521,24 +528,4 @@ public function setContentFromUrl($srcUrl) unlink($tmpPath); } } - - 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->getSource(); - $x []= $this->getFileHash(); - - natcasesort($x); - - $x = join(' ', $x); - return md5($x); - } } diff --git a/src/Models/PostModel.php b/src/Models/PostModel.php index 4727caae0..16137ed03 100644 --- a/src/Models/PostModel.php +++ b/src/Models/PostModel.php @@ -30,6 +30,7 @@ protected static function saveSingle($post) 'image_height' => $post->getImageHeight(), 'uploader_id' => $post->getUploaderId(), 'source' => $post->getSource(), + 'revision' => $post->getRevision() + 1, ]; $stmt = Sql\Statements::update(); diff --git a/src/Upgrades/mysql/Upgrade16.sql b/src/Upgrades/mysql/Upgrade16.sql new file mode 100644 index 000000000..87677f77e --- /dev/null +++ b/src/Upgrades/mysql/Upgrade16.sql @@ -0,0 +1 @@ +ALTER TABLE post ADD COLUMN revision INTEGER DEFAULT 1; diff --git a/src/Upgrades/sqlite/Upgrade16.sql b/src/Upgrades/sqlite/Upgrade16.sql new file mode 100644 index 000000000..87677f77e --- /dev/null +++ b/src/Upgrades/sqlite/Upgrade16.sql @@ -0,0 +1 @@ +ALTER TABLE post ADD COLUMN revision INTEGER DEFAULT 1; diff --git a/src/Views/post/post-edit.phtml b/src/Views/post/post-edit.phtml index 52c60d018..9cd1a426f 100644 --- a/src/Views/post/post-edit.phtml +++ b/src/Views/post/post-edit.phtml @@ -8,9 +8,9 @@

edit post

+ name="revision" + id="revision" + value="context->transport->post->getRevision()) ?>"/> grantAccess('editPostContent'); $post = $this->postMocker->mockSingle(); + $this->assert->areEqual(1, $post->getRevision()); $args = [ @@ -20,10 +21,12 @@ public function testSaving() new ApiFileInput($this->testSupport->getPath('image.jpg'), 'test.jpg'), ]; - $this->assert->doesNotThrow(function() use ($args) + $post = $this->assert->doesNotThrow(function() use ($args) { - Api::run(new EditPostJob(), $args); + return Api::run(new EditPostJob(), $args); }); + + $this->assert->areEqual(2, $post->getRevision()); } public function testPartialPrivilegeFail()