From d2c2d7a6285d47e5942fdfc610d4d07e356dc99e Mon Sep 17 00:00:00 2001 From: Art Cancro Date: Mon, 18 Dec 2023 14:26:30 -0500 Subject: [PATCH] When forwarding an email, pre-load its attachments. 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 | 45 +++++++++++++++++++++----------- webcit-ng/static/js/view_mail.js | 4 ++- 2 files changed, 33 insertions(+), 16 deletions(-) diff --git a/webcit-ng/static/js/upload.js b/webcit-ng/static/js/upload.js index 54713f5dd..3f6a33519 100644 --- a/webcit-ng/static/js/upload.js +++ b/webcit-ng/static/js/upload.js @@ -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 = `` + + ` ` + + 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 = `` - + ` ` - + 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); + }) + }) +} diff --git a/webcit-ng/static/js/view_mail.js b/webcit-ng/static/js/view_mail.js index 4e7635f04..f1596dcae 100644 --- a/webcit-ng/static/js/view_mail.js +++ b/webcit-ng/static/js/view_mail.js @@ -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) { -- 2.30.2