FIX: Respect "display name on posts" site setting (#239)

When displaying the accepted answer in the OP.
This commit is contained in:
Penar Musaraj 2023-06-01 11:50:50 -04:00 committed by GitHub
parent 306f39c222
commit e0cd3d11c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 3 deletions

View File

@ -57,6 +57,7 @@ function acceptPost(post) {
topic.set("accepted_answer", { topic.set("accepted_answer", {
username: post.username, username: post.username,
name: post.name,
post_number: post.post_number, post_number: post.post_number,
excerpt: post.cooked, excerpt: post.cooked,
}); });
@ -214,16 +215,22 @@ export default {
// keeping this here cause there is complex localization // keeping this here cause there is complex localization
acceptedAnswerHtml: computed("accepted_answer", "id", function () { acceptedAnswerHtml: computed("accepted_answer", "id", function () {
const username = this.get("accepted_answer.username"); const username = this.get("accepted_answer.username");
const name = this.get("accepted_answer.name");
const postNumber = this.get("accepted_answer.post_number"); const postNumber = this.get("accepted_answer.post_number");
if (!username || !postNumber) { if (!username || !postNumber) {
return ""; return "";
} }
const displayedUser =
this.siteSettings.display_name_on_posts && name
? name
: formatUsername(username);
return I18n.t("solved.accepted_html", { return I18n.t("solved.accepted_html", {
icon: iconHTML("check-square", { class: "accepted" }), icon: iconHTML("check-square", { class: "accepted" }),
username_lower: username.toLowerCase(), username_lower: username.toLowerCase(),
username: formatUsername(username), username: displayedUser,
post_path: `${this.url}/${postNumber}`, post_path: `${this.url}/${postNumber}`,
post_number: postNumber, post_number: postNumber,
user_path: User.create({ username }).path, user_path: User.create({ username }).path,

View File

@ -444,7 +444,7 @@ SQL
def accepted_answer def accepted_answer
if info = accepted_answer_post_info if info = accepted_answer_post_info
{ post_number: info[0], username: info[1], excerpt: info[2] } { post_number: info[0], username: info[1], excerpt: info[2], name: info[3] }
end end
end end
@ -454,7 +454,7 @@ SQL
Post Post
.where(id: accepted_answer_post_id, topic_id: object.topic.id) .where(id: accepted_answer_post_id, topic_id: object.topic.id)
.joins(:user) .joins(:user)
.pluck("post_number", "username", "cooked") .pluck("post_number", "username", "cooked", "name")
.first .first
if postInfo if postInfo
@ -463,6 +463,8 @@ SQL
else else
nil nil
end end
postInfo[3] = SiteSetting.display_name_on_posts ? postInfo[3] : nil
postInfo postInfo
end end
end end

View File

@ -64,6 +64,32 @@ RSpec.describe TopicsController do
expect(response.body).to include('"text":"This is a quoted text."') expect(response.body).to include('"text":"This is a quoted text."')
end end
it "should include user name in output with the corresponding site setting" do
SiteSetting.display_name_on_posts = true
p2.custom_fields["is_accepted_answer"] = true
p2.save_custom_fields
topic.custom_fields["accepted_answer_post_id"] = p2.id
topic.save_custom_fields
get "/t/#{topic.slug}/#{topic.id}.json"
expect(response.parsed_body["accepted_answer"]["name"]).to eq(p2.user.name)
expect(response.parsed_body["accepted_answer"]["username"]).to eq(p2.user.username)
end
it "should not include user name when site setting is disabled" do
SiteSetting.display_name_on_posts = false
p2.custom_fields["is_accepted_answer"] = true
p2.save_custom_fields
topic.custom_fields["accepted_answer_post_id"] = p2.id
topic.save_custom_fields
get "/t/#{topic.slug}/#{topic.id}.json"
expect(response.parsed_body["accepted_answer"]["name"]).to eq(nil)
expect(response.parsed_body["accepted_answer"]["username"]).to eq(p2.user.username)
end
end end
context "with solved enabled for topics with specific tags" do context "with solved enabled for topics with specific tags" do