FIX: Replace use of global Discourse

FIX: Replace use of global Discourse

The plugin used on Discourse.SiteSettings and Discourse.User, which is no longer supported by core and Ember CLI.

diff --git a/assets/javascripts/discourse/connectors/composer-action-after/encrypt.js.es6 b/assets/javascripts/discourse/connectors/composer-action-after/encrypt.js.es6
index f5f3e74..023e1f9 100644
--- a/assets/javascripts/discourse/connectors/composer-action-after/encrypt.js.es6
+++ b/assets/javascripts/discourse/connectors/composer-action-after/encrypt.js.es6
@@ -1,5 +1,4 @@
 import I18n from "I18n";
-import { getOwner } from "discourse-common/lib/get-owner";
 import {
   ENCRYPT_ACTIVE,
   ENCRYPT_DISABLED,
@@ -8,8 +7,8 @@ import {
 
 export default {
   setupComponent(args, component) {
-    const currentUser = getOwner(component).lookup("current-user:main");
-    const status = getEncryptionStatus(currentUser, component.siteSettings);
+    const { currentUser, siteSettings } = component;
+    const status = getEncryptionStatus(currentUser, siteSettings);
 
     component.setProperties({
       isEncryptEnabled: status !== ENCRYPT_DISABLED,
@@ -17,10 +16,7 @@ export default {
 
       /** Listens for encryption status updates. */
       listener() {
-        const newStatus = getEncryptionStatus(
-          currentUser,
-          component.siteSettings
-        );
+        const newStatus = getEncryptionStatus(currentUser, siteSettings);
         component.setProperties({
           isEncryptEnabled: newStatus !== ENCRYPT_DISABLED,
           isEncryptActive: newStatus === ENCRYPT_ACTIVE,
diff --git a/assets/javascripts/discourse/connectors/user-preferences-security/encrypt.js.es6 b/assets/javascripts/discourse/connectors/user-preferences-security/encrypt.js.es6
index 0ed7744..217b5b3 100644
--- a/assets/javascripts/discourse/connectors/user-preferences-security/encrypt.js.es6
+++ b/assets/javascripts/discourse/connectors/user-preferences-security/encrypt.js.es6
@@ -1,5 +1,4 @@
 import showModal from "discourse/lib/show-modal";
-import User from "discourse/models/user";
 import {
   deleteDb,
   saveDbIdentity,
@@ -21,7 +20,7 @@ import I18n from "I18n";
 
 export default {
   setupComponent(args, component) {
-    const currentUser = User.current();
+    const { currentUser, siteSettings } = component;
     const isCurrentUser = args.model.id === currentUser.id;
 
     component.setProperties({
@@ -32,13 +31,13 @@ export default {
       /** Whether current user is the same as model user. */
       isCurrentUser,
       /** Whether plugin is enabled for current user. */
-      canEnableEncrypt: canEnableEncrypt(args.model, component.siteSettings),
+      canEnableEncrypt: canEnableEncrypt(args.model, siteSettings),
       /** Whether the encryption is enabled or not. */
       isEncryptEnabled: !!args.model.encrypt_public,
     });
 
     if (isCurrentUser) {
-      const status = getEncryptionStatus(args.model, component.siteSettings);
+      const status = getEncryptionStatus(args.model, siteSettings);
       component.setProperties({
         /** Value of passphrase input.
          *  It should stay in memory for as little time as possible.
@@ -59,10 +58,7 @@ export default {
         isEncryptActive: status === ENCRYPT_ACTIVE,
         /** Listens for encryption status updates. */
         listener() {
-          const newStatus = getEncryptionStatus(
-            args.model,
-            component.siteSettings
-          );
+          const newStatus = getEncryptionStatus(args.model, siteSettings);
           component.setProperties({
             isEncryptEnabled: newStatus !== ENCRYPT_DISABLED,
             isEncryptActive: newStatus === ENCRYPT_ACTIVE,
diff --git a/assets/javascripts/discourse/initializers/hook-save.js.es6 b/assets/javascripts/discourse/initializers/hook-save.js.es6
index 8c8c926..5aeda1e 100644
--- a/assets/javascripts/discourse/initializers/hook-save.js.es6
+++ b/assets/javascripts/discourse/initializers/hook-save.js.es6
@@ -2,7 +2,6 @@ import I18n from "I18n";
 import PostAdapter from "discourse/adapters/post";
 import { ajax } from "discourse/lib/ajax";
 import Topic from "discourse/models/topic";
-import User from "discourse/models/user";
 import {
   ENCRYPT_ACTIVE,
   getEncryptionStatus,
@@ -123,7 +122,7 @@ export default {
         }
 
         if (usernames.length > 0) {
-          usernames.push(User.current().username);
+          usernames.push(currentUser.username);
           const identitiesPromise = getUserIdentities(usernames);
 
           encryptedKeysPromise = Promise.all([
diff --git a/assets/javascripts/discourse/initializers/update-state.js.es6 b/assets/javascripts/discourse/initializers/update-state.js.es6
index 80b3953..0856336 100644
--- a/assets/javascripts/discourse/initializers/update-state.js.es6
+++ b/assets/javascripts/discourse/initializers/update-state.js.es6
@@ -14,7 +14,6 @@ export default {
 
   initialize(container) {
     const currentUser = container.lookup("current-user:main");
-    const messageBus = container.lookup("message-bus:main");
     const siteSettings = container.lookup("site-settings:main");
     const appEvents = container.lookup("service:app-events");
     appEvents.on("encrypt:status-changed", (skipReload) => {
@@ -48,6 +47,7 @@ export default {
       });
     }
 
+    const messageBus = container.lookup("message-bus:main");
     if (messageBus && status !== ENCRYPT_DISABLED) {
       messageBus.subscribe("/plugin/encrypt/keys", function (data) {
         currentUser.setProperties({
diff --git a/assets/javascripts/lib/discourse.js.es6 b/assets/javascripts/lib/discourse.js.es6
index f5e45f4..a88b913 100644
--- a/assets/javascripts/lib/discourse.js.es6
+++ b/assets/javascripts/lib/discourse.js.es6
@@ -282,6 +282,7 @@ export function waitForPendingTitles() {
  * Gets current encryption status.
  *
  * @param {User} user
+ * @param {Object} siteSettings
  *
  * @return {Number} See `ENCRYPT_DISABLED`, `ENCRYPT_ENABLED` and
  *                  `ENCRYPT_ACTIVE`.
diff --git a/test/javascripts/lib/discourse-test.js.es6 b/test/javascripts/lib/discourse-test.js.es6
index f85bd5a..b99ad95 100644
--- a/test/javascripts/lib/discourse-test.js.es6
+++ b/test/javascripts/lib/discourse-test.js.es6
@@ -4,10 +4,11 @@ import { canEnableEncrypt } from "discourse/plugins/discourse-encrypt/lib/discou
 QUnit.module("discourse-encrypt:lib:discourse");
 
 test("canEnableEncrypt", async (assert) => {
-  Discourse.SiteSettings.encrypt_enabled = true;
-  Discourse.SiteSettings.encrypt_groups = "gRoUp";
-
   const user = User.create({ groups: [{ name: "GrOuP" }] });
+  const siteSettings = {
+    encrypt_enabled: true,
+    encrypt_groups: "gRoUp",
+  };
 
-  assert.ok(canEnableEncrypt(user, Discourse.SiteSettings));
+  assert.ok(canEnableEncrypt(user, siteSettings));
 });

GitHub sha: 792fb8c28db41349f2aaff44bf082b4c24b4a581

This commit appears in #97 which was approved by eviltrout. It was merged by udan11.