Skip to content

A platform for community discussion. Free, open, simple.

REFACTOR: Add `full_url` and `display_name` to `User`

Gerhard Schlagerb3cda195b8a4
diff --git a/app/controllers/list_controller.rb b/app/controllers/list_controller.rb
index 75d3ff1318d..0ec590a3ea5 100644
--- a/app/controllers/list_controller.rb
+++ b/app/controllers/list_controller.rb
@@ -245,8 +245,8 @@ class ListController < ApplicationController
ensure_can_see_profile!(target_user)
@title = "#{SiteSetting.title} - #{I18n.t("rss_description.user_topics", username: target_user.username)}"
- @link = "#{Discourse.base_url}/u/#{target_user.username}/activity/topics"
- @atom_link = "#{Discourse.base_url}/u/#{target_user.username}/activity/topics.rss"
+ @link = "#{target_user.full_url}/activity/topics"
+ @atom_link = "#{target_user.full_url}/activity/topics.rss"
@description = I18n.t("rss_description.user_topics", username: target_user.username)
@topic_list = TopicQuery
diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb
index 956e1d2e366..b7e0df39c52 100644
--- a/app/controllers/posts_controller.rb
+++ b/app/controllers/posts_controller.rb
@@ -132,7 +132,7 @@ class PostsController < ApplicationController
format.rss do
@posts = posts
@title = "#{SiteSetting.title} - #{I18n.t("rss_description.user_posts", username: user.username)}"
- @link = "#{Discourse.base_url}/u/#{user.username}/activity"
+ @link = "#{user.full_url}/activity"
@description = I18n.t("rss_description.user_posts", username: user.username)
render 'posts/latest', formats: [:rss]
end
diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb
index 0ffc2d70014..dcca3946025 100644
--- a/app/helpers/application_helper.rb
+++ b/app/helpers/application_helper.rb
@@ -658,13 +658,7 @@ module ApplicationHelper
end
def rss_creator(user)
- if user
- if SiteSetting.prioritize_username_in_ux
- "#{user.username}"
- else
- "#{user.name.presence || user.username }"
- end
- end
+ user&.display_name
end
def authentication_data
diff --git a/app/mailers/group_smtp_mailer.rb b/app/mailers/group_smtp_mailer.rb
index 25bb887b0d5..367297d4a5a 100644
--- a/app/mailers/group_smtp_mailer.rb
+++ b/app/mailers/group_smtp_mailer.rb
@@ -83,18 +83,10 @@ class GroupSmtpMailer < ActionMailer::Base
post.topic.allowed_users.each do |u|
next if u.id == recipient_user.id
- if SiteSetting.prioritize_username_in_ux?
- if u.staged?
- list.push("#{u.email}")
- else
- list.push("[#{u.username}](#{Discourse.base_url}/u/#{u.username_lower})")
- end
+ if u.staged?
+ list.push("#{u.email}")
else
- if u.staged?
- list.push("#{u.email}")
- else
- list.push("[#{u.name.blank? ? u.username : u.name}](#{Discourse.base_url}/u/#{u.username_lower})")
- end
+ list.push("[#{u.display_name}](#{u.full_url})")
end
end
diff --git a/app/mailers/user_notifications.rb b/app/mailers/user_notifications.rb
index 5cca25310fd..892f407b987 100644
--- a/app/mailers/user_notifications.rb
+++ b/app/mailers/user_notifications.rb
@@ -561,12 +561,7 @@ class UserNotifications < ActionMailer::Base
post.topic.allowed_users.each do |u|
next if u.id == user.id
-
- if SiteSetting.prioritize_username_in_ux?
- participant_list.push "[#{u.username}](#{Discourse.base_url}/u/#{u.username_lower})"
- else
- participant_list.push "[#{u.name.blank? ? u.username : u.name}](#{Discourse.base_url}/u/#{u.username_lower})"
- end
+ participant_list.push "[#{u.display_name}](#{u.full_url})"
end
participants += participant_list.join(", ")
diff --git a/app/models/topic_poster.rb b/app/models/topic_poster.rb
index 3ff6a1e09ee..762734799c2 100644
--- a/app/models/topic_poster.rb
+++ b/app/models/topic_poster.rb
@@ -16,12 +16,6 @@ class TopicPoster < OpenStruct
end
def name_and_description
- if SiteSetting.prioritize_username_in_ux? || user.name.blank?
- name = user.username
- else
- name = user.name
- end
-
- I18n.t("js.user.avatar.name_and_description", name: name, description: description)
+ I18n.t("js.user.avatar.name_and_description", name: user.display_name, description: description)
end
end
diff --git a/app/models/user.rb b/app/models/user.rb
index ac0ce5c7eaa..2e5bab0b851 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -1461,6 +1461,18 @@ class User < ActiveRecord::Base
username_lower == User.normalize_username(another_username)
end
+ def full_url
+ "#{Discourse.base_url}/u/#{encoded_username}"
+ end
+
+ def display_name
+ if SiteSetting.prioritize_username_in_ux?
+ username
+ else
+ name.presence || username
+ end
+ end
+
protected
def badge_grant
diff --git a/lib/system_message.rb b/lib/system_message.rb
index d7f75541e96..27e24c398d7 100644
--- a/lib/system_message.rb
+++ b/lib/system_message.rb
@@ -62,7 +62,7 @@ class SystemMessage
{
site_name: SiteSetting.title,
username: @recipient.username,
- user_preferences_url: "#{Discourse.base_url}/u/#{@recipient.username_lower}/preferences",
+ user_preferences_url: "#{@recipient.full_url}/preferences",
new_user_tips: I18n.with_locale(@recipient.effective_locale) { I18n.t('system_messages.usage_tips.text_body_template', base_url: Discourse.base_url) },
site_password: "",
base_url: Discourse.base_url,