FIX: Reset decryption state if post changes.

FIX: Reset decryption state if post changes.

From 708b00d1b1e67eb769516a9f5603e37275e3ef98 Mon Sep 17 00:00:00 2001
From: Dan Ungureanu <dan@ungureanu.me>
Date: Thu, 29 Nov 2018 18:18:55 +0200
Subject: [PATCH] FIX: Reset decryption state if post changes.


diff --git a/assets/javascripts/discourse/initializers/hook-decrypt-post.js.es6 b/assets/javascripts/discourse/initializers/hook-decrypt-post.js.es6
index dff5745..32fe307 100644
--- a/assets/javascripts/discourse/initializers/hook-decrypt-post.js.es6
+++ b/assets/javascripts/discourse/initializers/hook-decrypt-post.js.es6
@@ -25,9 +25,17 @@ export default {
             return this._super(...arguments);
           }
 
+          // Check if post has been updated (if last decrypted ciphertext
+          // is different than the current ciphertext).
+          const ciphertext = $(attrs.cooked).text();
+          if (state.encrypted && state.encrypted !== ciphertext) {
+            state.decrypting = false;
+            state.decrypted = undefined;
+          }
+
           if (hasTopicKey(topicId) && !state.decrypted) {
+            state.encrypted = ciphertext;
             state.decrypting = true;
-            const ciphertext = $(attrs.cooked).text();
 
             getTopicKey(topicId)
               .then(key => decrypt(key, ciphertext))

GitHub