]> code.citadel.org Git - citadel.git/blob - webcit-ng/static/js/main.js
Client side of SLRP is done
[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.  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 ctdl_startup = async() => {
35         response = await fetch("/ctdl/c/info");
36
37         if (response.ok) {
38                 serv_info = await(response.json());
39                 if (serv_info.serv_rev_level < 905) {
40                         alert(_("Citadel server is too old, some functions may not work"));
41                 }
42
43                 update_banner();
44
45                 // What do we do upon landing?
46
47                 if ( (serv_info.serv_supports_guest) || (logged_in) ) {                 // If the Lobby is visible,
48                         gotonext(1);                                                    // go there.
49                 }
50                 else {                                                                  // Otherwise,
51                         display_login_screen("");                                       // display the login modal.
52                 }
53         }
54         else {
55                 document.getElementById("ctdl-main").innerHTML =
56                         "<div class=\"w3-panel w3-red\"><p>"
57                         + _("This program was unable to connect or stay connected to the Citadel server.  Please report this problem to your system administrator.")
58                         + "</div>";
59         }
60 }
61
62
63 // Display a room list in the main div.
64
65 // Update the "banner" div with all relevant info.
66 function update_banner() {
67         detect_logged_in();
68         if (current_room) {
69                 document.getElementById("ctdl_banner_title").innerHTML = current_room;
70                 document.title = current_room;
71         }
72         else {
73                 document.getElementById("ctdl_banner_title").innerHTML = serv_info.serv_humannode;
74         }
75         document.getElementById("current_user").innerHTML = current_user ;
76         if (logged_in) {
77                 document.getElementById("lilo").innerHTML = "<a href=\"/ctdl/a/logout\"><i class=\"fa-regular fa-right-from-bracket\"></i>" + _("Log off") + "</a>" ;
78         }
79         else {
80                 document.getElementById("lilo").innerHTML = "<a href=\"javascript:display_login_screen('')\">" + _("Log in") + "</a>" ;
81         }
82 }
83
84
85 // goto room
86 function gotoroom(roomname) {
87
88         fetch_room = async() => {
89                 response = await fetch("/ctdl/r/" + escapeHTMLURI(roomname) + "/");
90                 if (response.ok) {
91                         data = await(response.json());
92                         current_room = data.name;
93                         new_messages = data.new_messages;
94                         total_messages = data.total_messages;
95                         current_view = data.current_view;
96                         default_view = data.default_view;
97                         last_seen = data.last_seen;
98                         update_banner();
99                         render_room_view(0, 9999999999);
100                 }
101         }
102         fetch_room();
103 }
104
105
106 // Goto next room with unread messages
107 // which_oper is 0=ungoto, 1=skip, 2=goto
108 function gotonext(which_oper) {
109
110         if (which_oper == 2) {                                  // Goto needs to mark messages as seen
111                 console.log("FIXME set lrp to " + last_seen);
112                 set_last_read_pointer = async() => {
113                         response = await fetch("/ctdl/r/" + escapeHTMLURI(current_room) + "/slrp?last=" + last_seen);
114                 }
115                 set_last_read_pointer();
116         }
117
118         if ((which_oper == 1) || (which_oper == 2)) {           // Skip or Goto both take us to the "next" room
119                 if (march_list.length == 0) {
120                         console.log("Loading march list");
121                         load_new_march_list(which_oper);        // we will recurse back here
122                 }
123                 else {
124                         next_room = march_list[0].name;
125                         march_list.splice(0, 1);
126                         console.log("going to " + next_room + " , " + march_list.length + " rooms remaining");
127                         gotoroom(next_room);
128                 }
129         }
130
131         // FIXME implement mode 0 (ungoto)
132
133 }
134
135
136 // Called by gotonext() when the march list is empty.
137 function load_new_march_list(which_oper) {
138         fetchm = async() => {
139                 response = await fetch("/ctdl/r/");
140                 march_list = await(response.json());
141                 if (response.ok) {
142                         march_list = march_list.filter(function(room) {
143                                 return room.hasnewmsgs;
144                         });
145                         march_list = march_list.sort(function(a,b) {
146                                 if (a.floor != b.floor) {
147                                         return(a.floor - b.floor);
148                                 }
149                                 if (a.rorder != b.rorder) {
150                                         return(a.rorder - b.rorder);
151                                 }
152                                 return(a.name < b.name);
153                         });
154
155                         // If there are still no rooms with new messages, go to the Lobby.
156                         if (march_list.length == 0) {
157                                 march_list.push({name:"_BASEROOM_",known:true,hasnewmsgs:true,floor:0});
158                         }
159
160                         console.log(march_list);
161                         gotonext(which_oper);
162                 }
163         }
164         fetchm();
165 }
166
167
168 // Activate the "Loading..." modal
169 function activate_loading_modal() {
170         document.getElementById("ctdl_big_modal").innerHTML =
171                   "<div class=\"w3-modal-content\">"
172                 + "  <div class=\"w3-container\">"
173
174                 + "<i class=\"fas fa-spinner fa-spin\"></i>&nbsp;&nbsp;"
175                 + _("Loading messages from server, please wait")
176
177                 + "  </div>"
178                 + "</div>";
179         document.getElementById("ctdl_big_modal").style.display = "block";
180 }
181
182
183 // Disappear the "Loading..." modal
184 function deactivate_loading_modal() {
185         document.getElementById("ctdl_big_modal").style.display = "none";
186 }