Skip to content

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

FIX: Respect enable_names across user displays and AI outputs (#44637)

Roman Rizzifbce98a62ecc
diff --git a/app/models/user.rb b/app/models/user.rb
index 2a71ddc6685..f9927848a6f 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -2007,7 +2007,7 @@ class User < ActiveRecord::Base
end
def display_name
- if SiteSetting.prioritize_username_in_ux?
+ if !SiteSetting.enable_names? || SiteSetting.prioritize_username_in_ux?
username
else
name.presence || username
diff --git a/app/serializers/notification_serializer.rb b/app/serializers/notification_serializer.rb
index ae04d3aac35..9285e79a0c2 100644
--- a/app/serializers/notification_serializer.rb
+++ b/app/serializers/notification_serializer.rb
@@ -34,7 +34,7 @@ class NotificationSerializer < ApplicationSerializer
def data
return object.data_hash if SiteSetting.enable_names?
- object.data_hash.except(:display_name)
+ object.data_hash.except(:display_name, :name, :name2, :original_name)
end
def external_id
diff --git a/app/services/post_alerter.rb b/app/services/post_alerter.rb
index 837947bb2d7..6e9bf06108e 100644
--- a/app/services/post_alerter.rb
+++ b/app/services/post_alerter.rb
@@ -678,7 +678,7 @@ class PostAlerter
notification_data[:consolidated_count] = consolidated_count if consolidated_count
end
- if display_name = opts[:display_name] || post.user.name
+ if SiteSetting.enable_names? && (display_name = opts[:display_name] || post.user.name)
notification_data[:display_name] = display_name
end
diff --git a/app/views/email/_post.html.erb b/app/views/email/_post.html.erb
index c5a88444d4b..d615cb23746 100644
--- a/app/views/email/_post.html.erb
+++ b/app/views/email/_post.html.erb
@@ -11,8 +11,8 @@
<span class='user-name username-title'><%= post.user.name %></span>
<% end %>
<%- else %>
- <a rel='nofollow' class="username username-link" href="<%=Discourse.base_url%>/u/<%= post.user.username_lower%>" target="_blank"><%= post.user.name %></a>
- <%- if show_username_on_post(post) %>
+ <a rel='nofollow' class="username username-link" href="<%=Discourse.base_url%>/u/<%= post.user.username_lower%>" target="_blank"><%= post.user.display_name %></a>
+ <%- if SiteSetting.enable_names? && show_username_on_post(post) %>
<span class='username username-title'><%= post.user.username %></span>
<% end %>
<%- end %>
diff --git a/frontend/discourse/app/components/user-card-contents.gjs b/frontend/discourse/app/components/user-card-contents.gjs
index facc0511015..ae5364bfcc1 100644
--- a/frontend/discourse/app/components/user-card-contents.gjs
+++ b/frontend/discourse/app/components/user-card-contents.gjs
@@ -156,9 +156,13 @@ export default class UserCardContents extends CardContentsBase {
);
}
- @computed("user.name", "user.username")
+ @computed("siteSettings.enable_names", "user.name", "user.username")
get showName() {
- return this.user.name !== this.user.username;
+ return (
+ this.siteSettings.enable_names &&
+ this.user.name &&
+ this.user.name !== this.user.username
+ );
}
@computed("user.id", "currentUser.id")
@@ -203,9 +207,9 @@ export default class UserCardContents extends CardContentsBase {
return this.user?.trust_level === 0 ? "new-user" : "";
}
- @computed("user.name")
+ @computed("showName")
get nameFirst() {
- return prioritizeNameInUx(this.user?.name);
+ return this.showName && prioritizeNameInUx(this.user.name);
}
@computed("user")
@@ -578,7 +582,11 @@ export default class UserCardContents extends CardContentsBase {
(formatUsername this.user.username)
}}
</span>
- {{userStatus this.user currentUser=this.currentUser}}
+ {{userStatus
+ this.user
+ currentUser=this.currentUser
+ showName=this.showName
+ }}
</a>
{{/if}}
</div>
@@ -595,7 +603,7 @@ export default class UserCardContents extends CardContentsBase {
class="names__secondary username"
>{{this.user.username}}</div>
{{else}}
- {{#if this.user.name}}
+ {{#if this.showName}}
<div
class="names__secondary full-name"
>{{this.user.name}}</div>
diff --git a/frontend/discourse/app/controllers/user.js b/frontend/discourse/app/controllers/user.js
index 11c5f35dd31..911201ad9b4 100644
--- a/frontend/discourse/app/controllers/user.js
+++ b/frontend/discourse/app/controllers/user.js
@@ -236,9 +236,16 @@ export default class UserController extends Controller {
return this.viewingSelf || this.currentUser?.admin;
}
- @computed("model.name")
+ @computed("model.name", "siteSettings.enable_names")
+ get showName() {
+ return (
+ this.siteSettings.enable_names && this.model?.name?.trim().length > 0
+ );
+ }
+
+ @computed("showName")
get nameFirst() {
- return prioritizeNameInUx(this.model?.name);
+ return this.showName && prioritizeNameInUx(this.model.name);
}
@computed("model.badge_count")
diff --git a/frontend/discourse/app/helpers/user-status.js b/frontend/discourse/app/helpers/user-status.js
index 3f8ec7f6d3f..ee79e9f4e2a 100644
--- a/frontend/discourse/app/helpers/user-status.js
+++ b/frontend/discourse/app/helpers/user-status.js
@@ -3,12 +3,12 @@ import { iconHTML } from "discourse/lib/icon-library";
import { escapeExpression } from "discourse/lib/utilities";
import { i18n } from "discourse-i18n";
-export default function userStatus(user, { currentUser } = {}) {
+export default function userStatus(user, { currentUser, showName } = {}) {
if (!user) {
return;
}
- const name = escapeExpression(user.name);
+ const name = escapeExpression(showName ? user.name : user.username);
if (user.admin && currentUser?.staff) {
return trustHTML(
diff --git a/frontend/discourse/app/lib/settings.js b/frontend/discourse/app/lib/settings.js
index c153e23baf5..ffd37ed81f7 100644
--- a/frontend/discourse/app/lib/settings.js
+++ b/frontend/discourse/app/lib/settings.js
@@ -4,13 +4,17 @@ export function prioritizeNameInUx(name) {
let siteSettings = helperContext().siteSettings;
return (
- !siteSettings.prioritize_username_in_ux && name && name.trim().length > 0
+ siteSettings.enable_names &&
+ !siteSettings.prioritize_username_in_ux &&
+ name &&
+ name.trim().length > 0
);
}
export function prioritizeNameFallback(name, username) {
let siteSettings = helperContext().siteSettings;
if (
+ siteSettings.enable_names &&
siteSettings.display_name_on_posts &&
!siteSettings.prioritize_username_in_ux
) {
diff --git a/frontend/discourse/app/templates/user.gjs b/frontend/discourse/app/templates/user.gjs
index 76e7d382113..e5e99f8ef86 100644
--- a/frontend/discourse/app/templates/user.gjs
+++ b/frontend/discourse/app/templates/user.gjs
@@ -206,17 +206,20 @@ export default <template>
{{userStatus
@controller.model
}}
{{#if @controller.model.status}}
<DUserStatusMessage @status={{@controller.model.status}} />
{{/if}}
</div>
- <div
- class="{{if @controller.nameFirst 'username' 'full-name'}}
- user-profile-names__secondary"
- >{{#if
- @controller.nameFirst
- }}{{@controller.model.username}}{{else}}{{@controller.model.name}}{{/if}}</div>
+ {{#if @controller.showName}}
+ <div
+ class="{{if @controller.nameFirst 'username' 'full-name'}}
+ user-profile-names__secondary"
+ >{{#if
+ @controller.nameFirst
+ }}{{@controller.model.username}}{{else}}{{@controller.model.name}}{{/if}}</div>
+ {{/if}}
{{#if @controller.model.staged}}
<div class="staged user-profile-names__secondary">{{i18n
"user.staged"
diff --git a/frontend/discourse/app/ui-kit/d-user-info.gjs b/frontend/discourse/app/ui-kit/d-user-info.gjs
index 5c9462f26ef..3d736013d07 100644
--- a/frontend/discourse/app/ui-kit/d-user-info.gjs
+++ b/frontend/discourse/app/ui-kit/d-user-info.gjs
@@ -33,9 +33,14 @@ export default class DUserInfo extends Component {
return userPath(this.user?.username);
}
- @computed("user.name")
+ @computed("siteSettings.enable_names", "user.name")
+ get showName() {
+ return this.siteSettings.enable_names && this.user?.name?.trim().length > 0;
+ }
+
+ @computed("showName")
get nameFirst() {
- return prioritizeNameInUx(this.user?.name);
+ return this.showName && prioritizeNameInUx(this.user.name);
}
didInsertElement() {
@@ -95,7 +100,7 @@ export default class DUserInfo extends Component {
/>
{{/if}}
</span>
- {{#if @user.name}}
+ {{#if this.showName}}
<span class="name-wrapper">
{{#if this.includeLink}}
<a data-user-card={{@user.username}} href={{this.userPath}}>
diff --git a/frontend/discourse/tests/acceptance/user-card-test.js b/frontend/discourse/tests/acceptance/user-card-test.js
index 43c5c48b331..0c8616db4eb 100644
--- a/frontend/discourse/tests/acceptance/user-card-test.js
+++ b/frontend/discourse/tests/acceptance/user-card-test.js
@@ -158,6 +158,24 @@ acceptance(
}
);
+acceptance("User Card - names disabled", function (needs) {
+ needs.user();
+ needs.settings({ enable_names: false, prioritize_username_in_ux: false });
+ needs.pretender((server, helper) => {
+ const cardResponse = cloneJSON(userFixtures["/u/eviltrout/card.json"]);
+ server.get("/u/eviltrout/card.json", () => helper.response(cardResponse));
+ });
+
+ test("does not display a name present in the card payload", async function (assert) {
+ await visit("/t/this-is-a-test-topic/9");
+ await click('a[data-user-card="eviltrout"]');
+
+ assert.dom(".user-card .username").includesText("eviltrout");
+ assert.dom(".user-card .full-name").doesNotExist();
+ assert.dom(".user-card").doesNotIncludeText("Robin Ward");
+ });
+});
+
acceptance("User Card - User Status", function (needs) {
needs.user();
needs.pretender((server, helper) => {
diff --git a/frontend/discourse/tests/acceptance/user-test.js b/frontend/discourse/tests/acceptance/user-test.js
index 40f351c430a..d93cc1548de 100644
--- a/frontend/discourse/tests/acceptance/user-test.js
+++ b/frontend/discourse/tests/acceptance/user-test.js
@@ -132,6 +132,23 @@ acceptance("User Routes", function (needs) {
});
});
+acceptance("User Routes - names disabled", function (needs) {
+ needs.user();
+ needs.settings({ enable_names: false, prioritize_username_in_ux: false });
+ needs.pretender((server, helper) => {
+ const response = cloneJSON(userFixtures["/u/eviltrout.json"]);
+ server.get("/u/eviltrout.json", () => helper.response(response));
+ });
+
+ test("does not display a name present in the profile payload", async function (assert) {
+ await visit("/u/eviltrout");
+
+ assert.dom(".user-profile-names .username").includesText("eviltrout");
+ assert.dom(".user-profile-names .full-name").doesNotExist();
+ assert.dom(".user-profile-names").doesNotIncludeText("Robin Ward");
+ });
+});
+
acceptance(
"User Routes - Periods in current user's username",
function (needs) {
diff --git a/frontend/discourse/tests/integration/ui-kit/d-user-info-test.gjs b/frontend/discourse/tests/integration/ui-kit/d-user-info-test.gjs
index e78579e9c35..ef42bc4f1d3 100644
--- a/frontend/discourse/tests/integration/ui-kit/d-user-info-test.gjs
+++ b/frontend/discourse/tests/integration/ui-kit/d-user-info-test.gjs
@@ -27,6 +27,17 @@ module("Integration | ui-kit | DUserInfo", function (hooks) {
assert.dom(".name").hasText("Evil Trout");
});
+ test("names disabled", async function (assert) {
+ this.siteSettings.enable_names = false;
+ this.siteSettings.prioritize_username_in_ux = false;
+ this.currentUser.name = "Evil Trout";
+
+ await render(<template><DUserInfo @user={{this.currentUser}} /></template>);
+
+ assert.dom(".username").hasText("eviltrout");
+ assert.dom(".name").doesNotExist();
+ });
+
test("includeLink", async function (assert) {
await render(
<template>
diff --git a/plugins/chat/app/queries/chat/thread_participant_query.rb b/plugins/chat/app/queries/chat/thread_participant_query.rb
index 82381f9c284..89b8341f6fa 100644
--- a/plugins/chat/app/queries/chat/thread_participant_query.rb
+++ b/plugins/chat/app/queries/chat/thread_participant_query.rb
@@ -57,12 +57,7 @@ module Chat
SQL
most_recent_participants =
most_recent_participants.reduce({}) do |hash, mrm|
- hash[mrm.thread_id] = {
- id: mrm.user_id,
- username: mrm.username,
- name: mrm.name,
- uploaded_avatar_id: mrm.uploaded_avatar_id,
- }
+ hash[mrm.thread_id] = user_data(mrm)
hash
end
@@ -77,14 +72,7 @@ module Chat
# can just increase the number here.
if thread_participants[thread_id][:users].length < (MAX_PARTICIPANTS - 1) &&
thread_participant_stat.user_id != most_recent_participants[thread_id][:id]
- thread_participants[thread_id][:users].push(
- {
- id: thread_participant_stat.user_id,
- username: thread_participant_stat.username,
- name: thread_participant_stat.name,
- uploaded_avatar_id: thread_participant_stat.uploaded_avatar_id,
- },
- )
+ thread_participants[thread_id][:users].push(user_data(thread_participant_stat))
end
thread_participants[thread_id][:total_count] += 1
@@ -97,5 +85,17 @@ module Chat
thread_participants
end
+
+ def self.user_data(row)
+ result = {
+ id: row.user_id,
+ username: row.username,
+ uploaded_avatar_id: row.uploaded_avatar_id,
+ }
+ result[:name] = row.name if SiteSetting.enable_names?
+ result
+ end
+
+ private_class_method :user_data
end
end
diff --git a/plugins/chat/assets/javascripts/discourse/components/chat-user-display-name.gjs b/plugins/chat/assets/javascripts/discourse/components/chat-user-display-name.gjs
index 44809aedb44..ab16e598901 100644
--- a/plugins/chat/assets/javascripts/discourse/components/chat-user-display-name.gjs
+++ b/plugins/chat/assets/javascripts/discourse/components/chat-user-display-name.gjs
@@ -11,7 +11,11 @@ export default class ChatUserDisplayName extends Component {
}
get hasValidName() {
- return this.args.user?.name && this.args.user.name.trim().length > 0;
+ return (
+ this.siteSettings.enable_names &&
+ this.args.user?.name &&
+ this.args.user.name.trim().length > 0
+ );
}
get formattedUsername() {
diff --git a/plugins/chat/spec/queries/chat/thread_participant_query_spec.rb b/plugins/chat/spec/queries/chat/thread_participant_query_spec.rb
index 7b996d611af..6bf67b0dbbd 100644
--- a/plugins/chat/spec/queries/chat/thread_participant_query_spec.rb
+++ b/plugins/chat/spec/queries/chat/thread_participant_query_spec.rb
@@ -35,6 +35,14 @@ RSpec.describe Chat::ThreadParticipantQuery do
)
end
+ it "omits names when names are disabled" do
+ SiteSetting.enable_names = false
+
+ result = described_class.call(thread_ids: [thread_1.id])
+
+ expect(result[thread_1.id][:users]).to all(exclude(:name))
+ end
+
it "returns up to 10 thread participants" do
result = described_class.call(thread_ids: [thread_1.id])
expect(result[thread_1.id][:users].length).to eq(4)
diff --git a/plugins/chat/test/javascripts/components/chat-user-display-name-test.gjs b/plugins/chat/test/javascripts/components/chat-user-display-name-test.gjs
index ecd4eb7722e..76a2310e11d 100644
--- a/plugins/chat/test/javascripts/components/chat-user-display-name-test.gjs
+++ b/plugins/chat/test/javascripts/components/chat-user-display-name-test.gjs
@@ -32,6 +32,22 @@ module(
}
);
+module("Component | ChatUserDisplayName | names disabled", function (hooks) {
+ setupRenderingTest(hooks);
+
+ test("does not display a user's name", async function (assert) {
+ this.siteSettings.enable_names = false;
+ this.siteSettings.prioritize_username_in_ux = false;
+ this.set("user", { username: "bob", name: "Bobcat" });
+
+ await render(
+ <template><ChatUserDisplayName @user={{this.user}} /></template>
+ );
+
+ assert.dom(".chat-user-display-name").hasText("bob");
+ });
+});
+
module(
"Component | ChatUserDisplayName | prioritize name in UX",
function (hooks) {
diff --git a/plugins/discourse-ai/app/jobs/regular/stream_discover_reply.rb b/plugins/discourse-ai/app/jobs/regular/stream_discover_reply.rb
index af65934966b..9fb71d949de 100644
--- a/plugins/discourse-ai/app/jobs/regular/stream_discover_reply.rb
+++ b/plugins/discourse-ai/app/jobs/regular/stream_discover_reply.rb
@@ -437,7 +437,7 @@ module Jobs
category_id: source["category_id"],
topic_replies: source["topic_replies"].to_i,
username: source["username"],
- name: source["name"],
+ name: SiteSetting.enable_names? ? source["name"] : nil,
avatar_template: source["avatar_template"],
}
end
diff --git a/plugins/discourse-ai/lib/agents/tools/read.rb b/plugins/discourse-ai/lib/agents/tools/read.rb
index 24bcde52d5d..d914b897a54 100644
--- a/plugins/discourse-ai/lib/agents/tools/read.rb
+++ b/plugins/discourse-ai/lib/agents/tools/read.rb
@@ -84,7 +84,13 @@ module DiscourseAi
end
posts.each do |post|
- content << "\n\n#{post.user&.name}(#{post.username}) said:\n\n#{post.raw}"
+ author =
+ if SiteSetting.enable_names?
+ "#{post.user&.name}(#{post.username})"
+ else
+ post.username
+ end
+ content << "\n\n#{author} said:\n\n#{post.raw}"
end
truncated_content =
diff --git a/plugins/discourse-ai/lib/agents/user_finder.rb b/plugins/discourse-ai/lib/agents/user_finder.rb
index cf3621fd9df..ae71f47a4dd 100644
--- a/plugins/discourse-ai/lib/agents/user_finder.rb
+++ b/plugins/discourse-ai/lib/agents/user_finder.rb
@@ -97,10 +97,14 @@ module DiscourseAi
return users if query.blank?
query_like = "%#{ActiveRecord::Base.sanitize_sql_like(query.downcase)}%"
- users.where(
- "users.username_lower LIKE :query OR users.name ILIKE :query",
- query: query_like,
- )
+ if SiteSetting.enable_names?
+ users.where(
+ "users.username_lower LIKE :query OR users.name ILIKE :query",
+ query: query_like,
+ )
+ else
+ users.where("users.username_lower LIKE :query", query: query_like)
+ end
end
def filter_by_status(users)
@@ -160,15 +164,16 @@ module DiscourseAi
end
def serialize_user(user, real:)
- {
+ result = {
id: user.id,
username: user.username,
- name: user.name,
active: user.active,
staged: user.staged,
suspended: user.suspended?,
real: real,
}
+ result[:name] = user.name if SiteSetting.enable_names?
+ result
end
end
end
diff --git a/plugins/discourse-ai/lib/completions/prompt_messages_builder.rb b/plugins/discourse-ai/lib/completions/prompt_messages_builder.rb
index c70fc4b944b..c8cb6b2457a 100644
--- a/plugins/discourse-ai/lib/completions/prompt_messages_builder.rb
+++ b/plugins/discourse-ai/lib/completions/prompt_messages_builder.rb
@@ -1040,7 +1040,8 @@ module DiscourseAi
info << "Trust level #{user.trust_level}" if user.trust_level > 0
info << "#{account_age(user)}"
info << "#{user.user_stat.post_count} posts" if user.user_stat.post_count.to_i > 0
- "#{user.username} (#{user.name}): #{info.compact.join(", ")}"
+ author = SiteSetting.enable_names? ? "#{user.username} (#{user.name})" : user.username
+ "#{author}: #{info.compact.join(", ")}"
end
def format_timestamp(timestamp)
diff --git a/plugins/discourse-ai/lib/discoveries/retrieval.rb b/plugins/discourse-ai/lib/discoveries/retrieval.rb
index 38e95f4a154..93a0d817daf 100644
--- a/plugins/discourse-ai/lib/discoveries/retrieval.rb
+++ b/plugins/discourse-ai/lib/discoveries/retrieval.rb
@@ -296,7 +296,7 @@ module DiscourseAi
search_metadata,
"category_id" => topic.category_id,
"username" => post.user&.username,
- "name" => post.user&.name,
+ "name" => SiteSetting.enable_names? ? post.user&.name : nil,
"avatar_template" => post.user&.avatar_template,
"author_is_staff" => post.user&.staff? || false,
"is_topic_op" => post.post_number == 1,
diff --git a/plugins/discourse-ai/spec/jobs/regular/stream_discover_reply_spec.rb b/plugins/discourse-ai/spec/jobs/regular/stream_discover_reply_spec.rb
index 2f45e65f8ee..8d3e5a68626 100644
--- a/plugins/discourse-ai/spec/jobs/regular/stream_discover_reply_spec.rb
+++ b/plugins/discourse-ai/spec/jobs/regular/stream_discover_reply_spec.rb
@@ -166,6 +166,19 @@ describe Jobs::StreamDiscoverReply do
)
end
+ it "publishes sources with usernames only when names are disabled" do
+ SiteSetting.enable_names = false
+
+ messages =
+ MessageBus.track_publish("/discourse-ai/discoveries") do
+ job.execute(user_id: user.id, query:, request_id:)
+ end
+ sources = messages.flat_map { |message| message.data[:sources] || [] }
+
+ expect(sources).to be_present
+ expect(sources).to all(include(username: source_post.user.username, name: nil))
+ end
+
it "publishes the complete candidate topic list independently of selected sources" do
other_post = Fabricate(:post)
other_candidate =
diff --git a/plugins/discourse-ai/spec/lib/agents/tools/list_users_spec.rb b/plugins/discourse-ai/spec/lib/agents/tools/list_users_spec.rb
index fe25a23657e..0a6e90e781c 100644
--- a/plugins/discourse-ai/spec/lib/agents/tools/list_users_spec.rb
+++ b/plugins/discourse-ai/spec/lib/agents/tools/list_users_spec.rb
@@ -57,6 +57,28 @@ RSpec.describe DiscourseAi::Agents::Tools::ListUsers do
)
end
+ it "does not search or return names when names are disabled" do
+ SiteSetting.enable_names = false
+ user = Fabricate(:user, username: "visible_handle", name: "Hidden Person")
+
+ name_result =
+ described_class.new(
+ { query: "Hidden Person", limit: 100 },
+ bot_user: bot_user,
+ llm: llm,
+ ).invoke
+ username_result =
+ described_class.new(
+ { query: "visible_handle", limit: 100 },
+ bot_user: bot_user,
+ llm: llm,
+ ).invoke
+
+ expect(usernames_from(name_result)).not_to include(user.username)
+ expect(usernames_from(username_result)).to include(user.username)
+ expect(username_result[:column_names]).not_to include("name")
+ end
+
it "filters users by account status when requested" do
eligible_user = Fabricate(:user, username: "filter_alice")
inactive_user = Fabricate(:inactive_user, username: "filter_inactive")
diff --git a/plugins/discourse-ai/spec/lib/agents/tools/read_spec.rb b/plugins/discourse-ai/spec/lib/agents/tools/read_spec.rb
index 908e28e9118..c11102c5fa2 100644
--- a/plugins/discourse-ai/spec/lib/agents/tools/read_spec.rb
+++ b/plugins/discourse-ai/spec/lib/agents/tools/read_spec.rb
@@ -34,6 +34,16 @@ RSpec.describe DiscourseAi::Agents::Tools::Read do
end
describe "#process" do
+ it "uses usernames for authors when names are disabled" do
+ SiteSetting.enable_names = false
+ post1.user.update!(name: "Hidden Author")
+
+ content = tool.invoke[:content]
+
+ expect(content).to include("#{post1.username} said:")
+ expect(content).not_to include(post1.user.name)
+ end
+
it "can read private topics if allowed to" do
category = topic_with_tags.category
category.set_permissions(Group::AUTO_GROUPS[:staff] => :full)
diff --git a/plugins/discourse-ai/spec/lib/completions/prompt_messages_builder_spec.rb b/plugins/discourse-ai/spec/lib/completions/prompt_messages_builder_spec.rb
index 9f7abcf6c78..d68ff30edef 100644
--- a/plugins/discourse-ai/spec/lib/completions/prompt_messages_builder_spec.rb
+++ b/plugins/discourse-ai/spec/lib/completions/prompt_messages_builder_spec.rb
@@ -260,6 +260,17 @@ describe DiscourseAi::Completions::PromptMessagesBuilder do
expect(builder.to_a).to eq(expected)
end
+ it "uses usernames in topic user information when names are disabled" do
+ SiteSetting.enable_names = false
+ user.update!(name: "Hidden Participant")
+ builder.push(type: :user, content: "How do I solve this?", id: user.username)
+
+ content = builder.to_a(style: :topic).first[:content]
+
+ expect(content).to include("User information:", "#{user.username}:")
+ expect(content).not_to include(user.name)
+ end
+
it "formats messages for topic style" do
# Create a topic with tags
topic = Fabricate(:topic, title: "This is an Example Topic")
diff --git a/plugins/discourse-ai/spec/lib/discoveries/retrieval_spec.rb b/plugins/discourse-ai/spec/lib/discoveries/retrieval_spec.rb
index abe941a5cb6..d6b972df063 100644
--- a/plugins/discourse-ai/spec/lib/discoveries/retrieval_spec.rb
+++ b/plugins/discourse-ai/spec/lib/discoveries/retrieval_spec.rb
@@ -34,6 +34,23 @@ describe DiscourseAi::Discoveries::Retrieval do
end
describe "#call" do
+ it "clears names from candidates when names are disabled" do
+ SiteSetting.enable_names = false
+ post_1.user.update!(name: "Hidden Author")
+ retrieval =
+ described_class.new(
+ user:,
+ lexical_retriever: ->(_query) { [source(post_1).merge("name" => post_1.user.name)] },
+ semantic_retriever: ->(_query) { [] },
+ )
+
+ result = retrieval.call("useful")
+
+ expect(result.candidates).to contain_exactly(
+ include("username" => post_1.user.username, "name" => nil),
+ )
+ end
+
it "uses semantic retrieval with embeddings configured even when full-page semantic search is disabled" do
SiteSetting.ai_embeddings_semantic_search_enabled = false
diff --git a/plugins/discourse-assign/lib/discourse_assign/helpers.rb b/plugins/discourse-assign/lib/discourse_assign/helpers.rb
index 573955e4c35..adb9f373291 100644
--- a/plugins/discourse-assign/lib/discourse_assign/helpers.rb
+++ b/plugins/discourse-assign/lib/discourse_assign/helpers.rb
@@ -4,7 +4,9 @@ module DiscourseAssign
module Helpers
def self.build_assigned_to_user(user, topic)
return if !user
- { username: user.username, name: user.name, avatar_template: user.avatar_template }
+ result = { username: user.username, avatar_template: user.avatar_template }
+ result[:name] = user.name if SiteSetting.enable_names?
+ result
end
def self.build_assigned_to_group(group, topic)
diff --git a/plugins/discourse-assign/spec/serializers/topic_view_serializer_spec.rb b/plugins/discourse-assign/spec/serializers/topic_view_serializer_spec.rb
index d966b0fd573..888a4d1a622 100644
--- a/plugins/discourse-assign/spec/serializers/topic_view_serializer_spec.rb
+++ b/plugins/discourse-assign/spec/serializers/topic_view_serializer_spec.rb
@@ -21,6 +21,19 @@ RSpec.describe TopicViewSerializer do
expect(serializer.as_json[:topic_view][:assigned_to_group]).to be nil
end
+ it "omits the assigned user's name when names are disabled" do
+ SiteSetting.enable_names = false
+ Assigner.new(topic, user).assign(user)
+
+ assigned_user =
+ TopicViewSerializer.new(TopicView.new(topic), scope: guardian).as_json[:topic_view][
+ :assigned_to_user
+ ]
+
+ expect(assigned_user[:username]).to eq(user.username)
+ expect(assigned_user).not_to have_key(:name)
+ end
+
it "includes assigned group in serializer" do
Assigner.new(topic, user).assign(assign_allowed_group)
serializer = TopicViewSerializer.new(TopicView.new(topic), scope: guardian)
diff --git a/spec/integration/email_style_spec.rb b/spec/integration/email_style_spec.rb
index 3f753bfc2e7..cb162e0a51f 100644
--- a/spec/integration/email_style_spec.rb
+++ b/spec/integration/email_style_spec.rb
@@ -77,6 +77,14 @@ RSpec.describe EmailStyle do
expect(matches[1]).to include("padding-top:5px") # div.body
end
+ it "does not include the author's name when names are disabled" do
+ SiteSetting.enable_names = false
+ SiteSetting.prioritize_username_in_ux = false
+
+ expect(mail_html).to include(response_by_user.username)
+ expect(mail_html).not_to include(response_by_user.name)
+ end
+
# TODO: translation override
end
diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb
index 5fe908bf50c..6d56910cfad 100644
--- a/spec/models/user_spec.rb
+++ b/spec/models/user_spec.rb
@@ -241,6 +241,22 @@ RSpec.describe User do
) { user.update(name: "Batman") }
end
end
+
+ describe "#display_name" do
+ it "uses the name when names are enabled and names are prioritized" do
+ SiteSetting.enable_names = true
+ SiteSetting.prioritize_username_in_ux = false
+
+ expect(user.display_name).to eq(user.name)
+ end
+
+ it "uses the username when names are disabled" do
+ SiteSetting.enable_names = false
+ SiteSetting.prioritize_username_in_ux = false
+
+ expect(user.display_name).to eq(user.username)
+ end
+ end
end
describe "Validations" do
diff --git a/spec/requests/notifications_controller_spec.rb b/spec/requests/notifications_controller_spec.rb
index 9a2cb54998a..5a9c13a510e 100644
--- a/spec/requests/notifications_controller_spec.rb
+++ b/spec/requests/notifications_controller_spec.rb
@@ -545,7 +545,7 @@ RSpec.describe NotificationsController do
notification_type: Notification.types[:mentioned],
topic: post.topic,
)
- expect(mention_notification.data_hash[:display_name]).to eq(mentioner.name)
+ expect(mention_notification.data_hash).not_to have_key(:display_name)
get "/notifications.json"
diff --git a/spec/serializers/notification_serializer_spec.rb b/spec/serializers/notification_serializer_spec.rb
index 3543bddd6f7..5d15d4d04ef 100644
--- a/spec/serializers/notification_serializer_spec.rb
+++ b/spec/serializers/notification_serializer_spec.rb
@@ -14,6 +14,21 @@ RSpec.describe NotificationSerializer do
it "does not include external_id when sso is disabled" do
expect(json[:notification].key?(:external_id)).to eq(false)
end
+
+ it "removes name fields from notification data when names are disabled" do
+ SiteSetting.enable_names = false
+ notification.update!(
+ data: {
+ display_name: "Display Name",
+ name: "Name",
+ name2: "Second Name",
+ original_name: "Original Name",
+ safe_key: "safe value",
+ }.to_json,
+ )
+
+ expect(json[:notification][:data]).to eq("safe_key" => "safe value")
+ end
end
describe "#sso_enabled" do
diff --git a/spec/services/post_alerter_spec.rb b/spec/services/post_alerter_spec.rb
index e48b13ee0b1..90d32e7fd75 100644
--- a/spec/services/post_alerter_spec.rb
+++ b/spec/services/post_alerter_spec.rb
@@ -1281,6 +1281,14 @@ RSpec.describe PostAlerter do
DiscoursePluginRegistry.clear_modifiers!
end
+ it "does not add a display name when names are disabled" do
+ SiteSetting.enable_names = false
+
+ notification = PostAlerter.new.create_notification(user, type, post)
+
+ expect(notification.data_hash).not_to have_key(:display_name)
+ end
+
it "applies modifiers to notification_data" do
Plugin::Instance
.new