Capture the new message number from the etag of the post transaction
[citadel.git] / webcit-ng / static / js / view_forum.js
1 //
2 // Copyright (c) 2016-2021 by the citadel.org team
3 //
4 // This program is open source software.  It runs great on the
5 // Linux operating system (and probably elsewhere).  You can use,
6 // copy, and run it under the terms of the GNU General Public
7 // License version 3.  Richard Stallman is an asshole communist.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13
14
15 // Forum view (flat)
16 //
17 function forum_readmessages(target_div, gt_msg, lt_msg) {
18         original_text = document.getElementById(target_div).innerHTML;          // in case we need to replace it after an error
19         document.getElementById(target_div).innerHTML = 
20                 "<div class=\"ctdl-forum-nav\"><i class=\"fas fa-spinner fa-spin\"></i>&nbsp;&nbsp;"
21                 + _("Loading messages from server, please wait") + "</div>";
22
23         if (lt_msg < 9999999999) {
24                 url = "/ctdl/r/" + escapeHTMLURI(current_room) + "/msgs.lt|" + lt_msg;
25         }
26         else {
27                 url = "/ctdl/r/" + escapeHTMLURI(current_room) + "/msgs.gt|" + gt_msg;
28         }
29
30         fetch_msg_list = async() => {
31                 response = await fetch(url);
32                 msgs = await(response.json());
33                 if (response.ok) {
34                         document.getElementById(target_div).innerHTML = "" ;
35
36                         // If we were given an explicit starting point, by all means start there.
37                         // Note that we don't have to remove them from the array because we did a 'msgs gt|xxx' command to Citadel.
38                         if (gt_msg > 0) {
39                                 msgs = msgs.slice(0, messages_per_page);
40                         }
41
42                         // Otherwise, show us the last 20 messages
43                         else {
44                                 if (msgs.length > messages_per_page) {
45                                         msgs = msgs.slice(msgs.length - messages_per_page);
46                                 }
47                                 new_old_div_name = randomString(5);
48                                 if (msgs.length < 1) {
49                                         newlt = lt_msg;
50                                 }
51                                 else {
52                                         newlt = msgs[0];
53                                 }
54                                 document.getElementById(target_div).innerHTML +=
55                                         "<div id=\"" + new_old_div_name + "\">" +
56                                         "<div class=\"ctdl-forum-nav\">" +
57                                         "<a href=\"javascript:forum_readmessages('" + new_old_div_name + "', 0, " + newlt + ");\">" +
58                                         "<i class=\"fa fa-arrow-circle-up\"></i>&nbsp;&nbsp;" +
59                                         _("Older posts") + "&nbsp;&nbsp;<i class=\"fa fa-arrow-circle-up\"></a></div></div></a></div></div>" ;
60                         }
61
62                         // Render an empty div for each message.  We will fill them in later.
63                         for (var i in msgs) {
64                                 document.getElementById(target_div).innerHTML += "<div id=\"ctdl_msg_" + msgs[i] + "\"> </div>" ;
65                                 document.getElementById("ctdl_msg_"+msgs[i]).style.display = "none";
66                         }
67                         if (lt_msg == 9999999999) {
68                                 new_new_div_name = randomString(5);
69                                 if (msgs.length <= 0) {
70                                         newgt = gt_msg;
71                                 }
72                                 else {
73                                         newgt = msgs[msgs.length-1];
74                                 }
75                                 document.getElementById(target_div).innerHTML +=
76                                         "<div id=\"" + new_new_div_name + "\">" +
77                                         "<div class=\"ctdl-forum-nav\">" +
78                                         "<a href=\"javascript:forum_readmessages('" + new_new_div_name + "', " + newgt + ", 9999999999);\">" +
79                                         "<i class=\"fa fa-arrow-circle-down\"></i>&nbsp;&nbsp;" +
80                                         _("Newer posts") + "&nbsp;&nbsp;<i class=\"fa fa-arrow-circle-down\"></a></div></div>" ;
81                         }
82
83                         // Now figure out where to scroll to after rendering.
84                         if (gt_msg > 0) {
85                                 scroll_to = msgs[0];
86                         }
87                         else if (lt_msg < 9999999999) {
88                                 scroll_to = msgs[msgs.length-1];
89                         }
90                         else if ( (logged_in) && (gt_msg == 0) && (lt_msg == 9999999999) ) {
91                                 scroll_to = msgs[msgs.length-1];
92                         }
93                         else {
94                                 scroll_to = msgs[0];            // FIXME this is too naive
95                         }
96
97                         // Render the individual messages in the divs
98                         forum_render_messages(msgs, "ctdl_msg_", scroll_to)
99                 }
100                 else {
101                         // if xhr fails, this will make the link reappear so the user can try again
102                         document.getElementById(target_div).innerHTML = original_text;
103                 }
104         }
105         fetch_msg_list();
106 }
107
108
109 // Render a range of messages, with the div prefix specified
110 //
111 function forum_render_messages(msgs, prefix, scroll_to) {
112         for (i=0; i<msgs.length; ++i) {
113                 forum_render_one(prefix, msgs[i], scroll_to);
114         }
115 }
116
117
118 // We have to put each XHR for forum_render_messages() into its own stack frame, otherwise it jumbles them together.  I don't know why.
119 function forum_render_one(prefix, msgnum, scroll_to) {
120         document.body.style.cursor = "wait";
121         fetch_message = async() => {
122                 response = await fetch("/ctdl/r/" + escapeHTMLURI(current_room) + "/" + msgs[i] + "/json");
123                 msg = await response.json();
124                 if (response.ok) {
125                         outmsg =
126                           "<div class=\"ctdl-msg-wrapper\">"                            // begin message wrapper
127                         + "<div class=\"ctdl-avatar\">"                                 // begin avatar
128                         + "<img src=\"/ctdl/u/" + msg.from + "/userpic\" width=\"32\" "
129                         + "onerror=\"this.parentNode.innerHTML='&lt;i class=&quot;fa fa-user-circle fa-2x&quot;&gt;&lt;/i&gt; '\">"
130                         + "</div>"                                                      // end avatar
131                         + "<div class=\"ctdl-msg-content\">"                            // begin content
132                         + "<div class=\"ctdl-msg-header\">"                             // begin header
133                         + "<span class=\"ctdl-msg-header-info\">"                       // begin header info on left side
134                         + "<span class=\"ctdl-username\"><a href=\"#\">"                // FIXME link to user profile
135                         + msg.from
136                         + "</a></span>"                                                 // end username
137                         + "<span class=\"ctdl-msgdate\">"
138                         + msg.time
139                         + "</span>"                                                     // end msgdate
140                         + "</span>"                                                     // end header info on left side
141                         + "<span class=\"ctdl-msg-header-buttons\">"                    // begin buttons on right side
142
143                         + "<span class=\"ctdl-msg-button\">"                            // Reply button FIXME make this work
144                         + "<a href=\"javascript:open_reply_box('"+prefix+"',"+msgnum+",false);\">"
145                         + "<i class=\"fa fa-reply\"></i> " 
146                         + _("Reply")
147                         + "</a></span>"
148
149                         + "<span class=\"ctdl-msg-button\">"                            // ReplyQuoted , only show in forums FIXME
150                         + "<a href=\"javascript:open_reply_box('"+prefix+"',"+msgnum+",true);\">"
151                         + "<i class=\"fa fa-comment\"></i> " 
152                         + _("ReplyQuoted")
153                         + "</a></span>"
154
155                         + "<span class=\"ctdl-msg-button\"><a href=\"#\">"              // Delete , show only with permission FIXME
156                         + "<i class=\"fa fa-trash\"></i> " 
157                         + _("Delete")
158                         + "</a></span>"
159
160                         + "</span>";                                                    // end buttons on right side
161                         if (msg.subj) {
162                                 outmsg +=
163                                 "<br><span class=\"ctdl-msgsubject\">" + msg.subj + "</span>";
164                         }
165                         outmsg +=
166                           "</div><br>"                                                  // end header
167                         + "<div class=\"ctdl-msg-body\">"                               // begin body
168                         + msg.text
169                         + "</div>"                                                      // end body
170                         + "</div>"                                                      // end content
171                         + "</div>"                                                      // end wrapper
172                         ;
173                         document.getElementById(prefix+msgnum).innerHTML = outmsg;
174                 }
175                 else {
176                         document.getElementById(prefix+msgnum).innerHTML = "ERROR";
177                 }
178                 document.getElementById(prefix+msgnum).style.display  = "inline";
179                 if (msgnum == scroll_to) {
180                         document.getElementById(prefix+msgnum).scrollIntoView({behavior: "smooth", block: "start", inline: "nearest"});
181                         document.body.style.cursor = "default";
182                 }
183         }
184         fetch_message();
185 }
186
187
188 // Open a reply box directly below a specific message
189 function open_reply_box(prefix, msgnum, is_quoted) {
190         target_div_name = prefix+msgnum;
191         new_div_name = prefix + "_reply_to_" + msgnum;
192         document.getElementById(target_div_name).outerHTML += "<div id=\"" + new_div_name + "\">reply box put here</div>";
193
194         replybox =
195           "<div class=\"ctdl-msg-wrapper\">"                            // begin message wrapper
196         + "<div class=\"ctdl-avatar\">"                                 // begin avatar
197         + "<img src=\"/ctdl/u/" + "FIXME my name" + "/userpic\" width=\"32\" "
198         + "onerror=\"this.parentNode.innerHTML='&lt;i class=&quot;fa fa-user-circle fa-2x&quot;&gt;&lt;/i&gt; '\">"
199         + "</div>"                                                      // end avatar
200         + "<div class=\"ctdl-msg-content\">"                            // begin content
201         + "<div class=\"ctdl-msg-header\">"                             // begin header
202         + "<span class=\"ctdl-msg-header-info\">"                       // begin header info on left side
203         + "<span class=\"ctdl-username\"><a href=\"#\">"                // FIXME link to user profile
204         + "FIXME my name"
205         + "</a></span>"                                                 // end username
206         + "<span class=\"ctdl-msgdate\">"
207         + "FIXME now time"
208         + "</span>"                                                     // end msgdate
209         + "</span>"                                                     // end header info on left side
210         + "<span class=\"ctdl-msg-header-buttons\">"                    // begin buttons on right side
211
212         + "<span class=\"ctdl-msg-button\">"                            // bold button
213         + "<a href=\"javascript:void(0)\" onclick=\"forum_format('bold')\">"
214         + "<i class=\"fa fa-bold fa-fw\"></i>" 
215         + "</a></span>"
216
217         + "<span class=\"ctdl-msg-button\">"                            // italic button
218         + "<a href=\"javascript:void(0)\" onclick=\"forum_format('italic')\">" 
219         + "<i class=\"fa fa-italic fa-fw\"></i>" 
220         + "</a></span>"
221
222         + "<span class=\"ctdl-msg-button\">"                            // list button
223         + "<a href=\"javascript:void(0)\" onclick=\"forum_format('insertunorderedlist')\">"
224         + "<i class=\"fa fa-list fa-fw\"></i>" 
225         + "</a></span>"
226
227         + "<span class=\"ctdl-msg-button\">"                            // link button
228         + "<a href=\"javascript:void(0)\" onclick=\"forum_display_urlbox()\">"
229         + "<i class=\"fa fa-link fa-fw\"></i>" 
230         + "</a></span>"
231
232         + "</span>";                                                    // end buttons on right side
233         if (msg.subj) {
234                 replybox +=
235                 "<br><span class=\"ctdl-msgsubject\">" + "FIXME subject" + "</span>";
236         }
237         replybox +=
238           "</div><br>"                                                  // end header
239
240                                                                         // begin body
241         + "<div class=\"ctdl-msg-body\" id=\"ctdl-editor-body\" style=\"padding:5px;\" contenteditable=\"true\">"
242         + "\n"                                                          // empty initial content
243         + "</div>"                                                      // end body
244
245         + "<div class=\"ctdl-msg-header\">"                             // begin footer
246         + "<span class=\"ctdl-msg-header-info\">"                       // begin footer info on left side
247         + "x"
248         + "</span>"                                                     // end footer info on left side
249         + "<span class=\"ctdl-msg-header-buttons\">"                    // begin buttons on right side
250
251         + "<span class=\"ctdl-msg-button\"><a href=\"javascript:forum_save_message('" +  new_div_name + "', '" + msgnum + "');\">"
252         + "<i class=\"fa fa-check\" style=\"color:green\"></i> "        // save button
253         + _("Post message")
254         + "</a></span>"
255
256         + "<span class=\"ctdl-msg-button\"><a href=\"javascript:forum_cancel_post('" +  new_div_name + "');\">"
257         + "<i class=\"fa fa-trash\" style=\"color:red\"></i> "          // cancel button
258         + _("Cancel")
259         + "</a></span>"
260
261         + "</span>"                                                     // end buttons on right side
262         + "</div><br>"                                                  // end footer
263
264
265         + "</div>"                                                      // end content
266         + "</div>"                                                      // end wrapper
267
268         + "<div id=\"forum_url_entry_box\" class=\"w3-modal\">"         // begin URL entry modal
269         + "     <div class=\"w3-modal-content w3-animate-top w3-card-4\">"
270         + "             <header class=\"w3-container w3-blue\"> "
271         + "                     <p><span>URL:</span></p>"
272         + "             </header>"
273         + "             <div class=\"w3-container w3-blue\">"
274         + "                     <input id=\"forum_txtFormatUrl\" placeholder=\"http://\" style=\"width:100%\">"
275         + "             </div>"
276         + "             <footer class=\"w3-container w3-blue\">"
277         + "                     <p><span class=\"ctdl-msg-button\"><a href=\"javascript:forum_close_urlbox(true);\">"
278         + "                             <i class=\"fa fa-check\" style=\"color:green\"></i> "
279         +                               _("Save")
280         +                       "</a></span>"
281         + "                     <span class=\"ctdl-msg-button\"><a href=\"javascript:forum_close_urlbox(false);\">"
282         + "                             <i class=\"fa fa-trash\" style=\"color:red\"></i> "
283         +                               _("Cancel")
284         +                       "</a></span></p>"
285         + "             </footer>"
286         + "             </div>"
287         + "     </div>"
288         + "     <input id=\"forum_selection_start\" style=\"display:none\"></input>"    // hidden fields
289         + "     <input id=\"forum_selection_end\" style=\"display:none\"></input>"      // to store selection range
290         + "</div>"                                                      // end URL entry modal
291         ;
292
293         document.getElementById(new_div_name).innerHTML = replybox;
294         document.getElementById(new_div_name).scrollIntoView({behavior: "smooth", block: "end", inline: "nearest"});
295
296         // These actions must happen *after* the initial render loop completes.
297         setTimeout(function() {
298                 var tag = document.getElementById("ctdl-editor-body");
299                 tag.focus();                                            // sets the focus
300                 window.getSelection().collapse(tag.firstChild, 0);      // positions the cursor
301         }, 0);
302 }
303
304
305 // Abort a message post (it simply destroys the div)
306 function forum_cancel_post(div_name) {
307         document.getElementById(div_name).outerHTML = "";               // make it cease to exist
308 }
309
310
311 // Save the posted message to the server
312 function forum_save_message(div_name, reply_to_msgnum) {
313
314         document.body.style.cursor = "wait";
315
316         url = "/ctdl/r/" + escapeHTMLURI(current_room) + "/dummy_name_for_new_message";
317         boundary = randomString(20);
318         body_text =
319                 "--" + boundary + "\r\n"
320                 + "Content-type: text/html\r\n"
321                 + "Content-transfer-encoding: quoted-printable\r\n"
322                 + "\r\n"
323                 + quoted_printable_encode(
324                         "<html><body>" + document.getElementById("ctdl-editor-body").innerHTML + "</body></html>"
325                 ) + "\r\n"
326                 + "--" + boundary + "--\r\n"
327         ;
328
329         var request = new XMLHttpRequest();
330         request.open("PUT", url, true);
331         request.setRequestHeader("Content-type", "multipart/mixed; boundary=\"" + boundary + "\"");
332         request.onreadystatechange = function() {
333                 if (request.readyState == 4) {
334                         document.body.style.cursor = "default";
335                         if (Math.trunc(request.status / 100) == 2) {
336                                 headers = request.getAllResponseHeaders().split("\n");
337                                 for (var i in headers) {
338                                         if (headers[i].startsWith("etag: ")) {
339                                                 new_msg_num = headers[i].split(" ")[1];
340                                                 alert(new_msg_num);
341                                         }
342                                 }
343                                 document.getElementById(div_name).outerHTML = "";               // close the editor
344                         }
345                         else {
346                                 error_message = request.responseText;
347                                 if (error_message.length == 0) {
348                                         error_message = _("An error has occurred.");
349                                 }
350                                 alert(error_message);                                           // FIXME make this pretty
351                         }
352                 }
353         };
354         request.send(body_text);
355 }
356
357
358 // Bold, italics, etc.
359 function forum_format(command, value) {
360         document.execCommand(command, false, value);
361 }
362
363
364 // Make the URL entry box appear.
365 // When the user clicks into the URL box it will make the previous focus disappear, so we have to save it.
366 function forum_display_urlbox() {
367         document.getElementById("forum_selection_start").value = window.getSelection().anchorOffset;
368         document.getElementById("forum_selection_end").value = window.getSelection().focusOffset;
369         document.getElementById("forum_url_entry_box").style.display = "block";
370 }
371
372
373 // When the URL box is closed, this gets called.  do_save is true for Save, false for Cancel.
374 function forum_close_urlbox(do_save) {
375         if (do_save) {
376                 var tag = document.getElementById("ctdl-editor-body");
377                 var start_replace = document.getElementById("forum_selection_start").value;     // use saved selection range
378                 var end_replace = document.getElementById("forum_selection_end").value;
379                 new_text = tag.innerHTML.substring(0, start_replace)
380                         + "<a href=\"" + document.getElementById("forum_txtFormatUrl").value + "\">"
381                         + tag.innerHTML.substring(start_replace, end_replace)
382                         + "</a>"
383                         + tag.innerHTML.substring(end_replace);
384                 tag.innerHTML = new_text;
385         }
386         document.getElementById("forum_txtFormatUrl").value = "";                               // clear url box for next time
387         document.getElementById("forum_url_entry_box").style.display = "none";
388 }