Simplified post revision management

This commit is contained in:
Marcin Kurczewski 2014-06-01 14:07:53 +02:00
parent 894457363e
commit 320cd2e194
7 changed files with 20 additions and 27 deletions

View file

@ -157,8 +157,8 @@ class PostController extends AbstractController
{
$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 =

View file

@ -47,6 +47,7 @@ final class PostEntity extends AbstractEntity implements IValidatable, ISerializ
$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 @@ final class PostEntity extends AbstractEntity implements IValidatable, ISerializ
'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 @@ final class PostEntity extends AbstractEntity implements IValidatable, ISerializ
return $favorites;
}
public function getRevision()
{
return (int) $this->getColumnWithCache('revision');
}
public function getScore()
{
return (int) $this->getColumnWithCache('score');
@ -521,24 +528,4 @@ final class PostEntity extends AbstractEntity implements IValidatable, ISerializ
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);
}
}

View file

@ -30,6 +30,7 @@ final class PostModel extends AbstractCrudModel
'image_height' => $post->getImageHeight(),
'uploader_id' => $post->getUploaderId(),
'source' => $post->getSource(),
'revision' => $post->getRevision() + 1,
];
$stmt = Sql\Statements::update();

View file

@ -0,0 +1 @@
ALTER TABLE post ADD COLUMN revision INTEGER DEFAULT 1;

View file

@ -0,0 +1 @@
ALTER TABLE post ADD COLUMN revision INTEGER DEFAULT 1;

View file

@ -8,9 +8,9 @@
<h1>edit post</h1>
<input type="hidden"
name="edit-token"
id="edit-token"
value="<?= htmlspecialchars($this->context->transport->post->getEditToken()) ?>"/>
name="revision"
id="revision"
value="<?= htmlspecialchars($this->context->transport->post->getRevision()) ?>"/>
<?php
if (Access::check(new Privilege(

View file

@ -10,6 +10,7 @@ class EditPostJobTest extends AbstractTest
$this->grantAccess('editPostContent');
$post = $this->postMocker->mockSingle();
$this->assert->areEqual(1, $post->getRevision());
$args =
[
@ -20,10 +21,12 @@ class EditPostJobTest extends AbstractTest
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()