Skip to content

Commit

Permalink
Simplified post revision management
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcin Kurczewski committed Jun 1, 2014
1 parent 8944573 commit 320cd2e
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 27 deletions.
4 changes: 2 additions & 2 deletions src/Controllers/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
27 changes: 7 additions & 20 deletions src/Models/Entities/PostEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']));
}
Expand All @@ -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(),
];
}

Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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);
}
}
1 change: 1 addition & 0 deletions src/Models/PostModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions src/Upgrades/mysql/Upgrade16.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE post ADD COLUMN revision INTEGER DEFAULT 1;
1 change: 1 addition & 0 deletions src/Upgrades/sqlite/Upgrade16.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE post ADD COLUMN revision INTEGER DEFAULT 1;
6 changes: 3 additions & 3 deletions src/Views/post/post-edit.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
7 changes: 5 additions & 2 deletions tests/Tests/JobTests/EditPostJobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public function testSaving()
$this->grantAccess('editPostContent');

$post = $this->postMocker->mockSingle();
$this->assert->areEqual(1, $post->getRevision());

$args =
[
Expand All @@ -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()
Expand Down

0 comments on commit 320cd2e

Please sign in to comment.