Remove `Discourse` constant usage (#10470)

Remove Discourse constant usage (#10470)

  • REFACTOR: Get us closer to no Discourse constants in tests

  • REFACTOR: Remove Discourse.currentUser

  • REFACTOR: prioritizeNameInUx is really a helper and can use context

  • REFACTOR: Rename test

  • REFACTOR: Remove Discourse.MarkdownItURL and use session

  • REFACTOR: Remove unused LetterAvatarVersion

  • REFACTOR: Remove unused Discourse.ThemeSettings

  • REFACTOR: Remove unused CDN constants

  • REFACTOR: The globalNotice property doesn’t exist anymore

  • REFACTOR: Remove Discourse.__container__ from plugin api

  • REFACTOR: Consider logout() a helper and remove container.

diff --git a/app/assets/javascripts/discourse/app/components/about-page-users.js b/app/assets/javascripts/discourse/app/components/about-page-users.js
index 87d8bcf..afc0ce0 100644
--- a/app/assets/javascripts/discourse/app/components/about-page-users.js
+++ b/app/assets/javascripts/discourse/app/components/about-page-users.js
@@ -11,7 +11,7 @@ export default Component.extend({
     return (this.users || []).map(user => {
       let name = user.name;
       let username = user.username;
-      let prioritizeName = prioritizeNameInUx(name, this.siteSettings);
+      let prioritizeName = prioritizeNameInUx(name);
       let hideName = false;
       if (name && normalize(username) === normalize(name)) {
         hideName = true;
diff --git a/app/assets/javascripts/discourse/app/components/user-card-contents.js b/app/assets/javascripts/discourse/app/components/user-card-contents.js
index 3760b77..45a9884 100644
--- a/app/assets/javascripts/discourse/app/components/user-card-contents.js
+++ b/app/assets/javascripts/discourse/app/components/user-card-contents.js
@@ -68,7 +68,7 @@ export default Component.extend(CardContentsBase, CanCheckEmails, CleansUp, {
 
   @discourseComputed("user.name")
   nameFirst(name) {
-    return prioritizeNameInUx(name, this.siteSettings);
+    return prioritizeNameInUx(name);
   },
 
   @discourseComputed("user")
diff --git a/app/assets/javascripts/discourse/app/controllers/user.js b/app/assets/javascripts/discourse/app/controllers/user.js
index e0ee6c6..d20c3cc 100644
--- a/app/assets/javascripts/discourse/app/controllers/user.js
+++ b/app/assets/javascripts/discourse/app/controllers/user.js
@@ -103,7 +103,7 @@ export default Controller.extend(CanCheckEmails, {
 
   @discourseComputed("model.name")
   nameFirst(name) {
-    return prioritizeNameInUx(name, this.siteSettings);
+    return prioritizeNameInUx(name);
   },
 
   @discourseComputed("model.badge_count")
diff --git a/app/assets/javascripts/discourse/app/helpers/user-avatar.js b/app/assets/javascripts/discourse/app/helpers/user-avatar.js
index 7624ce7..0421799 100644
--- a/app/assets/javascripts/discourse/app/helpers/user-avatar.js
+++ b/app/assets/javascripts/discourse/app/helpers/user-avatar.js
@@ -1,5 +1,5 @@
 import { get } from "@ember/object";
-import { registerUnbound, helperContext } from "discourse-common/lib/helpers";
+import { registerUnbound } from "discourse-common/lib/helpers";
 import { avatarImg, formatUsername } from "discourse/lib/utilities";
 import { prioritizeNameInUx } from "discourse/lib/settings";
 import { htmlSafe } from "@ember/template";
@@ -32,7 +32,6 @@ export function classesForUser(u) {
 function renderAvatar(user, options) {
   options = options || {};
 
-  let siteSettings = helperContext().siteSettings;
   if (user) {
     const name = get(user, options.namePath || "name");
     const username = get(user, options.usernamePath || "username");
@@ -45,7 +44,7 @@ function renderAvatar(user, options) {
       return "";
     }
 
-    let displayName = prioritizeNameInUx(name, siteSettings)
+    let displayName = prioritizeNameInUx(name)
       ? name
       : formatUsername(username);
 
diff --git a/app/assets/javascripts/discourse/app/initializers/auto-load-modules.js b/app/assets/javascripts/discourse/app/initializers/auto-load-modules.js
index b1b3873..2c7c4f0 100644
--- a/app/assets/javascripts/discourse/app/initializers/auto-load-modules.js
+++ b/app/assets/javascripts/discourse/app/initializers/auto-load-modules.js
@@ -17,7 +17,8 @@ export function autoLoadModules(container, registry) {
   });
   let siteSettings = container.lookup("site-settings:main");
   let themeSettings = container.lookup("service:theme-settings");
-  createHelperContext({ siteSettings, themeSettings });
+  let keyValueStore = container.lookup("key-value-store:main");
+  createHelperContext({ siteSettings, themeSettings, keyValueStore });
   registerHelpers(registry);
   registerRawHelpers(RawHandlebars, Handlebars);
 }
diff --git a/app/assets/javascripts/discourse/app/lib/ajax.js b/app/assets/javascripts/discourse/app/lib/ajax.js
index c08a29d..64d2ab9 100644
--- a/app/assets/javascripts/discourse/app/lib/ajax.js
+++ b/app/assets/javascripts/discourse/app/lib/ajax.js
@@ -7,6 +7,7 @@ import Session from "discourse/models/session";
 import { Promise } from "rsvp";
 import Site from "discourse/models/site";
 import { isTesting } from "discourse-common/config/environment";
+import User from "discourse/models/user";
 
 let _trackView = false;
 let _transientHeader = null;
@@ -80,7 +81,7 @@ export function ajax() {
   function performAjax(resolve, reject) {
     args.headers = args.headers || {};
 
-    if (Discourse.__container__.lookup("current-user:main")) {
+    if (User.current()) {
       args.headers["Discourse-Logged-In"] = "true";
     }
 
diff --git a/app/assets/javascripts/discourse/app/lib/logout.js b/app/assets/javascripts/discourse/app/lib/logout.js
index 2d5bf99..d8aa425 100644
--- a/app/assets/javascripts/discourse/app/lib/logout.js
+++ b/app/assets/javascripts/discourse/app/lib/logout.js
@@ -1,14 +1,12 @@
 import getURL from "discourse-common/lib/get-url";
 import { isEmpty } from "@ember/utils";
 import { findAll } from "discourse/models/login-method";
+import { helperContext } from "discourse-common/lib/helpers";
 
-export default function logout(siteSettings, keyValueStore) {
-  if (!siteSettings || !keyValueStore) {
-    const container = Discourse.__container__;
-    siteSettings = siteSettings || container.lookup("site-settings:main");
-    keyValueStore = keyValueStore || container.lookup("key-value-store:main");
-  }
-
+export default function logout() {
+  const ctx = helperContext();
+  let siteSettings = ctx.siteSettings;
+  let keyValueStore = ctx.keyValueStore;
   keyValueStore.abandonLocal();
 
   const redirect = siteSettings.logout_redirect;
diff --git a/app/assets/javascripts/discourse/app/lib/plugin-api.js b/app/assets/javascripts/discourse/app/lib/plugin-api.js
index fd5a0e4..b49eb57 100644
--- a/app/assets/javascripts/discourse/app/lib/plugin-api.js
+++ b/app/assets/javascripts/discourse/app/lib/plugin-api.js
@@ -1200,11 +1200,17 @@ function cmpVersions(a, b) {
   return segmentsA.length - segmentsB.length;
 }
 
+let _container;
+
+export function setPluginContainer(container) {
+  _container = container;
+}
+
 function getPluginApi(version) {
   version = version.toString();
   if (cmpVersions(version, PLUGIN_API_VERSION) <= 0) {
     if (!_pluginv01) {
-      _pluginv01 = new PluginApi(version, Discourse.__container__);
+      _pluginv01 = new PluginApi(version, _container);
     }
 
     // We are recycling the compatible object, but let's update to the higher version
@@ -1267,4 +1273,5 @@ function decorate(klass, evt, cb, id) {
 
 export function resetPluginApi() {
   _pluginv01 = null;
+  _container = null;
 }
diff --git a/app/assets/javascripts/discourse/app/lib/settings.js b/app/assets/javascripts/discourse/app/lib/settings.js
index 2013dec..2de9fb7 100644
--- a/app/assets/javascripts/discourse/app/lib/settings.js
+++ b/app/assets/javascripts/discourse/app/lib/settings.js
@@ -1,16 +1,7 @@
-import deprecated from "discourse-common/lib/deprecated";
+import { helperContext } from "discourse-common/lib/helpers";
 
-export function prioritizeNameInUx(name, siteSettings) {
-  if (!siteSettings) {
-    deprecated(
-      "You must supply `prioritizeNameInUx` with a `siteSettings` object",
-      {
-        since: "2.6.0",
-        dropFrom: "2.7.0"
-      }
-    );
-    siteSettings = Discourse.SiteSettings;
-  }
+export function prioritizeNameInUx(name) {
+  let siteSettings = helperContext().siteSettings;
 
   return (
     !siteSettings.prioritize_username_in_ux && name && name.trim().length > 0
diff --git a/app/assets/javascripts/discourse/app/lib/text.js b/app/assets/javascripts/discourse/app/lib/text.js
index 9cf0103..097e777 100644
--- a/app/assets/javascripts/discourse/app/lib/text.js

[... diff too long, it was truncated ...]

GitHub sha: 3640c00b

1 Like

This commit appears in #10470 which was merged by eviltrout.

image

1 Like