2 // Copyright (c) 2016-2020 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.
15 // List of defined views shamelessly swiped from libcitadel headers
18 VIEW_BBS : 0, /* Bulletin board view */
19 VIEW_MAILBOX : 1, /* Mailbox summary */
20 VIEW_ADDRESSBOOK : 2, /* Address book view */
21 VIEW_CALENDAR : 3, /* Calendar view */
22 VIEW_TASKS : 4, /* Tasks view */
23 VIEW_NOTES : 5, /* Notes view */
24 VIEW_WIKI : 6, /* Wiki view */
25 VIEW_CALBRIEF : 7, /* Brief Calendar view */
26 VIEW_JOURNAL : 8, /* Journal view */
27 VIEW_DRAFTS : 9, /* Drafts view */
28 VIEW_BLOG : 10, /* Blog view */
29 VIEW_QUEUE : 11, /* SMTP QUEUE rooms */
30 VIEW_WIKIMD : 12, /* Markdown Wiki view */
34 // This function is the dispatcher that determines the correct view for a room,
35 // and calls the correct renderer. Greater/Less than bounds are accepted.
37 function render_room_view(gt_msg, lt_msg)
39 switch(current_view) {
40 case views.VIEW_MAILBOX: // FIXME view mail rooms as forums for now
42 forum_readmessages("ctdl-main", gt_msg, lt_msg);
45 document.getElementById("ctdl-main").innerHTML =
46 "The view for " + current_room + " is " + current_view + " but there is no renderer." ;
53 // Forum view (flat) -- let's have another go at this with the rendering done client-side
55 function forum_readmessages(target_div, gt_msg, lt_msg)
57 original_text = document.getElementById(target_div).innerHTML; // in case we need to replace it after an error
58 document.getElementById(target_div).innerHTML =
59 "<div align=\"center\"><i class=\"fas fa-spinner fa-spin\"></i> "
60 + _("Loading messages from server, please wait") + "</div>";
62 if (lt_msg < 9999999999) {
63 url = "/ctdl/r/" + escapeHTMLURI(current_room) + "/msgs.lt|" + lt_msg;
66 url = "/ctdl/r/" + escapeHTMLURI(current_room) + "/msgs.gt|" + gt_msg;
69 fetch_msg_list = async() => {
70 response = await fetch(url);
71 msgs = await(response.json());
73 document.getElementById(target_div).innerHTML = "" ;
75 // If we were given an explicit starting point, by all means start there.
76 // Note that we don't have to remove them from the array because we did a 'msgs gt|xxx' command to Citadel.
78 msgs = msgs.slice(0, messages_per_page);
81 // Otherwise, show us the last 20 messages
83 if (msgs.length > messages_per_page) {
84 msgs = msgs.slice(msgs.length - messages_per_page);
86 new_old_div_name = randomString(5);
87 if (msgs.length < 1) {
93 document.getElementById(target_div).innerHTML +=
94 "<div id=\"" + new_old_div_name + "\">" +
95 "<div align=\"center\">" +
96 "<a href=\"javascript:forum_readmessages('" + new_old_div_name + "', 0, " + newlt + ");\">" +
97 "<i class=\"fa fa-arrow-circle-up\"></i> " +
98 _("Older posts") + " <i class=\"fa fa-arrow-circle-up\"></a></div></div></a></div></div>" ;
101 // Render an empty div for each message. We will fill them in later.
102 for (var i in msgs) {
103 document.getElementById(target_div).innerHTML += "<div id=\"ctdl_msg_" + msgs[i] + "\"> </div>" ;
104 document.getElementById("ctdl_msg_"+msgs[i]).style.display = "none";
106 if (lt_msg == 9999999999) {
107 new_new_div_name = randomString(5);
108 if (msgs.length <= 0) {
112 newgt = msgs[msgs.length-1];
114 document.getElementById(target_div).innerHTML +=
115 "<div id=\"" + new_new_div_name + "\">" +
116 "<div align=\"center\">" +
117 "<a href=\"javascript:forum_readmessages('" + new_new_div_name + "', " + newgt + ", 9999999999);\">" +
118 "<i class=\"fa fa-arrow-circle-down\"></i> " +
119 _("Newer posts") + " <i class=\"fa fa-arrow-circle-down\"></a></div></div>" ;
122 // Now figure out where to scroll to after rendering.
126 else if (lt_msg < 9999999999) {
127 scroll_to = msgs[msgs.length-1];
129 else if ( (logged_in) && (gt_msg == 0) && (lt_msg == 9999999999) ) {
130 scroll_to = msgs[msgs.length-1];
133 scroll_to = msgs[0]; // FIXME this is too naive
136 // Render the individual messages in the divs
137 forum_render_messages(msgs, "ctdl_msg_", scroll_to)
140 // if xhr fails, this will make the link reappear so the user can try again
141 document.getElementById(target_div).innerHTML = original_text;
148 // Render a range of messages, with the div prefix specified
150 function forum_render_messages(msgs, prefix, scroll_to)
152 for (i=0; i<msgs.length; ++i) {
153 forum_render_one(prefix+msgs[i], msgs[i], scroll_to);
158 // 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.
159 function forum_render_one(div, msgnum, scroll_to) {
160 fetch_message = async() => {
161 response = await fetch("/ctdl/r/" + escapeHTMLURI(current_room) + "/" + msgs[i] + "/json");
162 msg = await response.json();
165 document.getElementById(div).innerHTML =
166 "<div class=\"ctdl-msg-wrapper\">" // begin message wrapper
167 + "<div class=\"ctdl-avatar\">" // begin avatar
168 + "<img src=\"/ctdl/u/" + msg.from + "/userpic\" width=\"32\" "
169 + "onerror=\"this.parentNode.innerHTML='<i class="fa fa-user-circle fa-2x"></i> '\">"
170 + "</div>" // end avatar
171 + "<div class=\"ctdl-msg-content\">" // begin content
172 + "<div class=\"ctdl-msg-header\">" // begin header
173 + "<span class=\"ctdl-username\"><a href=\"#\">" // FIXME link to user profile
176 + "<span class=\"ctdl-msgdate\">"
179 + "</div>" // end header
180 + "<div>" // begin body
182 + "</div>" // end body
183 + "</div>" // end content
184 + "</div>" // end wrapper
188 document.getElementById(div).innerHTML = "ERROR";
190 document.getElementById(div).style.display = "inline";
191 if (msgnum == scroll_to) {
192 window.location.hash = div;