DEV: Refactor composer's encryption indicator.

DEV: Refactor composer’s encryption indicator.

diff --git a/assets/javascripts/discourse/connectors/composer-action-after/encrypt.hbs b/assets/javascripts/discourse/connectors/composer-action-after/encrypt.hbs
index cbe4217..2b1d159 100644
--- a/assets/javascripts/discourse/connectors/composer-action-after/encrypt.hbs
+++ b/assets/javascripts/discourse/connectors/composer-action-after/encrypt.hbs
@@ -1,7 +1,7 @@
 {{#if (or model.creatingPrivateMessage model.topic.encrypted_title)}}
   {{#if isEncryptActive}}
     <a {{action clicked}} title={{i18n title}}>
-      {{d-icon (if model.isEncrypted "lock" "unlock") class=(if model.isEncryptedDisabled "disabled")}}
+      {{d-icon (if model.isEncrypted "lock" "unlock") class=(if model.disableEncryptIndicator "disabled")}}
     </a>
 
     {{#if model.showEncryptError}}
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 5d44f38..a70ffc1 100644
--- a/assets/javascripts/discourse/connectors/composer-action-after/encrypt.js.es6
+++ b/assets/javascripts/discourse/connectors/composer-action-after/encrypt.js.es6
@@ -20,7 +20,7 @@ export default {
         const newStatus = getEncryptionStatus(currentUser);
         component.setProperties({
           isEncryptEnabled: newStatus !== ENCRYPT_DISABLED,
-          isEncryptActive: newStatus === ENCRYPT_ACTIVE,
+          isEncryptActive: newStatus === ENCRYPT_ACTIVE
         });
       },
 
@@ -36,7 +36,7 @@ export default {
 
       clicked() {
         this.set("model.showEncryptError", true);
-        if (!this.get("model.isEncryptedDisabled")) {
+        if (!this.get("model.disableEncryptIndicator")) {
           this.set("model.isEncrypted", !this.get("model.isEncrypted"));
         }
       }
@@ -47,12 +47,12 @@ export default {
       "title",
       Ember.computed(
         "model.isEncrypted",
-        "model.isEncryptedDisabled",
+        "model.disableEncryptIndicator",
         () => {
-          if (this.model.isEncryptedDisabled) {
-            return "encrypt.checkbox.disabled";
-          } else if (this.model.isEncrypted) {
+          if (this.model.isEncrypted) {
             return "encrypt.checkbox.checked";
+          } else if (this.model.disableEncryptIndicator) {
+            return "encrypt.checkbox.disabled";
           } else {
             return "encrypt.checkbox.unchecked";
           }
diff --git a/assets/javascripts/discourse/initializers/hook-composer.js.es6 b/assets/javascripts/discourse/initializers/hook-composer.js.es6
index 2a1413a..86575f9 100644
--- a/assets/javascripts/discourse/initializers/hook-composer.js.es6
+++ b/assets/javascripts/discourse/initializers/hook-composer.js.es6
@@ -28,42 +28,46 @@ export default {
 
     // Check recipients and show encryption status in composer.
     Composer.reopen({
-      @on("init")
-      initEncrypt() {
+      updateEncryptProperties() {
+        const encryptedTopic = this.topic && this.topic.encrypted_title;
         this.setProperties({
-          isEncryptedDisabled: false,
-          isEncrypted: false,
+          /** @var Whether the current message is going to be encrypted. */
+          isEncrypted: encryptedTopic,
+          /** @var Disable encrypt indicator to enforce encrypted message, if
+                   message is encrypted, or enforce decrypted message if one
+                   of the recipients does not have encryption enabled. */
+          disableEncryptIndicator: encryptedTopic,
+          /** @var Current encryption error. */
           encryptError: "",
+          /** @var Immediately show encryption error if it is fatal. */
           showEncryptError: false
         });
       },
 
-      @observes("topic")
-      topicUpdated() {
-        const value = hasTopicKey(this.get("topic.id"));
-        this.setProperties({
-          isEncryptedDisabled: false,
-          isEncrypted: value,
-          encryptError: value ? "" : I18n.t("encrypt.cannot_encrypt"),
-          showEncryptError: true
-        });
+      @on("init")
+      initEncrypt() {
+        this.updateEncryptProperties();
+      },
+
+      @observes("creatingPrivateMessage", "topic")
+      testasdd() {
+        this.updateEncryptProperties();
       },
 
       @observes("targetUsernames")
       checkKeys() {
-        const targetUsernames = this.get("targetUsernames");
-        const usernames = targetUsernames ? targetUsernames.split(",") : [];
-        usernames.push(this.get("user.username"));
-
-        if (usernames.length === 0) {
+        if (!this.targetUsernames) {
           this.setProperties({
-            isEncryptedDisabled: false,
             isEncrypted: true,
+            disableEncryptIndicator: false,
             encryptError: ""
           });
           return;
         }
 
+        const usernames = this.targetUsernames.split(",");
+        usernames.push(this.user.username);
+
         ajax("/encrypt/user", {
           type: "GET",
           data: { usernames }
@@ -72,23 +76,25 @@ export default {
             const username = usernames[i];
             if (!userKeys[username]) {
               this.setProperties({
-                isEncryptedDisabled: true,
                 isEncrypted: false,
+                disableEncryptIndicator: true,
                 encryptError: I18n.t("encrypt.composer.user_has_no_key", {
                   username
-                })
+                }),
+                showEncryptError: this.showEncryptError || this.isEncrypted
               });
               return;
             }
           }
 
-          // Remember user preferences. If user enters a recipient, unchecks
+          // Automatically enable encryption so people do not forget, but
+          // remember user preferences. If user enters a recipient, unchecks
           // encryption and then adds another recipient, this will not revert
           // his uncheck.
-          if (this.isEncryptedDisabled) {
+          if (this.disableEncryptIndicator) {
             this.setProperties({
-              isEncryptedDisabled: false,
               isEncrypted: true,
+              disableEncryptIndicator: false,
               encryptError: ""
             });
           }

GitHub sha: 5d40189e