diff --git a/app/helpers/helper.rb b/app/helpers/helper.rb index 3062df7..6aaf830 100644 --- a/app/helpers/helper.rb +++ b/app/helpers/helper.rb @@ -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 diff --git a/lib/discourse_chat_integration/provider/discord/discord_provider.rb b/lib/discourse_chat_integration/provider/discord/discord_provider.rb index fed1982..be16516 100644 --- a/lib/discourse_chat_integration/provider/discord/discord_provider.rb +++ b/lib/discourse_chat_integration/provider/discord/discord_provider.rb @@ -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 diff --git a/lib/discourse_chat_integration/provider/flowdock/flowdock_provider.rb b/lib/discourse_chat_integration/provider/flowdock/flowdock_provider.rb index e0a7d3c..5e827c9 100644 --- a/lib/discourse_chat_integration/provider/flowdock/flowdock_provider.rb +++ b/lib/discourse_chat_integration/provider/flowdock/flowdock_provider.rb @@ -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, diff --git a/lib/discourse_chat_integration/provider/groupme/groupme_provider.rb b/lib/discourse_chat_integration/provider/groupme/groupme_provider.rb index 83c4a5d..f3a845f 100644 --- a/lib/discourse_chat_integration/provider/groupme/groupme_provider.rb +++ b/lib/discourse_chat_integration/provider/groupme/groupme_provider.rb @@ -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 diff --git a/lib/discourse_chat_integration/provider/matrix/matrix_provider.rb b/lib/discourse_chat_integration/provider/matrix/matrix_provider.rb index d2fbdbf..06f6dfb 100644 --- a/lib/discourse_chat_integration/provider/matrix/matrix_provider.rb +++ b/lib/discourse_chat_integration/provider/matrix/matrix_provider.rb @@ -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', diff --git a/lib/discourse_chat_integration/provider/mattermost/mattermost_provider.rb b/lib/discourse_chat_integration/provider/mattermost/mattermost_provider.rb index 1b4575b..ef69768 100644 --- a/lib/discourse_chat_integration/provider/mattermost/mattermost_provider.rb +++ b/lib/discourse_chat_integration/provider/mattermost/mattermost_provider.rb @@ -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 diff --git a/lib/discourse_chat_integration/provider/rocketchat/rocketchat_provider.rb b/lib/discourse_chat_integration/provider/rocketchat/rocketchat_provider.rb index d7bc713..74c0640 100644 --- a/lib/discourse_chat_integration/provider/rocketchat/rocketchat_provider.rb +++ b/lib/discourse_chat_integration/provider/rocketchat/rocketchat_provider.rb @@ -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 diff --git a/lib/discourse_chat_integration/provider/slack/slack_provider.rb b/lib/discourse_chat_integration/provider/slack/slack_provider.rb index f7be0d8..056eeee 100644 --- a/lib/discourse_chat_integration/provider/slack/slack_provider.rb +++ b/lib/discourse_chat_integration/provider/slack/slack_provider.rb @@ -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 diff --git a/lib/discourse_chat_integration/provider/teams/teams_provider.rb b/lib/discourse_chat_integration/provider/teams/teams_provider.rb index 9561d66..267719a 100644 --- a/lib/discourse_chat_integration/provider/teams/teams_provider.rb +++ b/lib/discourse_chat_integration/provider/teams/teams_provider.rb @@ -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 diff --git a/lib/discourse_chat_integration/provider/telegram/telegram_provider.rb b/lib/discourse_chat_integration/provider/telegram/telegram_provider.rb index a38097a..b9dac86 100644 --- a/lib/discourse_chat_integration/provider/telegram/telegram_provider.rb +++ b/lib/discourse_chat_integration/provider/telegram/telegram_provider.rb @@ -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 diff --git a/lib/discourse_chat_integration/provider/webex/webex_provider.rb b/lib/discourse_chat_integration/provider/webex/webex_provider.rb index d83e665..aaaefef 100644 --- a/lib/discourse_chat_integration/provider/webex/webex_provider.rb +++ b/lib/discourse_chat_integration/provider/webex/webex_provider.rb @@ -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, diff --git a/lib/discourse_chat_integration/provider/zulip/zulip_provider.rb b/lib/discourse_chat_integration/provider/zulip/zulip_provider.rb index e608c39..142dcf5 100644 --- a/lib/discourse_chat_integration/provider/zulip/zulip_provider.rb +++ b/lib/discourse_chat_integration/provider/zulip/zulip_provider.rb @@ -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, diff --git a/spec/helpers/helper_spec.rb b/spec/helpers/helper_spec.rb index fddbd5f..e3709e1 100644 --- a/spec/helpers/helper_spec.rb +++ b/spec/helpers/helper_spec.rb @@ -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