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 b41299712e2..8140c5f1e88 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 @@ -28,7 +28,10 @@ export default { const messageBus = window.MessageBus; app.register("message-bus:main", messageBus, { instantiate: false }); - ALL_TARGETS.forEach(t => app.inject(t, "messageBus", "message-bus:main")); + + ALL_TARGETS.concat("service").forEach(t => + app.inject(t, "messageBus", "message-bus:main") + ); const currentUser = User.current(); app.register("current-user:main", currentUser, { instantiate: false }); @@ -47,7 +50,7 @@ export default { const siteSettings = app.SiteSettings; app.register("site-settings:main", siteSettings, { instantiate: false }); - ALL_TARGETS.forEach(t => + ALL_TARGETS.concat("service").forEach(t => app.inject(t, "siteSettings", "site-settings:main") ); @@ -77,7 +80,7 @@ export default { ); if (currentUser) { - ["component", "route", "controller"].forEach(t => { + ["component", "route", "controller", "service"].forEach(t => { app.inject(t, "currentUser", "current-user:main"); }); } diff --git a/plugins/discourse-presence/assets/javascripts/discourse/components/composer-presence-display.js.es6 b/plugins/discourse-presence/assets/javascripts/discourse/components/composer-presence-display.js.es6 index a371e5f4a91..6f94bad9935 100644 --- a/plugins/discourse-presence/assets/javascripts/discourse/components/composer-presence-display.js.es6 +++ b/plugins/discourse-presence/assets/javascripts/discourse/components/composer-presence-display.js.es6 @@ -1,17 +1,12 @@ import Component from "@ember/component"; -import { getOwner } from "@ember/application"; import { cancel } from "@ember/runloop"; import { equal, gt } from "@ember/object/computed"; +import { inject as service } from "@ember/service"; import discourseComputed, { observes, on } from "discourse-common/utils/decorators"; -import { - REPLYING, - CLOSED, - EDITING, - COMPOSER_TYPE -} from "../lib/presence-manager"; +import { REPLYING, CLOSED, EDITING, COMPOSER_TYPE } from "../lib/presence"; import { REPLY, EDIT } from "discourse/models/composer"; export default Component.extend({ @@ -22,15 +17,7 @@ export default Component.extend({ reply: null, title: null, isWhispering: null, - presenceManager: null, - - init() { - this._super(...arguments); - - this.setProperties({ - presenceManager: getOwner(this).lookup("presence-manager:main") - }); - }, + presenceManager: service(), @discourseComputed("topic.id") users(topicId) { diff --git a/plugins/discourse-presence/assets/javascripts/discourse/components/topic-presence-display.js.es6 b/plugins/discourse-presence/assets/javascripts/discourse/components/topic-presence-display.js.es6 index 2f683ca741b..c44742c9aa9 100644 --- a/plugins/discourse-presence/assets/javascripts/discourse/components/topic-presence-display.js.es6 +++ b/plugins/discourse-presence/assets/javascripts/discourse/components/topic-presence-display.js.es6 @@ -1,17 +1,12 @@ import Component from "@ember/component"; -import { getOwner } from "@ember/application"; import { gt } from "@ember/object/computed"; +import { inject as service } from "@ember/service"; import discourseComputed, { on } from "discourse-common/utils/decorators"; -import { TOPIC_TYPE } from "../lib/presence-manager"; +import { TOPIC_TYPE } from "../lib/presence"; export default Component.extend({ topic: null, - presenceManager: null, - - init() { - this._super(...arguments); - this.set("presenceManager", getOwner(this).lookup("presence-manager:main")); - }, + presenceManager: service(), @discourseComputed("topic.id") users(topicId) { diff --git a/plugins/discourse-presence/assets/javascripts/discourse/initializers/discourse-presence.js.es6 b/plugins/discourse-presence/assets/javascripts/discourse/initializers/discourse-presence.js.es6 deleted file mode 100644 index 1009fc1453a..00000000000 --- a/plugins/discourse-presence/assets/javascripts/discourse/initializers/discourse-presence.js.es6 +++ /dev/null @@ -1,32 +0,0 @@ -import { withPluginApi } from "discourse/lib/plugin-api"; -import PresenceManager from "../lib/presence-manager"; -import ENV from "discourse-common/config/environment"; - -function initializeDiscoursePresence(api, { app }) { - const currentUser = api.getCurrentUser(); - - if (currentUser) { - app.register( - "presence-manager:main", - PresenceManager.create({ - currentUser, - messageBus: api.container.lookup("message-bus:main"), - siteSettings: api.container.lookup("site-settings:main") - }), - { instantiate: false } - ); - } -} - -export default { - name: "discourse-presence", - after: "message-bus", - - initialize(container, app) { - const siteSettings = container.lookup("site-settings:main"); - - if (siteSettings.presence_enabled && ENV.environment !== "test") { - withPluginApi("0.8.40", initializeDiscoursePresence, { app }); - } - } -}; diff --git a/plugins/discourse-presence/assets/javascripts/discourse/lib/presence-manager.js.es6 b/plugins/discourse-presence/assets/javascripts/discourse/lib/presence.js.es6 similarity index 77% rename from plugins/discourse-presence/assets/javascripts/discourse/lib/presence-manager.js.es6 rename to plugins/discourse-presence/assets/javascripts/discourse/lib/presence.js.es6 index 1be35640caa..57eedc05255 100644 --- a/plugins/discourse-presence/assets/javascripts/discourse/lib/presence-manager.js.es6 +++ b/plugins/discourse-presence/assets/javascripts/discourse/lib/presence.js.es6 @@ -214,73 +214,4 @@ const Presence = EmberObject.extend({ } }); -const PresenceManager = EmberObject.extend({ - presences: null, - currentUser: null, - messageBus: null, - siteSettings: null, - - init() { - this._super(...arguments); - - this.setProperties({ - presences: {} - }); - }, - - subscribe(topicId, type) { - if (!topicId) return; - this._getPresence(topicId).subscribe(type); - }, - - unsubscribe(topicId, type) { - if (!topicId) return; - const presence = this._getPresence(topicId); - - if (presence.unsubscribe(type)) { - delete this.presences[topicId]; - } - }, - - users(topicId) { - if (!topicId) return []; - return this._getPresence(topicId).users; - }, - - editingUsers(topicId) { - if (!topicId) return []; - return this._getPresence(topicId).editingUsers; - }, - - throttlePublish(topicId, state, whisper, postId) { - if (!topicId) return; - return this._getPresence(topicId).throttlePublish(state, whisper, postId); - }, - - publish(topicId, state, whisper, postId) { - if (!topicId) return; - return this._getPresence(topicId).publish(state, whisper, postId); - }, - - cleanUpPresence(type) { - Object.keys(this.presences).forEach(key => { - this.publish(key, CLOSED); - this.unsubscribe(key, type); - }); - }, - - _getPresence(topicId) { - if (!this.presences[topicId]) { - this.presences[topicId] = Presence.create({ - messageBus: this.messageBus, - siteSettings: this.siteSettings, - currentUser: this.currentUser, - topicId - }); - } - - return this.presences[topicId]; - } -}); - -export default PresenceManager; +export default Presence; diff --git a/plugins/discourse-presence/assets/javascripts/discourse/services/presence-manager.js.es6 b/plugins/discourse-presence/assets/javascripts/discourse/services/presence-manager.js.es6 new file mode 100644 index 00000000000..1aaca2d1ac3 --- /dev/null +++ b/plugins/discourse-presence/assets/javascripts/discourse/services/presence-manager.js.es6 @@ -0,0 +1,70 @@ +import Service from "@ember/service"; +import Presence, { CLOSED } from "../lib/presence"; + +const PresenceManager = Service.extend({ + presences: null, + + init() { + this._super(...arguments); + + this.setProperties({ + presences: {} + }); + }, + + subscribe(topicId, type) { + if (!topicId) return; + this._getPresence(topicId).subscribe(type); + }, + + unsubscribe(topicId, type) { + if (!topicId) return; + const presence = this._getPresence(topicId); + + if (presence.unsubscribe(type)) { + delete this.presences[topicId]; + } + }, + + users(topicId) { + if (!topicId) return []; + return this._getPresence(topicId).users; + }, + + editingUsers(topicId) { + if (!topicId) return []; + return this._getPresence(topicId).editingUsers; + }, + + throttlePublish(topicId, state, whisper, postId) { + if (!topicId) return; + return this._getPresence(topicId).throttlePublish(state, whisper, postId); + }, + + publish(topicId, state, whisper, postId) { + if (!topicId) return; + return this._getPresence(topicId).publish(state, whisper, postId); + }, + + cleanUpPresence(type) { + Object.keys(this.presences).forEach(key => { + this.publish(key, CLOSED); + this.unsubscribe(key, type); + }); + }, + + _getPresence(topicId) { + if (!this.presences[topicId]) { + this.presences[topicId] = Presence.create({ + messageBus: this.messageBus, + siteSettings: this.siteSettings, + currentUser: this.currentUser, + topicId + }); + } + + return this.presences[topicId]; + } +}); + +export default PresenceManager;