FIX: Refactor invite hook.

FIX: Refactor invite hook.

diff --git a/assets/javascripts/discourse/initializers/hook-invite.js.es6 b/assets/javascripts/discourse/initializers/hook-invite.js.es6
index 26d163e..afc4289 100644
--- a/assets/javascripts/discourse/initializers/hook-invite.js.es6
+++ b/assets/javascripts/discourse/initializers/hook-invite.js.es6
@@ -2,11 +2,12 @@ import { ajax } from "discourse/lib/ajax";
 import Topic from "discourse/models/topic";
 import {
   ENCRYPT_ACTIVE,
-  exportKey,
   getEncryptionStatus,
   getTopicKey,
+  getUserIdentities,
   hasTopicKey
 } from "discourse/plugins/discourse-encrypt/lib/discourse";
+import { exportKey } from "discourse/plugins/discourse-encrypt/lib/protocol";
 
 export default {
   name: "hook-invite",
@@ -25,31 +26,25 @@ export default {
           return _super.call(this, ...arguments);
         }
 
-        const topicKeyPromise = getTopicKey(this.id);
-        const userKeyPromise = ajax("/encrypt/user", {
-          type: "GET",
-          data: { usernames: [user] }
-        })
-          .then(userKeys => {
-            if (!userKeys[user]) {
-              bootbox.alert(
-                I18n.t("encrypt.composer.user_has_no_key", { username: user })
-              );
-              return Ember.RSVP.Promise.reject(user);
-            }
-
-            return userKeys[user];
-          })
-          .then(key => importPublicKey(key));
-
-        return Ember.RSVP.Promise.all([topicKeyPromise, userKeyPromise])
-          .then(([topicKey, userKey]) => exportKey(topicKey, userKey))
-          .then(key =>
+        return Ember.RSVP.Promise.all([
+          getTopicKey(this.id),
+          getUserIdentities([user])
+        ])
+          .then(([key, identities]) =>
+            exportKey(key, identities[user].encryptPublic)
+          )
+          .then(key => {
             ajax(`/t/${this.get("id")}/invite`, {
               type: "POST",
               data: { user, key, group_names, custom_message }
-            })
-          );
+            });
+          })
+          .catch(username => {
+            bootbox.alert(
+              I18n.t("encrypt.composer.user_has_no_key", { username })
+            );
+            return Ember.RSVP.Promise.reject(username);
+          });
       }
     });
   }

GitHub sha: 6ff38734