FIX: Respect core settings when rendering user names/usernames (#129)

This commit centralizes 'display name' generation into a helper, and updates it to respect core preferences for `enable_names` and `prioritize_username_in_ux`.
This commit is contained in:
David Taylor 2022-05-30 17:13:55 +01:00 committed by GitHub
parent 6c052c118d
commit 157f3e910d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 58 additions and 59 deletions

View File

@ -196,5 +196,23 @@ module DiscourseChatIntegration
secret
end
def self.formatted_display_name(user)
if !SiteSetting.enable_names || user.name.blank?
return "@#{user.username}"
end
full_name = user.name
full_name_normalized = User.normalize_username(full_name.strip)
similar = full_name_normalized.gsub(' ', '_') == user.username_lower || full_name_normalized.gsub(' ', '') == user.username_lower
if similar && SiteSetting.prioritize_username_in_ux?
"@#{user.username}"
elsif similar
full_name
elsif SiteSetting.prioritize_username_in_ux?
"@#{user.username} (#{full_name})"
else
"#{full_name} (@#{user.username})"
end
end
end
end

View File

@ -30,13 +30,7 @@ module DiscourseChatIntegration
end
def self.generate_discord_message(post)
display_name = "@#{post.user.username}"
full_name = post.user.name || ""
if !(full_name.strip.empty?) && (full_name.strip.gsub(' ', '_').casecmp(post.user.username) != 0) && (full_name.strip.gsub(' ', '').casecmp(post.user.username) != 0)
display_name = "#{full_name} @#{post.user.username}"
end
display_name = ::DiscourseChatIntegration::Helper.formatted_display_name(post.user)
topic = post.topic

View File

@ -22,12 +22,7 @@ module DiscourseChatIntegration::Provider::FlowdockProvider
end
def self.generate_flowdock_message(post, flow_token)
display_name = "@#{post.user.username}"
full_name = post.user.name || ""
if !(full_name.strip.empty?) && (full_name.strip.gsub(' ', '_').casecmp(post.user.username) != 0) && (full_name.strip.gsub(' ', '').casecmp(post.user.username) != 0)
display_name = "#{full_name} @#{post.user.username}"
end
display_name = ::DiscourseChatIntegration::Helper.formatted_display_name(post.user)
message = {
flow_token: flow_token,

View File

@ -7,11 +7,7 @@ module DiscourseChatIntegration::Provider::GroupmeProvider
]
def self.generate_groupme_message(post)
display_name = "@#{post.user.username}"
full_name = post.user.name || ""
if !(full_name.strip.empty?) && (full_name.strip.gsub(' ', '_').casecmp(post.user.username) != 0) && (full_name.strip.gsub(' ', '').casecmp(post.user.username) != 0)
display_name = "#{full_name} @#{post.user.username}"
end
display_name = ::DiscourseChatIntegration::Helper.formatted_display_name(post.user)
topic = post.topic

View File

@ -32,13 +32,7 @@ module DiscourseChatIntegration
end
def self.generate_matrix_message(post)
display_name = "@#{post.user.username}"
full_name = post.user.name || ""
if !(full_name.strip.empty?) && (full_name.strip.gsub(' ', '_').casecmp(post.user.username) != 0) && (full_name.strip.gsub(' ', '').casecmp(post.user.username) != 0)
display_name = "#{full_name} @#{post.user.username}"
end
display_name = ::DiscourseChatIntegration::Helper.formatted_display_name(post.user)
message = {
msgtype: SiteSetting.chat_integration_matrix_use_notice ? 'm.notice' : 'm.text',

View File

@ -31,12 +31,7 @@ module DiscourseChatIntegration
end
def self.mattermost_message(post, channel)
display_name = "@#{post.user.username}"
full_name = post.user.name || ""
if !(full_name.strip.empty?) && (full_name.strip.gsub(' ', '_').casecmp(post.user.username) != 0) && (full_name.strip.gsub(' ', '').casecmp(post.user.username) != 0)
display_name = "#{full_name} @#{post.user.username}"
end
display_name = ::DiscourseChatIntegration::Helper.formatted_display_name(post.user)
topic = post.topic

View File

@ -10,12 +10,7 @@ module DiscourseChatIntegration::Provider::RocketchatProvider
]
def self.rocketchat_message(post, channel)
display_name = "@#{post.user.username}"
full_name = post.user.name || ""
if !(full_name.strip.empty?) && (full_name.strip.gsub(' ', '_').casecmp(post.user.username) != 0) && (full_name.strip.gsub(' ', '').casecmp(post.user.username) != 0)
display_name = "#{full_name} @#{post.user.username}"
end
display_name = ::DiscourseChatIntegration::Helper.formatted_display_name(post.user)
topic = post.topic

View File

@ -30,12 +30,7 @@ module DiscourseChatIntegration::Provider::SlackProvider
end
def self.slack_message(post, channel, filter)
display_name = "@#{post.user.username}"
full_name = post.user.name || ""
if !(full_name.strip.empty?) && (full_name.strip.gsub(' ', '_').casecmp(post.user.username) != 0) && (full_name.strip.gsub(' ', '').casecmp(post.user.username) != 0)
display_name = "#{full_name} @#{post.user.username}"
end
display_name = ::DiscourseChatIntegration::Helper.formatted_display_name(post.user)
topic = post.topic

View File

@ -32,7 +32,7 @@ module DiscourseChatIntegration::Provider::TeamsProvider
def self.get_message(post)
display_name = "@#{post.user.username}"
full_name = post.user.name || ""
full_name = SiteSetting.enable_names ? post.user.name : ""
topic = post.topic

View File

@ -50,12 +50,7 @@ module DiscourseChatIntegration
end
def self.message_text(post)
display_name = "@#{post.user.username}"
full_name = post.user.name || ""
if !(full_name.strip.empty?) && (full_name.strip.gsub(' ', '_').casecmp(post.user.username) != 0) && (full_name.strip.gsub(' ', '').casecmp(post.user.username) != 0)
display_name = "#{full_name} @#{post.user.username}"
end
display_name = ::DiscourseChatIntegration::Helper.formatted_display_name(post.user)
topic = post.topic

View File

@ -37,8 +37,7 @@ module DiscourseChatIntegration::Provider::WebexProvider
end
def self.get_message(post)
display_name = "@#{post.user.username}"
full_name = post.user.name || ""
display_name = ::DiscourseChatIntegration::Helper.formatted_display_name(post.user)
topic = post.topic
@ -52,7 +51,7 @@ module DiscourseChatIntegration::Provider::WebexProvider
markdown = "**#{topic.title}**: #{category}"
markdown += " #{topic.tags.map(&:name).join(', ')} " if topic.tags.present?
markdown += "(#{post.full_url}) from #{full_name} (#{display_name}):\n"
markdown += "(#{post.full_url}) from #{display_name}:\n"
markdown += post.excerpt(SiteSetting.chat_integration_webex_excerpt_length,
text_entities: true,
strip_links: true,

View File

@ -26,12 +26,7 @@ module DiscourseChatIntegration
end
def self.generate_zulip_message(post, stream, subject)
display_name = "@#{post.user.username}"
full_name = post.user.name || ""
if !(full_name.strip.empty?) && (full_name.strip.gsub(' ', '_').casecmp(post.user.username) != 0) && (full_name.strip.gsub(' ', '').casecmp(post.user.username) != 0)
display_name = "#{full_name} @#{post.user.username}"
end
display_name = ::DiscourseChatIntegration::Helper.formatted_display_name(post.user)
message = I18n.t('chat_integration.provider.zulip.message', user: display_name,
post_url: post.full_url,

View File

@ -324,4 +324,32 @@ RSpec.describe DiscourseChatIntegration::Manager do
end
end
describe ".formatted_display_name" do
let(:user) { Fabricate(:user, name: "John Smith", username: 'js1') }
it "prioritizes correctly" do
SiteSetting.prioritize_username_in_ux = true
expect(DiscourseChatIntegration::Helper.formatted_display_name(user)).to eq("@#{user.username} (John Smith)")
SiteSetting.prioritize_username_in_ux = false
expect(DiscourseChatIntegration::Helper.formatted_display_name(user)).to eq("John Smith (@#{user.username})")
end
it "only displays one when name/username are similar" do
user.update!(username: "john_smith")
SiteSetting.prioritize_username_in_ux = true
expect(DiscourseChatIntegration::Helper.formatted_display_name(user)).to eq("@#{user.username}")
SiteSetting.prioritize_username_in_ux = false
expect(DiscourseChatIntegration::Helper.formatted_display_name(user)).to eq("John Smith")
end
it "only displays username when names are disabled" do
SiteSetting.enable_names = false
SiteSetting.prioritize_username_in_ux = true
expect(DiscourseChatIntegration::Helper.formatted_display_name(user)).to eq("@#{user.username}")
SiteSetting.prioritize_username_in_ux = false
expect(DiscourseChatIntegration::Helper.formatted_display_name(user)).to eq("@#{user.username}")
end
end
end