REFACTOR: Add full_url and display_name to User

Lets stop writing the same code over and over again.
This commit is contained in:
Gerhard Schlager 2022-04-13 15:52:56 +02:00 committed by Gerhard Schlager
parent 650adbe423
commit b3cda195b8
8 changed files with 22 additions and 35 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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
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

View File

@ -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(", ")

View File

@ -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

View File

@ -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

View File

@ -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,