From c59db253876a408bde841243c2c826ae59e07551 Mon Sep 17 00:00:00 2001 From: Dana Scheider Date: Fri, 22 May 2020 00:07:51 +1000 Subject: [PATCH] [deploy] Gracefully handle missing linked users from comments (#7941) --- app/decorators/user_decorator.rb | 2 +- app/helpers/application_helper.rb | 16 ++++++++++++++++ app/liquid_tags/user_tag.rb | 20 ++++++++++++++------ app/views/users/_liquid.html.erb | 16 ++++++++++------ spec/liquid_tags/user_tag_spec.rb | 10 ++++++---- 5 files changed, 47 insertions(+), 17 deletions(-) diff --git a/app/decorators/user_decorator.rb b/app/decorators/user_decorator.rb index 635eda1f8..eedbb50b2 100644 --- a/app/decorators/user_decorator.rb +++ b/app/decorators/user_decorator.rb @@ -91,7 +91,7 @@ class UserDecorator < ApplicationDecorator }, ] colors |= WHITE_TEXT_COLORS - colors[id % 10] + colors[(id || rand(100)) % 10] end # returns true if the user has been suspended and has no content diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 1430aef3d..5a313beff 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1,4 +1,16 @@ module ApplicationHelper + # rubocop:disable Performance/OpenStruct + DELETED_USER = OpenStruct.new( + id: nil, + darker_color: HexComparer.new(bg: "#19063A", text: "#dce9f3").brightness, + username: "[deleted user]", + name: "[Deleted User]", + summary: nil, + twitter_username: nil, + github_username: nil, + ) + # rubocop:enable Performance/OpenStruct + def user_logged_in_status user_signed_in? ? "logged-in" : "logged-out" end @@ -113,6 +125,8 @@ module ApplicationHelper end def follow_button(followable, style = "full") + return if followable == DELETED_USER + tag :button, # Yikes class: "cta follow-action-button", data: { @@ -127,6 +141,8 @@ module ApplicationHelper end def user_colors(user) + return { bg: "#19063A", text: "#dce9f3" } if user == DELETED_USER + user.decorate.enriched_colors end diff --git a/app/liquid_tags/user_tag.rb b/app/liquid_tags/user_tag.rb index d843455c9..313068e74 100644 --- a/app/liquid_tags/user_tag.rb +++ b/app/liquid_tags/user_tag.rb @@ -13,18 +13,26 @@ class UserTag < LiquidTagBase ActionController::Base.new.render_to_string( partial: PARTIAL, locals: { - user: @user.decorate, + user: user_object_for_partial(@user), follow_button: @follow_button, - user_colors: @user_colors + user_colors: @user_colors, + user_path: path_to_profile(@user) }, ) end - def parse_username_to_user(user) - user = User.find_by(username: user) - raise StandardError, "Invalid username" if user.nil? + private - user + def parse_username_to_user(user) + User.find_by(username: user) || DELETED_USER + end + + def path_to_profile(user) + user == DELETED_USER ? nil : user.path + end + + def user_object_for_partial(user) + user == DELETED_USER ? user : user.decorate end end diff --git a/app/views/users/_liquid.html.erb b/app/views/users/_liquid.html.erb index 471d38b11..a15580ca7 100644 --- a/app/views/users/_liquid.html.erb +++ b/app/views/users/_liquid.html.erb @@ -6,17 +6,21 @@ border-color: <%= user_colors[:bg].casecmp("#ffffff").zero? ? user_colors[:text] : user_colors[:bg] %> !important; } - + <% if user_path.present? %> + +
+ <%= " /> +
+
+ <% else %>
<%= " />
- + <% end %>
-

<%= user.name %><%= follow_button %>

+

<%= link_to_if user_path.present?, user.name, user_path, class: "ltag__user__link" %><%= follow_button %>

- - <%= user.summary %> - + <%= link_to_if user_path.present?, user.summary, user_path, class: "ltag__user__link" %>