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:
parent
6c052c118d
commit
157f3e910d
|
@ -196,5 +196,23 @@ module DiscourseChatIntegration
|
||||||
secret
|
secret
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
|
|
@ -30,13 +30,7 @@ module DiscourseChatIntegration
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.generate_discord_message(post)
|
def self.generate_discord_message(post)
|
||||||
|
display_name = ::DiscourseChatIntegration::Helper.formatted_display_name(post.user)
|
||||||
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
|
|
||||||
|
|
||||||
topic = post.topic
|
topic = post.topic
|
||||||
|
|
||||||
|
|
|
@ -22,12 +22,7 @@ module DiscourseChatIntegration::Provider::FlowdockProvider
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.generate_flowdock_message(post, flow_token)
|
def self.generate_flowdock_message(post, flow_token)
|
||||||
display_name = "@#{post.user.username}"
|
display_name = ::DiscourseChatIntegration::Helper.formatted_display_name(post.user)
|
||||||
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
|
|
||||||
|
|
||||||
message = {
|
message = {
|
||||||
flow_token: flow_token,
|
flow_token: flow_token,
|
||||||
|
|
|
@ -7,11 +7,7 @@ module DiscourseChatIntegration::Provider::GroupmeProvider
|
||||||
]
|
]
|
||||||
|
|
||||||
def self.generate_groupme_message(post)
|
def self.generate_groupme_message(post)
|
||||||
display_name = "@#{post.user.username}"
|
display_name = ::DiscourseChatIntegration::Helper.formatted_display_name(post.user)
|
||||||
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
|
|
||||||
|
|
||||||
topic = post.topic
|
topic = post.topic
|
||||||
|
|
||||||
|
|
|
@ -32,13 +32,7 @@ module DiscourseChatIntegration
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.generate_matrix_message(post)
|
def self.generate_matrix_message(post)
|
||||||
|
display_name = ::DiscourseChatIntegration::Helper.formatted_display_name(post.user)
|
||||||
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
|
|
||||||
|
|
||||||
message = {
|
message = {
|
||||||
msgtype: SiteSetting.chat_integration_matrix_use_notice ? 'm.notice' : 'm.text',
|
msgtype: SiteSetting.chat_integration_matrix_use_notice ? 'm.notice' : 'm.text',
|
||||||
|
|
|
@ -31,12 +31,7 @@ module DiscourseChatIntegration
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.mattermost_message(post, channel)
|
def self.mattermost_message(post, channel)
|
||||||
display_name = "@#{post.user.username}"
|
display_name = ::DiscourseChatIntegration::Helper.formatted_display_name(post.user)
|
||||||
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
|
|
||||||
|
|
||||||
topic = post.topic
|
topic = post.topic
|
||||||
|
|
||||||
|
|
|
@ -10,12 +10,7 @@ module DiscourseChatIntegration::Provider::RocketchatProvider
|
||||||
]
|
]
|
||||||
|
|
||||||
def self.rocketchat_message(post, channel)
|
def self.rocketchat_message(post, channel)
|
||||||
display_name = "@#{post.user.username}"
|
display_name = ::DiscourseChatIntegration::Helper.formatted_display_name(post.user)
|
||||||
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
|
|
||||||
|
|
||||||
topic = post.topic
|
topic = post.topic
|
||||||
|
|
||||||
|
|
|
@ -30,12 +30,7 @@ module DiscourseChatIntegration::Provider::SlackProvider
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.slack_message(post, channel, filter)
|
def self.slack_message(post, channel, filter)
|
||||||
display_name = "@#{post.user.username}"
|
display_name = ::DiscourseChatIntegration::Helper.formatted_display_name(post.user)
|
||||||
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
|
|
||||||
|
|
||||||
topic = post.topic
|
topic = post.topic
|
||||||
|
|
||||||
|
|
|
@ -32,7 +32,7 @@ module DiscourseChatIntegration::Provider::TeamsProvider
|
||||||
|
|
||||||
def self.get_message(post)
|
def self.get_message(post)
|
||||||
display_name = "@#{post.user.username}"
|
display_name = "@#{post.user.username}"
|
||||||
full_name = post.user.name || ""
|
full_name = SiteSetting.enable_names ? post.user.name : ""
|
||||||
|
|
||||||
topic = post.topic
|
topic = post.topic
|
||||||
|
|
||||||
|
|
|
@ -50,12 +50,7 @@ module DiscourseChatIntegration
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.message_text(post)
|
def self.message_text(post)
|
||||||
display_name = "@#{post.user.username}"
|
display_name = ::DiscourseChatIntegration::Helper.formatted_display_name(post.user)
|
||||||
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
|
|
||||||
|
|
||||||
topic = post.topic
|
topic = post.topic
|
||||||
|
|
||||||
|
|
|
@ -37,8 +37,7 @@ module DiscourseChatIntegration::Provider::WebexProvider
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.get_message(post)
|
def self.get_message(post)
|
||||||
display_name = "@#{post.user.username}"
|
display_name = ::DiscourseChatIntegration::Helper.formatted_display_name(post.user)
|
||||||
full_name = post.user.name || ""
|
|
||||||
|
|
||||||
topic = post.topic
|
topic = post.topic
|
||||||
|
|
||||||
|
@ -52,7 +51,7 @@ module DiscourseChatIntegration::Provider::WebexProvider
|
||||||
|
|
||||||
markdown = "**#{topic.title}**: #{category}"
|
markdown = "**#{topic.title}**: #{category}"
|
||||||
markdown += " #{topic.tags.map(&:name).join(', ')} " if topic.tags.present?
|
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,
|
markdown += post.excerpt(SiteSetting.chat_integration_webex_excerpt_length,
|
||||||
text_entities: true,
|
text_entities: true,
|
||||||
strip_links: true,
|
strip_links: true,
|
||||||
|
|
|
@ -26,12 +26,7 @@ module DiscourseChatIntegration
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.generate_zulip_message(post, stream, subject)
|
def self.generate_zulip_message(post, stream, subject)
|
||||||
display_name = "@#{post.user.username}"
|
display_name = ::DiscourseChatIntegration::Helper.formatted_display_name(post.user)
|
||||||
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
|
|
||||||
|
|
||||||
message = I18n.t('chat_integration.provider.zulip.message', user: display_name,
|
message = I18n.t('chat_integration.provider.zulip.message', user: display_name,
|
||||||
post_url: post.full_url,
|
post_url: post.full_url,
|
||||||
|
|
|
@ -324,4 +324,32 @@ RSpec.describe DiscourseChatIntegration::Manager do
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|
Loading…
Reference in New Issue