1 // This module handles the view for "mailbox" rooms.
3 // Copyright (c) 2016-2022 by the citadel.org team
5 // This program is open source software. Use, duplication, or
6 // disclosure are subject to the GNU General Public License v3.
9 var selected_message = 0; // Remember the last message that was selected
10 var RefreshMailboxInterval; // We store our refresh timer here
13 // Render a message into the mailbox view
14 function mail_render_one(msg, target_div) {
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='<i class="fa fa-user-circle fa-2x"></i> '\">"
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 + "');\">"
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
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> "
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> "
47 if (can_delete_messages) {
49 "<span class=\"ctdl-msg-button\">"
50 + "<a href=\"javascript:forum_delete_message('"+div+"','"+msg.msgnum+"');\">"
51 + "<i class=\"fa fa-trash\"></i> "
57 "</span>"; // end buttons on right side
60 "<br><span class=\"ctdl-msgsubject\">" + msg.subj + "</span>";
63 "</div><br>" // end header
64 + "<div class=\"ctdl-msg-body\" id=\"" + div + "_body\">" // begin body
66 + "</div>" // end body
67 + "</div>" // end content
68 + "</div>" // end wrapper
72 outmsg = "<div class=\"ctdl-mmsg-wrapper\">" + err.message + "</div>";
75 target_div.innerHTML = outmsg;
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());
86 mail_render_one(msg, target_div);
93 // A message has been selected...
94 function select_message(msgnum) {
95 // unhighlight any previously selected message
97 document.getElementById("ctdl-msgsum-" + selected_message).classList.remove("w3-blue");
102 // highlight the newly selected message
103 document.getElementById("ctdl-msgsum-" + msgnum).classList.add("w3-blue");
104 document.getElementById("ctdl-msgsum-" + msgnum).scrollIntoView();
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"));
114 // render one row in the mailbox table (this could be called from one of several places)
115 function mail_render_row(msg) {
117 + "id=\"ctdl-msgsum-" + msg["msgnum"] + "\" "
118 + "onClick=\"select_message(" + msg["msgnum"] + ");\" "
119 //+ "onmouseenter=\"console.log('mouse in');\" "
120 //+ "onmouseleave=\"console.log('mouse out');\""
122 + "<td>" + msg["subject"] + "</td>"
123 + "<td>" + msg["author"] + " <" + msg["addr"] + "></td>"
124 + "<td>" + convertTimestamp(msg["time"]) + "</td>"
125 + "<td>" + msg["msgnum"] + "</td>"
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>";
137 refresh_mail_display();
138 try { // if this was already set up, clear it so there aren't multiple
139 clearInterval(RefreshMailboxInterval);
143 RefreshMailboxInterval = setInterval(refresh_mail_display, 10000);
147 // Display or refresh the mailbox
148 function refresh_mail_display() {
149 console.log("refresh_mail_display()");
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.
154 document.getElementById("ctdl-mailbox-pane").innerHTML;
157 console.log("ending refresh_mail_display()");
158 clearInterval(RefreshMailboxInterval);
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());
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>"
177 for (var i=0; i<msgs.length; ++i) {
178 box += mail_render_row(msgs[i]);
182 document.getElementById("ctdl-mailbox-pane").innerHTML = box;
184 if (selected_message > 0) { // if we had a message selected, keep it selected
185 select_message(selected_message);