Moved comment listing to API

This commit is contained in:
Marcin Kurczewski 2014-05-02 09:50:13 +02:00
parent 6a28be5e3e
commit c0a7fe5209
3 changed files with 56 additions and 23 deletions

View file

@ -1,33 +1,21 @@
<?php
class CommentController
{
public function listView($page)
public function listView($page = 1)
{
Access::assert(Privilege::ListComments);
$page = max(1, intval($page));
$commentsPerPage = intval(getConfig()->comments->commentsPerPage);
$searchQuery = 'comment_min:1 order:comment_date,desc';
$posts = PostSearchService::getEntities($searchQuery, $commentsPerPage, $page);
$postCount = PostSearchService::getEntityCount($searchQuery);
$pageCount = ceil($postCount / $commentsPerPage);
PostModel::preloadTags($posts);
PostModel::preloadComments($posts);
$comments = [];
foreach ($posts as $post)
$comments = array_merge($comments, $post->getComments());
CommentModel::preloadCommenters($comments);
$ret = Api::run(
new ListCommentsJob(),
[
JobArgs::PAGE_NUMBER => $page,
]);
$context = getContext();
$context->postGroups = true;
$context->transport->posts = $posts;
$context->transport->posts = $ret->posts;
$context->transport->paginator = new StdClass;
$context->transport->paginator->page = $page;
$context->transport->paginator->pageCount = $pageCount;
$context->transport->paginator->entityCount = $postCount;
$context->transport->paginator->entities = $posts;
$context->transport->paginator->params = func_get_args();
$context->transport->paginator->page = $ret->page;
$context->transport->paginator->pageCount = $ret->pageCount;
$context->transport->paginator->entityCount = $ret->postCount;
$context->transport->paginator->entities = $ret->posts;
}
public function previewAction()

View file

@ -4,4 +4,5 @@ class JobArgs
const COMMENT_ID = 'comment-id';
const POST_ID = 'post-id';
const TEXT = 'text';
const PAGE_NUMBER = 'page-number';
}

View file

@ -0,0 +1,44 @@
<?php
class ListCommentsJob extends AbstractJob
{
public function execute()
{
$page = $this->getArgument(JobArgs::PAGE_NUMBER);
$page = max(1, intval($page));
$commentsPerPage = intval(getConfig()->comments->commentsPerPage);
$searchQuery = 'comment_min:1 order:comment_date,desc';
$posts = PostSearchService::getEntities($searchQuery, $commentsPerPage, $page);
$postCount = PostSearchService::getEntityCount($searchQuery);
$pageCount = ceil($postCount / $commentsPerPage);
PostModel::preloadTags($posts);
PostModel::preloadComments($posts);
$comments = [];
foreach ($posts as $post)
$comments = array_merge($comments, $post->getComments());
CommentModel::preloadCommenters($comments);
$ret = new StdClass;
$ret->posts = $posts;
$ret->postCount = $postCount;
$ret->page = $page;
$ret->pageCount = $pageCount;
return $ret;
}
public function requiresPrivilege()
{
return Privilege::ListComments;
}
public function requiresAuthentication()
{
return false;
}
public function requiresConfirmedEmail()
{
return false;
}
}