utf8ify_rfc822_string() is in libcitadel now
[citadel.git] / webcit-ng / static / js / view_mail.js
1 // This module handles the view for "mailbox" rooms.
2 //
3 // Copyright (c) 2016-2022 by the citadel.org team
4 //
5 // This program is open source software.  Use, duplication, or
6 // disclosure are subject to the GNU General Public License v3.
7
8
9 var selected_message = 0;                                                       // Remember the last message that was selected
10 var RefreshMailboxInterval;                                                     // We store our refresh timer here
11
12
13 // Render a message into the mailbox view
14 function mail_render_one(msg, target_div) {
15         let div = "FIXME";
16         try {
17                 outmsg =
18                   "<div class=\"ctdl-mmsg-wrapper\">"                           // begin message wrapper
19                 + "<div class=\"ctdl-avatar\" onClick=\"javascript:user_profile('" + msg.from + "');\">"
20                 + "<img src=\"/ctdl/u/" + msg.from + "/userpic\" width=\"32\" "
21                 + "onerror=\"this.parentNode.innerHTML='&lt;i class=&quot;fa fa-user-circle fa-2x&quot;&gt;&lt;/i&gt; '\">"
22                 + "</div>"                                                      // end avatar
23                 + "<div class=\"ctdl-mmsg-content\">"                           // begin content
24                 + "<div class=\"ctdl-msg-header\">"                             // begin header
25                 + "<span class=\"ctdl-msg-header-info\">"                       // begin header info on left side
26                 + "<span class=\"ctdl-username\" onClick=\"javascript:user_profile('" + msg.from + "');\">"
27                 + msg.from
28                 + "</a></span>"                                                 // end username
29                 + "<span class=\"ctdl-msgdate\">"
30                 + convertTimestamp(msg.time)
31                 + "</span>"                                                     // end msgdate
32                 + "</span>"                                                     // end header info on left side
33                 + "<span class=\"ctdl-msg-header-buttons\">"                    // begin buttons on right side
34         
35                 + "<span class=\"ctdl-msg-button\">"                            // Reply
36                 + "<a href=\"javascript:open_reply_box('"+div+"',false,'"+msg.wefw+"','"+msg.msgn+"');\">"
37                 + "<i class=\"fa fa-reply\"></i> " 
38                 + _("Reply")
39                 + "</a></span>"
40         
41                 + "<span class=\"ctdl-msg-button\">"                            // ReplyQuoted
42                 + "<a href=\"javascript:open_reply_box('"+div+"',true,'"+msg.wefw+"','"+msg.msgn+"');\">"
43                 + "<i class=\"fa fa-comment\"></i> " 
44                 + _("ReplyQuoted")
45                 + "</a></span>";
46         
47                 if (can_delete_messages) {
48                         outmsg +=
49                         "<span class=\"ctdl-msg-button\">"
50                         + "<a href=\"javascript:forum_delete_message('"+div+"','"+msg.msgnum+"');\">"
51                         + "<i class=\"fa fa-trash\"></i> " 
52                         + _("Delete")
53                         + "</a></span>";
54                 }
55         
56                 outmsg +=
57                   "</span>";                                                    // end buttons on right side
58                 if (msg.subj) {
59                         outmsg +=
60                         "<br><span class=\"ctdl-msgsubject\">" + msg.subj + "</span>";
61                 }
62                 outmsg +=
63                   "</div><br>"                                                  // end header
64                 + "<div class=\"ctdl-msg-body\" id=\"" + div + "_body\">"       // begin body
65                 + msg.text
66                 + "</div>"                                                      // end body
67                 + "</div>"                                                      // end content
68                 + "</div>"                                                      // end wrapper
69                 ;
70         }
71         catch(err) {
72                 outmsg = "<div class=\"ctdl-mmsg-wrapper\">" + err.message + "</div>";
73         }
74
75         target_div.innerHTML = outmsg;
76 }
77
78
79 // display an individual message
80 function mail_display_message(msgnum, target_div) {
81         url = "/ctdl/r/" + escapeHTMLURI(current_room) + "/" + msgnum + "/json";
82         mail_fetch_msg = async() => {
83                 response = await fetch(url);
84                 msg = await(response.json());
85                 if (response.ok) {
86                         mail_render_one(msg, target_div);
87                 }
88         }
89         mail_fetch_msg();
90 }
91
92
93 // A message has been selected...
94 function select_message(msgnum) {
95         // unhighlight any previously selected message
96         try {
97                 document.getElementById("ctdl-msgsum-" + selected_message).classList.remove("w3-blue");
98         }
99         catch {
100         }
101
102         // highlight the newly selected message
103         document.getElementById("ctdl-msgsum-" + msgnum).classList.add("w3-blue");
104         document.getElementById("ctdl-msgsum-" + msgnum).scrollIntoView();
105
106         // display the message if it isn't already displayed
107         if (selected_message != msgnum) {
108                 selected_message = msgnum;
109                 mail_display_message(msgnum, document.getElementById("ctdl-reading-pane"));
110         }
111 }
112
113
114 // render one row in the mailbox table (this could be called from one of several places)
115 function mail_render_row(msg) {
116         row     = "<tr "
117                 + "id=\"ctdl-msgsum-" + msg["msgnum"] + "\" "
118                 + "onClick=\"select_message(" + msg["msgnum"] + ");\" "
119                 //+ "onmouseenter=\"console.log('mouse in');\" "
120                 //+ "onmouseleave=\"console.log('mouse out');\""
121                 + ">"
122                 + "<td>" + msg["subject"] + "</td>"
123                 + "<td>" + msg["author"] + " &lt;" + msg["addr"] + "&gt;</td>"
124                 + "<td>" + convertTimestamp(msg["time"]) + "</td>"
125                 + "<td>" + msg["msgnum"] + "</td>"
126                 + "</tr>";
127         return(row);
128 }
129
130
131 // Set up the mailbox view
132 function mail_display() {
133         document.getElementById("ctdl-main").innerHTML
134                 = "<div id=\"ctdl-mailbox-pane\" class=\"ctdl-mailbox-pane\"></div>"
135                 + "<div id=\"ctdl-reading-pane\" class=\"ctdl-reading-pane\"></div>";
136         ;
137         refresh_mail_display();
138         try {                                                   // if this was already set up, clear it so there aren't multiple
139                 clearInterval(RefreshMailboxInterval);
140         }
141         catch {
142         }
143         RefreshMailboxInterval = setInterval(refresh_mail_display, 10000);
144 }
145
146
147 // Display or refresh the mailbox
148 function refresh_mail_display() {
149         console.log("refresh_mail_display()");
150
151         // If the "ctdl-mailbox-pane" no longer exists, the user has navigated to a different part of the site,
152         // so cancel the refresh.
153         try {
154                 document.getElementById("ctdl-mailbox-pane").innerHTML;
155         }
156         catch {
157                 console.log("ending refresh_mail_display()");
158                 clearInterval(RefreshMailboxInterval);
159                 return;
160         }
161
162         // Now go to the server.
163         url = "/ctdl/r/" + escapeHTMLURI(current_room) + "/mailbox";
164         fetch_mailbox = async() => {
165                 response = await fetch(url);
166                 msgs = await(response.json());
167                 if (response.ok) {
168
169                         box =   "<table class=\"w3-table-all w3-hoverable\" width=100%>"
170                                 + "<tr class=\"ctdl-mailbox-heading w3-blue\">"
171                                 + "<th>" + _("Subject") + "</th>"
172                                 + "<th>" + _("Sender") + "</th>"
173                                 + "<th>" + _("Date") + "</th>"
174                                 + "<th>#</th>"
175                                 + "</tr>";
176
177                         for (var i=0; i<msgs.length; ++i) {
178                                 box += mail_render_row(msgs[i]);
179                         }
180
181                         box +=  "</table>";
182                         document.getElementById("ctdl-mailbox-pane").innerHTML = box;
183
184                         if (selected_message > 0) {                     // if we had a message selected, keep it selected
185                                 select_message(selected_message);
186                         }
187                 }
188         }
189         fetch_mailbox();
190 }