diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 23fe6445f..545fc383d 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -16,9 +16,11 @@ class CommentsController < ApplicationController @root_comment = Comment.find(params[:id_code].to_i(26)) if params[:id_code].present? - # hide low quality comments w/o children - if @root_comment && @root_comment.decorate.low_quality && !@root_comment.has_children? - not_found + if @root_comment + # 404 for all low-quality for not signed in + not_found if @root_comment.score < Comment::LOW_QUALITY_THRESHOLD && !user_signed_in? + # 404 only for < -400 w/o children for signed in + not_found if @root_comment.score < Comment::HIDE_THRESHOLD && !@root_comment.has_children? end if @podcast diff --git a/app/decorators/comment_decorator.rb b/app/decorators/comment_decorator.rb index 1179de252..916b9cee3 100644 --- a/app/decorators/comment_decorator.rb +++ b/app/decorators/comment_decorator.rb @@ -3,6 +3,10 @@ class CommentDecorator < ApplicationDecorator score < Comment::LOW_QUALITY_THRESHOLD end + def super_low_quality + score < Comment::HIDE_THRESHOLD + end + def published_timestamp return "" if created_at.nil? diff --git a/app/helpers/comments_helper.rb b/app/helpers/comments_helper.rb index c1f6735c3..5f8e15664 100644 --- a/app/helpers/comments_helper.rb +++ b/app/helpers/comments_helper.rb @@ -14,20 +14,12 @@ module CommentsHelper !(commentable.any_comments_hidden || any_hidden_negative_comments?(commentable)) end - def article_comment_tree(article, count, order) - @article_comment_tree ||= begin - collection = Comment.tree_for(article, count, order) - collection.reject! { |comment| comment.score.negative? } unless user_signed_in? - collection - end + def article_comment_tree(article, limit, order) + Comments::Tree.for_commentable(article, limit: limit, order: order, include_negative: user_signed_in?) end def podcast_comment_tree(episode) - @podcast_comment_tree ||= begin - collection = Comment.tree_for(episode, 12) - collection.reject! { |comment| comment.score.negative? } unless user_signed_in? - collection - end + Comments::Tree.for_commentable(episode, include_negative: user_signed_in?, limit: 12) end def comment_class(comment, is_view_root: false) @@ -58,10 +50,6 @@ module CommentsHelper end end - def tree_for(comment, sub_comments, commentable) - nested_comments(tree: { comment => sub_comments }, commentable: commentable, is_view_root: true) - end - def should_be_hidden?(comment, root_comment) # when opened by a permalink + root comment is hidden => show root comment and its descendants comment.hidden_by_commentable_user && comment != root_comment && !root_comment&.hidden_by_commentable_user diff --git a/app/models/comment.rb b/app/models/comment.rb index 114469178..4f269249d 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -10,6 +10,7 @@ class Comment < ApplicationRecord COMMENTABLE_TYPES = %w[Article PodcastEpisode].freeze LOW_QUALITY_THRESHOLD = -75 + HIDE_THRESHOLD = -400 # hide comments below this threshold VALID_SORT_OPTIONS = %w[top latest oldest].freeze @@ -90,14 +91,6 @@ class Comment < ApplicationRecord alias touch_by_reaction save - def self.tree_for(commentable, limit = 0, order = nil) - commentable.comments - .includes(user: %i[setting profile]) - .arrange(order: build_sort_query(order)) - .to_a[0..limit - 1] - .to_h - end - def self.title_deleted I18n.t("models.comment.deleted") end @@ -114,17 +107,6 @@ class Comment < ApplicationRecord includes(user: :profile).new(params, &blk) end - def self.build_sort_query(order) - case order - when "latest" - "created_at DESC" - when "oldest" - "created_at ASC" - else - "score DESC" - end - end - def search_id "comment_#{id}" end @@ -208,8 +190,6 @@ class Comment < ApplicationRecord Comments::CalculateScoreWorker.perform_async(id) end - private_class_method :build_sort_query - private def remove_notifications? diff --git a/app/queries/comments/tree.rb b/app/queries/comments/tree.rb new file mode 100644 index 000000000..434677a17 --- /dev/null +++ b/app/queries/comments/tree.rb @@ -0,0 +1,34 @@ +module Comments + module Tree + module_function + + def for_commentable(commentable, limit: 0, order: nil, include_negative: false) + collection = commentable.comments + .includes(user: %i[setting profile]) + .arrange(order: build_sort_query(order)) + .to_a[0..limit - 1] + .to_h + collection.reject! { |comment| comment.score.negative? } unless include_negative + collection + end + + def for_root_comment(root_comment, include_negative: false) + sub_comments = root_comment.subtree.includes(user: %i[setting profile]).arrange[root_comment] + sub_comments.reject! { |comment| comment.score.negative? } unless include_negative + { root_comment => sub_comments } + end + + def build_sort_query(order) + case order + when "latest" + "created_at DESC" + when "oldest" + "created_at ASC" + else + "score DESC" + end + end + + private_class_method :build_sort_query + end +end diff --git a/app/views/articles/_comment_tree.html.erb b/app/views/articles/_comment_tree.html.erb index 07f30ccb2..bac08c262 100644 --- a/app/views/articles/_comment_tree.html.erb +++ b/app/views/articles/_comment_tree.html.erb @@ -1,4 +1,4 @@ <% comment, sub_comments = comment_node %> -<% unless comment.decorate.low_quality && sub_comments.empty? %> +<% unless comment.decorate.super_low_quality && sub_comments.empty? %> <%= nested_comments(tree: { comment => sub_comments }, commentable: @article, is_view_root: true) %> <% end %> diff --git a/app/views/comments/_comment_proper.html.erb b/app/views/comments/_comment_proper.html.erb index b19eb4fc1..d7e6a544c 100644 --- a/app/views/comments/_comment_proper.html.erb +++ b/app/views/comments/_comment_proper.html.erb @@ -5,7 +5,7 @@