This is a better version of detect_logged_in() for webcit-ng that
[citadel.git] / webcit-ng / static / js / main.js
1 // Copyright (c) 2016-2020 by the citadel.org team
2 //
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.
7 //
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.
12
13
14 var current_room = "_BASEROOM_";
15 var new_messages = 0;
16 var total_messages = 0;
17 var default_view = 0;
18 var current_view = 0;
19 var logged_in = 0;
20 var current_user = _("Not logged in.");
21 var serv_info;
22 var last_seen = 0;
23 var messages_per_page = 20;
24 var march_list = [] ;
25
26
27 // Placeholder for when we add i18n later
28 function _(x) {
29         return x;
30 }
31
32
33 // This is called at the very beginning of the main page load.
34 //
35 ctdl_startup = async() => {
36         response = await fetch("/ctdl/c/info");
37
38         if (response.ok) {
39                 serv_info = await(response.json());
40                 if (serv_info.serv_rev_level < 905) {
41                         alert(_("Citadel server is too old, some functions may not work"));
42                 }
43
44                 update_banner();
45
46                 // What do we do upon landing?
47
48                 if ( (serv_info.serv_supports_guest) || (logged_in) ) {                 // If the Lobby is visible,
49                         gotoroom("_BASEROOM_");                                         // go there.
50                         display_room_list();
51                 }
52                 else {                                                                  // Otherwise,
53                         display_login_screen("logged in users only.  sheeeeeeeeeit.");  // display the login modal.
54                 }
55         }
56         else {
57                 document.getElementById("ctdl-main").innerHTML =
58                         "<div class=\"w3-panel w3-red\"><p>"
59                         + _("This program was unable to connect or stay connected to the Citadel server.  Please report this problem to your system administrator.")
60                         + "</div>";
61         }
62 }
63
64
65 // Display a room list in the main div.
66 function display_room_list() {
67         document.getElementById("roomlist").innerHTML = "<img src=\"/ctdl/s/images/throbber.gif\" />";  // show throbber while loading
68
69         fetch_room_list = async() => {
70                 response = await fetch("/ctdl/r/");
71                 room_list = await(response.json());
72                 if (response.ok) {
73                         display_room_list_renderer(room_list);
74                 }
75                 else {
76                         document.getElementById("roomlist").innerHTML = "<i>error</i>";
77                 }
78         }
79         fetch_room_list();
80 }
81
82
83 // Renderer for display_room_list()
84 function display_room_list_renderer(data) {
85         data = data.sort(function(a,b) {
86                 if (a.floor != b.floor) {
87                         return(a.floor - b.floor);
88                 }
89                 if (a.rorder != b.rorder) {
90                         return(a.rorder - b.rorder);
91                 }
92                 return(a.name < b.name);
93         });
94
95         new_roomlist_text = "<ul>" ;
96
97         for (var i in data) {
98                 if (i > 0) {
99                         if (data[i].floor != data[i-1].floor) {
100                                 new_roomlist_text = new_roomlist_text + "<li class=\"divider\"></li>" ;
101                         }
102                 }
103                 new_roomlist_text = new_roomlist_text +
104                         "<li>"
105                         + (data[i].hasnewmsgs ? "<b>" : "")
106                         + "<a href=\"javascript:gotoroom('" + escapeJS(escapeHTML(data[i].name)) + "');\">"
107                         + escapeHTML(data[i].name)
108                         + (data[i].hasnewmsgs ? "</b>" : "")
109                         + "</a></li>"
110                 ;
111         }
112         new_roomlist_text = new_roomlist_text + "</ul>";
113         document.getElementById("roomlist").innerHTML = new_roomlist_text ;
114 }
115
116
117 // Update the "banner" div with all relevant info.
118 //
119 function update_banner() {
120         detect_logged_in();
121         if (current_room) {
122                 document.getElementById("ctdl_banner_title").innerHTML = current_room;
123                 document.title = current_room;
124         }
125         else {
126                 document.getElementById("ctdl_banner_title").innerHTML = serv_info.serv_humannode;
127         }
128         document.getElementById("current_user").innerHTML = current_user ;
129         if (logged_in) {
130                 document.getElementById("lilo").innerHTML = "<a href=\"/ctdl/a/logout\">" + _("Log off") + "</a>" ;
131         }
132         else {
133                 document.getElementById("lilo").innerHTML = "<a href=\"javascript:display_login_screen('')\">" + _("Log in") + "</a>" ;
134         }
135 }
136
137
138 // goto room
139 function gotoroom(roomname) {
140
141         fetch_room = async() => {
142                 response = await fetch("/ctdl/r/" + escapeHTMLURI(roomname) + "/");
143                 if (response.ok) {
144                         data = await(response.json());
145                         current_room = data.name;
146                         new_messages = data.new_messages;
147                         total_messages = data.total_messages;
148                         current_view = data.current_view;
149                         default_view = data.default_view;
150                         last_seen = data.last_seen;
151                         update_banner();
152                         render_room_view(0, 9999999999);
153                 }
154         }
155         fetch_room();
156 }
157
158
159 // Goto next room with unread messages
160 // which_oper is 0=ungoto, 1=skip, 2=goto
161 //
162 function gotonext(which_oper) {
163         if (which_oper != 2) return;            // FIXME implement the other two
164         if (march_list.length == 0) {
165                 load_new_march_list();          // we will recurse back here
166         }
167         else {
168                 next_room = march_list[0].name;
169                 march_list.splice(0, 1);
170                 console.log("going to " + next_room + " , " + march_list.length + " rooms remaining in march list");
171                 gotoroom(next_room);
172         }
173 }
174
175
176 // Called by gotonext() when the march list is empty.
177 //
178 function load_new_march_list() {
179         fetchm = async() => {
180                 response = await fetch("/ctdl/r/");
181                 march_list = await(response.json());
182                 if (response.ok) {
183                         march_list = march_list.filter(function(room) {
184                                 return room.hasnewmsgs;
185                         });
186                         march_list = march_list.sort(function(a,b) {
187                                 if (a.floor != b.floor) {
188                                         return(a.floor - b.floor);
189                                 }
190                                 if (a.rorder != b.rorder) {
191                                         return(a.rorder - b.rorder);
192                                 }
193                                 return(a.name < b.name);
194                         });
195                         march_list.push({name:"_BASEROOM_",known:true,hasnewmsgs:true,floor:0});
196                         gotonext();
197                 }
198         }
199         fetchm();
200 }
201
202
203 // Activate the "Loading..." modal
204 function activate_loading_modal() {
205         document.getElementById("ctdl_big_modal").innerHTML =
206                   "<div class=\"w3-modal-content\">"
207                 + "  <div class=\"w3-container\">"
208
209                 + "<i class=\"fas fa-spinner fa-spin\"></i>&nbsp;&nbsp;"
210                 + _("Loading messages from server, please wait")
211
212                 + "  </div>"
213                 + "</div>";
214         document.getElementById("ctdl_big_modal").style.display = "block";
215 }
216
217
218 // Disappear the "Loading..." modal
219 function deactivate_loading_modal() {
220         document.getElementById("ctdl_big_modal").style.display = "none";
221 }