docbrown/app/helpers/comments_helper.rb
Anna Buianova 4cab190285
Hiding comments: an option to hide or keep children (#15178)
* Optional hiding child comments (start)

* Changed confirm button in comments hide form

* Hide children comments if hide_children was passed

* Added a spec for hidden child comment notifications

* Hide only explicitly hidden comments

* Hide comment modal on article page

* Prevent default behaviour for hide comment link

* Improved hide comments modal looks

* Improved hide comments modal looks

* Removed unused code

* Send hide comment form via fetch

* Hide comment descendants when hide_children was passed

* Don't hide hidden comments descendants on permalink

* Removed unnecesary span

* Improved hide comments modal styling

* Removed unused styles and js, improved styling

* Clickable label

Co-authored-by: ludwiczakpawel <ludwiczakpawel@gmail.com>

* Fixed showing hidden comments spoiler for article author

* Fixed hideArticleComments.spec.js

* Fixed displaying hidden comments text for artice author, added specs

* Target hide comments modal inside the modal when adding a listener

Co-authored-by: Suzanne Aitchison <suzanne@forem.com>

* Replaced hide comment link with a button

* Refactored adding hide_children url param

Co-authored-by: Suzanne Aitchison <suzanne@forem.com>

* Fixed cypress hide comments test

* Removed aria-label for submit on the hide comments modal

* Fixed formatting

Co-authored-by: ludwiczakpawel <ludwiczakpawel@gmail.com>
Co-authored-by: Suzanne Aitchison <suzanne@forem.com>
2021-11-18 11:57:40 +03:00

76 lines
2.1 KiB
Ruby

module CommentsHelper
MAX_COMMENTS_TO_RENDER = 250
MIN_COMMENTS_TO_RENDER = 8
def comment_class(comment, is_view_root: false)
if comment.root? || is_view_root
"root"
else
"child"
end
end
def comment_user_id_unless_deleted(comment)
comment.deleted ? 0 : comment.user_id
end
def commentable_author_is_op?(commentable, comment)
commentable &&
[
commentable.user_id,
commentable.co_author_ids,
].flatten.any?(comment.user_id)
end
def get_ama_or_op_banner(commentable)
commentable.decorate.cached_tag_list_array.include?("ama") ? "Ask Me Anything" : "Author"
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
end
def high_number_of_comments?(comments_number)
comments_number > MAX_COMMENTS_TO_RENDER
end
def view_all_comments?(comments_number)
comments_number > MIN_COMMENTS_TO_RENDER
end
def number_of_comments_to_render
MAX_COMMENTS_TO_RENDER
end
def comment_count(view)
view == "comments" ? MAX_COMMENTS_TO_RENDER : MIN_COMMENTS_TO_RENDER
end
def like_button_text(comment)
case comment.public_reactions_count
when 0
I18n.t("core.like")
when 1
"&nbsp;#{I18n.t('core.like').downcase}"
else
"&nbsp;#{I18n.t('core.like').downcase}s"
end
end
private
def nested_comments(tree:, commentable:, is_view_root: false)
comments = tree.map do |comment, sub_comments|
render("comments/comment", comment: comment, commentable: commentable,
is_view_root: is_view_root, is_childless: sub_comments.empty?,
subtree_html: nested_comments(tree: sub_comments, commentable: commentable))
end
safe_join(comments)
end
end