diff --git a/app/assets/javascripts/discourse-common/addon/resolver.js b/app/assets/javascripts/discourse-common/addon/resolver.js index cbe2e27c220..1cb42376d18 100644 --- a/app/assets/javascripts/discourse-common/addon/resolver.js +++ b/app/assets/javascripts/discourse-common/addon/resolver.js @@ -68,6 +68,11 @@ const DEPRECATED_MODULES = new Map( since: "2.9.0.beta7", dropFrom: "3.0.0", }, + "current-user:main": { + newName: "service:current-user", + since: "2.9.0.beta7", + dropFrom: "3.0.0", + }, }) ); diff --git a/app/assets/javascripts/discourse/app/components/glimmer.js b/app/assets/javascripts/discourse/app/components/glimmer.js index 1813d6f0b3c..8511e81222d 100644 --- a/app/assets/javascripts/discourse/app/components/glimmer.js +++ b/app/assets/javascripts/discourse/app/components/glimmer.js @@ -18,12 +18,7 @@ export default class DiscourseGlimmerComponent extends GlimmerComponent { @service keyValueStore; @service pmTopicTrackingState; @service siteSettings; - - @cached - get currentUser() { - const applicationInstance = getOwner(this); - return applicationInstance.lookup("current-user:main"); - } + @service currentUser; @cached get messageBus() { 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 64be4cf1f5f..b61726b3229 100644 --- a/app/assets/javascripts/discourse/app/initializers/auto-load-modules.js +++ b/app/assets/javascripts/discourse/app/initializers/auto-load-modules.js @@ -21,7 +21,7 @@ export function autoLoadModules(container, registry) { siteSettings: container.lookup("service:site-settings"), keyValueStore: container.lookup("service:key-value-store"), capabilities: container.lookup("capabilities:main"), - currentUser: container.lookup("current-user:main"), + currentUser: container.lookup("service:current-user"), site: container.lookup("site:main"), session: container.lookup("session:main"), topicTrackingState: container.lookup("topic-tracking-state:main"), diff --git a/app/assets/javascripts/discourse/app/initializers/badging.js b/app/assets/javascripts/discourse/app/initializers/badging.js index a347e30f2ae..6ac623bdb87 100644 --- a/app/assets/javascripts/discourse/app/initializers/badging.js +++ b/app/assets/javascripts/discourse/app/initializers/badging.js @@ -8,7 +8,7 @@ export default { return; } // must have the Badging API - const user = container.lookup("current-user:main"); + const user = container.lookup("service:current-user"); if (!user) { return; } // must be logged in diff --git a/app/assets/javascripts/discourse/app/initializers/logs-notice.js b/app/assets/javascripts/discourse/app/initializers/logs-notice.js index 8612d4b314b..d696bfb35d5 100644 --- a/app/assets/javascripts/discourse/app/initializers/logs-notice.js +++ b/app/assets/javascripts/discourse/app/initializers/logs-notice.js @@ -14,7 +14,7 @@ export default { const siteSettings = container.lookup("service:site-settings"); const messageBus = container.lookup("service:message-bus"); const keyValueStore = container.lookup("service:key-value-store"); - const currentUser = container.lookup("current-user:main"); + const currentUser = container.lookup("service:current-user"); LogsNotice.reopenClass(Singleton, { createCurrent() { return this.create({ diff --git a/app/assets/javascripts/discourse/app/initializers/message-bus.js b/app/assets/javascripts/discourse/app/initializers/message-bus.js index fcc8cc6e622..7b0593a863f 100644 --- a/app/assets/javascripts/discourse/app/initializers/message-bus.js +++ b/app/assets/javascripts/discourse/app/initializers/message-bus.js @@ -41,7 +41,7 @@ export default { } const messageBus = container.lookup("service:message-bus"), - user = container.lookup("current-user:main"), + user = container.lookup("service:current-user"), siteSettings = container.lookup("service:site-settings"); messageBus.alwaysLongPoll = !isProduction(); diff --git a/app/assets/javascripts/discourse/app/initializers/signup-cta.js b/app/assets/javascripts/discourse/app/initializers/signup-cta.js index c2dc2399812..ba905454e3c 100644 --- a/app/assets/javascripts/discourse/app/initializers/signup-cta.js +++ b/app/assets/javascripts/discourse/app/initializers/signup-cta.js @@ -13,7 +13,7 @@ export default { const session = Session.current(); const siteSettings = container.lookup("service:site-settings"); const keyValueStore = container.lookup("service:key-value-store"); - const user = container.lookup("current-user:main"); + const user = container.lookup("service:current-user"); const appEvents = container.lookup("service:app-events"); // Preconditions diff --git a/app/assets/javascripts/discourse/app/initializers/subscribe-user-notifications.js b/app/assets/javascripts/discourse/app/initializers/subscribe-user-notifications.js index b56c47aa415..eed101e32fd 100644 --- a/app/assets/javascripts/discourse/app/initializers/subscribe-user-notifications.js +++ b/app/assets/javascripts/discourse/app/initializers/subscribe-user-notifications.js @@ -19,7 +19,7 @@ export default { after: "message-bus", initialize(container) { - const user = container.lookup("current-user:main"); + const user = container.lookup("service:current-user"); const bus = container.lookup("service:message-bus"); const appEvents = container.lookup("service:app-events"); diff --git a/app/assets/javascripts/discourse/app/initializers/url-redirects.js b/app/assets/javascripts/discourse/app/initializers/url-redirects.js index ad5b9475d46..e04798f90b0 100644 --- a/app/assets/javascripts/discourse/app/initializers/url-redirects.js +++ b/app/assets/javascripts/discourse/app/initializers/url-redirects.js @@ -7,7 +7,7 @@ export default { after: "inject-objects", initialize(container) { - const currentUser = container.lookup("current-user:main"); + const currentUser = container.lookup("service:current-user"); if (currentUser) { const username = currentUser.get("username"); const escapedUsername = escapeRegExp(username); diff --git a/app/assets/javascripts/discourse/app/lib/keyboard-shortcuts.js b/app/assets/javascripts/discourse/app/lib/keyboard-shortcuts.js index 0708f047c30..df99e5275eb 100644 --- a/app/assets/javascripts/discourse/app/lib/keyboard-shortcuts.js +++ b/app/assets/javascripts/discourse/app/lib/keyboard-shortcuts.js @@ -135,7 +135,7 @@ export default { this.searchService = this.container.lookup("service:search"); this.appEvents = this.container.lookup("service:app-events"); - this.currentUser = this.container.lookup("current-user:main"); + this.currentUser = this.container.lookup("service:current-user"); this.siteSettings = this.container.lookup("service:site-settings"); // Disable the shortcut if private messages are disabled diff --git a/app/assets/javascripts/discourse/app/lib/plugin-api.js b/app/assets/javascripts/discourse/app/lib/plugin-api.js index cb577ae8d47..7eb485b579e 100644 --- a/app/assets/javascripts/discourse/app/lib/plugin-api.js +++ b/app/assets/javascripts/discourse/app/lib/plugin-api.js @@ -152,7 +152,7 @@ class PluginApi { * If the user is not logged in, it will be `null`. **/ getCurrentUser() { - return this._lookupContainer("current-user:main"); + return this._lookupContainer("service:current-user"); } _lookupContainer(path) { 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 285c2e37fb1..d4a94bf2b17 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 @@ -8,7 +8,7 @@ import User from "discourse/models/user"; const ALL_TARGETS = ["controller", "component", "route", "model", "adapter"]; -function injectServiceIntoService({ app, property, specifier }) { +export function injectServiceIntoService({ app, property, specifier }) { // app.inject doesn't allow implicit injection of services into services. // However, we need to do it in order to convert our old service-like objects // into true services, without breaking existing implicit injections. @@ -34,8 +34,10 @@ export default { const siteSettings = container.lookup("service:site-settings"); const currentUser = User.current(); - app.register("current-user:main", currentUser, { instantiate: false }); - app.currentUser = currentUser; + + // We can't use a 'real' service factory (i.e. services/current-user.js) because we need + // to register a null value for anon + app.register("service:current-user", currentUser, { instantiate: false }); const topicTrackingState = TopicTrackingState.create({ messageBus: container.lookup("service:message-bus"), @@ -87,8 +89,13 @@ export default { }); if (currentUser) { - ["controller", "component", "route", "service"].forEach((t) => { - app.inject(t, "currentUser", "current-user:main"); + ["controller", "component", "route"].forEach((t) => { + app.inject(t, "currentUser", "service:current-user"); + }); + injectServiceIntoService({ + app, + property: "currentUser", + specifier: "service:current-user", }); } diff --git a/app/assets/javascripts/discourse/app/pre-initializers/theme-errors-handler.js b/app/assets/javascripts/discourse/app/pre-initializers/theme-errors-handler.js index b1215db31a4..e0c476ca22a 100644 --- a/app/assets/javascripts/discourse/app/pre-initializers/theme-errors-handler.js +++ b/app/assets/javascripts/discourse/app/pre-initializers/theme-errors-handler.js @@ -20,7 +20,7 @@ export default { return; } - this.currentUser = container.lookup("current-user:main"); + this.currentUser = container.lookup("service:current-user"); getAndClearUnhandledThemeErrors().forEach((e) => { reportThemeError(this.currentUser, e); diff --git a/app/assets/javascripts/discourse/app/widgets/widget.js b/app/assets/javascripts/discourse/app/widgets/widget.js index 804cb69bb02..13bc3f25333 100644 --- a/app/assets/javascripts/discourse/app/widgets/widget.js +++ b/app/assets/javascripts/discourse/app/widgets/widget.js @@ -142,7 +142,7 @@ export default class Widget { this.key = this.buildKey ? this.buildKey(attrs) : null; this.site = register.lookup("site:main"); this.siteSettings = register.lookup("service:site-settings"); - this.currentUser = register.lookup("current-user:main"); + this.currentUser = register.lookup("service:current-user"); this.capabilities = register.lookup("capabilities:main"); this.store = register.lookup("service:store"); this.appEvents = register.lookup("service:app-events"); diff --git a/app/assets/javascripts/discourse/tests/helpers/component-test.js b/app/assets/javascripts/discourse/tests/helpers/component-test.js index 66658cd37c8..3ae23e4f179 100644 --- a/app/assets/javascripts/discourse/tests/helpers/component-test.js +++ b/app/assets/javascripts/discourse/tests/helpers/component-test.js @@ -8,6 +8,7 @@ import QUnit, { test } from "qunit"; import { setupRenderingTest as emberSetupRenderingTest } from "ember-qunit"; import { currentSettings } from "discourse/tests/helpers/site-settings"; import { testCleanup } from "discourse/tests/helpers/qunit-helpers"; +import { injectServiceIntoService } from "discourse/pre-initializers/inject-discourse-objects"; export function setupRenderingTest(hooks) { emberSetupRenderingTest(hooks); @@ -31,12 +32,16 @@ export function setupRenderingTest(hooks) { timezone: "Australia/Brisbane", }); this.currentUser = currentUser; - this.owner.unregister("current-user:main"); - this.owner.register("current-user:main", currentUser, { + this.owner.unregister("service:current-user"); + this.owner.register("service:current-user", currentUser, { instantiate: false, }); - this.owner.inject("component", "currentUser", "current-user:main"); - this.owner.inject("service", "currentUser", "current-user:main"); + this.owner.inject("component", "currentUser", "service:current-user"); + injectServiceIntoService({ + app: this.owner.application, + property: "currentUser", + specifier: "service:current-user", + }); this.owner.unregister("topic-tracking-state:main"); this.owner.register( @@ -85,7 +90,7 @@ export default function (name, hooks, opts) { test(name, async function (assert) { if (opts.anonymous) { - this.owner.unregister("current-user:main"); + this.owner.unregister("service:current-user"); } if (opts.beforeEach) { diff --git a/app/assets/javascripts/discourse/tests/integration/components/site-header-test.js b/app/assets/javascripts/discourse/tests/integration/components/site-header-test.js index 5be14b861b2..44d2477df61 100644 --- a/app/assets/javascripts/discourse/tests/integration/components/site-header-test.js +++ b/app/assets/javascripts/discourse/tests/integration/components/site-header-test.js @@ -32,7 +32,7 @@ module("Integration | Component | site-header", function (hooks) { }); test("do not call authenticated endpoints as anonymous", async function (assert) { - this.owner.unregister("current-user:main"); + this.owner.unregister("service:current-user"); await render(hbs``); diff --git a/app/assets/javascripts/discourse/tests/integration/components/widgets/hamburger-menu-test.js b/app/assets/javascripts/discourse/tests/integration/components/widgets/hamburger-menu-test.js index ac19903a9f0..f0913a66780 100644 --- a/app/assets/javascripts/discourse/tests/integration/components/widgets/hamburger-menu-test.js +++ b/app/assets/javascripts/discourse/tests/integration/components/widgets/hamburger-menu-test.js @@ -68,7 +68,7 @@ module("Integration | Component | Widget | hamburger-menu", function (hooks) { }); test("general links", async function (assert) { - this.owner.unregister("current-user:main"); + this.owner.unregister("service:current-user"); await render(hbs``); @@ -86,7 +86,7 @@ module("Integration | Component | Widget | hamburger-menu", function (hooks) { let maxCategoriesToDisplay; test("top categories - anonymous", async function (assert) { - this.owner.unregister("current-user:main"); + this.owner.unregister("service:current-user"); this.siteSettings.header_dropdown_category_count = 8; await render(hbs``); @@ -102,7 +102,7 @@ module("Integration | Component | Widget | hamburger-menu", function (hooks) { }); test("top categories - allow_uncategorized_topics", async function (assert) { - this.owner.unregister("current-user:main"); + this.owner.unregister("service:current-user"); this.siteSettings.allow_uncategorized_topics = false; this.siteSettings.header_dropdown_category_count = 8; diff --git a/app/assets/javascripts/discourse/tests/integration/components/widgets/header-test.js b/app/assets/javascripts/discourse/tests/integration/components/widgets/header-test.js index 7736316d245..f37a625c3e6 100644 --- a/app/assets/javascripts/discourse/tests/integration/components/widgets/header-test.js +++ b/app/assets/javascripts/discourse/tests/integration/components/widgets/header-test.js @@ -15,7 +15,7 @@ module("Integration | Component | Widget | header", function (hooks) { }); test("sign up / login buttons", async function (assert) { - this.owner.unregister("current-user:main"); + this.owner.unregister("service:current-user"); this.set("args", { canSignUp: true }); this.set("showCreateAccount", () => (this.signupShown = true)); this.set("showLogin", () => (this.loginShown = true)); @@ -40,7 +40,7 @@ module("Integration | Component | Widget | header", function (hooks) { }); test("anon when login required", async function (assert) { - this.owner.unregister("current-user:main"); + this.owner.unregister("service:current-user"); this.set("args", { canSignUp: true }); this.set("showCreateAccount", () => (this.signupShown = true)); this.set("showLogin", () => (this.loginShown = true)); diff --git a/app/assets/javascripts/discourse/tests/integration/components/widgets/post-test.js b/app/assets/javascripts/discourse/tests/integration/components/widgets/post-test.js index 650a9627a8a..b6ee555991c 100644 --- a/app/assets/javascripts/discourse/tests/integration/components/widgets/post-test.js +++ b/app/assets/javascripts/discourse/tests/integration/components/widgets/post-test.js @@ -237,7 +237,7 @@ module("Integration | Component | Widget | post", function (hooks) { }); test("anon liking", async function (assert) { - this.owner.unregister("current-user:main"); + this.owner.unregister("service:current-user"); const args = { showLike: true }; this.set("args", args); this.set("showLogin", () => (this.loginShown = true)); diff --git a/app/assets/javascripts/discourse/tests/unit/controllers/topic-test.js b/app/assets/javascripts/discourse/tests/unit/controllers/topic-test.js index 9e3da6a4ef4..ebdd1d1289c 100644 --- a/app/assets/javascripts/discourse/tests/unit/controllers/topic-test.js +++ b/app/assets/javascripts/discourse/tests/unit/controllers/topic-test.js @@ -21,7 +21,7 @@ discourseModule("Unit | Controller | topic", function (hooks) { }); hooks.afterEach(function () { - this.registry.unregister("current-user:main"); + this.registry.unregister("service:current-user"); let topic = this.container.lookup("controller:topic"); topic.setProperties({ selectedPostIds: [], @@ -264,10 +264,14 @@ discourseModule("Unit | Controller | topic", function (hooks) { test("canDeleteSelected", function (assert) { const currentUser = User.create({ admin: false }); - this.registry.register("current-user:main", currentUser, { + this.registry.register("service:current-user", currentUser, { instantiate: false, }); - this.registry.injection("controller", "currentUser", "current-user:main"); + this.registry.injection( + "controller", + "currentUser", + "service:current-user" + ); let model = topicWithStream({ posts: [ { id: 1, can_delete: false }, @@ -362,10 +366,14 @@ discourseModule("Unit | Controller | topic", function (hooks) { test("canChangeOwner", function (assert) { const currentUser = User.create({ admin: false }); - this.registry.register("current-user:main", currentUser, { + this.registry.register("service:current-user", currentUser, { instantiate: false, }); - this.registry.injection("controller", "currentUser", "current-user:main"); + this.registry.injection( + "controller", + "currentUser", + "service:current-user" + ); let model = topicWithStream({ posts: [ @@ -405,10 +413,14 @@ discourseModule("Unit | Controller | topic", function (hooks) { test("modCanChangeOwner", function (assert) { const currentUser = User.create({ moderator: false }); - this.registry.register("current-user:main", currentUser, { + this.registry.register("service:current-user", currentUser, { instantiate: false, }); - this.registry.injection("controller", "currentUser", "current-user:main"); + this.registry.injection( + "controller", + "currentUser", + "service:current-user" + ); let model = topicWithStream({ posts: [ @@ -667,10 +679,14 @@ discourseModule("Unit | Controller | topic", function (hooks) { }); const currentUser = EmberObject.create({ moderator: true }); - this.registry.register("current-user:main", currentUser, { + this.registry.register("service:current-user", currentUser, { instantiate: false, }); - this.registry.injection("controller", "currentUser", "current-user:main"); + this.registry.injection( + "controller", + "currentUser", + "service:current-user" + ); let model = topicWithStream({ stream: [2, 3, 4], diff --git a/app/assets/javascripts/discourse/tests/unit/services/current-user-test.js b/app/assets/javascripts/discourse/tests/unit/services/current-user-test.js index ca850853351..54a5913738b 100644 --- a/app/assets/javascripts/discourse/tests/unit/services/current-user-test.js +++ b/app/assets/javascripts/discourse/tests/unit/services/current-user-test.js @@ -5,7 +5,7 @@ acceptance("current-user", function (needs) { needs.user(); test("currentUser has appEvents", function (assert) { - let currentUser = this.container.lookup("current-user:main"); + let currentUser = this.container.lookup("service:current-user"); assert.ok(currentUser.appEvents); }); });