78dfc0c41b4670538e0f5c3cb5c098f447ebd009
[citadel.git] / webcit-ng / static / js / main.js
1 // Copyright (c) 2016-2022 by the citadel.org team
2 //
3 // This program is open source software.  Use, duplication, or
4 // disclosure are subject to the GNU General Public License v3.
5
6
7 do_biff = async() => {
8         response = await fetch("/ctdl/a/biff");
9         if (response.ok) {
10                 biff_result = await(response.text());
11                 try {
12                         new_mail += parseInt(biff_result);
13                 }
14                 catch {
15                 }
16         }
17
18         if (new_mail > 0) {
19                 console.log("YOU'VE GOT MAIL!  new_mail = " + new_mail);
20                 new_mail_sound.play();                  // FIXME do a visual notification as well
21                 new_mail = 0;
22         }
23 }
24
25
26 // This is called at the very beginning of the main page load.
27 ctdl_startup = async() => {
28         response = await fetch("/ctdl/c/info");
29
30         if (response.ok) {
31                 serv_info = await(response.json());
32                 if (serv_info.serv_rev_level < 905) {
33                         alert(_("Citadel server is too old, some functions may not work"));
34                 }
35
36                 update_banner();
37
38                 // What do we do upon landing?
39
40                 if ( (serv_info.serv_supports_guest) || (logged_in) ) {                 // If the Lobby is visible,
41                         gotonext(1);                                                    // go there.
42                 }
43                 else {                                                                  // Otherwise,
44                         display_login_screen("");                                       // display the login modal.
45                 }
46
47                 var biff_interval;
48                 try {                                                                   // if this was already set up,
49                         clearInterval(biff_interval);                                   // clear the old one so there's only one.
50                 }
51                 catch {
52                 }
53                 do_biff();
54                 biff_interval = setInterval(do_biff, 10000);
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
67 // Update the "banner" div with all relevant info.
68 function update_banner() {
69         detect_logged_in();
70         if (current_room) {
71                 document.getElementById("ctdl_banner_title").innerHTML = current_room;
72                 if (is_room_aide) {
73                         document.getElementById("ctdl_banner_title").innerHTML += "&nbsp;<i class=\"fa fa-user-cog\"></i>";
74                 }
75                 document.title = current_room;
76         }
77         else {
78                 document.getElementById("ctdl_banner_title").innerHTML = serv_info.serv_humannode;
79         }
80         document.getElementById("current_user").innerHTML = render_userpic(current_user) + current_user ;
81
82         if (logged_in) {
83                 document.getElementById("lilo").innerHTML = "<a href=\"/ctdl/a/logout\"><i class=\"fa fa-right-from-bracket\"></i>" + _("Log off") + "</a>" ;
84         }
85         else {
86                 document.getElementById("lilo").innerHTML = "<a href=\"javascript:display_login_screen('')\"><i class=\"fa fa-right-to-bracket\"></i>" + _("Log in") + "</a>" ;
87         }
88 }
89
90
91 // goto room
92 function gotoroom(roomname) {
93
94         fetch_room = async() => {
95                 response = await fetch("/ctdl/r/" + escapeHTMLURI(roomname) + "/");
96                 if (response.ok) {
97                         data = await(response.json());
98                         current_room = data.name;
99                         new_messages = data.new_messages;
100                         total_messages = data.total_messages;
101                         current_view = data.current_view;
102                         default_view = data.default_view;
103                         last_seen = data.last_seen;
104                         is_room_aide = data.is_room_aide;
105                         room_mtime = data.room_mtime;
106                         can_delete_messages = data.can_delete_messages;
107                         update_banner();
108                         render_room_view();
109                 }
110         }
111         fetch_room();
112 }
113
114
115 // Goto next room with unread messages
116 // which_oper is 0=ungoto, 1=skip, 2=goto
117 function gotonext(which_oper) {
118         if (which_oper == 2) {                                  // Goto needs to mark messages as seen
119
120                 set_last_read_pointer = async() => {
121                         response = await fetch("/ctdl/r/" + escapeHTMLURI(current_room) + "/slrp?last=" + last_seen);
122                 }
123                 set_last_read_pointer();
124         }
125
126         if ((which_oper == 1) || (which_oper == 2)) {           // Skip or Goto both take us to the "next" room
127                 if (march_list.length == 0) {
128                         load_new_march_list(which_oper);        // we will recurse back here
129                 }
130                 else {
131                         next_room = march_list[0].name;
132                         march_list.splice(0, 1);
133                         gotoroom(next_room);
134                 }
135         }
136
137         // FIXME implement mode 0 (ungoto)
138
139 }
140
141
142 // Called by gotonext() when the march list is empty.
143 function load_new_march_list(which_oper) {
144         fetchm = async() => {
145                 response = await fetch("/ctdl/r/");
146                 march_list = await(response.json());
147                 if (response.ok) {
148                         march_list = march_list.filter(function(room) {
149                                 return(room.hasnewmsgs && room.default_view == views.VIEW_BBS);
150                         });
151                         march_list = march_list.sort(function(a,b) {
152                                 if (a.floor != b.floor) {
153                                         return(a.floor - b.floor);
154                                 }
155                                 if (a.rorder != b.rorder) {
156                                         return(a.rorder - b.rorder);
157                                 }
158                                 return(a.name < b.name);
159                         });
160
161                         // If there are still no rooms with new messages, go to the Lobby.
162                         if (march_list.length == 0) {
163                                 march_list.push({name:"_BASEROOM_",known:true,hasnewmsgs:true,floor:0});
164                         }
165
166                         gotonext(which_oper);
167                 }
168         }
169         fetchm();
170 }
171
172
173 // Activate the "Loading..." modal
174 function activate_loading_modal() {
175         document.getElementById("ctdl_big_modal").innerHTML =
176                   "<div class=\"w3-modal-content\">"
177                 + "  <div class=\"w3-container\">"
178
179                 + "<i class=\"fas fa-spinner fa-spin\"></i>&nbsp;&nbsp;"
180                 + _("Loading messages from server, please wait")
181
182                 + "  </div>"
183                 + "</div>";
184         document.getElementById("ctdl_big_modal").style.display = "block";
185 }
186
187
188 // Disappear the "Loading..." modal
189 function deactivate_loading_modal() {
190         document.getElementById("ctdl_big_modal").style.display = "none";
191 }