1 // Copyright (c) 2016-2022 by the citadel.org team
3 // This program is open source software. It runs great on the
4 // Linux operating system (and probably elsewhere). You can use,
5 // copy, and run it under the terms of the GNU General Public
6 // License version 3. Richard Stallman is an asshole communist.
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
15 function forum_readmessages(target_div_name, gt_msg, lt_msg) {
16 target_div = document.getElementById(target_div_name);
17 original_text = target_div.innerHTML; // in case we need to replace it after an error
18 target_div.innerHTML = ""
20 if (lt_msg < 9999999999) {
21 url = "/ctdl/r/" + escapeHTMLURI(current_room) + "/msgs.lt|" + lt_msg;
24 url = "/ctdl/r/" + escapeHTMLURI(current_room) + "/msgs.gt|" + gt_msg;
27 fetch_msg_list = async() => {
28 response = await fetch(url);
29 msgs = await(response.json());
31 target_div.innerHTML = "" ;
33 // If we were given an explicit starting point, by all means start there.
34 // Note that we don't have to remove them from the array because we did a 'msgs gt|xxx' command to Citadel.
36 msgs = msgs.slice(0, messages_per_page);
39 // Otherwise, show us the last 20 messages
41 if (msgs.length > messages_per_page) {
42 msgs = msgs.slice(msgs.length - messages_per_page);
44 new_old_div_name = randomString();
45 if (msgs.length < 1) {
51 target_div.innerHTML +=
52 "<div id=\"" + new_old_div_name + "\">" +
53 "<div class=\"ctdl-forum-nav\">" +
54 "<a href=\"javascript:forum_readmessages('" + new_old_div_name + "', 0, " + newlt + ");\">" +
55 "<i class=\"fa fa-arrow-circle-up\"></i> " +
56 _("Older posts") + " <i class=\"fa fa-arrow-circle-up\"></a></div></div></a></div></div>" ;
59 // The messages will go here.
60 let msgs_div_name = randomString();
61 target_div.innerHTML += "<div id=\"" + msgs_div_name + "\"> </div>" ;
63 if (lt_msg == 9999999999) {
64 new_new_div_name = randomString();
65 if (msgs.length <= 0) {
69 newgt = msgs[msgs.length-1];
71 target_div.innerHTML +=
72 "<div id=\"" + new_new_div_name + "\">" +
73 "<div id=\"ctdl-newmsg-here\"></div>" +
74 "<div class=\"ctdl-forum-nav\">" +
75 "<a href=\"javascript:forum_readmessages('" + new_new_div_name + "', " + newgt + ", 9999999999);\">" +
76 "<i class=\"fa fa-arrow-circle-down\"></i> " +
77 _("Newer posts") + " <i class=\"fa fa-arrow-circle-down\"></a></div></div>" ;
80 // Now figure out where to scroll to after rendering.
84 else if (lt_msg < 9999999999) {
85 scroll_to = msgs[msgs.length-1];
87 else if ( (logged_in) && (gt_msg == 0) && (lt_msg == 9999999999) ) {
88 scroll_to = msgs[msgs.length-1];
91 scroll_to = msgs[0]; // FIXME this is too naive
94 // Render the individual messages in the divs
95 forum_render_messages(msgs, msgs_div_name, scroll_to)
98 // if xhr fails, this will make the link reappear so the user can try again
99 target_div.innerHTML = original_text;
104 // make the new message button appear
105 document.getElementById("ctdl-newmsg-button").innerHTML = "<i class=\"far fa-edit\"></i>" + _("Post message");
106 document.getElementById("ctdl-newmsg-button").style.display = "block";
110 // Render a range of messages into the specified target div
111 function forum_render_messages(message_numbers, msgs_div_name, scroll_to) {
113 // Build an array of Promises and then wait for them all to resolve.
114 let num_msgs = message_numbers.length;
115 let msg_promises = Array.apply(null, Array(num_msgs));
116 for (i=0; i<num_msgs; ++i) {
117 msg_promises[i] = fetch("/ctdl/r/" + escapeHTMLURI(current_room) + "/" + message_numbers[i] + "/json")
118 .then(response => response.json())
121 console.error('Error: ', error);
126 // Here is the async function that waits for all the messages to be loaded, and then renders them.
127 fetch_msg_list = async() => {
128 document.body.style.cursor = "wait";
129 activate_loading_modal();
130 await Promise.all(msg_promises);
131 deactivate_loading_modal();
132 document.body.style.cursor = "default";
134 // At this point all of the Promises are resolved and we can render.
135 // Note: "let" keeps "i" in scope even through the .then scope
136 let scroll_to_div = null;
137 for (let i=0; i<num_msgs; ++i) {
138 msg_promises[i].then((one_message) => {
139 let new_msg_div = forum_render_one(one_message, null);
140 document.getElementById(msgs_div_name).append(new_msg_div);
141 if (message_numbers[i] == scroll_to) {
142 scroll_to_div = new_msg_div;
144 if (i == num_msgs - 1) {
145 scroll_to_div.scrollIntoView({behavior: "smooth", block: "start", inline: "nearest"});
155 // Render a message. Returns a div object.
156 function forum_render_one(msg, existing_div) {
158 if (existing_div != null) { // If an existing div was supplied, render into it
161 else { // Otherwise, create a new one
162 div = document.createElement("div");
165 mdiv = randomString(); // Give the div a new name
170 "<div class=\"ctdl-msg-wrapper\">" // begin message wrapper
171 + "<div class=\"ctdl-avatar\">" // begin avatar
172 + "<img src=\"/ctdl/u/" + msg.from + "/userpic\" width=\"32\" "
173 + "onerror=\"this.parentNode.innerHTML='<i class="fa fa-user-circle fa-2x"></i> '\">"
174 + "</div>" // end avatar
175 + "<div class=\"ctdl-msg-content\">" // begin content
176 + "<div class=\"ctdl-msg-header\">" // begin header
177 + "<span class=\"ctdl-msg-header-info\">" // begin header info on left side
178 + "<span class=\"ctdl-username\"><a href=\"#\">" // FIXME link to user profile
180 + "</a></span>" // end username
181 + "<span class=\"ctdl-msgdate\">"
182 + convertTimestamp(msg.time)
183 + "</span>" // end msgdate
184 + "</span>" // end header info on left side
185 + "<span class=\"ctdl-msg-header-buttons\">" // begin buttons on right side
187 + "<span class=\"ctdl-msg-button\">" // Reply
188 + "<a href=\"javascript:open_reply_box('"+mdiv+"',false,'"+msg.wefw+"','"+msg.msgn+"');\">"
189 + "<i class=\"fa fa-reply\"></i> "
193 + "<span class=\"ctdl-msg-button\">" // ReplyQuoted
194 + "<a href=\"javascript:open_reply_box('"+mdiv+"',true,'"+msg.wefw+"','"+msg.msgn+"');\">"
195 + "<i class=\"fa fa-comment\"></i> "
199 + "<span class=\"ctdl-msg-button\"><a href=\"#\">" // Delete , show only with permission FIXME
200 + "<i class=\"fa fa-trash\"></i> "
204 + "</span>"; // end buttons on right side
207 "<br><span class=\"ctdl-msgsubject\">" + msg.subj + "</span>";
210 "</div><br>" // end header
211 + "<div class=\"ctdl-msg-body\" id=\"" + mdiv + "_body\">" // begin body
213 + "</div>" // end body
214 + "</div>" // end content
215 + "</div>" // end wrapper
219 outmsg = "<div class=\"ctdl-msg-wrapper\">" + err.message + "</div>";
222 div.innerHTML = outmsg;
227 // Compose a references string using existing references plus the message being replied to
228 function compose_references(references, msgid) {
229 if (references.includes("@")) {
230 refs = references + "|";
237 // If the resulting string is too big, we can trim it here
238 while (refs.length > 900) {
240 r.splice(1,1); // remove the second element so we keep the root
247 // Open a reply box directly below a specific message
248 function open_reply_box(parent_div, is_quoted, references, msgid) {
249 let new_div = document.createElement("div");
250 let new_div_name = randomString();
251 new_div.id = new_div_name;
253 document.getElementById(parent_div).append(new_div);
256 "<div class=\"ctdl-msg-wrapper ctdl-msg-reply\">" // begin message wrapper
257 + "<div class=\"ctdl-avatar\">" // begin avatar
258 + "<img src=\"/ctdl/u/" + current_user + "/userpic\" width=\"32\" "
259 + "onerror=\"this.parentNode.innerHTML='<i class="fa fa-user-circle fa-2x"></i> '\">"
260 + "</div>" // end avatar
261 + "<div class=\"ctdl-msg-content\">" // begin content
262 + "<div class=\"ctdl-msg-header\">" // begin header
263 + "<span class=\"ctdl-msg-header-info\">" // begin header info on left side
264 + "<span class=\"ctdl-username\">"
265 + current_user // user = me !
267 + "<span class=\"ctdl-msgdate\">"
268 + convertTimestamp(Date.now() / 1000) // the current date/time (temporary for display)
270 + "</span>" // end header info on left side
271 + "<span class=\"ctdl-msg-header-buttons\">" // begin buttons on right side
273 + "<span class=\"ctdl-msg-button\">" // bold button
274 + "<a href=\"javascript:void(0)\" onclick=\"forum_format('bold')\">"
275 + "<i class=\"fa fa-bold fa-fw\"></i>"
278 + "<span class=\"ctdl-msg-button\">" // italic button
279 + "<a href=\"javascript:void(0)\" onclick=\"forum_format('italic')\">"
280 + "<i class=\"fa fa-italic fa-fw\"></i>"
283 + "<span class=\"ctdl-msg-button\">" // list button
284 + "<a href=\"javascript:void(0)\" onclick=\"forum_format('insertunorderedlist')\">"
285 + "<i class=\"fa fa-list fa-fw\"></i>"
288 + "<span class=\"ctdl-msg-button\">" // link button
289 + "<a href=\"javascript:void(0)\" onclick=\"forum_display_urlbox()\">"
290 + "<i class=\"fa fa-link fa-fw\"></i>"
293 + "</span>"; // end buttons on right side
296 //"<br><span id=\"ctdl-subject\" class=\"ctdl-msgsubject\">" + "FIXME subject" + "</span>";
298 //else { // hidden filed for empty subject
299 replybox += "<span id=\"ctdl-subject\" style=\"display:none\"></span>";
302 "</div><br>" // end header
304 + "<span id=\"ctdl-replyreferences\" style=\"display:none\">" // hidden field for references
305 + compose_references(references,msgid) + "</span>"
307 + "<div class=\"ctdl-msg-body\" id=\"ctdl-editor-body\" style=\"padding:5px;\" contenteditable=\"true\">"
308 + "\n"; // empty initial content
311 replybox += "<br><blockquote>"
312 + document.getElementById(parent_div+"_body").innerHTML
319 + "<div class=\"ctdl-msg-header\">" // begin footer
320 + "<span class=\"ctdl-msg-header-info\">" // begin footer info on left side
321 + " " // (nothing here for now)
322 + "</span>" // end footer info on left side
323 + "<span class=\"ctdl-msg-header-buttons\">" // begin buttons on right side
325 + "<span class=\"ctdl-msg-button\"><a href=\"javascript:forum_save_message('" + new_div_name + "');\">"
326 + "<i class=\"fa fa-check\" style=\"color:green\"></i> " // save button
330 + "<span class=\"ctdl-msg-button\"><a href=\"javascript:forum_cancel_post('" + new_div_name + "');\">"
331 + "<i class=\"fa fa-trash\" style=\"color:red\"></i> " // cancel button
335 + "</span>" // end buttons on right side
336 + "</div><br>" // end footer
339 + "</div>" // end content
340 + "</div>" // end wrapper
342 + "<div id=\"forum_url_entry_box\" class=\"w3-modal\">" // begin URL entry modal
343 + " <div class=\"w3-modal-content w3-animate-top w3-card-4\">"
344 + " <header class=\"w3-container w3-blue\"> "
345 + " <p><span>URL:</span></p>"
347 + " <div class=\"w3-container w3-blue\">"
348 + " <input id=\"forum_txtFormatUrl\" placeholder=\"http://\" style=\"width:100%\">"
350 + " <footer class=\"w3-container w3-blue\">"
351 + " <p><span class=\"ctdl-msg-button\"><a href=\"javascript:forum_close_urlbox(true);\">"
352 + " <i class=\"fa fa-check\" style=\"color:green\"></i> "
355 + " <span class=\"ctdl-msg-button\"><a href=\"javascript:forum_close_urlbox(false);\">"
356 + " <i class=\"fa fa-trash\" style=\"color:red\"></i> "
362 + " <input id=\"forum_selection_start\" style=\"display:none\"></input>" // hidden fields
363 + " <input id=\"forum_selection_end\" style=\"display:none\"></input>" // to store selection range
364 + "</div>" // end URL entry modal
367 document.getElementById(new_div_name).innerHTML = replybox;
368 document.getElementById(new_div_name).scrollIntoView({behavior: "smooth", block: "end", inline: "nearest"});
370 // These actions must happen *after* the initial render loop completes.
371 setTimeout(function() {
372 var tag = document.getElementById("ctdl-editor-body");
373 tag.focus(); // sets the focus
374 window.getSelection().collapse(tag.firstChild, 0); // positions the cursor
379 // Abort a message post (it simply destroys the div)
380 function forum_cancel_post(div_name) {
381 document.getElementById(div_name).outerHTML = ""; // make it cease to exist
385 // Save the posted message to the server
386 function forum_save_message(editor_div_name) {
388 document.body.style.cursor = "wait";
389 wefw = (document.getElementById("ctdl-replyreferences").innerHTML).replaceAll("|","!"); // references (if present)
390 subj = document.getElementById("ctdl-subject").innerHTML; // subject (if present)
392 url = "/ctdl/r/" + escapeHTMLURI(current_room)
393 + "/dummy_name_for_new_message"
396 boundary = randomString();
398 "--" + boundary + "\r\n"
399 + "Content-type: text/html\r\n"
400 + "Content-transfer-encoding: quoted-printable\r\n"
402 + quoted_printable_encode(
403 "<html><body>" + document.getElementById("ctdl-editor-body").innerHTML + "</body></html>"
405 + "--" + boundary + "--\r\n"
408 var request = new XMLHttpRequest();
409 request.open("PUT", url, true);
410 request.setRequestHeader("Content-type", "multipart/mixed; boundary=\"" + boundary + "\"");
411 request.onreadystatechange = function() {
412 if (request.readyState == 4) {
413 document.body.style.cursor = "default";
414 if (Math.trunc(request.status / 100) == 2) {
415 headers = request.getAllResponseHeaders().split("\n");
416 for (var i in headers) {
417 if (headers[i].startsWith("etag: ")) {
418 new_msg_num = headers[i].split(" ")[1];
422 // After saving the message, load it back from the server and replace the editor div with it.
423 replace_editor_with_final_message = async() => {
424 response = await fetch("/ctdl/r/" + escapeHTMLURI(current_room) + "/" + new_msg_num + "/json");
426 newly_posted_message = await(response.json());
427 forum_render_one(newly_posted_message, document.getElementById(editor_div_name));
430 replace_editor_with_final_message();
434 error_message = request.responseText;
435 if (error_message.length == 0) {
436 error_message = _("An error has occurred.");
438 alert(error_message); // editor remains open
442 request.send(body_text);
446 // Bold, italics, etc.
447 function forum_format(command, value) {
448 document.execCommand(command, false, value);
452 // Make the URL entry box appear.
453 // When the user clicks into the URL box it will make the previous focus disappear, so we have to save it.
454 function forum_display_urlbox() {
455 document.getElementById("forum_selection_start").value = window.getSelection().anchorOffset;
456 document.getElementById("forum_selection_end").value = window.getSelection().focusOffset;
457 document.getElementById("forum_url_entry_box").style.display = "block";
461 // When the URL box is closed, this gets called. do_save is true for Save, false for Cancel.
462 function forum_close_urlbox(do_save) {
464 var tag = document.getElementById("ctdl-editor-body");
465 var start_replace = document.getElementById("forum_selection_start").value; // use saved selection range
466 var end_replace = document.getElementById("forum_selection_end").value;
467 new_text = tag.innerHTML.substring(0, start_replace)
468 + "<a href=\"" + document.getElementById("forum_txtFormatUrl").value + "\">"
469 + tag.innerHTML.substring(start_replace, end_replace)
471 + tag.innerHTML.substring(end_replace);
472 tag.innerHTML = new_text;
474 document.getElementById("forum_txtFormatUrl").value = ""; // clear url box for next time
475 document.getElementById("forum_url_entry_box").style.display = "none";
479 // User has clicked the "Post message" button. This is roughly the same as "reply" except there is no parent message.
480 function forum_entmsg() {
481 open_reply_box("ctdl-newmsg-here", false, "", "");