From ca13160435c7865f94499ed29a0786facceb1212 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Tue, 14 Jul 2020 12:31:03 -0400 Subject: [PATCH] REFACTOR: Remove `Discourse.SiteSettings` from user/admin-user --- .../admin/components/screened-ip-address-form.js | 7 +------ app/assets/javascripts/admin/models/admin-user.js | 2 +- app/assets/javascripts/discourse/app/models/rest.js | 12 ++++++++++-- app/assets/javascripts/discourse/app/models/store.js | 1 - app/assets/javascripts/discourse/app/models/user.js | 12 ++++++------ .../app/pre-initializers/inject-discourse-objects.js | 12 ++++++------ 6 files changed, 24 insertions(+), 22 deletions(-) diff --git a/app/assets/javascripts/admin/components/screened-ip-address-form.js b/app/assets/javascripts/admin/components/screened-ip-address-form.js index 1046f4c2100..9bd2323b6f9 100644 --- a/app/assets/javascripts/admin/components/screened-ip-address-form.js +++ b/app/assets/javascripts/admin/components/screened-ip-address-form.js @@ -21,12 +21,7 @@ export default Component.extend({ formSubmitted: false, actionName: "block", - @discourseComputed - adminWhitelistEnabled() { - return Discourse.SiteSettings.use_admin_ip_whitelist; - }, - - @discourseComputed("adminWhitelistEnabled") + @discourseComputed("siteSettings.use_admin_ip_whitelist") actionNames(adminWhitelistEnabled) { if (adminWhitelistEnabled) { return [ diff --git a/app/assets/javascripts/admin/models/admin-user.js b/app/assets/javascripts/admin/models/admin-user.js index 8dc7685cc10..53e469c465f 100644 --- a/app/assets/javascripts/admin/models/admin-user.js +++ b/app/assets/javascripts/admin/models/admin-user.js @@ -35,7 +35,7 @@ const AdminUser = User.extend({ bounceScoreExplanation(bounce_score) { if (bounce_score === 0) { return I18n.t("admin.user.bounce_score_explanation.none"); - } else if (bounce_score < Discourse.SiteSettings.bounce_score_threshold) { + } else if (bounce_score < this.siteSettings.bounce_score_threshold) { return I18n.t("admin.user.bounce_score_explanation.some"); } else { return I18n.t("admin.user.bounce_score_explanation.threshold_reached"); diff --git a/app/assets/javascripts/discourse/app/models/rest.js b/app/assets/javascripts/discourse/app/models/rest.js index e0a697e8275..f744facbd5f 100644 --- a/app/assets/javascripts/discourse/app/models/rest.js +++ b/app/assets/javascripts/discourse/app/models/rest.js @@ -2,6 +2,7 @@ import { warn } from "@ember/debug"; import { equal } from "@ember/object/computed"; import EmberObject from "@ember/object"; import { Promise } from "rsvp"; +import { getOwner } from "discourse-common/lib/get-owner"; const RestModel = EmberObject.extend({ isNew: equal("__state", "new"), @@ -100,9 +101,16 @@ RestModel.reopenClass({ create(args) { args = args || {}; + let owner = getOwner(this); + + // Some Discourse code calls `model.create()` directly without going through the + // store. In that case the injections are not made, so we do them here. Eventually + // we should use the store for everything to fix this. if (!args.store) { - const container = Discourse.__container__; - args.store = container.lookup("service:store"); + args.store = owner.lookup("service:store"); + } + if (!args.siteSettings) { + args.siteSettings = owner.lookup("site-settings:main"); } args.__munge = this.munge; diff --git a/app/assets/javascripts/discourse/app/models/store.js b/app/assets/javascripts/discourse/app/models/store.js index 94441169e1c..4cf09944bc0 100644 --- a/app/assets/javascripts/discourse/app/models/store.js +++ b/app/assets/javascripts/discourse/app/models/store.js @@ -262,7 +262,6 @@ export default EmberObject.extend({ // TODO: Have injections be automatic obj.topicTrackingState = this.register.lookup("topic-tracking-state:main"); obj.keyValueStore = this.register.lookup("key-value-store:main"); - obj.siteSettings = this.register.lookup("site-settings:main"); const klass = this.register.lookupFactory("model:" + type) || RestModel; const model = klass.create(obj); diff --git a/app/assets/javascripts/discourse/app/models/user.js b/app/assets/javascripts/discourse/app/models/user.js index eb59335dd10..72c2695aa7d 100644 --- a/app/assets/javascripts/discourse/app/models/user.js +++ b/app/assets/javascripts/discourse/app/models/user.js @@ -50,7 +50,7 @@ const User = RestModel.extend({ @discourseComputed("can_be_deleted", "post_count") canBeDeleted(canBeDeleted, postCount) { - const maxPostCount = Discourse.SiteSettings.delete_all_posts_max; + const maxPostCount = this.siteSettings.delete_all_posts_max; return canBeDeleted && postCount <= maxPostCount; }, @@ -100,7 +100,7 @@ const User = RestModel.extend({ @discourseComputed("username", "name") displayName(username, name) { - if (Discourse.SiteSettings.enable_names && !isEmpty(name)) { + if (this.siteSettings.enable_names && !isEmpty(name)) { return name; } return username; @@ -108,7 +108,7 @@ const User = RestModel.extend({ @discourseComputed("profile_background_upload_url") profileBackgroundUrl(bgUrl) { - if (isEmpty(bgUrl) || !Discourse.SiteSettings.allow_profile_backgrounds) { + if (isEmpty(bgUrl) || !this.siteSettings.allow_profile_backgrounds) { return "".htmlSafe(); } return ("background-image: url(" + getURLWithCDN(bgUrl) + ")").htmlSafe(); @@ -664,7 +664,7 @@ const User = RestModel.extend({ return ( this.staff || this.trust_level > 0 || - Discourse.SiteSettings[`newuser_max_${type}s`] > 0 + this.siteSettings[`newuser_max_${type}s`] > 0 ); }, @@ -724,7 +724,7 @@ const User = RestModel.extend({ @discourseComputed("can_delete_account") canDeleteAccount(canDeleteAccount) { - return !Discourse.SiteSettings.enable_sso && canDeleteAccount; + return !this.siteSettings.enable_sso && canDeleteAccount; }, delete: function() { @@ -881,7 +881,7 @@ const User = RestModel.extend({ @discourseComputed("second_factor_enabled", "staff") enforcedSecondFactor(secondFactorEnabled, staff) { - const enforce = Discourse.SiteSettings.enforce_second_factor; + const enforce = this.siteSettings.enforce_second_factor; return ( !secondFactorEnabled && (enforce === "all" || (enforce === "staff" && staff)) diff --git a/app/assets/javascripts/discourse/app/pre-initializers/inject-discourse-objects.js b/app/assets/javascripts/discourse/app/pre-initializers/inject-discourse-objects.js index 9c2a3e47799..784759e6993 100644 --- a/app/assets/javascripts/discourse/app/pre-initializers/inject-discourse-objects.js +++ b/app/assets/javascripts/discourse/app/pre-initializers/inject-discourse-objects.js @@ -35,6 +35,12 @@ export default { app.inject(t, "messageBus", "message-bus:main") ); + const siteSettings = app.SiteSettings; + app.register("site-settings:main", siteSettings, { instantiate: false }); + ALL_TARGETS.concat("service").forEach(t => + app.inject(t, "siteSettings", "site-settings:main") + ); + const currentUser = User.current(); app.register("current-user:main", currentUser, { instantiate: false }); app.currentUser = currentUser; @@ -50,12 +56,6 @@ export default { app.inject(t, "topicTrackingState", "topic-tracking-state:main") ); - const siteSettings = app.SiteSettings; - app.register("site-settings:main", siteSettings, { instantiate: false }); - ALL_TARGETS.concat("service").forEach(t => - app.inject(t, "siteSettings", "site-settings:main") - ); - const site = Site.current(); app.register("site:main", site, { instantiate: false }); ALL_TARGETS.forEach(t => app.inject(t, "site", "site:main"));