Converting recipient arrays to recipient strings:
authorArt Cancro <ajc@citadel.org>
Thu, 30 Nov 2023 19:07:12 +0000 (09:07 -1000)
committerArt Cancro <ajc@citadel.org>
Thu, 30 Nov 2023 19:07:12 +0000 (09:07 -1000)
Fixed the dedupe so it works before now, and
also made it DRY.

webcit-ng/static/js/view_mail.js

index d5673193a4e86fab587fdae351ba98e42f8ab4c7..0641bace48b7bc5df3c7f611d738d2783e4a5467 100644 (file)
@@ -152,19 +152,19 @@ function mail_render_one(msgnum, msg, target_div, include_controls) {
                        + "</span>"                                             // end header info on left side
                        + "<span class=\"ctdl-msg-header-buttons\">"            // begin buttons on right side
                
-                       + "<span class=\"ctdl-msg-button\">"                    // Reply (mail is always Quoted)
+                       + "<span class=\"ctdl-msg-button\">"                    // Reply
                        + "<a href=\"javascript:mail_compose(true,'"+msg.wefw+"','"+msgnum+"', reply_addr(msg), [], 'Re: '+msg.subj);\">"
                        + "<i class=\"fa fa-reply\"></i> " 
                        + _("Reply")
                        + "</a></span>"
                
-                       + "<span class=\"ctdl-msg-button\">"                    // Reply-All (mail is always Quoted)
+                       + "<span class=\"ctdl-msg-button\">"                    // Reply-All
                        + `<a href="javascript:mail_compose(true, msg.wefw, ${msgnum}, replyall_to(msg), msg.cccc, 'Re: ${subject}');">`
                        + "<i class=\"fa fa-reply-all\"></i> " 
                        + _("ReplyAll")
                        + "</a></span>"
                
-                       + "<span class=\"ctdl-msg-button\">"                    // Forward (server handles this)
+                       + "<span class=\"ctdl-msg-button\">"                    // Forward
                        + "<a href=\"javascript:mail_compose(true,'"+msg.wefw+"','"+msgnum+"', '', '', 'Fwd: '+msg.subj);\">"
                        + "<i class=\"fa fa-mail-forward\"></i> " 
                        + _("Forward")
@@ -438,6 +438,38 @@ function render_mailbox_display(notify) {
 }
 
 
+// helper function for mail_compose() -- converts a recipient array to a string suitable for the To: or Cc: field
+function recipient_array_to_string(recps_arr) {
+
+       let returned_string = ""
+
+       if (recps_arr) {
+               is_reply = 1;
+
+               // first clean up the recipients
+               for (i=0; i<recps_arr.length; ++i) {
+                       recps_arr[i] = recps_arr[i].replaceAll("<", "&lt;").replaceAll(">", "&gt;");
+               }
+
+               // remove dupes
+               recps_arr = Array.from(new Set(recps_arr));
+
+               // now convert it to a string
+               returned_string = "";
+               for (i=0; i<recps_arr.length; ++i) {
+                       console.log("to #" + i + ": " + recps_arr[i]);
+                       if (i > 0) {
+                               returned_string += ", ";
+                       }
+                       returned_string += recps_arr[i];
+               }
+       }
+
+       return(returned_string);
+}
+
+
+
 // Compose a new mail message (called by the Reply button here, or by the dispatcher in views.js)
 //
 // is_quoted           true or false depending on whether the user selected "reply quoted" (is this appropriate for mail?)
@@ -451,37 +483,20 @@ function mail_compose(is_quoted, references, quoted_msgnum, m_to, m_cc, m_subjec
 
        let is_reply = 0;
 
+       console.log("");
+       console.log("mail_compose");
+       console.log("is_quoted: " + is_quoted);
+       console.log("references: " + references);
+       console.log("quoted_msgnum: " + quoted_msgnum);
+       console.log("m_to: " + m_to);
+       console.log("m_cc: " + m_cc);
+       console.log("m_subject: " + m_subject);
+
        // m_to will be an array of zero or more recipients for the To: field.  Convert it to a string.
-       if (m_to) {
-               is_reply = 1;
-               m_to = Array.from(new Set(m_to));       // remove dupes
-               m_to_str = "";
-               for (i=0; i<m_to.length; ++i) {
-                       if (i > 0) {
-                               m_to_str += ", ";
-                       }
-                       m_to_str += m_to[i].replaceAll("<", "&lt;").replaceAll(">", "&gt;");
-               }
-       }
-       else {
-               m_to_str = "";
-       }
+       m_to_str = recipient_array_to_string(m_to);
 
-       // m_to will be an array of zero or more recipients for the Cc: field.  Convert it to a string.
-       if (m_cc) {
-               is_reply = 1;
-               m_cc = Array.from(new Set(m_cc));       // remove dupes
-               m_cc_str = "";
-               for (i=0; i<m_cc.length; ++i) {
-                       if (i > 0) {
-                               m_cc_str += ", ";
-                       }
-                       m_cc_str += m_cc[i].replaceAll("<", "&lt;").replaceAll(">", "&gt;");
-               }
-       }
-       else {
-               m_cc_str = "";
-       }
+       // m_cc will be an array of zero or more recipients for the Cc: field.  Convert it to a string.
+       m_cc_str = recipient_array_to_string(m_cc);
 
        quoted_div_name = randomString();