Moved post retrieval to API

This commit is contained in:
Marcin Kurczewski 2014-05-03 20:32:47 +02:00
parent cebff0ef4e
commit 9f4d97aa23
8 changed files with 62 additions and 21 deletions

View file

@ -87,6 +87,9 @@ $postValidation =
\Chibi\Router::register(['PostController', 'favoritesView'], 'GET', '/favorites/{page}', $postValidation);
\Chibi\Router::register(['PostController', 'upvotedView'], 'GET', '/upvoted', $postValidation);
\Chibi\Router::register(['PostController', 'upvotedView'], 'GET', '/upvoted/{page}', $postValidation);
\Chibi\Router::register(['PostController', 'genericView'], 'GET', '/post/{id}', $postValidation);
\Chibi\Router::register(['PostController', 'toggleTagAction'], 'POST', '/post/{id}/toggle-tag/{tag}/{enable}', $postValidation);
\Chibi\Router::register(['PostController', 'flagAction'], 'POST', '/post/{id}/flag', $postValidation);
\Chibi\Router::register(['PostController', 'hideAction'], 'POST', '/post/{id}/hide', $postValidation);
@ -96,16 +99,21 @@ $postValidation =
\Chibi\Router::register(['PostController', 'scoreAction'], 'POST', '/post/{id}/score/{score}', $postValidation);
\Chibi\Router::register(['PostController', 'featureAction'], 'POST', '/post/{id}/feature', $postValidation);
\Chibi\Router::register(['CommentController', 'listView'], 'GET', '/comments');
\Chibi\Router::register(['CommentController', 'listView'], 'GET', '/comments/{page}', ['page' => '\d+']);
\Chibi\Router::register(['CommentController', 'addAction'], 'POST', '/comment/add');
\Chibi\Router::register(['CommentController', 'deleteAction'], 'POST', '/comment/{id}/delete', ['id' => '\d+']);
\Chibi\Router::register(['CommentController', 'editView'], 'GET', '/comment/{id}/edit', ['id' => '\d+']);
\Chibi\Router::register(['CommentController', 'editAction'], 'POST', '/comment/{id}/edit', ['id' => '\d+']);
$commentValidation =
[
'id' => '\d+',
'page' => '\d+',
];
\Chibi\Router::register(['CommentController', 'listView'], 'GET', '/comments', $commentValidation);
\Chibi\Router::register(['CommentController', 'listView'], 'GET', '/comments/{page}', $commentValidation);
\Chibi\Router::register(['CommentController', 'addAction'], 'POST', '/comment/add', $commentValidation);
\Chibi\Router::register(['CommentController', 'deleteAction'], 'POST', '/comment/{id}/delete', $commentValidation);
\Chibi\Router::register(['CommentController', 'editView'], 'GET', '/comment/{id}/edit', $commentValidation);
\Chibi\Router::register(['CommentController', 'editAction'], 'POST', '/comment/{id}/edit', $commentValidation);
foreach (['GET', 'POST'] as $method)
{
\Chibi\Router::register(['PostController', 'viewAction'], $method, '/post/{id}', $postValidation);
\Chibi\Router::register(['PostController', 'retrieveAction'], $method, '/post/{name}/retrieve', $postValidation);
\Chibi\Router::register(['PostController', 'thumbAction'], $method, '/post/{name}/thumb', $postValidation);

View file

@ -218,16 +218,13 @@ class PostController
FeaturePostJob::POST_ID => $id]);
}
public function viewAction($id)
public function genericView($id)
{
$context = getContext();
$post = PostModel::findByIdOrName($id);
CommentModel::preloadCommenters($post->getComments());
$context->viewName = 'post-view';
if ($post->hidden)
Access::assert(Privilege::ViewPost, 'hidden');
Access::assert(Privilege::ViewPost);
Access::assert(Privilege::ViewPost, PostSafety::toString($post->safety));
$post = Api::run(new GetPostJob(), [
GetPostJob::POST_ID => $id]);
try
{
@ -245,6 +242,8 @@ class PostController
$context->transport->lastSearchQuery, $id);
}
//todo:
//move these to PostEntity when implementing ApiController
$favorite = Auth::getCurrentUser()->hasFavorited($post);
$score = Auth::getCurrentUser()->getScore($post);
$flagged = in_array(TextHelper::reprPost($post), SessionHelper::get('flagged', []));

View file

@ -127,7 +127,7 @@ class CustomMarkdown extends \Michelf\MarkdownExtra
protected function doPosts($text)
{
$link = \Chibi\Router::linkTo(['PostController', 'viewAction'], ['id' => '_post_']);
$link = \Chibi\Router::linkTo(['PostController', 'genericView'], ['id' => '_post_']);
return preg_replace_callback('/(?:(?<![^\s\(\)\[\]]))@(\d+)/', function($x) use ($link)
{
return $this->hashPart('<a href="' . str_replace('_post_', $x[1], $link) . '"><code>' . $x[0] . '</code></a>');

34
src/Jobs/GetPostJob.php Normal file
View file

@ -0,0 +1,34 @@
<?php
class GetPostJob extends AbstractPostEditJob
{
public function execute()
{
$post = $this->post;
//todo: refactor this so that requiresPrivilege can accept multiple privileges
if ($this->post->hidden)
Access::assert(Privilege::ViewPost, 'hidden');
Access::assert(Privilege::ViewPost);
Access::assert(Privilege::ViewPost, PostSafety::toString($this->post->safety));
CommentModel::preloadCommenters($post->getComments());
return $post;
}
public function requiresPrivilege()
{
//temporarily enforced in execute
return false;
}
public function requiresAuthentication()
{
return false;
}
public function requiresConfirmedEmail()
{
return false;
}
}

View file

@ -32,7 +32,7 @@ Assets::setSubTitle('comments');
<?php if (count($comments) > count($commentsToDisplay)): ?>
<a href="<?= \Chibi\Router::linkTo(
['PostController', 'viewAction'],
['PostController', 'genericView'],
['id' => $this->context->post->id]) ?>">
<span class="hellip">(more&hellip;)</span>
</a>

View file

@ -44,7 +44,7 @@ if ($masstag)
<?php if (Auth::getCurrentUser()->hasEnabledPostTagTitles()): ?>
title="<?= TextHelper::reprTags($this->context->post->getTags()) ?>"
<?php endif ?>
href="<?= \Chibi\Router::linkTo(['PostController', 'viewAction'], ['id' => $this->context->post->id]) ?>">
href="<?= \Chibi\Router::linkTo(['PostController', 'genericView'], ['id' => $this->context->post->id]) ?>">
<img
class="thumb"

View file

@ -31,7 +31,7 @@ $canEditAnything = count(array_filter($editPostPrivileges)) > 0;
<div class="left">
<?php if ($this->context->transport->nextPostId): ?>
<a href="<?= \Chibi\Router::linkTo(
['PostController', 'viewAction'],
['PostController', 'genericView'],
['id' => $this->context->transport->nextPostId]) ?>">
<?php else: ?>
<a class="disabled">
@ -44,7 +44,7 @@ $canEditAnything = count(array_filter($editPostPrivileges)) > 0;
<div class="right">
<?php if ($this->context->transport->prevPostId): ?>
<a href="<?= \Chibi\Router::linkTo(
['PostController', 'viewAction'],
['PostController', 'genericView'],
['id' => $this->context->transport->prevPostId]) ?>">
<?php else: ?>
<a class="disabled">
@ -259,7 +259,7 @@ $canEditAnything = count(array_filter($editPostPrivileges)) > 0;
<ul>
<?php foreach ($this->context->transport->post->getRelations() as $relatedPost): ?>
<li>
<a href="<?= \Chibi\Router::linkTo(['PostController', 'viewAction'], ['id' => $relatedPost->id]) ?>">
<a href="<?= \Chibi\Router::linkTo(['PostController', 'genericView'], ['id' => $relatedPost->id]) ?>">
@<?= $relatedPost->id ?>
</a>
</li>

View file

@ -14,7 +14,7 @@ Assets::addStylesheet('static-main.css');
<div class="body">
<?php
$this->context->transport->post = $this->context->featuredPost;
$this->context->imageLink = \Chibi\Router::linkTo(['PostController', 'viewAction'], [
$this->context->imageLink = \Chibi\Router::linkTo(['PostController', 'genericView'], [
'id' => $this->context->featuredPost->id]);
?>