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 + render_msg_author(msg)
27 + "<span class=\"ctdl-msgdate\">"
28 + string_timestamp(msg.time,0)
29 + "</span>" // end msgdate
30 + "</span>" // end header info on left side
31 + "<span class=\"ctdl-msg-header-buttons\">" // begin buttons on right side
33 + "<span class=\"ctdl-msg-button\">" // Reply (mail is always Quoted)
34 + "<a href=\"javascript:mail_compose(true,'"+msg.wefw+"','"+msg.msgn+"');\">"
35 + "<i class=\"fa fa-reply\"></i> "
39 if (can_delete_messages) {
41 "<span class=\"ctdl-msg-button\">"
42 + "<a href=\"javascript:forum_delete_message('"+div+"','"+msg.msgnum+"');\">"
43 + "<i class=\"fa fa-trash\"></i> "
49 "</span>"; // end buttons on right side
52 "<br><span class=\"ctdl-msgsubject\">" + msg.subj + "</span>";
55 "</div>" // end header
56 + "<div class=\"ctdl-msg-body\" id=\"" + div + "_body\">" // begin body
58 + "</div>" // end body
59 + "</div>" // end content
60 + "</div>" // end wrapper
64 outmsg = "<div class=\"ctdl-mmsg-wrapper\">" + err.message + "</div>";
67 target_div.innerHTML = outmsg;
71 // display an individual message
72 function mail_display_message(msgnum, target_div) {
73 url = "/ctdl/r/" + escapeHTMLURI(current_room) + "/" + msgnum + "/json";
74 mail_fetch_msg = async() => {
75 response = await fetch(url);
76 msg = await(response.json());
78 mail_render_one(msg, target_div);
85 // A message has been selected...
86 function select_message(msgnum) {
87 // unhighlight any previously selected message
89 document.getElementById("ctdl-msgsum-" + selected_message).classList.remove("w3-blue");
94 // highlight the newly selected message
95 document.getElementById("ctdl-msgsum-" + msgnum).classList.add("w3-blue");
96 //document.getElementById("ctdl-msgsum-" + msgnum).scrollIntoView();
98 // display the message if it isn't already displayed
99 if (selected_message != msgnum) {
100 selected_message = msgnum;
101 mail_display_message(msgnum, document.getElementById("ctdl-mailbox-reading-pane"));
106 // render one row in the mailbox table (this could be called from one of several places)
107 function mail_render_row(msg) {
109 + "id=\"ctdl-msgsum-" + msg["msgnum"] + "\" "
110 + "onClick=\"select_message(" + msg["msgnum"] + ");\" "
111 //+ "onmouseenter=\"console.log('mouse in');\" "
112 //+ "onmouseleave=\"console.log('mouse out');\""
114 + "<td class=\"ctdl-mail-subject\">" + msg["subject"] + "</td>"
115 + "<td class=\"ctdl-mail-sender\">" + msg["author"] + "</td>"
116 + "<td class=\"ctdl-mail-date\">" + string_timestamp(msg["time"],1) + "</td>"
117 + "<td class=\"ctdl-mail-msgnum\">" + msg["msgnum"] + "</td>"
123 // Set up the mailbox view
124 function mail_display() {
126 // Put the "enter new message" button into the navbar
127 document.getElementById("ctdl-newmsg-button").innerHTML = "<i class=\"fa fa-edit\"></i>" + _("Write mail");
128 document.getElementById("ctdl-newmsg-button").style.display = "block";
130 document.getElementById("ctdl-main").innerHTML
131 = "<div id=\"ctdl-mailbox-grid-container\" class=\"ctdl-mailbox-grid-container\">"
132 + "<div id=\"ctdl-mailbox-pane\" class=\"ctdl-mailbox-pane\"></div>"
133 + "<div id=\"ctdl-mailbox-reading-pane\" class=\"ctdl-mailbox-reading-pane\"></div>"
137 render_mailbox_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 // Refresh the mailbox, either for the first time or whenever needed
148 function refresh_mail_display() {
150 // If the "ctdl-mailbox-pane" no longer exists, the user has navigated to a different part of the site,
151 // so cancel the refresh.
153 document.getElementById("ctdl-mailbox-pane").innerHTML;
156 console.log("ending refresh_mail_display()");
157 clearInterval(RefreshMailboxInterval);
161 // Ask the server if the room has been written to since our last look at it.
162 url = "/ctdl/r/" + escapeHTMLURI(current_room) + "/stat";
163 fetch_stat = async() => {
164 response = await fetch(url);
165 stat = await(response.json());
166 if (stat.room_mtime > room_mtime) {
167 room_mtime = stat.room_mtime;
168 render_mailbox_display();
175 // This is where the rendering of the message list in the mailbox view is performed.
176 function render_mailbox_display() {
178 url = "/ctdl/r/" + escapeHTMLURI(current_room) + "/mailbox";
179 fetch_mailbox = async() => {
180 response = await fetch(url);
181 msgs = await(response.json());
184 box = "<table class=\"ctdl-mailbox-table\" width=100%><tr>"
185 + "<th>" + _("Subject") + "</th>"
186 + "<th>" + _("Sender") + "</th>"
187 + "<th>" + _("Date") + "</th>"
191 for (var i=0; i<msgs.length; ++i) {
192 box += mail_render_row(msgs[i]);
196 document.getElementById("ctdl-mailbox-pane").innerHTML = box;
198 if (selected_message > 0) { // if we had a message selected, keep it selected
199 select_message(selected_message);
207 // Compose a new mail message (called by the Reply button here, or by the dispatcher in views.js)
208 function mail_compose(is_quoted, references, msgid) {
210 document.getElementById("ctdl-main").innerHTML
211 = "<div id=\"ctdl-compose-mail\" class=\"ctdl-compose-mail\">"
213 + "<table border=\"1\" width=\"100%\">"
214 + "<tr><td>is_quoted</td><td>" + is_quoted + "</td></tr>"
215 + "<tr><td>references</td><td>" + references + "</td></tr>"
216 + "<tr><td>msgid</td><td>" + msgid + "</td></tr>"
217 + "<tr><td>from</td><td>" + current_user + "</td></tr>"
218 + "<tr><td>to</td><td></td></tr>"
219 + "<tr><td>subject</td><td></td></tr></table>"
220 + "<div class=\"ctdl-msg-body\" id=\"ctdl-editor-body\" style=\"padding:5px;\" contenteditable=\"true\">"