From 3640c00b03e6b7acffa6429a066181e2435a3a16 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Wed, 19 Aug 2020 08:51:48 -0400 Subject: [PATCH] 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. --- .../app/components/about-page-users.js | 2 +- .../app/components/user-card-contents.js | 2 +- .../discourse/app/controllers/user.js | 2 +- .../discourse/app/helpers/user-avatar.js | 5 +-- .../app/initializers/auto-load-modules.js | 3 +- .../javascripts/discourse/app/lib/ajax.js | 3 +- .../javascripts/discourse/app/lib/logout.js | 12 +++--- .../discourse/app/lib/plugin-api.js | 9 ++++- .../javascripts/discourse/app/lib/settings.js | 15 ++----- .../javascripts/discourse/app/lib/text.js | 22 ++++++---- .../discourse/app/models/bookmark.js | 2 +- .../discourse/app/models/composer.js | 1 - .../javascripts/discourse/app/models/user.js | 5 --- .../pre-initializers/discourse-bootstrap.js | 40 +++++++------------ .../discourse/app/routes/application.js | 4 +- .../javascripts/discourse/app/widgets/post.js | 3 +- .../discourse/app/widgets/poster-name.js | 3 +- test/javascripts/helpers/qunit-helpers.js | 3 +- test/javascripts/models/email-log-test.js | 2 +- test/javascripts/test_helper.js | 11 ++--- 20 files changed, 67 insertions(+), 82 deletions(-) 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 87d8bcfd567..afc0ce04edc 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 3760b773a5e..45a9884f4d3 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 e0ee6c61a23..d20c3cc69b7 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 7624ce73b60..0421799eceb 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 b1b38738e76..2c7c4f00638 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 c08a29d87ba..64d2ab923f0 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 2d5bf9966b1..d8aa425e8ec 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 fd5a0e4c578..b49eb573f4e 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 2013dec079a..2de9fb7f352 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 9cf0103a8c3..097e777fc05 100644 --- a/app/assets/javascripts/discourse/app/lib/text.js +++ b/app/assets/javascripts/discourse/app/lib/text.js @@ -8,6 +8,7 @@ import { formatUsername } from "discourse/lib/utilities"; import { Promise } from "rsvp"; import { htmlSafe } from "@ember/template"; import { helperContext } from "discourse-common/lib/helpers"; +import Session from "discourse/models/session"; function getOpts(opts) { const siteSettings = Discourse.__container__.lookup("site-settings:main"), @@ -59,14 +60,19 @@ export function sanitizeAsync(text, options) { } function loadMarkdownIt() { - if (Discourse.MarkdownItURL) { - return loadScript(Discourse.MarkdownItURL).catch(e => { - // eslint-disable-next-line no-console - console.error(e); - }); - } else { - return Promise.resolve(); - } + return new Promise(resolve => { + let markdownItURL = Session.currentProp("markdownItURL"); + if (markdownItURL) { + loadScript(markdownItURL) + .then(() => resolve()) + .catch(e => { + // eslint-disable-next-line no-console + console.error(e); + }); + } else { + resolve(); + } + }); } function createPrettyText(options) { diff --git a/app/assets/javascripts/discourse/app/models/bookmark.js b/app/assets/javascripts/discourse/app/models/bookmark.js index 51da517b8c4..a704e55a24d 100644 --- a/app/assets/javascripts/discourse/app/models/bookmark.js +++ b/app/assets/javascripts/discourse/app/models/bookmark.js @@ -165,7 +165,7 @@ const Bookmark = RestModel.extend({ Bookmark.reopenClass({ create(args) { args = args || {}; - args.currentUser = args.currentUser || Discourse.currentUser; + args.currentUser = args.currentUser || User.current(); return this._super(args); } }); diff --git a/app/assets/javascripts/discourse/app/models/composer.js b/app/assets/javascripts/discourse/app/models/composer.js index 571cfe268c0..99dc32bf148 100644 --- a/app/assets/javascripts/discourse/app/models/composer.js +++ b/app/assets/javascripts/discourse/app/models/composer.js @@ -1045,7 +1045,6 @@ const Composer = RestModel.extend({ x => x.id === (parseInt(createdPost.category, 10) || 1) ); if (category) category.incrementProperty("topic_count"); - Discourse.notifyPropertyChange("globalNotice"); } composer.clearState(); diff --git a/app/assets/javascripts/discourse/app/models/user.js b/app/assets/javascripts/discourse/app/models/user.js index 6013cae781b..a30d248e3c5 100644 --- a/app/assets/javascripts/discourse/app/models/user.js +++ b/app/assets/javascripts/discourse/app/models/user.js @@ -973,11 +973,6 @@ User.reopenClass(Singleton, { return null; }, - resetCurrent(user) { - this._super(user); - Discourse.currentUser = user; - }, - checkUsername(username, email, for_user_id) { return ajax(userPath("check_username"), { data: { username, email, for_user_id } diff --git a/app/assets/javascripts/discourse/app/pre-initializers/discourse-bootstrap.js b/app/assets/javascripts/discourse/app/pre-initializers/discourse-bootstrap.js index b4167a08675..f18a630e109 100644 --- a/app/assets/javascripts/discourse/app/pre-initializers/discourse-bootstrap.js +++ b/app/assets/javascripts/discourse/app/pre-initializers/discourse-bootstrap.js @@ -11,12 +11,15 @@ import { import { setupURL, setupS3CDN } from "discourse-common/lib/get-url"; import deprecated from "discourse-common/lib/deprecated"; import { setIconList } from "discourse-common/lib/icon-library"; +import { setPluginContainer } from "discourse/lib/plugin-api"; export default { name: "discourse-bootstrap", // The very first initializer to run initialize(container, app) { + setPluginContainer(container); + // Our test environment has its own bootstrap code if (isTesting()) { return; @@ -38,8 +41,6 @@ export default { }); } - app.CDN = setupData.cdn; - let baseUrl = setupData.baseUrl; Object.defineProperty(app, "BaseUrl", { get() { @@ -63,51 +64,40 @@ export default { setupURL(setupData.cdn, baseUrl, setupData.baseUri); setEnvironment(setupData.environment); app.SiteSettings = PreloadStore.get("siteSettings"); - app.ThemeSettings = PreloadStore.get("themeSettings"); - app.LetterAvatarVersion = setupData.letterAvatarVersion; - app.MarkdownItURL = setupData.markdownItUrl; I18n.defaultLocale = setupData.defaultLocale; window.Logster = window.Logster || {}; window.Logster.enabled = setupData.enableJsErrorReporting === "true"; - Session.currentProp("serviceWorkerURL", setupData.serviceWorkerUrl); - Session.currentProp("assetVersion", setupData.assetVersion); - - Session.currentProp( - "disableCustomCSS", - setupData.disableCustomCss === "true" - ); + let session = Session.current(); + session.serviceWorkerURL = setupData.serviceWorkerUrl; + session.assetVersion = setupData.assetVersion; + session.disableCustomCSS = setupData.disableCustomCss === "true"; + session.markdownItURL = setupData.markdownItUrl; if (setupData.safeMode) { - Session.currentProp("safe_mode", setupData.safeMode); + session.safe_mode = setupData.safeMode; } - Session.currentProp( - "darkModeAvailable", + session.darkModeAvailable = document.head.querySelectorAll( 'link[media="(prefers-color-scheme: dark)"]' - ).length > 0 - ); + ).length > 0; - Session.currentProp( - "defaultColorSchemeIsDark", + session.darkColorScheme = !window.matchMedia("(prefers-color-scheme: dark)").matches && getComputedStyle(document.documentElement) .getPropertyValue("--scheme-type") - .trim() === "dark" - ); + .trim() === "dark"; - Session.currentProp("highlightJsPath", setupData.highlightJsPath); - Session.currentProp("svgSpritePath", setupData.svgSpritePath); + session.highlightJsPath = setupData.highlightJsPath; + session.svgSpritePath = setupData.svgSpritePath; if (isDevelopment()) { setIconList(setupData.svgIconList); } if (setupData.s3BaseUrl) { - app.S3CDN = setupData.s3Cdn; - app.S3BaseUrl = setupData.s3BaseUrl; setupS3CDN(setupData.s3BaseUrl, setupData.s3Cdn); } diff --git a/app/assets/javascripts/discourse/app/routes/application.js b/app/assets/javascripts/discourse/app/routes/application.js index b31d29b0e40..816b356b06a 100644 --- a/app/assets/javascripts/discourse/app/routes/application.js +++ b/app/assets/javascripts/discourse/app/routes/application.js @@ -283,9 +283,7 @@ const ApplicationRoute = DiscourseRoute.extend(OpenComposer, { _handleLogout() { if (this.currentUser) { - this.currentUser - .destroySession() - .then(() => logout(this.siteSettings, this.keyValueStore)); + this.currentUser.destroySession().then(() => logout()); } } }); diff --git a/app/assets/javascripts/discourse/app/widgets/post.js b/app/assets/javascripts/discourse/app/widgets/post.js index ea30d110e4a..cd72875a59c 100644 --- a/app/assets/javascripts/discourse/app/widgets/post.js +++ b/app/assets/javascripts/discourse/app/widgets/post.js @@ -474,8 +474,7 @@ createWidget("post-notice", { html(attrs) { const user = - this.siteSettings.display_name_on_posts && - prioritizeNameInUx(attrs.name, this.siteSettings) + this.siteSettings.display_name_on_posts && prioritizeNameInUx(attrs.name) ? attrs.name : attrs.username; let text, icon; diff --git a/app/assets/javascripts/discourse/app/widgets/poster-name.js b/app/assets/javascripts/discourse/app/widgets/poster-name.js index ebc12a8c858..0a160cc3740 100644 --- a/app/assets/javascripts/discourse/app/widgets/poster-name.js +++ b/app/assets/javascripts/discourse/app/widgets/poster-name.js @@ -68,8 +68,7 @@ export default createWidget("poster-name", { const username = attrs.username; const name = attrs.name; const nameFirst = - this.siteSettings.display_name_on_posts && - prioritizeNameInUx(name, this.siteSettings); + this.siteSettings.display_name_on_posts && prioritizeNameInUx(name); const classNames = nameFirst ? ["first", "full-name"] : ["first", "username"]; diff --git a/test/javascripts/helpers/qunit-helpers.js b/test/javascripts/helpers/qunit-helpers.js index dd1528fc8ab..75c41026a27 100644 --- a/test/javascripts/helpers/qunit-helpers.js +++ b/test/javascripts/helpers/qunit-helpers.js @@ -5,7 +5,7 @@ import { later } from "@ember/runloop"; import sessionFixtures from "fixtures/session-fixtures"; import HeaderComponent from "discourse/components/site-header"; import { forceMobile, resetMobile } from "discourse/lib/mobile"; -import { resetPluginApi } from "discourse/lib/plugin-api"; +import { resetPluginApi, setPluginContainer } from "discourse/lib/plugin-api"; import { clearCache as clearOutletCache, resetExtraClasses @@ -173,6 +173,7 @@ export function acceptance(name, options) { resetPluginApi(); Discourse.reset(); this.container = getOwner(this); + setPluginContainer(this.container); if (options.beforeEach) { options.beforeEach.call(this); } diff --git a/test/javascripts/models/email-log-test.js b/test/javascripts/models/email-log-test.js index 7391960decf..80b6f8c5e56 100644 --- a/test/javascripts/models/email-log-test.js +++ b/test/javascripts/models/email-log-test.js @@ -1,7 +1,7 @@ import EmailLog from "admin/models/email-log"; import { setPrefix } from "discourse-common/lib/get-url"; -QUnit.module("Discourse.EmailLog"); +QUnit.module("model:email-log"); QUnit.test("create", assert => { assert.ok(EmailLog.create(), "it can be created without arguments"); diff --git a/test/javascripts/test_helper.js b/test/javascripts/test_helper.js index 95f2f6f84fd..692454e8646 100644 --- a/test/javascripts/test_helper.js +++ b/test/javascripts/test_helper.js @@ -42,12 +42,13 @@ // //= require jquery.magnific-popup.min.js +let App = window.Discourse; let resetSettings = require("helpers/site-settings").resetSettings; let createHelperContext = require("discourse-common/lib/helpers") .createHelperContext; const buildResolver = require("discourse-common/resolver").buildResolver; -window.setResolver(buildResolver("discourse").create({ namespace: Discourse })); +window.setResolver(buildResolver("discourse").create({ namespace: App })); sinon.config = { injectIntoThis: false, @@ -71,10 +72,10 @@ d.write( "" ); -Discourse.rootElement = "#ember-testing"; -Discourse.setupForTesting(); -Discourse.injectTestHelpers(); -Discourse.start(); +App.rootElement = "#ember-testing"; +App.setupForTesting(); +App.injectTestHelpers(); +App.start(); // disable logster error reporting if (window.Logster) {