DEV: Refactoring.

DEV: Refactoring.
From 3f9525eee4078ccc23e93fdd681e1471e7f723b2 Mon Sep 17 00:00:00 2001
From: Dan Ungureanu <dan@ungureanu.me>
Date: Fri, 7 Dec 2018 13:29:17 +0200
Subject: [PATCH] DEV: Refactoring.


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 9f430b0..c3f3af6 100644
--- a/assets/javascripts/discourse/connectors/composer-action-after/encrypt.js.es6
+++ b/assets/javascripts/discourse/connectors/composer-action-after/encrypt.js.es6
@@ -3,9 +3,12 @@ import { hideComponentIfDisabled } from "discourse/plugins/discourse-encrypt/lib
 export default {
   setupComponent(args, component) {
     component.setProperties({
-      model: args.model
+      model: args.model,
+      handler: hideComponentIfDisabled(component),
+      willDestroyElement() {
+        this._super(...arguments);
+        this.appEvents.off("encrypt:status-changed", this, this.get("handler"));
+      }
     });
-
-    hideComponentIfDisabled(component);
   }
 };
diff --git a/assets/javascripts/discourse/connectors/user-preferences-account/encrypt.hbs b/assets/javascripts/discourse/connectors/user-preferences-account/encrypt.hbs
index 25fb139..05ceeeb 100644
--- a/assets/javascripts/discourse/connectors/user-preferences-account/encrypt.hbs
+++ b/assets/javascripts/discourse/connectors/user-preferences-account/encrypt.hbs
@@ -2,8 +2,8 @@
   <div class="control-group pref-title">
     <label class="control-label">{{i18n 'encrypt.title'}}</label>
     <div class="controls">
-      {{#if isEnabled}}
-        {{#if isActive}}
+      {{#if isEncryptEnabled}}
+        {{#if isEncryptActive}}
           <p>{{i18n 'encrypt.preferences.status_enabled'}}</p>
           {{#if passphraseInput}}
             <form>
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 fd0b69a..ddcb7b2 100644
--- a/assets/javascripts/discourse/connectors/user-preferences-account/encrypt.js.es6
+++ b/assets/javascripts/discourse/connectors/user-preferences-account/encrypt.js.es6
@@ -15,9 +15,10 @@ import {
   deleteIndexedDb
 } from "discourse/plugins/discourse-encrypt/lib/keys_db";
 import {
-  ENCRYPT_DISBLED,
+  ENCRYPT_DISABLED,
   ENCRYPT_ACTIVE,
-  getEncryptionStatus
+  getEncryptionStatus,
+  hideComponentIfDisabled
 } from "discourse/plugins/discourse-encrypt/lib/discourse";
 
 // TODO: I believe this should get into core.
@@ -27,39 +28,42 @@ registerHelper("or", ([a, b]) => a || b);
 
 export default {
   setupComponent(args, component) {
-    getEncryptionStatus().then(status => {
-      component.setProperties({
-        model: args.model,
-        save: args.save,
-        /** @var Value of passphrase input.
-         *       It should stay in memory for as little time as possible.
-         *       Clear it often.
-         */
-        passphrase: "",
-        passphrase2: "",
-        /** @var Whether the passphrase input is shown. */
-        passphraseInput: false,
-        /** @var Whether any operation (AJAX request, key generation, etc.) is
-         *       in progress. */
-        inProgress: false,
-        /** @var Whether the encryption is enabled or not. */
-        isEnabled: status !== ENCRYPT_DISBLED,
-        /** @var Whether the encryption is active on this device. */
-        isActive: status === ENCRYPT_ACTIVE,
-        // TOOD: Check out if there is a way to define functions like this in
-        //       the `export default` scope.
-        passphraseStatus: function() {
-          const passphrase = component.get("passphrase");
-          const passphrase2 = component.get("passphrase2");
-          if (!passphrase) {
-            return "encrypt.preferences.passphrase_enter";
-          } else if (passphrase.length < 15) {
-            return "encrypt.preferences.passphrase_insecure";
-          } else if (passphrase !== passphrase2) {
-            return "encrypt.preferences.passphrase_mismatch";
-          }
-        }.property("passphrase", "passphrase2")
-      });
+    component.setProperties({
+      model: args.model,
+      handler: hideComponentIfDisabled(component),
+      save: args.save,
+      /** @var Value of passphrase input.
+       *       It should stay in memory for as little time as possible.
+       *       Clear it often.
+       */
+      passphrase: "",
+      passphrase2: "",
+      /** @var Whether the passphrase input is shown. */
+      passphraseInput: false,
+      /** @var Whether any operation (AJAX request, key generation, etc.) is
+       *       in progress. */
+      inProgress: false,
+      /** @var Whether the encryption is enabled or not. */
+      isEncryptEnabled: false,
+      /** @var Whether the encryption is active on this device. */
+      isEncryptActive: false,
+      // TOOD: Check out if there is a way to define functions like this in
+      //       the `export default` scope.
+      passphraseStatus: function() {
+        const passphrase = component.get("passphrase");
+        const passphrase2 = component.get("passphrase2");
+        if (!passphrase) {
+          return "encrypt.preferences.passphrase_enter";
+        } else if (passphrase.length < 15) {
+          return "encrypt.preferences.passphrase_insecure";
+        } else if (passphrase !== passphrase2) {
+          return "encrypt.preferences.passphrase_mismatch";
+        }
+      }.property("passphrase", "passphrase2"),
+      willDestroyElement() {
+        this._super(...arguments);
+        this.appEvents.off("encrypt:status-changed", this, this.get("handler"));
+      }
     });
   },
 
@@ -131,14 +135,18 @@ export default {
         )
 
         // 6. Reset component status.
-        .then(() => {
-          this.send("hidePassphraseInput");
-          this.setProperties({
-            inProgress: false,
-            isEnabled: true,
-            isActive: true
-          });
-        })
+        .then(() =>
+          Ember.run(() => {
+            this.appEvents.trigger("encrypt:status-changed");
+
+            this.send("hidePassphraseInput");
+            this.setProperties({
+              inProgress: false,
+              isEncryptEnabled: true,
+              isEncryptActive: true
+            });
+          })
+        )
 
         .catch(popupAjaxError);
     },
@@ -166,16 +174,18 @@ export default {
         )
 
         // 3. Reset component status.
-        .then(() => {
-          this.appEvents.trigger("encrypt:status-changed");
-
-          this.send("hidePassphraseInput");
-          this.setProperties({
-            inProgress: false,
-            isEnabled: true,
-            isActive: true
-          });
-        })
+        .then(() =>
+          Ember.run(() => {
+            this.appEvents.trigger("encrypt:status-changed");
+
+            this.send("hidePassphraseInput");
+            this.setProperties({
+              inProgress: false,
+              isEncryptEnabled: true,
+              isEncryptActive: true
+            });
+          })
+        )
 
         .catch(() => {
           this.set("inProgress", false);
@@ -238,8 +248,8 @@ export default {
         this.appEvents.trigger("encrypt:status-changed");
         this.setProperties({
           inProgress: false,
-          isEnabled: true,
-          isActive: false
+          isEncryptEnabled: true,
+          isEncryptActive: false
         });
       });
     }
diff --git a/assets/javascripts/lib/discourse.js.es6 b/assets/javascripts/lib/discourse.js.es6
index bb576f1..80fcb06 100644
--- a/assets/javascripts/lib/discourse.js.es6
+++ b/assets/javascripts/lib/discourse.js.es6
@@ -8,11 +8,11 @@ import { loadKeyPairFromIndexedDb } from "discourse/plugins/discourse-encrypt/li
 /**
  * Possible states of the encryption system.
  *
- * @var ENCRYPT_DISBLED User does not have any generated keys
+ * @var ENCRYPT_DISABLED User does not have any gener

GitHub

Probably a slightly better description is needed here I can tell there are a few bug fixes here.

Yes, the commit message is not very good.

This commit ensures that subscriptions are canceled on element destroy (and also fixes a bug).

2 Likes