DEV: Minor code improvements.

DEV: Minor code improvements.

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 c3f3af6..fdc78d1 100644
--- a/assets/javascripts/discourse/connectors/composer-action-after/encrypt.js.es6
+++ b/assets/javascripts/discourse/connectors/composer-action-after/encrypt.js.es6
@@ -1,13 +1,27 @@
-import { hideComponentIfDisabled } from "discourse/plugins/discourse-encrypt/lib/discourse";
+import {
+  ENCRYPT_ACTIVE,
+  getEncryptionStatus
+} from "discourse/plugins/discourse-encrypt/lib/discourse";
 
 export default {
   setupComponent(args, component) {
+    const currentUser = Discourse.User.current();
     component.setProperties({
       model: args.model,
-      handler: hideComponentIfDisabled(component),
+      /** @var Whether the encryption is active on this device. */
+      isEncryptActive: getEncryptionStatus(currentUser) === ENCRYPT_ACTIVE,
+      /** Listens for encryptino status updates. */
+      listener() {
+        const newStatus = getEncryptionStatus(currentUser);
+        component.set("isEncryptActive", newStatus === ENCRYPT_ACTIVE);
+      },
+      didInsertElement() {
+        this._super(...arguments);
+        this.appEvents.on("encrypt:status-changed", this.get("listener"));
+      },
       willDestroyElement() {
         this._super(...arguments);
-        this.appEvents.off("encrypt:status-changed", this, this.get("handler"));
+        this.appEvents.off("encrypt:status-changed", this.get("listener"));
       }
     });
   }
diff --git a/assets/javascripts/discourse/connectors/user-preferences-account/encrypt.js.es6 b/assets/javascripts/discourse/connectors/user-preferences-account/encrypt.js.es6
index fb029b9..3b2ff44 100644
--- a/assets/javascripts/discourse/connectors/user-preferences-account/encrypt.js.es6
+++ b/assets/javascripts/discourse/connectors/user-preferences-account/encrypt.js.es6
@@ -1,13 +1,13 @@
 import { ajax } from "discourse/lib/ajax";
 import { popupAjaxError } from "discourse/lib/ajax-error";
-import { registerHelper } from "discourse-common/lib/helpers";
 import showModal from "discourse/lib/show-modal";
+import { registerHelper } from "discourse-common/lib/helpers";
 import {
   exportPrivateKey,
   exportPublicKey,
   generateKeyPair,
-  generateSalt,
   generatePassphraseKey,
+  generateSalt,
   importPrivateKey,
   importPublicKey
 } from "discourse/plugins/discourse-encrypt/lib/keys";
@@ -16,13 +16,12 @@ import {
   deleteIndexedDb
 } from "discourse/plugins/discourse-encrypt/lib/keys_db";
 import {
-  isEncryptEnabled,
-  hideComponentIfDisabled
-} from "discourse/plugins/discourse-encrypt/lib/discourse";
-import {
+  ENCRYPT_ACTIVE,
+  ENCRYPT_DISABLED,
+  getEncryptionStatus,
+  PACKED_KEY_FOOTER,
   PACKED_KEY_HEADER,
-  PACKED_KEY_SEPARATOR,
-  PACKED_KEY_FOOTER
+  PACKED_KEY_SEPARATOR
 } from "discourse/plugins/discourse-encrypt/lib/discourse";
 
 // TODO: I believe this should get into core.
@@ -65,11 +64,10 @@ function canEnableEncrypt(user) {
 export default {
   setupComponent(args, component) {
     const currentUser = Discourse.User.current();
-    const status = getEncryptionStatus(args.model);
     if (args.model.get("id") === currentUser.get("id")) {
+      const status = getEncryptionStatus(args.model);
       component.setProperties({
         model: args.model,
-        handler: hideComponentIfDisabled(component),
         /** @var Value of passphrase input (old, current and retyped).
          *       It should stay in memory for as little time as possible.
          *       Clear it often.
@@ -94,15 +92,21 @@ export default {
         importKey: false,
         /** @var Key to be imported .*/
         key: "",
-        // TOOD: Check out if there is a way to define functions like this in
-        //       the `export default` scope.
+        /** Listens for encryptino status updates. */
+        listener() {
+          const newStatus = getEncryptionStatus(args.model);
+          component.setProperties({
+            isEncryptEnabled: newStatus !== ENCRYPT_DISABLED,
+            isEncryptActive: newStatus === ENCRYPT_ACTIVE
+          });
+        },
+        didInsertElement() {
+          this._super(...arguments);
+          this.appEvents.on("encrypt:status-changed", this.get("listener"));
+        },
         willDestroyElement() {
           this._super(...arguments);
-          this.appEvents.off(
-            "encrypt:status-changed",
-            this,
-            this.get("handler")
-          );
+          this.appEvents.off("encrypt:status-changed", this.get("listener"));
         }
       });
       Ember.defineProperty(
@@ -233,11 +237,11 @@ export default {
           this.send("hidePassphraseInput");
           this.setProperties({
             inProgress: false,
-            isEncryptEnabled: true,
-            isEncryptActive: true,
             importKey: false,
             key: ""
           });
+
+          // window.location.reload();
         })
 
         .catch(popupAjaxError);
@@ -270,11 +274,9 @@ export default {
           this.appEvents.trigger("encrypt:status-changed");
 
           this.send("hidePassphraseInput");
-          this.setProperties({
-            inProgress: false,
-            isEncryptEnabled: true,
-            isEncryptActive: true
-          });
+          this.set("inProgress", false);
+
+          // window.location.reload();
         })
 
         .catch(() => {
@@ -336,11 +338,8 @@ export default {
 
       deleteIndexedDb().then(() => {
         this.appEvents.trigger("encrypt:status-changed");
-        this.setProperties({
-          inProgress: false,
-          isEncryptEnabled: true,
-          isEncryptActive: false
-        });
+        this.set("inProgress", false);
+        // window.location.reload();
       });
     },
 
diff --git a/assets/javascripts/discourse/controllers/export-keypair.js.es6 b/assets/javascripts/discourse/controllers/export-keypair.js.es6
index 2f07997..6b11c63 100644
--- a/assets/javascripts/discourse/controllers/export-keypair.js.es6
+++ b/assets/javascripts/discourse/controllers/export-keypair.js.es6
@@ -1,9 +1,9 @@
-import ModalFunctionality from "discourse/mixins/modal-functionality";
 import copyText from "discourse/lib/copy-text";
+import ModalFunctionality from "discourse/mixins/modal-functionality";
 import {
+  exportPublicKey,
   generatePassphraseKey,
-  importPrivateKey,
-  exportPublicKey
+  importPrivateKey
 } from "discourse/plugins/discourse-encrypt/lib/keys";
 import {
   PACKED_KEY_COLUMNS,
diff --git a/assets/javascripts/discourse/initializers/hook-composer.js.es6 b/assets/javascripts/discourse/initializers/hook-composer.js.es6
index 6fc699e..7302c82 100644
--- a/assets/javascripts/discourse/initializers/hook-composer.js.es6
+++ b/assets/javascripts/discourse/initializers/hook-composer.js.es6
@@ -1,26 +1,27 @@
-import Composer from "discourse/models/composer";
-import { ajax } from "discourse/lib/ajax";
 import {
   default as computed,
   observes,
   on
 } from "ember-addons/ember-computed-decorators";
+import { ajax } from "discourse/lib/ajax";
+import Composer from "discourse/models/composer";
 import {
-  encrypt,
   decrypt,
-  rsaDecrypt,
+  encrypt,
   exportKey,
-  importKey,
   generateKey,
-  importPublicKey
+  importKey,
+  importPublicKey,
+  rsaDecrypt
 } from "discourse/plugins/discourse-encrypt/lib/keys";
 import {
-  putTopicKey,
-  getTopicKey,
-  hasTopicKey,
+  ENCRYPT_ACTIVE,
+  getEncryptionStatus,
   getPrivateKey,
+  getTopicKey,
   getTopicTitle,
-  isEncryptEnabled
+  hasTopicKey,
+  putTopicKey
 } from "discourse/plugins/discourse-encrypt/lib/discourse";
 
 export default {
diff --git a/assets/javascripts/discourse/initializers/hook-decrypt-post.js.es6 b/assets/javascripts/discourse/initializers/hook-decrypt-post.js.es6
index 3ea8d92..c45a89c 100644
--- a/assets/javascripts/discourse/initializers/hook-decrypt-post.js.es6

[... diff too long, it was truncated ...]

GitHub sha: 9225ee73

1 Like

Did you mean to leave this commented?

Did you mean to leave this commented?

Regarding the commented lines, followed up in 63507b9e1669b2eb15ea26f385431456f0b11a98 and 1693ffe047a0cb6d02492f593b7642050b3c8b6c.

1 Like