When forwarding an email, pre-load its attachments.
authorArt Cancro <ajc@citadel.org>
Mon, 18 Dec 2023 19:26:30 +0000 (14:26 -0500)
committerArt Cancro <ajc@citadel.org>
Mon, 18 Dec 2023 19:26:30 +0000 (14:26 -0500)
This completes the effort to server-side-preload the attachments from
the original message.  They appear as attachments in the forwarded copy
of the message in the composer, using the same API.  A lot of effort
went into making the 'attach' and 'forward attachments' functions DRY
with respect to each other.

webcit-ng/static/js/upload.js
webcit-ng/static/js/view_mail.js

index 54713f5dd9c6493564aad330e6a0453d9b3d1911..3f6a33519f4372f64f0102c0e3d1b3eda048577a 100644 (file)
@@ -97,11 +97,25 @@ delete_upload = async(ref) => {
                el.parentNode.removeChild(el);
                uploads = uploads.filter((r) => r.ref != ref);                                  // ...remove it from the array...
                update_attachment_count();
-               // document.getElementById(attachment_counter_divs[0]).innerHTML = uploads.length;      // ...and update our count
        }
 }
 
 
+// When an upload (or reupload) is complete, call this function to add it to the on-screen attachments list.
+function add_upload_to_displayed_list(item) {
+       let new_upl = document.createElement("li");
+       new_upl.setAttribute("id", item["ref"]);        // set the element id to the upload reference
+       new_upl.innerHTML = `<i class="fa-solid fa-circle-xmark" style="color:red" onClick="delete_upload('` + item["ref"] + `')"></i>`
+       + `&nbsp;`
+       + item["uploadfilename"] + " (" + item["contenttype"] + ", " + item["contentlength"] + " " + _("bytes") + ")";
+       document.getElementById("ctdl-upload_list").appendChild(new_upl);
+
+       // append it to the global list of uploads
+       uploads.push(item);
+       update_attachment_count();
+}
+
+
 function upload_file(file) {
        var url = '/ctdl/p/';
        var xhr = new XMLHttpRequest();
@@ -119,19 +133,7 @@ function upload_file(file) {
                        var j_response = JSON.parse(xhr.response);
 
                        // Add these uploads to the displayed list
-                       j_response.forEach((item) => {
-                               let new_upl = document.createElement("li");
-                               new_upl.setAttribute("id", item["ref"]);        // set the element id to the upload reference
-                               new_upl.innerHTML = `<i class="fa-solid fa-circle-xmark" style="color:red" onClick="delete_upload('` + item["ref"] + `')"></i>`
-                               + `&nbsp;`
-                               + item["uploadfilename"] + " (" + item["contenttype"] + ", " + item["contentlength"] + " " + _("bytes") + ")";
-                               document.getElementById("ctdl-upload_list").appendChild(new_upl);
-
-                               // append it to the global list of uploads
-                               uploads.push(item);
-                       });
-                       update_attachment_count();
-                       // document.getElementById(attachment_counter_divs[0]).innerHTML = uploads.length;
+                       j_response.forEach((item) => add_upload_to_displayed_list(item));
                }
                else if (xhr.readyState == 4 && xhr.status != 200) {
                        // remove the "uploading in progress" message (there was an error, so just let it disappear)
@@ -204,7 +206,20 @@ function flush_uploads() {
        });
         uploads=[];
        update_attachment_count();
-       // document.getElementById(attachment_counter_divs[0]).innerHTML = uploads.length;
 
        deactivate_uploads();   // this makes the window get destroyed too
 }
+
+
+// Preload a forwarded message's attachments into the new copy currently being composed.
+function forward_attachments(msgnum) {
+       fetch(
+               "/ctdl/p/" + msgnum, { method: "GET" }
+       )
+       .then(response => response.json())
+       .then(j => {
+               j.forEach((item) => {
+                       add_upload_to_displayed_list(item);
+               })
+       })
+}
index 4e7635f041b980e5293a83d36ccf5b8ab20be1b3..f1596dcae7c5a5425cf08f080cd1c33ddf4ae799 100644 (file)
@@ -562,8 +562,10 @@ function mail_compose(references, quoted_msgnum, m_to, m_cc, m_subject) {
        if (is_quoted) {
                mail_display_message(quoted_msgnum, document.getElementById(quoted_div_name), 0);
        }
+
+       // If this is a forwarded messages, preload its attachments into the forwarded copy.
        if (is_fwd) {
-               console.log("FIXME we have to load the attachments from message " + quoted_msgnum);
+               forward_attachments(quoted_msgnum);
        }
 
        if (is_reply) {