From e5e36a2e2dc25481cbc6433c9ffde9bcbe20fe80 Mon Sep 17 00:00:00 2001 From: Fernando Valverde Date: Mon, 3 Oct 2022 07:58:34 -0600 Subject: [PATCH] Comment fragment link when signed out (#18517) * Comment fragment link when signed out * Optional chaining for article path * Fix comment liquid tag rendering * Extract signed in/out logic into helper * Debugging specs * Fix specs * Use named empty anchor to link comments with id_code --- app/helpers/comments_helper.rb | 11 +++++++++++ app/lib/url.rb | 12 ++++++++++++ .../articles/_full_comment_area.html.erb | 2 +- app/views/comments/_comment.html.erb | 1 + app/views/comments/_comment_date.erb | 2 +- app/views/comments/_comment_header.html.erb | 2 +- .../user_views_article_comments_spec.rb | 19 ++++++++++++++++++- 7 files changed, 45 insertions(+), 4 deletions(-) diff --git a/app/helpers/comments_helper.rb b/app/helpers/comments_helper.rb index 4da608367..9b802c13b 100644 --- a/app/helpers/comments_helper.rb +++ b/app/helpers/comments_helper.rb @@ -64,6 +64,17 @@ module CommentsHelper end end + def contextual_comment_url(comment, article: nil) + # Liquid tag parsing doesn't have Devise/Warden (request middleware) + return URL.comment(comment) if request.env["warden"].nil? + + # Logged in users should get the comment permalink + return URL.comment(comment) if user_signed_in? + + # Logged out users should get the article URL with the comment anchor + URL.fragment_comment(comment, path: article&.path) + end + private def nested_comments(tree:, commentable:, is_view_root: false) diff --git a/app/lib/url.rb b/app/lib/url.rb index 90896e890..28f382a95 100644 --- a/app/lib/url.rb +++ b/app/lib/url.rb @@ -45,6 +45,18 @@ module URL url(comment.path) end + # Creates a fragment URL for a comment on an article page + # if an article path is available + # + # @param comment [Comment] the comment to create the URL for + # @param path [String, nil] the path of the article to anchor the + # comment link instead of using the comment's permalink + def self.fragment_comment(comment, path:) + return comment(comment) if path.nil? + + url("#{path}#comment-#{comment.id_code}") + end + # Creates a reaction URL # # A reaction URL is the URL of its reactable. diff --git a/app/views/articles/_full_comment_area.html.erb b/app/views/articles/_full_comment_area.html.erb index 61b139ca2..a87976789 100644 --- a/app/views/articles/_full_comment_area.html.erb +++ b/app/views/articles/_full_comment_area.html.erb @@ -1,4 +1,4 @@ -<% cache("whole-comment-area-#{@article.id}-#{@article.last_comment_at}-#{@article.show_comments}-#{@discussion_lock&.updated_at}-#{@comments_order}", expires_in: 2.hours) do %> +<% cache("whole-comment-area-#{@article.id}-#{@article.last_comment_at}-#{@article.show_comments}-#{@discussion_lock&.updated_at}-#{@comments_order}-#{user_signed_in?}", expires_in: 2.hours) do %>
<% if @article.show_comments %>
diff --git a/app/views/comments/_comment.html.erb b/app/views/comments/_comment.html.erb index 4f4593be2..1d4261c35 100644 --- a/app/views/comments/_comment.html.erb +++ b/app/views/comments/_comment.html.erb @@ -24,6 +24,7 @@ data-path="<%= commentable&.path %>/comments/<%= comment.id_code_generated %>" data-comment-author-id="<%= comment_user_id_unless_deleted comment %>" data-content-user-id="<%= comment_user_id_unless_deleted comment %>"> +   <%= render("comments/comment_proper", comment: comment, commentable: commentable, diff --git a/app/views/comments/_comment_date.erb b/app/views/comments/_comment_date.erb index cb7159af6..729de1100 100644 --- a/app/views/comments/_comment_date.erb +++ b/app/views/comments/_comment_date.erb @@ -1,6 +1,6 @@ - + diff --git a/app/views/comments/_comment_header.html.erb b/app/views/comments/_comment_header.html.erb index d229b23b2..50fed7cd9 100644 --- a/app/views/comments/_comment_header.html.erb +++ b/app/views/comments/_comment_header.html.erb @@ -29,7 +29,7 @@