From 4c1bb44e594f8ecce37e000a21a38bdd69186713 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Sun, 1 Dec 2013 14:47:35 +0100 Subject: [PATCH] Fixed rare bug in prev/next post If tag/user/whatever from last search was deleted after viewing a post, refreshing the page with that post would yield "Invalid tag/user/whatever" error. I changed it so that when retrieving previous/next post for latest search query throws any errors, saved search query gets resetted to empty one. --- src/Controllers/PostController.php | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/Controllers/PostController.php b/src/Controllers/PostController.php index b5747243..56ae870b 100644 --- a/src/Controllers/PostController.php +++ b/src/Controllers/PostController.php @@ -452,18 +452,27 @@ class PostController 'ownComment.commenter' => 'user']); R::preload($this->context->user, ['ownFavoritee']); - $this->context->transport->lastSearchQuery = InputHelper::get('last-search-query'); - if ($post->hidden) PrivilegesHelper::confirmWithException(Privilege::ViewPost, 'hidden'); PrivilegesHelper::confirmWithException(Privilege::ViewPost); PrivilegesHelper::confirmWithException(Privilege::ViewPost, PostSafety::toString($post->safety)); Model_Post_QueryBuilder::enableTokenLimit(false); - $prevPostQuery = $this->context->transport->lastSearchQuery . ' prev:' . $id; - $nextPostQuery = $this->context->transport->lastSearchQuery . ' next:' . $id; - $prevPost = current(Model_Post::getEntities($prevPostQuery, 1, 1)); - $nextPost = current(Model_Post::getEntities($nextPostQuery, 1, 1)); + try + { + $this->context->transport->lastSearchQuery = InputHelper::get('last-search-query'); + $prevPostQuery = $this->context->transport->lastSearchQuery . ' prev:' . $id; + $nextPostQuery = $this->context->transport->lastSearchQuery . ' next:' . $id; + $prevPost = current(Model_Post::getEntities($prevPostQuery, 1, 1)); + $nextPost = current(Model_Post::getEntities($nextPostQuery, 1, 1)); + } + #search for some reason was invalid, e.g. tag was deleted in the meantime + catch (Exception $e) + { + $this->context->transport->lastSearchQuery = ''; + $prevPost = current(Model_Post::getEntities('prev:' . $id, 1, 1)); + $nextPost = current(Model_Post::getEntities('next:' . $id, 1, 1)); + } Model_Post_QueryBuilder::enableTokenLimit(true); $favorite = $this->context->user->hasFavorited($post);