2 // Copyright (c) 2016-2021 by the citadel.org team
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.
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.
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> "
21 + _("Loading messages from server, please wait") + "</div>";
23 if (lt_msg < 9999999999) {
24 url = "/ctdl/r/" + escapeHTMLURI(current_room) + "/msgs.lt|" + lt_msg;
27 url = "/ctdl/r/" + escapeHTMLURI(current_room) + "/msgs.gt|" + gt_msg;
30 fetch_msg_list = async() => {
31 response = await fetch(url);
32 msgs = await(response.json());
34 document.getElementById(target_div).innerHTML = "" ;
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.
39 msgs = msgs.slice(0, messages_per_page);
42 // Otherwise, show us the last 20 messages
44 if (msgs.length > messages_per_page) {
45 msgs = msgs.slice(msgs.length - messages_per_page);
47 new_old_div_name = randomString(5);
48 if (msgs.length < 1) {
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> " +
59 _("Older posts") + " <i class=\"fa fa-arrow-circle-up\"></a></div></div></a></div></div>" ;
62 // Render an empty div for each message. We will fill them in later.
64 document.getElementById(target_div).innerHTML += "<div id=\"ctdl_msg_" + msgs[i] + "\"> </div>" ;
65 document.getElementById("ctdl_msg_"+msgs[i]).style.display = "none";
67 if (lt_msg == 9999999999) {
68 new_new_div_name = randomString(5);
69 if (msgs.length <= 0) {
73 newgt = msgs[msgs.length-1];
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> " +
80 _("Newer posts") + " <i class=\"fa fa-arrow-circle-down\"></a></div></div>" ;
83 // Now figure out where to scroll to after rendering.
87 else if (lt_msg < 9999999999) {
88 scroll_to = msgs[msgs.length-1];
90 else if ( (logged_in) && (gt_msg == 0) && (lt_msg == 9999999999) ) {
91 scroll_to = msgs[msgs.length-1];
94 scroll_to = msgs[0]; // FIXME this is too naive
97 // Render the individual messages in the divs
98 forum_render_messages(msgs, "ctdl_msg_", scroll_to)
101 // if xhr fails, this will make the link reappear so the user can try again
102 document.getElementById(target_div).innerHTML = original_text;
109 // Render a range of messages, with the div prefix specified
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);
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();
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='<i class="fa fa-user-circle fa-2x"></i> '\">"
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
136 + "</a></span>" // end username
137 + "<span class=\"ctdl-msgdate\">"
139 + "</span>" // end msgdate
140 + "</span>" // end header info on left side
141 + "<span class=\"ctdl-msg-header-buttons\">" // begin buttons on right side
143 + "<span class=\"ctdl-msg-button\">" // Reply
144 + "<a href=\"javascript:open_reply_box('"+prefix+"',"+msgnum+",false,'"+msg.wefw+"','"+msg.msgn+"');\">"
145 + "<i class=\"fa fa-reply\"></i> "
149 + "<span class=\"ctdl-msg-button\">" // ReplyQuoted
150 + "<a href=\"javascript:open_reply_box('"+prefix+"',"+msgnum+",true,'"+msg.wefw+"','"+msg.msgn+"');\">"
151 + "<i class=\"fa fa-comment\"></i> "
155 + "<span class=\"ctdl-msg-button\"><a href=\"#\">" // Delete , show only with permission FIXME
156 + "<i class=\"fa fa-trash\"></i> "
160 + "</span>"; // end buttons on right side
163 "<br><span class=\"ctdl-msgsubject\">" + msg.subj + "</span>";
166 "</div><br>" // end header
167 + "<div class=\"ctdl-msg-body\">" // begin body
169 + "</div>" // end body
170 + "</div>" // end content
171 + "</div>" // end wrapper
173 document.getElementById(prefix+msgnum).innerHTML = outmsg;
176 document.getElementById(prefix+msgnum).innerHTML = "ERROR";
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";
188 // Compose a references string using existing references plus the message being replied to
189 function compose_references(references, msgid) {
190 if (references.includes("@")) {
191 refs = references + "|";
198 console.log("initial len: " + refs.length);
199 // If the resulting string is too big, we can trim it here
200 while (refs.length > 900) {
202 r.splice(1,1); // remove the second element so we keep the root
204 console.log("split len: " + refs.length);
212 // Open a reply box directly below a specific message
213 function open_reply_box(prefix, msgnum, is_quoted, references, msgid) {
214 target_div_name = prefix+msgnum;
215 new_div_name = prefix + "reply_to_" + msgnum;
216 document.getElementById(target_div_name).outerHTML += "<div id=\"" + new_div_name + "\">reply box put here</div>";
218 // FIXME - we need to retain the message number being replied to
221 "<div class=\"ctdl-msg-wrapper ctdl-msg-reply\">" // begin message wrapper
222 + "<div class=\"ctdl-avatar\">" // begin avatar
223 + "<img src=\"/ctdl/u/" + "FIXME my name" + "/userpic\" width=\"32\" "
224 + "onerror=\"this.parentNode.innerHTML='<i class="fa fa-user-circle fa-2x"></i> '\">"
225 + "</div>" // end avatar
226 + "<div class=\"ctdl-msg-content\">" // begin content
227 + "<div class=\"ctdl-msg-header\">" // begin header
228 + "<span class=\"ctdl-msg-header-info\">" // begin header info on left side
229 + "<span class=\"ctdl-username\"><a href=\"#\">" // FIXME link to user profile
231 + "</a></span>" // end username
232 + "<span class=\"ctdl-msgdate\">"
234 + "</span>" // end msgdate
235 + "</span>" // end header info on left side
236 + "<span class=\"ctdl-msg-header-buttons\">" // begin buttons on right side
238 + "<span class=\"ctdl-msg-button\">" // bold button
239 + "<a href=\"javascript:void(0)\" onclick=\"forum_format('bold')\">"
240 + "<i class=\"fa fa-bold fa-fw\"></i>"
243 + "<span class=\"ctdl-msg-button\">" // italic button
244 + "<a href=\"javascript:void(0)\" onclick=\"forum_format('italic')\">"
245 + "<i class=\"fa fa-italic fa-fw\"></i>"
248 + "<span class=\"ctdl-msg-button\">" // list button
249 + "<a href=\"javascript:void(0)\" onclick=\"forum_format('insertunorderedlist')\">"
250 + "<i class=\"fa fa-list fa-fw\"></i>"
253 + "<span class=\"ctdl-msg-button\">" // link button
254 + "<a href=\"javascript:void(0)\" onclick=\"forum_display_urlbox()\">"
255 + "<i class=\"fa fa-link fa-fw\"></i>"
258 + "</span>"; // end buttons on right side
261 "<br><span id=\"ctdl-subject\" class=\"ctdl-msgsubject\">" + "FIXME subject" + "</span>";
263 else { // hidden filed for empty subject
264 replybox += "<span id=\"ctdl-subject\" style=\"display:none\"></span>";
267 "</div><br>" // end header
269 + "<span id=\"ctdl-replyreferences\" style=\"display:none\">" // hidden field for references
270 + compose_references(references,msgid) + "</span>"
272 + "<div class=\"ctdl-msg-body\" id=\"ctdl-editor-body\" style=\"padding:5px;\" contenteditable=\"true\">"
273 + "\n" // empty initial content
274 + "</div>" // end body
276 + "<div class=\"ctdl-msg-header\">" // begin footer
277 + "<span class=\"ctdl-msg-header-info\">" // begin footer info on left side
279 + "</span>" // end footer info on left side
280 + "<span class=\"ctdl-msg-header-buttons\">" // begin buttons on right side
282 + "<span class=\"ctdl-msg-button\"><a href=\"javascript:forum_save_message('" + new_div_name + "', '" + msgnum + "');\">"
283 + "<i class=\"fa fa-check\" style=\"color:green\"></i> " // save button
287 + "<span class=\"ctdl-msg-button\"><a href=\"javascript:forum_cancel_post('" + new_div_name + "');\">"
288 + "<i class=\"fa fa-trash\" style=\"color:red\"></i> " // cancel button
292 + "</span>" // end buttons on right side
293 + "</div><br>" // end footer
296 + "</div>" // end content
297 + "</div>" // end wrapper
299 + "<div id=\"forum_url_entry_box\" class=\"w3-modal\">" // begin URL entry modal
300 + " <div class=\"w3-modal-content w3-animate-top w3-card-4\">"
301 + " <header class=\"w3-container w3-blue\"> "
302 + " <p><span>URL:</span></p>"
304 + " <div class=\"w3-container w3-blue\">"
305 + " <input id=\"forum_txtFormatUrl\" placeholder=\"http://\" style=\"width:100%\">"
307 + " <footer class=\"w3-container w3-blue\">"
308 + " <p><span class=\"ctdl-msg-button\"><a href=\"javascript:forum_close_urlbox(true);\">"
309 + " <i class=\"fa fa-check\" style=\"color:green\"></i> "
312 + " <span class=\"ctdl-msg-button\"><a href=\"javascript:forum_close_urlbox(false);\">"
313 + " <i class=\"fa fa-trash\" style=\"color:red\"></i> "
319 + " <input id=\"forum_selection_start\" style=\"display:none\"></input>" // hidden fields
320 + " <input id=\"forum_selection_end\" style=\"display:none\"></input>" // to store selection range
321 + "</div>" // end URL entry modal
324 document.getElementById(new_div_name).innerHTML = replybox;
325 document.getElementById(new_div_name).scrollIntoView({behavior: "smooth", block: "end", inline: "nearest"});
327 // These actions must happen *after* the initial render loop completes.
328 setTimeout(function() {
329 var tag = document.getElementById("ctdl-editor-body");
330 tag.focus(); // sets the focus
331 window.getSelection().collapse(tag.firstChild, 0); // positions the cursor
336 // Abort a message post (it simply destroys the div)
337 function forum_cancel_post(div_name) {
338 document.getElementById(div_name).outerHTML = ""; // make it cease to exist
342 // Save the posted message to the server
343 function forum_save_message(div_name, reply_to_msgnum) {
345 document.body.style.cursor = "wait";
346 wefw = (document.getElementById("ctdl-replyreferences").innerHTML).replaceAll("|","!"); // references (if present)
347 subj = document.getElementById("ctdl-subject").innerHTML; // subject (if present)
349 url = "/ctdl/r/" + escapeHTMLURI(current_room)
350 + "/dummy_name_for_new_message"
353 boundary = randomString(20);
355 "--" + boundary + "\r\n"
356 + "Content-type: text/html\r\n"
357 + "Content-transfer-encoding: quoted-printable\r\n"
359 + quoted_printable_encode(
360 "<html><body>" + document.getElementById("ctdl-editor-body").innerHTML + "</body></html>"
362 + "--" + boundary + "--\r\n"
365 var request = new XMLHttpRequest();
366 request.open("PUT", url, true);
367 request.setRequestHeader("Content-type", "multipart/mixed; boundary=\"" + boundary + "\"");
368 request.onreadystatechange = function() {
369 if (request.readyState == 4) {
370 document.body.style.cursor = "default";
371 if (Math.trunc(request.status / 100) == 2) {
372 headers = request.getAllResponseHeaders().split("\n");
373 for (var i in headers) {
374 if (headers[i].startsWith("etag: ")) {
375 new_msg_num = headers[i].split(" ")[1];
376 alert("div_name=" + div_name + " , reply_to_msgnum = " + reply_to_msgnum + " , new_msg_num = " + new_msg_num);
379 document.getElementById(div_name).outerHTML = ""; // close the editor
382 error_message = request.responseText;
383 if (error_message.length == 0) {
384 error_message = _("An error has occurred.");
386 alert(error_message);
390 request.send(body_text);
394 // Bold, italics, etc.
395 function forum_format(command, value) {
396 document.execCommand(command, false, value);
400 // Make the URL entry box appear.
401 // When the user clicks into the URL box it will make the previous focus disappear, so we have to save it.
402 function forum_display_urlbox() {
403 document.getElementById("forum_selection_start").value = window.getSelection().anchorOffset;
404 document.getElementById("forum_selection_end").value = window.getSelection().focusOffset;
405 document.getElementById("forum_url_entry_box").style.display = "block";
409 // When the URL box is closed, this gets called. do_save is true for Save, false for Cancel.
410 function forum_close_urlbox(do_save) {
412 var tag = document.getElementById("ctdl-editor-body");
413 var start_replace = document.getElementById("forum_selection_start").value; // use saved selection range
414 var end_replace = document.getElementById("forum_selection_end").value;
415 new_text = tag.innerHTML.substring(0, start_replace)
416 + "<a href=\"" + document.getElementById("forum_txtFormatUrl").value + "\">"
417 + tag.innerHTML.substring(start_replace, end_replace)
419 + tag.innerHTML.substring(end_replace);
420 tag.innerHTML = new_text;
422 document.getElementById("forum_txtFormatUrl").value = ""; // clear url box for next time
423 document.getElementById("forum_url_entry_box").style.display = "none";