From b3cda195b8a4fc7df8808bb0b23ea4600852dce1 Mon Sep 17 00:00:00 2001 From: Gerhard Schlager Date: Wed, 13 Apr 2022 15:52:56 +0200 Subject: [PATCH] REFACTOR: Add `full_url` and `display_name` to `User` Lets stop writing the same code over and over again. --- app/controllers/list_controller.rb | 4 ++-- app/controllers/posts_controller.rb | 2 +- app/helpers/application_helper.rb | 8 +------- app/mailers/group_smtp_mailer.rb | 14 +++----------- app/mailers/user_notifications.rb | 7 +------ app/models/topic_poster.rb | 8 +------- app/models/user.rb | 12 ++++++++++++ lib/system_message.rb | 2 +- 8 files changed, 22 insertions(+), 35 deletions(-) diff --git a/app/controllers/list_controller.rb b/app/controllers/list_controller.rb index 75d3ff1318..0ec590a3ea 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 956e1d2e36..b7e0df39c5 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 0ffc2d7001..dcca394602 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 25bb887b0d..367297d4a5 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 5cca25310f..892f407b98 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 3ff6a1e09e..762734799c 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 ac0ce5c7ea..2e5bab0b85 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 d7f75541e9..27e24c398d 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,