diff --git a/app/controllers/stories_controller.rb b/app/controllers/stories_controller.rb index 48af8bc79..960e0562b 100644 --- a/app/controllers/stories_controller.rb +++ b/app/controllers/stories_controller.rb @@ -274,6 +274,9 @@ class StoriesController < ApplicationController @user = @article.user @organization = @article.organization @comments_order = fetch_sort_order + + @comments_count = Comments::Count.new(@article).call + if @article.collection @collection = @article.collection diff --git a/app/queries/comments/count.rb b/app/queries/comments/count.rb new file mode 100644 index 000000000..69f01b232 --- /dev/null +++ b/app/queries/comments/count.rb @@ -0,0 +1,25 @@ +# find comments count for an article based on our display rules for signed in users +# the count includes both comments displayed as usual (text), comments displayed as "deleted" or "hidden by post author" +# the count doesn't include comments not displayed at all (childless comments with score below HIDE_THRESHOLD) + +module Comments + class Count + attr_reader :article + + def initialize(article) + @article = article + end + + def call + # comments that are not displayed at all (not even a "comment deleted" message): + # with the score below hiding threshold and w/o children + count_sql = "SELECT COUNT(id) FROM comments c1 WHERE score < ? AND commentable_id = ? " \ + "AND commentable_type = ? AND NOT EXISTS " \ + "(SELECT 1 FROM comments c2 WHERE c2.ancestry LIKE CONCAT('%/', c1.id::varchar(255)) " \ + "OR c2.ancestry = c1.id::varchar(255))" + san_count_sql = Comment.sanitize_sql([count_sql, Comment::HIDE_THRESHOLD, @article.id, "Article"]) + hidden_comments_cnt = Comment.count_by_sql(san_count_sql) + article.comments_count - hidden_comments_cnt + end + end +end diff --git a/app/views/articles/_full_comment_area.html.erb b/app/views/articles/_full_comment_area.html.erb index 13027faf9..ad69d25d5 100644 --- a/app/views/articles/_full_comment_area.html.erb +++ b/app/views/articles/_full_comment_area.html.erb @@ -5,7 +5,7 @@

<%= t("views.articles.comments.subtitle.#{@comments_order}_html", - num: tag.span(t("views.articles.comments.num", num: @article.comments_count), class: "js-comments-count", data: { comments_count: @article.comments_count })) %> + num: tag.span(t("views.articles.comments.num", num: @comments_count), class: "js-comments-count", data: { comments_count: @comments_count })) %>

<% if user_signed_in? %>