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.
This commit is contained in:
Robin Ward 2020-08-19 08:51:48 -04:00 committed by GitHub
parent dde3d63b33
commit 3640c00b03
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
20 changed files with 67 additions and 82 deletions

View File

@ -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;

View File

@ -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")

View File

@ -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")

View File

@ -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);

View File

@ -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);
}

View File

@ -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";
}

View File

@ -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;

View File

@ -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;
}

View File

@ -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

View File

@ -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) {

View File

@ -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);
}
});

View File

@ -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();

View File

@ -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 }

View File

@ -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);
}

View File

@ -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());
}
}
});

View File

@ -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;

View File

@ -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"];

View File

@ -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);
}

View File

@ -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");

View File

@ -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(
"<style>#ember-testing-container { position: absolute; background: white; bottom: 0; right: 0; width: 640px; height: 384px; overflow: auto; z-index: 9999; border: 1px solid #ccc; } #ember-testing { zoom: 50%; }</style>"
);
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) {