FIX: Show same username or name for post notices. (#7862)

FIX: Show same username or name for post notices. (#7862)

diff --git a/app/assets/javascripts/discourse/components/user-card-contents.js.es6 b/app/assets/javascripts/discourse/components/user-card-contents.js.es6
index 03aa390..84e63e9 100644
--- a/app/assets/javascripts/discourse/components/user-card-contents.js.es6
+++ b/app/assets/javascripts/discourse/components/user-card-contents.js.es6
@@ -8,6 +8,7 @@ import { durationTiny } from "discourse/lib/formatter";
 import CanCheckEmails from "discourse/mixins/can-check-emails";
 import CardContentsBase from "discourse/mixins/card-contents-base";
 import CleansUp from "discourse/mixins/cleans-up";
+import { prioritizeNameInUx } from "discourse/lib/settings";
 
 export default Ember.Component.extend(
   CardContentsBase,
@@ -65,11 +66,7 @@ export default Ember.Component.extend(
 
     @computed("user.name")
     nameFirst(name) {
-      return (
-        !this.siteSettings.prioritize_username_in_ux &&
-        name &&
-        name.trim().length > 0
-      );
+      return prioritizeNameInUx(name, this.siteSettings);
     },
 
     @computed("username")
diff --git a/app/assets/javascripts/discourse/controllers/user.js.es6 b/app/assets/javascripts/discourse/controllers/user.js.es6
index 5045cf5..e59b0ed 100644
--- a/app/assets/javascripts/discourse/controllers/user.js.es6
+++ b/app/assets/javascripts/discourse/controllers/user.js.es6
@@ -2,6 +2,7 @@ import CanCheckEmails from "discourse/mixins/can-check-emails";
 import computed from "ember-addons/ember-computed-decorators";
 import User from "discourse/models/user";
 import optionalService from "discourse/lib/optional-service";
+import { prioritizeNameInUx } from "discourse/lib/settings";
 
 export default Ember.Controller.extend(CanCheckEmails, {
   indexStream: false,
@@ -87,11 +88,7 @@ export default Ember.Controller.extend(CanCheckEmails, {
 
   @computed("model.name")
   nameFirst(name) {
-    return (
-      !this.get("siteSettings.prioritize_username_in_ux") &&
-      name &&
-      name.trim().length > 0
-    );
+    return prioritizeNameInUx(name, this.siteSettings);
   },
 
   @computed("model.badge_count")
diff --git a/app/assets/javascripts/discourse/lib/settings.js.es6 b/app/assets/javascripts/discourse/lib/settings.js.es6
new file mode 100644
index 0000000..f4f6ae6
--- /dev/null
+++ b/app/assets/javascripts/discourse/lib/settings.js.es6
@@ -0,0 +1,7 @@
+export function prioritizeNameInUx(name, siteSettings) {
+  siteSettings = siteSettings || Discourse.SiteSettings;
+
+  return (
+    !siteSettings.prioritize_username_in_ux && name && name.trim().length > 0
+  );
+}
diff --git a/app/assets/javascripts/discourse/widgets/post.js.es6 b/app/assets/javascripts/discourse/widgets/post.js.es6
index 60511b3..4138fdb 100644
--- a/app/assets/javascripts/discourse/widgets/post.js.es6
+++ b/app/assets/javascripts/discourse/widgets/post.js.es6
@@ -15,6 +15,7 @@ import {
 } from "discourse/lib/utilities";
 import hbs from "discourse/widgets/hbs-compiler";
 import { durationTiny } from "discourse/lib/formatter";
+import { prioritizeNameInUx } from "discourse/lib/settings";
 
 function transformWithCallbacks(post) {
   let transformed = transformBasicPost(post);
@@ -454,9 +455,9 @@ createWidget("post-notice", {
 
   html(attrs) {
     const user =
-      this.siteSettings.prioritize_username_in_ux || !attrs.name
-        ? attrs.username
-        : attrs.name;
+      this.siteSettings.display_name_on_posts && prioritizeNameInUx(attrs.name)
+        ? attrs.name
+        : attrs.username;
     let text, icon;
     if (attrs.noticeType === "custom") {
       icon = "user-shield";
diff --git a/app/assets/javascripts/discourse/widgets/poster-name.js.es6 b/app/assets/javascripts/discourse/widgets/poster-name.js.es6
index 85c98e1..7babc63 100644
--- a/app/assets/javascripts/discourse/widgets/poster-name.js.es6
+++ b/app/assets/javascripts/discourse/widgets/poster-name.js.es6
@@ -2,6 +2,7 @@ import { iconNode } from "discourse-common/lib/icon-library";
 import { createWidget, applyDecorators } from "discourse/widgets/widget";
 import { h } from "virtual-dom";
 import { formatUsername } from "discourse/lib/utilities";
+import { prioritizeNameInUx } from "discourse/lib/settings";
 
 let sanitizeName = function(name) {
   return name.toLowerCase().replace(/[\s\._-]/g, "");
@@ -66,9 +67,7 @@ export default createWidget("poster-name", {
     const name = attrs.name;
     const nameFirst =
       this.siteSettings.display_name_on_posts &&
-      !this.siteSettings.prioritize_username_in_ux &&
-      name &&
-      name.trim().length > 0;
+      prioritizeNameInUx(name, this.siteSettings);
     const classNames = nameFirst
       ? ["first", "full-name"]
       : ["first", "username"];
diff --git a/test/javascripts/widgets/post-test.js.es6 b/test/javascripts/widgets/post-test.js.es6
index aa5d829..9489c9c 100644
--- a/test/javascripts/widgets/post-test.js.es6
+++ b/test/javascripts/widgets/post-test.js.es6
@@ -875,6 +875,7 @@ widgetTest("post notice - with username", {
   beforeEach() {
     const twoDaysAgo = new Date();
     twoDaysAgo.setDate(twoDaysAgo.getDate() - 2);
+    this.siteSettings.display_name_on_posts = false;
     this.siteSettings.prioritize_username_in_ux = true;
     this.siteSettings.old_post_notice_days = 14;
     this.set("args", {
@@ -901,6 +902,7 @@ widgetTest("post notice - with username", {
 widgetTest("post notice - with name", {
   template: '{{mount-widget widget="post" args=args}}',
   beforeEach() {
+    this.siteSettings.display_name_on_posts = true;
     this.siteSettings.prioritize_username_in_ux = false;
     this.siteSettings.old_post_notice_days = 14;
     this.set("args", {

GitHub sha: 9f500a4f