FIX: Encrypted title was not always available due to a race condition.

FIX: Encrypted title was not always available due to a race condition.

User may be redirected to topic page before its title is sent to the server which makes it unavailable for decryption.

diff --git a/assets/javascripts/discourse/initializers/hook-composer.js.es6 b/assets/javascripts/discourse/initializers/hook-composer.js.es6
index 7302c82..6225652 100644
--- a/assets/javascripts/discourse/initializers/hook-composer.js.es6
+++ b/assets/javascripts/discourse/initializers/hook-composer.js.es6
@@ -21,7 +21,8 @@ import {
   getTopicKey,
   getTopicTitle,
   hasTopicKey,
-  putTopicKey
+  putTopicKey,
+  putTopicTitle
 } from "discourse/plugins/discourse-encrypt/lib/discourse";
 
 export default {
@@ -111,10 +112,14 @@ export default {
             .then(key => {
               const p0 = encrypt(key, reply).then(r => this.set("reply", r));
               const p1 = 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: this.get("topic.id"), title: encTitle }
+                  data: { topic_id: topicId, title: encTitle }
                 });
               });
 
@@ -187,6 +192,8 @@ export default {
             const topicId = result.responseJson.post.topic_id;
 
             putTopicKey(topicId, key);
+            putTopicTitle(topicId, encTitle);
+
             ajax("/encrypt/topic", {
               type: "PUT",
               data: { topic_id: topicId, title: encTitle, keys: userKeys }
diff --git a/assets/javascripts/lib/discourse.js.es6 b/assets/javascripts/lib/discourse.js.es6
index db9de42..7c14572 100644
--- a/assets/javascripts/lib/discourse.js.es6
+++ b/assets/javascripts/lib/discourse.js.es6
@@ -140,7 +140,7 @@ export function hasTopicKey(topicId) {
  * @param key
  */
 export function putTopicTitle(topicId, title) {
-  if (topicId && !topicTitles[topicId]) {
+  if (topicId && title) {
     topicTitles[topicId] = title;
   }
 }

GitHub sha: 22ca3909

1 Like