Added ability to access posts using their hashes

This commit is contained in:
Marcin Kurczewski 2014-05-20 21:46:49 +02:00
parent 174fd80a73
commit ef4ba5a348
7 changed files with 105 additions and 90 deletions

View file

@ -90,19 +90,20 @@ class PostController extends AbstractController
$this->listView('order:random', $page, 'random');
}
public function toggleTagAction($id, $tag, $enable)
public function toggleTagAction($identifier, $tag, $enable)
{
Access::assert(new Privilege(
Privilege::MassTag,
Access::getIdentity(PostModel::getById($id)->getUploader())));
Access::getIdentity(PostModel::getByIdOrName($identifier)->getUploader())));
Api::run(
new TogglePostTagJob(),
$jobArgs =
[
JobArgs::ARG_POST_ID => $id,
JobArgs::ARG_TAG_NAME => $tag,
JobArgs::ARG_NEW_STATE => $enable,
]);
];
$jobArgs = $this->appendPostIdentifierArgument($args, $identifier);
Api::run(new TogglePostTagJob(), $jobArgs);
if ($this->isAjax())
$this->renderAjax();
@ -147,18 +148,19 @@ class PostController extends AbstractController
$this->redirectToPostList();
}
public function editView($id)
public function editView($identifier)
{
$post = Api::run(new GetPostJob(), [
JobArgs::ARG_POST_ID => $id]);
$jobArgs = [];
$jobArgs = $this->appendPostIdentifierArgument($jobArgs, $identifier);
$post = Api::run(new GetPostJob(), $jobArgs);
$context = Core::getContext()->transport->post = $post;
$this->renderView('post-edit');
}
public function editAction($id)
public function editAction($identifier)
{
$post = PostModel::getByIdOrName($id);
$post = PostModel::getByIdOrName($identifier);
$editToken = InputHelper::get('edit-token');
if ($editToken != $post->getEditToken())
@ -166,12 +168,12 @@ class PostController extends AbstractController
$jobArgs =
[
JobArgs::ARG_POST_ID => $id,
JobArgs::ARG_NEW_SAFETY => InputHelper::get('safety'),
JobArgs::ARG_NEW_TAG_NAMES => $this->splitTags(InputHelper::get('tags')),
JobArgs::ARG_NEW_SOURCE => InputHelper::get('source'),
JobArgs::ARG_NEW_RELATED_POST_IDS => $this->splitPostIds(InputHelper::get('relations')),
];
$jobArgs = $this->appendPostIdentifierArgument($jobArgs, $identifier);
if (!empty(InputHelper::get('url')))
{
@ -202,97 +204,100 @@ class PostController extends AbstractController
if ($this->isAjax())
$this->renderAjax();
else
$this->redirectToGenericView($id);
$this->redirectToGenericView($identifier);
}
public function flagAction($id)
public function flagAction($identifier)
{
Api::run(new FlagPostJob(), [JobArgs::ARG_POST_ID => $id]);
$jobArgs = [];
$jobArgs = $this->appendPostIdentifierArgument($jobArgs, $identifier);
Api::run(new FlagPostJob(), $jobArgs);
if ($this->isAjax())
$this->renderAjax();
else
$this->redirectToGenericView($id);
$this->redirectToGenericView($identifier);
}
public function hideAction($id)
public function hideAction($identifier)
{
Api::run(new TogglePostVisibilityJob(), [
JobArgs::ARG_POST_ID => $id,
JobArgs::ARG_NEW_STATE => false]);
$this->redirectToGenericView($id);
$jobArgs = [JobArgs::ARG_NEW_STATE => false];
$jobArgs = $this->appendPostIdentifierArgument($jobArgs, $identifier);
Api::run(new TogglePostVisibilityJob(), $jobArgs);
$this->redirectToGenericView($identifier);
}
public function unhideAction($id)
public function unhideAction($identifier)
{
Api::run(new TogglePostVisibilityJob(), [
JobArgs::ARG_POST_ID => $id,
JobArgs::ARG_NEW_STATE => true]);
$this->redirectToGenericView($id);
$jobArgs = [JobArgs::ARG_NEW_STATE => true];
$jobArgs = $this->appendPostIdentifierArgument($jobArgs, $identifier);
Api::run(new TogglePostVisibilityJob(), $jobArgs);
$this->redirectToGenericView($identifier);
}
public function deleteAction($id)
public function deleteAction($identifier)
{
Api::run(new DeletePostJob(), [
JobArgs::ARG_POST_ID => $id]);
$jobArgs = [];
$jobArgs = $this->appendPostIdentifierArgument($jobArgs, $identifier);
Api::run(new DeletePostJob(), $jobArgs);
$this->redirectToPostList();
}
public function addFavoriteAction($id)
public function addFavoriteAction($identifier)
{
Api::run(new TogglePostFavoriteJob(), [
JobArgs::ARG_POST_ID => $id,
JobArgs::ARG_NEW_STATE => true]);
$this->redirectToGenericView($id);
$jobArgs = [JobArgs::ARG_NEW_STATE => true];
$jobArgs = $this->appendPostIdentifierArgument($jobArgs, $identifier);
Api::run(new TogglePostFavoriteJob(), $jobArgs);
$this->redirectToGenericView($identifier);
}
public function removeFavoriteAction($id)
public function removeFavoriteAction($identifier)
{
Api::run(new TogglePostFavoriteJob(), [
JobArgs::ARG_POST_ID => $id,
JobArgs::ARG_NEW_STATE => false]);
$this->redirectToGenericView($id);
$jobArgs = [JobArgs::ARG_NEW_STATE => false];
$jobArgs = $this->appendPostIdentifierArgument($jobArgs, $identifier);
Api::run(new TogglePostFavoriteJob(), $jobArgs);
$this->redirectToGenericView($identifier);
}
public function scoreAction($id, $score)
public function scoreAction($identifier, $score)
{
Api::run(new ScorePostJob(), [
JobArgs::ARG_POST_ID => $id,
JobArgs::ARG_NEW_POST_SCORE => $score]);
$this->redirectToGenericView($id);
$jobArgs = [JobArgs::ARG_NEW_POST_SCORE => $score];
$jobArgs = $this->appendPostIdentifierArgument($jobArgs, $identifier);
Api::run(new ScorePostJob(), $jobArgs);
$this->redirectToGenericView($identifier);
}
public function featureAction($id)
public function featureAction($identifier)
{
Api::run(new FeaturePostJob(), [
JobArgs::ARG_POST_ID => $id]);
$this->redirectToGenericView($id);
$jobArgs = [];
$jobArgs = $this->appendPostIdentifierArgument($jobArgs, $identifier);
Api::run(new FeaturePostJob(), $jobArgs);
$this->redirectToGenericView($identifier);
}
public function genericView($id)
public function genericView($identifier)
{
$context = Core::getContext();
$post = Api::run(new GetPostJob(), [
JobArgs::ARG_POST_ID => $id]);
$jobArgs = [];
$jobArgs = $this->appendPostIdentifierArgument($jobArgs, $identifier);
$post = Api::run(new GetPostJob(), $jobArgs);
try
{
$context->transport->lastSearchQuery = InputHelper::get('last-search-query');
list ($prevPostId, $nextPostId) =
PostSearchService::getPostIdsAround(
$context->transport->lastSearchQuery, $id);
$context->transport->lastSearchQuery, $identifier);
}
#search for some reason was invalid, e.g. tag was deleted in the meantime
catch (Exception $e)
{
$context->transport->lastSearchQuery = '';
list ($prevPostId, $nextPostId) =
PostSearchService::getPostIdsAround($context->transport->lastSearchQuery, $id);
PostSearchService::getPostIdsAround($context->transport->lastSearchQuery, $identifier);
}
//todo:
//move these to PostEntity when implementing ApiController
$isUserFavorite = Auth::getCurrentUser()->hasFavorited($post);
$userScore = Auth::getCurrentUser()->getScore($post);
$flagged = in_array(TextHelper::reprPost($post), SessionHelper::get('flagged', []));
@ -336,6 +341,15 @@ class PostController extends AbstractController
}
private function appendPostIdentifierArgument(array $arguments, $postIdentifier)
{
if (is_numeric($postIdentifier))
$arguments[JobArgs::ARG_POST_ID] = $postIdentifier;
else
$arguments[JobArgs::ARG_POST_NAME] = $postIdentifier;
return $arguments;
}
private function splitPostIds($string)
{
$ids = preg_split('/\D/', trim($string));
@ -358,10 +372,10 @@ class PostController extends AbstractController
$this->redirect(\Chibi\Router::linkTo(['PostController', 'listView']));
}
private function redirectToGenericView($id)
private function redirectToGenericView($identifier)
{
$this->redirect(\Chibi\Router::linkTo(
['PostController', 'genericView'],
['id' => $id]));
['identifier' => $identifier]));
}
}

View file

@ -127,7 +127,7 @@ class CustomMarkdown extends \Michelf\MarkdownExtra
protected function doPosts($text)
{
$link = \Chibi\Router::linkTo(['PostController', 'genericView'], ['id' => '_post_']);
$link = \Chibi\Router::linkTo(['PostController', 'genericView'], ['identifier' => '_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>');

View file

@ -1,7 +1,7 @@
<form method="post"
action="<?= \Chibi\Router::linkTo(
['PostController', 'editAction'],
['id' => $this->context->transport->post->getId()]) ?>"
['identifier' => $this->context->transport->post->getId()]) ?>"
enctype="multipart/form-data"
class="edit-post">

View file

@ -30,7 +30,7 @@ if ($masstag)
<?php if ($masstag): ?>
<a class="toggle-tag"
href="<?= \Chibi\Router::linkTo(['PostController', 'toggleTagAction'], [
'id' => $this->context->post->getId(),
'identifier' => $this->context->post->getId(),
'tag' => $this->context->additionalInfo,
'enable' => in_array('tagged', $classNames) ? '0' : '1']) ?>"
data-text-tagged="Tagged"
@ -45,7 +45,7 @@ if ($masstag)
<?php if (Auth::getCurrentUser()->getSettings()->hasEnabledPostTagTitles()): ?>
title="<?= TextHelper::reprTags($this->context->post->getTags()) ?>"
<?php endif ?>
href="<?= \Chibi\Router::linkTo(['PostController', 'genericView'], ['id' => $this->context->post->getId()]) ?>">
href="<?= \Chibi\Router::linkTo(['PostController', 'genericView'], ['identifier' => $this->context->post->getId()]) ?>">
<img
class="thumb"

View file

@ -32,7 +32,7 @@ $canEditAnything = count(array_filter($editPostPrivileges)) > 0;
<?php if ($this->context->transport->nextPostId): ?>
<a href="<?= \Chibi\Router::linkTo(
['PostController', 'genericView'],
['id' => $this->context->transport->nextPostId]) ?>">
['identifier' => $this->context->transport->nextPostId]) ?>">
<?php else: ?>
<a class="disabled">
<?php endif ?>
@ -45,7 +45,7 @@ $canEditAnything = count(array_filter($editPostPrivileges)) > 0;
<?php if ($this->context->transport->prevPostId): ?>
<a href="<?= \Chibi\Router::linkTo(
['PostController', 'genericView'],
['id' => $this->context->transport->prevPostId]) ?>">
['identifier' => $this->context->transport->prevPostId]) ?>">
<?php else: ?>
<a class="disabled">
<?php endif ?>
@ -155,7 +155,7 @@ $canEditAnything = count(array_filter($editPostPrivileges)) > 0;
{
return \Chibi\Router::linkTo(
['PostController', 'scoreAction'],
['id' => $this->context->transport->post->getId(), 'score' => $score]);
['identifier' => $this->context->transport->post->getId(), 'score' => $score]);
}
?>
<?php if (Access::check(new Privilege(
@ -211,7 +211,7 @@ $canEditAnything = count(array_filter($editPostPrivileges)) > 0;
<a class="add-fav icon simple-action"
href="<?= \Chibi\Router::linkTo(
['PostController', 'addFavoriteAction'],
['id' => $this->context->transport->post->getId()]) ?>">
['identifier' => $this->context->transport->post->getId()]) ?>">
<i class="icon-fav"></i>
<span>Add to favorites</span>
</a>
@ -219,7 +219,7 @@ $canEditAnything = count(array_filter($editPostPrivileges)) > 0;
<a class="rem-fav icon simple-action"
href="<?= \Chibi\Router::linkTo(
['PostController', 'removeFavoriteAction'],
['id' => $this->context->transport->post->getId()]) ?>">
['identifier' => $this->context->transport->post->getId()]) ?>">
<i class="icon-fav"></i>
<span>Remove from favorites</span>
</a>
@ -231,7 +231,7 @@ $canEditAnything = count(array_filter($editPostPrivileges)) > 0;
<div class="hl-option">
<a class="edit-post icon" href="<?= \Chibi\Router::linkTo(
['PostController', 'editView'],
['id' => $this->context->transport->post->getId()]) ?>">
['identifier' => $this->context->transport->post->getId()]) ?>">
<i class="icon-edit"></i>
<span>Edit</span>
</a>
@ -264,7 +264,7 @@ $canEditAnything = count(array_filter($editPostPrivileges)) > 0;
<li>
<a href="<?= \Chibi\Router::linkTo(
['PostController', 'genericView'],
['id' => $relatedPost->getId()]) ?>">
['identifier' => $relatedPost->getId()]) ?>">
<?= TextHelper::reprPost($relatedPost) ?>
</a>
</li>
@ -286,7 +286,7 @@ $canEditAnything = count(array_filter($editPostPrivileges)) > 0;
'text' => 'Feature on main page',
'simple-action' => \Chibi\Router::linkTo(
['PostController', 'featureAction'],
['id' => $this->context->transport->post->getId()]),
['identifier' => $this->context->transport->post->getId()]),
'data-confirm-text' => 'Are you sure you want to feature this post on the main page?',
'data-redirect-url' => \Chibi\Router::linkTo(['StaticPagesController', 'mainPageView']),
];
@ -313,7 +313,7 @@ $canEditAnything = count(array_filter($editPostPrivileges)) > 0;
'text' => 'Flag for moderator attention',
'simple-action' => \Chibi\Router::linkTo(
['PostController', 'flagAction'],
['id' => $this->context->transport->post->getId()]),
['identifier' => $this->context->transport->post->getId()]),
'data-confirm-text' => 'Are you sure you want to flag this post?',
];
}
@ -331,7 +331,7 @@ $canEditAnything = count(array_filter($editPostPrivileges)) > 0;
'text' => 'Unhide',
'simple-action' => \Chibi\Router::linkTo(
['PostController', 'unhideAction'],
['id' => $this->context->transport->post->getId()]),
['identifier' => $this->context->transport->post->getId()]),
];
}
else
@ -342,7 +342,7 @@ $canEditAnything = count(array_filter($editPostPrivileges)) > 0;
'text' => 'Hide',
'simple-action' => \Chibi\Router::linkTo(
['PostController', 'hideAction'],
['id' => $this->context->transport->post->getId()]),
['identifier' => $this->context->transport->post->getId()]),
];
}
}
@ -357,7 +357,7 @@ $canEditAnything = count(array_filter($editPostPrivileges)) > 0;
'text' => 'Delete',
'simple-action' => \Chibi\Router::linkTo(
['PostController', 'deleteAction'],
['id' => $this->context->transport->post->getId()]),
['identifier' => $this->context->transport->post->getId()]),
'data-confirm-text' => 'Are you sure you want to delete this post?',
'data-redirect-url' => \Chibi\Router::linkTo(['PostController', 'listView']),
];

View file

@ -18,8 +18,9 @@ $this->assets->addStylesheet('static-main.css');
<div class="body">
<?php
$this->context->transport->post = $this->context->featuredPost;
$this->context->imageLink = \Chibi\Router::linkTo(['PostController', 'genericView'], [
'id' => $this->context->featuredPost->getId()]);
$this->context->imageLink = \Chibi\Router::linkTo(
['PostController', 'genericView'],
['identifier' => $this->context->featuredPost->getId()]);
?>
<?php $this->renderExternal('post-file-render') ?>

View file

@ -32,9 +32,9 @@ $postValidation =
\Chibi\Router::register(['PostController', 'uploadView'], 'GET', '/posts/upload', $postValidation);
\Chibi\Router::register(['PostController', 'uploadAction'], 'POST', '/posts/upload', $postValidation);
\Chibi\Router::register(['PostController', 'editView'], 'GET', '/post/{id}/edit', $postValidation);
\Chibi\Router::register(['PostController', 'editAction'], 'POST', '/post/{id}/edit', $postValidation);
\Chibi\Router::register(['PostController', 'deleteAction'], null, '/post/{id}/delete', $postValidation);
\Chibi\Router::register(['PostController', 'editView'], 'GET', '/post/{identifier}/edit', $postValidation);
\Chibi\Router::register(['PostController', 'editAction'], 'POST', '/post/{identifier}/edit', $postValidation);
\Chibi\Router::register(['PostController', 'deleteAction'], null, '/post/{identifier}/delete', $postValidation);
\Chibi\Router::register(['PostController', 'listView'], 'GET', '/{source}', $postValidation);
\Chibi\Router::register(['PostController', 'listView'], 'GET', '/{source}/{page}', $postValidation);
@ -49,18 +49,18 @@ $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', 'genericView'], 'GET', '/post/{identifier}', $postValidation);
\Chibi\Router::register(['PostController', 'fileView'], 'GET', '/post/{name}/retrieve', $postValidation);
\Chibi\Router::register(['PostController', 'thumbnailView'], 'GET', '/post/{name}/thumb', $postValidation);
\Chibi\Router::register(['PostController', 'toggleTagAction'], null, '/post/{id}/toggle-tag/{tag}/{enable}', $postValidation);
\Chibi\Router::register(['PostController', 'flagAction'], null, '/post/{id}/flag', $postValidation);
\Chibi\Router::register(['PostController', 'hideAction'], null, '/post/{id}/hide', $postValidation);
\Chibi\Router::register(['PostController', 'unhideAction'], null, '/post/{id}/unhide', $postValidation);
\Chibi\Router::register(['PostController', 'removeFavoriteAction'], null, '/post/{id}/rem-fav', $postValidation);
\Chibi\Router::register(['PostController', 'addFavoriteAction'], null, '/post/{id}/add-fav', $postValidation);
\Chibi\Router::register(['PostController', 'scoreAction'], null, '/post/{id}/score/{score}', $postValidation);
\Chibi\Router::register(['PostController', 'featureAction'], null, '/post/{id}/feature', $postValidation);
\Chibi\Router::register(['PostController', 'toggleTagAction'], null, '/post/{identifier}/toggle-tag/{tag}/{enable}', $postValidation);
\Chibi\Router::register(['PostController', 'flagAction'], null, '/post/{identifier}/flag', $postValidation);
\Chibi\Router::register(['PostController', 'hideAction'], null, '/post/{identifier}/hide', $postValidation);
\Chibi\Router::register(['PostController', 'unhideAction'], null, '/post/{identifier}/unhide', $postValidation);
\Chibi\Router::register(['PostController', 'removeFavoriteAction'], null, '/post/{identifier}/rem-fav', $postValidation);
\Chibi\Router::register(['PostController', 'addFavoriteAction'], null, '/post/{identifier}/add-fav', $postValidation);
\Chibi\Router::register(['PostController', 'scoreAction'], null, '/post/{identifier}/score/{score}', $postValidation);
\Chibi\Router::register(['PostController', 'featureAction'], null, '/post/{identifier}/feature', $postValidation);
$commentValidation =
[