FIX: Do not download and decrypt same file twice (#126)

FIX: Do not download and decrypt same file twice (#126)

The click handler that downloads and decrypts the file was registered every time the post was processed and that can happen multiple times.

  • DEV: Reduce usage of jQuery
diff --git a/assets/javascripts/discourse/initializers/decrypt-posts.js b/assets/javascripts/discourse/initializers/decrypt-posts.js
index 78e2051..40854db 100644
--- a/assets/javascripts/discourse/initializers/decrypt-posts.js
+++ b/assets/javascripts/discourse/initializers/decrypt-posts.js
@@ -150,7 +150,8 @@ function resolveShortUrlElement($el) {
       return;
     }
 
-    $el.on("click", () => {
+    $el.off("click.discourse-encrypt");
+    $el.on("click.discourse-encrypt", () => {
       downloadEncryptedFile(url, keyPromise, { type: $el.data("type") }).then(
         (file) => {
           const a = document.createElement("a");
@@ -200,7 +201,9 @@ function resolveShortUrlElement($el) {
   }
 }
 
-function postProcessPost(siteSettings, topicId, $post) {
+function postProcessPost(siteSettings, topicId, post) {
+  const $post = $(post);
+
   // Paint mentions
   const unseenMentions = linkSeenMentions($post, siteSettings);
   if (unseenMentions.length > 0) {
@@ -226,26 +229,27 @@ function postProcessPost(siteSettings, topicId, $post) {
   }
 
   // Resolve short URLs
-  $post.find("img[data-orig-src], a[data-orig-href]").each((_, el) => {
-    const $el = $(el);
-    const url = $el.data("orig-src") || $el.data("orig-href");
-
-    if (lookupCachedUploadUrl(url).url) {
-      resolveShortUrlElement($el);
-    } else {
-      if (!shortUrlsQueue) {
-        shortUrlsQueue = new DebouncedQueue(500, (items) =>
-          lookupUncachedUploadUrls(items, ajax)
-        );
-      }
+  post
+    .querySelectorAll("img[data-orig-src], a[data-orig-href]")
+    .forEach((el) => {
+      const url = el.dataset.origSrc || el.dataset.origHref;
+
+      if (lookupCachedUploadUrl(url).url) {
+        resolveShortUrlElement($(el));
+      } else {
+        if (!shortUrlsQueue) {
+          shortUrlsQueue = new DebouncedQueue(500, (items) =>
+            lookupUncachedUploadUrls(items, ajax)
+          );
+        }
 
-      shortUrlsQueue.push(url).then(() => resolveShortUrlElement($el));
-    }
-  });
+        shortUrlsQueue.push(url).then(() => resolveShortUrlElement($(el)));
+      }
+    });
 
   // Load Oneboxes
   loadOneboxes(
-    $post[0],
+    post,
     ajax,
     topicId,
     null, // categoryId
@@ -455,12 +459,10 @@ export default {
         } else if (state.encryptState === "decrypted") {
           attrs.cooked = state.plaintext;
           next(() => {
-            let $post = $(`article[data-post-id='${attrs.id}']`);
-            if ($post.length === 0) {
-              $post = $(`#post_${attrs.post_number}.small-action`);
-            }
-
-            postProcessPost(this.siteSettings, topicId, $post);
+            const post =
+              document.querySelector(`article[data-post-id='${attrs.id}']`) ||
+              document.querySelector(`#post_${attrs.post_number}.small-action`);
+            postProcessPost(this.siteSettings, topicId, post);
           });
         } else if (state.encryptState === "error") {
           attrs.cooked =

GitHub sha: 6e337e0ef68e35a1bf8000778234de95ae1a21e7

This commit appears in #126 which was approved by eviltrout. It was merged by udan11.