begin work on displaying avatars next to messages
[citadel.git] / webcit-ng / static / js / views.js
1 //
2 // Copyright (c) 2016-2018 by the citadel.org team
3 //
4 // This program is open source software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License version 3.
6 //
7 // This program is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10 // GNU General Public License for more details.
11
12
13 // List of defined views shamelessly swiped from libcitadel headers
14 //
15 var views = {
16         VIEW_BBS                : 0,    /* Bulletin board view */
17         VIEW_MAILBOX            : 1,    /* Mailbox summary */
18         VIEW_ADDRESSBOOK        : 2,    /* Address book view */
19         VIEW_CALENDAR           : 3,    /* Calendar view */
20         VIEW_TASKS              : 4,    /* Tasks view */
21         VIEW_NOTES              : 5,    /* Notes view */
22         VIEW_WIKI               : 6,    /* Wiki view */
23         VIEW_CALBRIEF           : 7,    /* Brief Calendar view */
24         VIEW_JOURNAL            : 8,    /* Journal view */
25         VIEW_DRAFTS             : 9,    /* Drafts view */
26         VIEW_BLOG               : 10,   /* Blog view */
27         VIEW_QUEUE              : 11,   /* SMTP QUEUE rooms */
28         VIEW_WIKIMD             : 12,   /* Markdown Wiki view */
29 };
30
31
32 // This function is the dispatcher that determines the correct view for a room,
33 // and calls the correct renderer.  Greater/Less than bounds are accepted.
34 //
35 function render_room_view(gt_msg, lt_msg)
36 {
37         switch(current_view)
38         {
39                 case views.VIEW_MAILBOX:                                                // FIXME view mail rooms as forums for now
40                 case views.VIEW_BBS:
41                         forum_readmessages("ctdl-main", gt_msg, lt_msg);
42                         break;
43                 default:
44                         document.getElementById("ctdl-main").innerHTML =
45                                 "The view for " + current_room + " is " + current_view + " but there is no renderer." ;
46                         break;
47         }
48
49 }
50
51
52 // Forum view (flat) -- let's have another go at this with the rendering done client-side
53 //
54 function forum_readmessages(target_div, gt_msg, lt_msg)
55 {
56         original_text = document.getElementById(target_div).innerHTML;          // in case we need to replace it after an error
57         document.getElementById(target_div).innerHTML = 
58                 "<i class=\"fas fa-spinner fa-spin\"></i>&nbsp;&nbsp;"
59                 + _("Loading messages from server, please wait") ;
60
61         var request = new XMLHttpRequest();
62         if (lt_msg < 9999999999)
63         {
64                 request.open("GET", "/ctdl/r/" + escapeHTMLURI(current_room) + "/msgs.lt|" + lt_msg, true);
65         }
66         else
67         {
68                 request.open("GET", "/ctdl/r/" + escapeHTMLURI(current_room) + "/msgs.gt|" + gt_msg, true);
69         }
70         request.onreadystatechange = function()
71         {
72                 if (this.readyState === 4)
73                 {
74                         if ((this.status / 100) == 2)
75                         {
76                                 msgs = JSON.parse(this.responseText);
77                                 document.getElementById(target_div).innerHTML = "" ;
78
79                                 // If we were given an explicit starting point, by all means start there.
80                                 // Note that we don't have to remove them from the array because we did a 'msgs gt|xxx' command to Citadel.
81                                 if (gt_msg > 0)
82                                 {
83                                         msgs = msgs.slice(0, messages_per_page);
84                                 }
85
86                                 // Otherwise, show us the last 20 messages
87                                 else
88                                 {
89                                         if (msgs.length > messages_per_page)
90                                         {
91                                                 msgs = msgs.slice(msgs.length - messages_per_page);
92                                         }
93                                         new_old_div_name = randomString(5);
94                                         if (msgs.length < 1)
95                                         {
96                                                 newlt = lt_msg;
97                                         }
98                                         else
99                                         {
100                                                 newlt = msgs[0];
101                                         }
102                                         document.getElementById(target_div).innerHTML +=
103                                                 "<div id=\"" + new_old_div_name + "\">" +
104                                                 "<a href=\"javascript:forum_readmessages('" + new_old_div_name + "', 0, " + newlt + ");\">" +
105                                                 "link to msgs less than " + newlt + "</a></div>" ;
106                                 }
107
108                                 // Render the divs (we will fill them in later)
109                                 for (var i in msgs)
110                                 {
111                                         document.getElementById(target_div).innerHTML +=
112                                                 "<div id=\"ctdl_msg_"
113                                                 + msgs[i] + "\">#" + msgs[i]
114                                                 + "</div>" ;
115                                 }
116                                 if (lt_msg == 9999999999)
117                                 {
118                                         new_new_div_name = randomString(5);
119                                         if (msgs.length <= 0)
120                                         {
121                                                 newgt = gt_msg;
122                                         }
123                                         else
124                                         {
125                                                 newgt = msgs[msgs.length-1];
126                                         }
127                                         document.getElementById(target_div).innerHTML +=
128                                                 "<div id=\"" + new_new_div_name + "\">" +
129                                                 "<a href=\"javascript:forum_readmessages('" + new_new_div_name + "', " + newgt + ", 9999999999);\">"
130                                                 + "link to msgs greater than " + newgt + "</a></div>" ;
131                                 }
132
133                                 // Now figure out where to scroll to after rendering.
134                                 if (gt_msg > 0)
135                                 {
136                                         scroll_to = msgs[0];
137                                 }
138                                 else if (lt_msg < 9999999999)
139                                 {
140                                         scroll_to = msgs[msgs.length-1];
141                                 }
142                                 else if ( (logged_in) && (gt_msg == 0) && (lt_msg == 9999999999) )
143                                 {
144                                         scroll_to = msgs[msgs.length-1];
145                                 }
146                                 else
147                                 {
148                                         scroll_to = msgs[0];            // FIXME this is too naive
149                                 }
150
151                                 // Render the individual messages in the divs
152                                 forum_render_messages(msgs, "ctdl_msg_", scroll_to)
153                         }
154                         else
155                         {
156                                 // if xhr fails, this will make the link reappear so the user can try again
157                                 document.getElementById(target_div).innerHTML = original_text;
158                         }
159                 }
160         };
161         request.send();
162         request = null;
163 }
164
165
166 // Render a range of messages, with the div prefix specified
167 //
168 function forum_render_messages(msgs, prefix, scroll_to)
169 {
170         for (i=0; i<msgs.length; ++i)
171         {
172                 forum_render_one(prefix+msgs[i], msgs[i], scroll_to)
173         }
174 }
175
176
177 // 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.
178 function forum_render_one(div, msgnum, scroll_to)
179 {
180         var request = new XMLHttpRequest();
181         request.open("GET", "/ctdl/r/" + escapeHTMLURI(current_room) + "/" + msgs[i] + "/json", true);
182         request.onreadystatechange = function()
183         {
184                 if (this.readyState === 4)
185                 {
186                         if ((this.status / 100) == 2)
187                         {
188                                 msg = JSON.parse(this.responseText);
189
190                                 document.getElementById(div).innerHTML =
191                                   "<div class=\"ctdl-msg-wrapper\">"                            // begin message wrapper
192                                 + "<div class=\"ctdl-avatar\">"                                 // begin avatar
193
194                                 + "<img src=\"/ctdl/u/" + msg.from + "/userpic\" width=\"32\" onerror=\"this.parentNode.innerHTML='FIXME'\">"
195
196                                 //+ "<i class=\"fa fa-user-circle fa-2x\"></i> "                        // FIXME temporary avatar
197                                 + "</div>"                                                      // end avatar
198                                 + "<div class=\"ctdl-msg-content\">"                            // begin content
199                                 + "<div class=\"ctdl-msg-header\">"                             // begin header
200                                 + "<span class=\"ctdl-username\"><a href=\"#\">"                // FIXME link to user profile
201                                 + msg.from
202                                 + "</a></span> "
203                                 + "<span class=\"ctdl-msgdate\">"
204                                 + msg.time
205                                 + "</span> "
206                                 + "</div>"                                                      // end header
207                                 + "<div>"                                                       // begin body
208                                 + msg.text
209                                 + "</div>"                                                      // end body
210                                 + "</div>"                                                      // end content
211                                 + "</div>"                                                      // end wrapper
212                                 ;
213                         }
214                         else
215                         {
216                                 document.getElementById(div).innerHTML = "ERROR";
217                         }
218                         if (msgnum == scroll_to)
219                         {
220                                 window.location.hash = div;
221                         }
222                 }
223         };
224         request.send();
225         request = null;
226 }
227