Mercurial Hosting > chat
comparison src/chat.js @ 73:a63faf49e1d7
last seen
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sat, 08 Mar 2025 20:26:19 -0700 |
parents | bce0480721c1 |
children | 377bdda60f0b |
comparison
equal
deleted
inserted
replaced
72:bce0480721c1 | 73:a63faf49e1d7 |
---|---|
1 'use strict'; | 1 'use strict'; |
2 | 2 |
3 let title = document.title; | 3 let title = document.title; |
4 let currentChatId = null; | 4 let currentChatId = null; |
5 let eventSource; | 5 let eventSource; |
6 let lastUpdate; | 6 let lastUpdate = Date.now(); |
7 let userId; | 7 let userId; |
8 let filebinUrl; | 8 let filebinUrl; |
9 | 9 |
10 function evalEvent(event) { | 10 function evalEvent(event) { |
11 // console.log(event); | 11 // console.log(event); |
225 pulldown.scrollIntoViewIfNeeded(false); | 225 pulldown.scrollIntoViewIfNeeded(false); |
226 } | 226 } |
227 } | 227 } |
228 | 228 |
229 function heartbeat() { | 229 function heartbeat() { |
230 showOnline(); | |
231 ajax(`heartbeat.js?last_update=${lastUpdate}`); | 230 ajax(`heartbeat.js?last_update=${lastUpdate}`); |
232 } | 231 } |
233 | 232 |
234 setInterval( heartbeat, 10000 ); | 233 setInterval( heartbeat, 10000 ); |
234 heartbeat(); | |
235 | 235 |
236 let online = {}; | 236 let online = {}; |
237 | |
238 function setOnline(userId) { | |
239 online[userId] = Date.now(); | |
240 } | |
241 | 237 |
242 function showOnline() { | 238 function showOnline() { |
243 let old = Date.now() - 70000; | 239 let old = Date.now() - 70000; |
244 for( let id of Object.keys(online) ) { | |
245 if( online[id] < old ) | |
246 delete online[id]; | |
247 } | |
248 let spans = document.querySelectorAll('span[online]'); | 240 let spans = document.querySelectorAll('span[online]'); |
249 for( let span of spans ) { | 241 for( let span of spans ) { |
250 let id = span.getAttribute('online'); | 242 let id = span.getAttribute('online'); |
251 span.setAttribute('is_online',!!online[id]); | 243 let wasOnline = online[id]; |
244 let isOnline = !!wasOnline && wasOnline > old; | |
245 span.setAttribute('is_online',isOnline); | |
252 } | 246 } |
253 } | 247 } |
254 | 248 |
255 function clearUnread() { | 249 function clearUnread() { |
256 let span = document.querySelector(`div[chat="${currentChatId}"] span[unread]`); | 250 let span = document.querySelector(`div[chat="${currentChatId}"] span[unread]`); |
326 uploadToFilebin(file.name,reader.result); | 320 uploadToFilebin(file.name,reader.result); |
327 }; | 321 }; |
328 reader.readAsArrayBuffer(file); | 322 reader.readAsArrayBuffer(file); |
329 } | 323 } |
330 | 324 |
325 let times = [ | |
326 { | |
327 time: 1000*60*60*24, | |
328 unit: 'day' | |
329 }, | |
330 { | |
331 time: 1000*60*60, | |
332 unit: 'hour' | |
333 }, | |
334 { | |
335 time: 1000*60, | |
336 unit: 'minute' | |
337 } | |
338 ]; | |
339 | |
340 function ago(time) { | |
341 for( let t of times ) { | |
342 let n = Math.floor(time / t.time); | |
343 if( n > 0 ) { | |
344 let s = `${n} ${t.unit}`; | |
345 if( n > 1 ) | |
346 s = s + 's'; | |
347 return s + ' ago'; | |
348 } | |
349 } | |
350 return 'just now'; | |
351 end | |
352 } | |
353 | |
331 function openPeople() { | 354 function openPeople() { |
332 let dialog = document.querySelector('dialog[people]'); | 355 let dialog = document.querySelector('dialog[people]'); |
333 openModal(dialog); | 356 let spans = dialog.querySelectorAll('span[last_seen]'); |
334 } | 357 let now = Date.now(); |
358 let old = now - 70000; | |
359 for( let span of spans ) { | |
360 let id = span.getAttribute('last_seen'); | |
361 let s; | |
362 let lastOnline = online[id]; | |
363 if( !lastOnline ) { | |
364 s = ''; | |
365 } else if( lastOnline > old ) { | |
366 s = 'Active now'; | |
367 } else { | |
368 s = 'Last seen ' + ago(now - lastOnline); | |
369 } | |
370 console.log(`${id} ${s}`); | |
371 span.textContent = s; | |
372 } | |
373 openModal(dialog); | |
374 } |