2022-06-24 07:59:09 -07:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-11-02 08:57:37 -08:00
|
|
|
module UserHelper
|
2014-11-10 22:09:36 -08:00
|
|
|
# Decides what user name to show.
|
2022-06-21 05:52:23 -07:00
|
|
|
# @param context_user [User] the user whose the profile preferences should be applied
|
2022-06-25 01:17:53 -07:00
|
|
|
# @param author_identifier [nil, String] the author identifier of the question (questions only)
|
2014-11-10 22:09:36 -08:00
|
|
|
# @return [String] The user name
|
2022-06-25 01:17:53 -07:00
|
|
|
def user_screen_name(user, context_user: nil, author_identifier: nil, url: true, link_only: false)
|
|
|
|
return unmask(user, context_user, author_identifier) if should_unmask?(author_identifier)
|
|
|
|
return anonymous_name(context_user) if anonymous?(user, author_identifier.present?)
|
2022-06-21 05:52:23 -07:00
|
|
|
|
2017-03-30 10:17:25 -07:00
|
|
|
if url
|
2022-07-23 03:06:05 -07:00
|
|
|
return user_path(user) if link_only
|
2022-06-21 05:52:23 -07:00
|
|
|
|
2022-06-24 07:59:09 -07:00
|
|
|
return profile_link(user)
|
2017-03-30 10:17:25 -07:00
|
|
|
end
|
2022-06-24 07:59:09 -07:00
|
|
|
user.profile.safe_name.strip
|
2014-11-10 22:09:36 -08:00
|
|
|
end
|
2022-06-21 07:27:07 -07:00
|
|
|
|
2022-06-25 01:17:53 -07:00
|
|
|
def moderation_view?
|
2022-08-20 08:08:07 -07:00
|
|
|
!!(current_user&.mod? && session[:moderation_view] == true)
|
2022-06-25 01:17:53 -07:00
|
|
|
end
|
|
|
|
|
2022-06-21 07:27:07 -07:00
|
|
|
private
|
|
|
|
|
2022-06-24 07:59:09 -07:00
|
|
|
def profile_link(user)
|
2022-07-23 03:06:05 -07:00
|
|
|
link_to(user.profile.safe_name, user_path(user), class: ("user--banned" if user.banned?).to_s)
|
2022-06-24 07:59:09 -07:00
|
|
|
end
|
|
|
|
|
2022-06-26 11:59:53 -07:00
|
|
|
def should_unmask?(author_identifier)
|
|
|
|
moderation_view? && author_identifier.present?
|
2022-06-25 01:17:53 -07:00
|
|
|
end
|
|
|
|
|
2022-06-26 11:59:53 -07:00
|
|
|
def unmask(user, context_user, author_identifier)
|
2022-06-25 01:17:53 -07:00
|
|
|
return profile_link(user) if user.present?
|
|
|
|
|
2022-06-26 11:59:53 -07:00
|
|
|
content_tag(:abbr, anonymous_name(context_user), title: author_identifier)
|
2022-06-24 07:59:09 -07:00
|
|
|
end
|
|
|
|
|
2022-06-21 07:27:07 -07:00
|
|
|
def anonymous_name(context_user)
|
2022-06-25 01:37:56 -07:00
|
|
|
sanitize(context_user&.profile&.anon_display_name.presence || APP_CONFIG["anonymous_name"], tags: [])
|
2022-06-21 07:27:07 -07:00
|
|
|
end
|
2022-06-24 07:59:09 -07:00
|
|
|
|
2022-06-26 15:36:00 -07:00
|
|
|
def anonymous?(user, author_identifier)
|
|
|
|
user.nil? || author_identifier.present?
|
2022-06-24 07:59:09 -07:00
|
|
|
end
|
2014-11-02 08:57:37 -08:00
|
|
|
end
|