DEV: Consolidate encrypted topic creation methods.

DEV: Consolidate encrypted topic creation methods.

Prefer overriding existing APIs rather than creating new ones that require additional calls.

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 d509852..b7eceee 100644
--- a/assets/javascripts/discourse/connectors/user-preferences-account/encrypt.js.es6
+++ b/assets/javascripts/discourse/connectors/user-preferences-account/encrypt.js.es6
@@ -242,17 +242,14 @@ export default {
         // 3. Reset component status.
         .then(() => {
           this.appEvents.trigger("encrypt:status-changed");
-
           this.send("hidePassphraseInput");
-          this.set("inProgress", false);
-
           reload();
         })
 
-        .catch(() => {
-          this.set("inProgress", false);
-          bootbox.alert(I18n.t("encrypt.preferences.passphrase_invalid"));
-        });
+        .catch(() =>
+          bootbox.alert(I18n.t("encrypt.preferences.passphrase_invalid"))
+        )
+        .finally(() => this.set("inProgress", false));
     },
 
     changeEncrypt() {
@@ -292,25 +289,22 @@ export default {
         })
 
         // 4. Reset component status.
-        .then(() => {
-          this.send("hidePassphraseInput");
-          this.set("inProgress", false);
-        })
-
-        .catch(() => {
-          this.set("inProgress", false);
-          bootbox.alert(I18n.t("encrypt.preferences.passphrase_invalid"));
-        });
+        .then(() => this.send("hidePassphraseInput"))
+        .catch(() =>
+          bootbox.alert(I18n.t("encrypt.preferences.passphrase_invalid"))
+        )
+        .finally(() => this.set("inProgress", false));
     },
 
     deactivateEncrypt() {
       this.setProperties("inProgress", true);
 
-      deleteIndexedDb().then(() => {
-        this.appEvents.trigger("encrypt:status-changed");
-        this.set("inProgress", false);
-        reload();
-      });
+      deleteIndexedDb()
+        .then(() => {
+          this.appEvents.trigger("encrypt:status-changed");
+          reload();
+        })
+        .finally(() => this.set("inProgress", false));
     },
 
     export() {
diff --git a/assets/javascripts/discourse/controllers/activate-encrypt.js.es6 b/assets/javascripts/discourse/controllers/activate-encrypt.js.es6
index c550e4a..5671d6d 100644
--- a/assets/javascripts/discourse/controllers/activate-encrypt.js.es6
+++ b/assets/javascripts/discourse/controllers/activate-encrypt.js.es6
@@ -60,10 +60,10 @@ export default Ember.Controller.extend(ModalFunctionality, {
           this.send("closeModal");
         })
 
-        .catch(() => {
-          this.set("inProgress", false);
-          this.set("error", I18n.t("encrypt.preferences.passphrase_invalid"));
-        });
+        .catch(() =>
+          this.set("error", I18n.t("encrypt.preferences.passphrase_invalid"))
+        )
+        .finally(() => this.set("inProgress", false));
     }
   }
 });
diff --git a/assets/javascripts/discourse/initializers/hook-composer.js.es6 b/assets/javascripts/discourse/initializers/hook-composer.js.es6
index 14a3e30..2a1413a 100644
--- a/assets/javascripts/discourse/initializers/hook-composer.js.es6
+++ b/assets/javascripts/discourse/initializers/hook-composer.js.es6
@@ -1,16 +1,8 @@
-import {
-  default as computed,
-  observes,
-  on
-} from "ember-addons/ember-computed-decorators";
+import { observes, on } from "ember-addons/ember-computed-decorators";
 import { ajax } from "discourse/lib/ajax";
 import Composer from "discourse/models/composer";
 import {
   decrypt,
-  encrypt,
-  exportKey,
-  generateKey,
-  importPublicKey,
   rsaDecrypt
 } from "discourse/plugins/discourse-encrypt/lib/keys";
 import {
@@ -19,13 +11,12 @@ import {
   getPrivateKey,
   getTopicKey,
   getTopicTitle,
-  hasTopicKey,
-  putTopicKey,
-  putTopicTitle
+  hasTopicKey
 } from "discourse/plugins/discourse-encrypt/lib/discourse";
 
 export default {
   name: "hook-composer",
+
   initialize(container) {
     const currentUser = container.lookup("current-user:main");
     if (getEncryptionStatus(currentUser) !== ENCRYPT_ACTIVE) {
@@ -33,133 +24,10 @@ export default {
     }
 
     // Register custom fields to be saved for new post.
-    Composer.serializeOnCreate("encryptedTitle", "encryptedTitle");
-    Composer.serializeOnCreate("encryptedRaw", "encryptedRaw");
-    Composer.serializeOnCreate("encryptedKeys", "encryptedKeys");
+    Composer.serializeOnCreate("is_encrypted", "isEncrypted");
 
-    // Encrypt the Composer contents on-the-fly right before it is sent over
-    // to the server.
+    // Check recipients and show encryption status in composer.
     Composer.reopen({
-      getCookedHtml() {
-        return hasTopicKey(this.get("topic.id"))
-          ? ""
-          : this._super(...arguments);
-      },
-
-      save() {
-        // TODO: https://github.com/emberjs/ember.js/issues/15291
-        let { _super } = this;
-        if (!this.privateMessage) {
-          return _super.call(this, ...arguments);
-        }
-
-        const title = this.title;
-        const reply = this.reply;
-
-        if (this.get("topic.topic_key")) {
-          putTopicKey(this.get("topic.id"), this.get("topic.topic_key"));
-          return getTopicKey(this.get("topic.id"))
-            .then(key => {
-              const promises = [];
-
-              if (title) {
-                promises.push(
-                  encrypt(key, title).then(encTitle => {
-                    const topicId = this.get("topic.id");
-
-                    this.set("title", I18n.t("encrypt.encrypted_topic_title"));
-                    putTopicTitle(topicId, encTitle);
-
-                    ajax("/encrypt/topic", {
-                      type: "PUT",
-                      data: { topic_id: topicId, title: encTitle }
-                    });
-                  })
-                );
-              }
-
-              if (reply) {
-                promises.push(
-                  encrypt(key, reply).then(encReply =>
-                    this.set("reply", encReply)
-                  )
-                );
-              }
-
-              return Ember.RSVP.Promise.all(promises);
-            })
-            .then(() => _super.call(this, ...arguments))
-            .finally(() => this.setProperties({ title, reply }));
-        }
-
-        // Not encrypted messages.
-        if (!this.isEncrypted) {
-          return _super.call(this, ...arguments);
-        }
-
-        // Generating a new topic key.
-        const topicKey = generateKey();
-
-        const usernames = this.recipients;
-        const encryptedKeys = topicKey.then(key =>
-          ajax("/encrypt/user", {
-            type: "GET",
-            data: { usernames }
-          })
-            .then(userKeys => {
-              const promises = [];
-
-              for (let i = 0; i < usernames.length; ++i) {
-                const username = usernames[i];
-                if (!userKeys[username]) {
-                  promises.push(Ember.RSVP.Promise.reject(username));
-                } else {
-                  promises.push(
-                    importPublicKey(userKeys[username]).then(userKey =>
-                      exportKey(key, userKey)
-                    )
-                  );
-                }
-              }
-
-              return Ember.RSVP.Promise.all(promises);
-            })
-            .catch(username => {
-              bootbox.alert(
-                I18n.t("encrypt.composer.user_has_no_key", { username })
-              );
-              return Ember.RSVP.Promise.reject(username);
-            })
-        );
-
-        const encryptedTitle = topicKey.then(key => encrypt(key, title));
-        const encryptedRaw = topicKey.then(key => encrypt(key, reply));
-
-        // Send user keys, title and reply encryption to the server.
-        return Ember.RSVP.Promise.all([
-          encryptedTitle,
-          encryptedRaw,
-          encryptedKeys
-        ]).then(([encTitle, encReply, encKeys]) => {
-          const userKeys = {};
-          for (let i = 0; i < encKeys.length; ++i) {

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

GitHub sha: 44635235