Changed /comments behaviour

Instead of showing comments chronologically, group them into posts, then sort
the posts by last comment date. Reason: improved comment context delivery
makes discussion bumping possible (no matter how old it is) and discussion is
what comments are about.

Comment count is limited to 5 per post.
This commit is contained in:
Marcin Kurczewski 2014-02-24 00:17:01 +01:00
parent b144321c76
commit e6b37afa8c
7 changed files with 61 additions and 35 deletions

View file

@ -44,7 +44,8 @@ maxRelatedPosts=50
[comments]
minLength = 5
maxLength = 2000
commentsPerPage = 20
commentsPerPage = 10
maxCommentsInList = 5
[registration]
staffActivation = 0

View file

@ -26,3 +26,8 @@
.small-screen .comment-group .comments {
margin-left: 0;
}
.hellip {
margin-bottom: 2em;
display: inline-block;
}

View file

@ -8,27 +8,29 @@ class CommentController
*/
public function listAction($page)
{
$page = intval($page);
$commentsPerPage = intval($this->config->comments->commentsPerPage);
PrivilegesHelper::confirmWithException(Privilege::ListComments);
$page = max(1, $page);
$comments = CommentSearchService::getEntities(null, $commentsPerPage, $page);
$commentCount = CommentSearchService::getEntityCount(null);
$pageCount = ceil($commentCount / $commentsPerPage);
CommentModel::preloadCommenters($comments);
CommentModel::preloadPosts($comments);
$posts = array_map(function($comment) { return $comment->getPost(); }, $comments);
$page = max(1, intval($page));
$commentsPerPage = intval($this->config->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);
$comments = [];
foreach ($posts as $post)
$comments = array_merge($comments, $post->getComments());
CommentModel::preloadCommenters($comments);
$this->context->postGroups = true;
$this->context->transport->posts = $posts;
$this->context->transport->paginator = new StdClass;
$this->context->transport->paginator->page = $page;
$this->context->transport->paginator->pageCount = $pageCount;
$this->context->transport->paginator->entityCount = $commentCount;
$this->context->transport->paginator->entities = $comments;
$this->context->transport->paginator->entityCount = $postCount;
$this->context->transport->paginator->entities = $posts;
$this->context->transport->paginator->params = func_get_args();
$this->context->transport->comments = $comments;
}

View file

@ -225,6 +225,9 @@ class PostSearchParser extends AbstractSearchParser
elseif (in_array($orderByString, ['tag', 'tags', 'tagcount', 'tag_count']))
$orderColumn = 'tag_count';
elseif (in_array($orderByString, ['commentdate', 'comment_date']))
$orderColumn = 'comment_date';
elseif ($orderByString == 'random')
{
//seeding works like this: if you visit anything

View file

@ -0,0 +1,13 @@
ALTER TABLE post ADD COLUMN comment_date INTEGER DEFAULT NULL;
CREATE TRIGGER comment_update_date AFTER UPDATE ON comment FOR EACH ROW
BEGIN
UPDATE post SET comment_date = (SELECT MAX(comment_date) FROM comment WHERE comment.post_id = post.id);
END;
CREATE TRIGGER comment_insert_date AFTER INSERT ON comment FOR EACH ROW
BEGIN
UPDATE post SET comment_date = (SELECT MAX(comment_date) FROM comment WHERE comment.post_id = post.id);
END;
UPDATE comment SET id = id;

View file

@ -0,0 +1,13 @@
ALTER TABLE post ADD COLUMN comment_date INTEGER DEFAULT NULL;
CREATE TRIGGER comment_update_date AFTER UPDATE ON comment FOR EACH ROW
BEGIN
UPDATE post SET comment_date = (SELECT MAX(comment_date) FROM comment WHERE comment.post_id = post.id);
END;
CREATE TRIGGER comment_insert_date AFTER INSERT ON comment FOR EACH ROW
BEGIN
UPDATE post SET comment_date = (SELECT MAX(comment_date) FROM comment WHERE comment.post_id = post.id);
END;
UPDATE comment SET id = id;

View file

@ -2,7 +2,7 @@
LayoutHelper::setSubTitle('comments');
?>
<?php if (empty($this->context->transport->comments)): ?>
<?php if (empty($this->context->transport->posts)): ?>
<p class="alert alert-warning">No comments to show.</p>
<?php else: ?>
<?php
@ -14,35 +14,24 @@ LayoutHelper::setSubTitle('comments');
<div class="comments-wrapper">
<div class="comments paginator-content">
<?php
$groups = [];
$posts = [];
$currentGroupPostId = null;
$currentGroup = null;
foreach ($this->context->transport->comments as $comment)
{
if ($comment->postId != $currentGroupPostId)
{
unset($currentGroup);
$currentGroup = [];
$currentGroupPostId = $comment->postId;
$posts[$comment->postId] = $comment->getPost();
$groups[] = &$currentGroup;
}
$currentGroup []= $comment;
}
?>
<?php foreach ($groups as $group): ?>
<?php foreach ($this->context->transport->posts as $post): ?>
<div class="comment-group">
<div class="post-wrapper">
<?php $this->context->post = $posts[reset($group)->postId] ?>
<?php $this->context->post = $post ?>
<?php echo $this->renderFile('post-small') ?>
</div>
<div class="comments">
<?php foreach ($group as $comment): ?>
<?php $comments = array_reverse($post->getComments()) ?>
<?php foreach (array_slice($comments, 0, $this->config->comments->maxCommentsInList) as $comment): ?>
<?php $this->context->comment = $comment ?>
<?php echo $this->renderFile('comment-small') ?>
<?php endforeach ?>
<?php if (count($comments) > $this->config->comments->maxCommentsInList): ?>
<a href="<?php echo \Chibi\UrlHelper::route('post', 'view', ['id' => $this->context->post->id]) ?>">
<span class="hellip">(more&hellip;)</span>
</a>
<?php endif ?>
</div>
<div class="clear"></div>
</div>