-
Notifications
You must be signed in to change notification settings - Fork 3
/
chat.js
executable file
·296 lines (246 loc) · 9.88 KB
/
chat.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
var meshchat_id;
var last_messages_update = epoch();
var call_sign = 'NOCALL';
var enable_video = 0;
var messages = new Messages();
let alert = new Audio('alert.mp3');
let context = {
config_loaded: false,
debug: true, // let startup funcs show debug
}
$(function() {
meshchat_init();
});
function monitor_last_update() {
var secs = epoch() - last_messages_update;
$('#last-update').html('<strong>Updated:</strong> ' + secs + ' seconds ago');
}
function update_messages(reason=Messages.MSG_UPDATE) {
if (reason != Messages.MSG_UPDATE) return;
let caller = (new Error()).stack.split("\n")[3].split("/")[0];
console.debug(caller + "->update_messages(reason=MSG_UPDATE)");
// update the message table
let html = messages.render($('#channels').val(), $('#search').val());
if (html) $('#message-table').html(html);
last_messages_update = epoch();
}
function new_messages(reason) {
if (reason != Messages.NEW_MSG) return;
let caller = (new Error()).stack.split("\n")[3].split("/")[0];
console.debug(caller + "->new_messages(reason=NEW_MSG)");
alert.play();
}
function update_channels(reason) {
if (reason != Messages.CHAN_UPDATE) return;
let caller = (new Error()).stack.split("\n")[3].split("/")[0];
console.debug(caller + "->update_channels(reason=CHAN_UPDATE)");
let msg_refresh = false;
let channels = messages.channels().sort();
let channel_filter = $('#channels').val();
let cur_send_channel = $('#send-channel').val();
// null signals a new channel was just created
if (cur_send_channel == null) {
channel_filter = messages.current_channel();
cur_send_channel = messages.current_channel();
msg_refresh = true;
}
// clear channel selection boxes
$('#send-channel').find('option').remove().end();
$('#channels').find('option').remove().end();
function add_option(select, title, value) {
select.append("<option value='"+value+"'>"+title+"</option>");
}
// Add static channels to channel selection boxes
add_option($('#send-channel'), "Everything", "");
add_option($('#send-channel'), "Add New Channel", "Add New Channel");
add_option($('#channels'), "Everything", "");
for (var chan of channels) {
if (chan != "") {
add_option($('#send-channel'), chan, chan);
add_option($('#channels'), chan, chan);
}
}
$("#channels").val(channel_filter);
$("#send-channel").val(cur_send_channel);
if (msg_refresh) update_messages();
}
function start_chat() {
debug("start_chat()");
// wait until the configuration is fully loaded
load_config().then(function(data) {
config = data;
document.title = 'Mesh Chat v' + data.version;
$('#version').html('<strong>Mesh Chat v' + data.version + '</strong>');
$('#node').html('<strong>Node:</strong> ' + data.node);
$('#zone').html('<strong>Zone:</strong> ' + data.zone);
$('#callsign').html('<strong>Call Sign:</strong> ' + Cookies.get('meshchat_call_sign'));
$('#copyright').html('Mesh Chat v' + data.version + ' Copyright © ' + new Date().getFullYear() + ' <a href="http://www.trevorsbench.com">Trevor Paskett - K7FPV</a> <small>(Lua by KN6PLV)</small>');
if ("default_channel" in data) {
default_channel = data.default_channel;
$('#send-channel').val(data.default_channel);
$('#channels').val(data.default_channel);
messages.set_channel(data.default_channel);
update_messages();
}
if ("debug" in data) {
context.debug = data.debug == 1 ? true : false;
}
// signal that the config has finished loading
context.config_loaded = true;
})
//$('#logout').html('Logout ' + call_sign);
messages.subscribe(update_messages);
messages.subscribe(new_messages);
messages.subscribe(update_channels);
messages.check();
load_users();
monitor_last_update();
// start event loops to update MeshChat client
setInterval(() => { messages.check() }, 15000);
setInterval(() => { load_users() }, 15000);
setInterval(() => { monitor_last_update() }, 2500);
}
function meshchat_init() {
debug("meshchat_init()");
$('#message').val('');
meshchat_id = Cookies.get('meshchat_id');
if (meshchat_id == undefined) {
// TODO set default expiration of cookie
Cookies.set('meshchat_id', make_id());
meshchat_id = Cookies.get('meshchat_id');
}
$('#submit-message').on('click', function(e) {
e.preventDefault();
if ($('#message').val().length == 0) return;
ohSnapX();
// disable message sending box
$(this).prop("disabled", true);
$('#message').prop("disabled", true);
$(this).html('<div class="loading"></div>');
let channel = $('#send-channel').val();
if ($('#new-channel').val() != '') {
channel = $('#new-channel').val();
$('#send-channel').val('Everything');
}
messages.send($('#message').val(), channel, call_sign).then(
// sent
(sent) => {
$('#message').val('');
ohSnap('Message sent', 'green');
update_messages(Messages.NEW_MSG);
// clear out new channel box in case it was used and
// reset to normal selection box
$('#new-channel').val('');
$('#new-channel').hide();
$('#send-channel').show();
},
// error
(err_msg) => {
ohSnap(err_msg, 'red', {time: '30000'});
}
).finally(() => {
// change the channel selector to the channel the message was
// just sent to
$('#channels').val(channel);
messages.set_channel(channel);
update_messages();
// re-enable message sending box
$(this).prop("disabled", false);
$('#message').prop("disabled", false);
$(this).html('Send');
});
});
$('#submit-call-sign').on('click', function(e) {
e.preventDefault();
if ($('#call-sign').val().length == 0) return;
call_sign = $('#call-sign').val().toUpperCase();
// TODO set default expiration of cookie
Cookies.set('meshchat_call_sign', call_sign);
$('#call-sign-container').addClass('hidden');
$('#chat-container').removeClass('hidden');
$('#callsign').html('<strong>Call Sign:</strong> ' + call_sign);
start_chat();
});
$('#channels').on('change', function() {
$('#send-channel').val(this.value);
messages.set_channel(this.value);
update_messages();
});
$('#search').keyup(function() {
//console.log(this.value);
update_messages();
});
$('#message-expand').on('click', function(e) {
$('#message-panel').toggleClass('message-panel-collapse');
$('#message-panel-body').toggleClass('message-panel-body-collapse');
$('#users-panel').toggleClass('users-panel-collapse');
$('#users-panel-body').toggleClass('users-panel-body-collapse');
});
// allow user to enter new channel
$('#send-channel').on('change', function() {
if (this.value == "Add New Channel") {
$('#new-channel').show().focus();
$(this).hide();
}
});
// process a CTRL <ENTER> to send a message
$('#message').keydown(function (e) {
if ((e.keyCode == 10 || e.keyCode == 13) && e.ctrlKey) {
$("#submit-message").trigger( "click" );
}
});
// login with a cookie
var cookie_call_sign = Cookies.get('meshchat_call_sign');
if (cookie_call_sign == undefined) {
$('#call-sign-container').removeClass('hidden');
} else {
$('#call-sign-container').addClass('hidden');
$('#chat-container').removeClass('hidden');
call_sign = cookie_call_sign;
start_chat();
}
}
let users_updating = false;
function load_users() {
debug("load_users()");
if (users_updating == true) return;
console.debug("load_users()");
// lock to prevent simultaneous updates
users_updating = true;
$.getJSON('/cgi-bin/meshchat?action=users&call_sign=' + call_sign + '&id=' + meshchat_id,
(data) => {
if (data == null || data == 0) return;
let html = '';
let count = 0;
for (var entry of data) {
var date = new Date(0);
date.setUTCSeconds(entry.epoch);
// user heartbeat timeout > 4 mins
if ((epoch() - entry.epoch) > 240) continue;
// user heartbeat > 2 mins, expiring
if ((epoch() - entry.epoch) > 120) {
html += '<tr class="grey-background">';
} else {
html += '<tr>';
}
if (enable_video == 0) {
html += '<td>' + entry.call_sign + '</td>';
} else {
html += '<td><a href="' + entry.id + '" onclick="start_video(\'' + entry.id + '\');return false;">' + entry.call_sign + '</td>';
}
if (entry.platform == 'node') {
html += '<td><a href="http://' + aredn_domain(entry.node) + ':8080" target="_blank">' + entry.node + '</a></td>';
} else {
html += '<td><a href="http://' + aredn_domain(entry.node) + '" target="_blank">' + entry.node + '</a></td>';
}
html += '<td>' + format_date(date) + '</td>';
html += '</tr>';
count++;
}
$('#users-table').html(html);
$('#users-count').html(count);
}).always(() => {
// allow updates again
users_updating = false;
});
}