FIX: Make sure all user identity promises are eventually resolved (#41)

FIX: Make sure all user identity promises are eventually resolved (#41)

getDebouncedUserIdentities used to resolve only the first returned promise. It has been refactored into getDebouncedUserIdentity to simplify the API and the logic behind it.

The green signature checkmark displayed in the UI is displayed only after the verification process finishes. This did not happen because the whole process was stuck waiting for the the promise returned by getDebouncedUserIdentities to be resolved.

diff --git a/assets/javascripts/discourse/initializers/hook-decrypt-post.js.es6 b/assets/javascripts/discourse/initializers/hook-decrypt-post.js.es6
index 59b9915..7d3aba7 100644
--- a/assets/javascripts/discourse/initializers/hook-decrypt-post.js.es6
+++ b/assets/javascripts/discourse/initializers/hook-decrypt-post.js.es6
@@ -16,7 +16,7 @@ import lightbox from "discourse/lib/lightbox";
 
 import {
   ENCRYPT_DISABLED,
-  getDebouncedUserIdentities,
+  getDebouncedUserIdentity,
   getEncryptionStatus,
   getIdentity,
   getTopicKey,
@@ -400,10 +400,10 @@ export default {
                   .then((key) => decrypt(key, ciphertext))
                   .then((plaintext) => {
                     if (plaintext.signature) {
-                      getDebouncedUserIdentities([plaintext.signed_by_name])
-                        .then((identities) => {
+                      getDebouncedUserIdentity(plaintext.signed_by_name)
+                        .then((userIdentity) => {
                           return verify(
-                            identities[plaintext.signed_by_name].signPublic,
+                            userIdentity.signPublic,
                             plaintext,
                             ciphertext
                           );
diff --git a/assets/javascripts/lib/discourse.js.es6 b/assets/javascripts/lib/discourse.js.es6
index e12842c..e91f007 100644
--- a/assets/javascripts/lib/discourse.js.es6
+++ b/assets/javascripts/lib/discourse.js.es6
@@ -114,28 +114,37 @@ export function getUserIdentities(usernames) {
   });
 }
 
-const debouncedUsernames = new Set();
+const queuedUsernames = {};
 
-function _getDebouncedUserIdentities(resolve, reject) {
-  getUserIdentities(Array.from(debouncedUsernames))
+function _getDebouncedUserIdentity() {
+  const usernames = Object.keys(queuedUsernames);
+  getUserIdentities(usernames)
     .then((identities) => {
-      Object.keys(identities).forEach((u) => debouncedUsernames.delete(u));
-      return identities;
+      Object.keys(identities).forEach((username) => {
+        if (queuedUsernames[username]) {
+          const identity = identities[username];
+          queuedUsernames[username].forEach(({ resolve }) => resolve(identity));
+          delete queuedUsernames[username];
+        }
+      });
     })
-    .then(resolve, reject);
+    .catch(() => {
+      usernames.forEach((username) => {
+        if (queuedUsernames[username]) {
+          queuedUsernames[username].forEach(({ reject }) => reject());
+          delete queuedUsernames[username];
+        }
+      });
+    });
 }
 
-export function getDebouncedUserIdentities(usernames) {
-  usernames.forEach((u) => debouncedUsernames.add(u));
-
+export function getDebouncedUserIdentity(username) {
   return new Promise((resolve, reject) => {
-    debounce(
-      debouncedUsernames,
-      _getDebouncedUserIdentities,
-      resolve,
-      reject,
-      500
-    );
+    if (!queuedUsernames[username]) {
+      queuedUsernames[username] = [];
+    }
+    queuedUsernames[username].push({ resolve, reject });
+    debounce(queuedUsernames, _getDebouncedUserIdentity, 500);
   });
 }
 

GitHub sha: c1bacf04

This commit appears in #41 which was approved by ZogStriP. It was merged by udan11.