Mercurial Hosting > chat
diff 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 |
line wrap: on
line diff
--- a/src/chat.js Sat Mar 08 17:29:57 2025 -0700 +++ b/src/chat.js Sat Mar 08 20:26:19 2025 -0700 @@ -3,7 +3,7 @@ let title = document.title; let currentChatId = null; let eventSource; -let lastUpdate; +let lastUpdate = Date.now(); let userId; let filebinUrl; @@ -227,28 +227,22 @@ } function heartbeat() { - showOnline(); ajax(`heartbeat.js?last_update=${lastUpdate}`); } setInterval( heartbeat, 10000 ); +heartbeat(); let online = {}; -function setOnline(userId) { - online[userId] = Date.now(); -} - function showOnline() { let old = Date.now() - 70000; - for( let id of Object.keys(online) ) { - if( online[id] < old ) - delete online[id]; - } let spans = document.querySelectorAll('span[online]'); for( let span of spans ) { let id = span.getAttribute('online'); - span.setAttribute('is_online',!!online[id]); + let wasOnline = online[id]; + let isOnline = !!wasOnline && wasOnline > old; + span.setAttribute('is_online',isOnline); } } @@ -328,7 +322,53 @@ reader.readAsArrayBuffer(file); } +let times = [ + { + time: 1000*60*60*24, + unit: 'day' + }, + { + time: 1000*60*60, + unit: 'hour' + }, + { + time: 1000*60, + unit: 'minute' + } +]; + +function ago(time) { + for( let t of times ) { + let n = Math.floor(time / t.time); + if( n > 0 ) { + let s = `${n} ${t.unit}`; + if( n > 1 ) + s = s + 's'; + return s + ' ago'; + } + } + return 'just now'; +end +} + function openPeople() { let dialog = document.querySelector('dialog[people]'); + let spans = dialog.querySelectorAll('span[last_seen]'); + let now = Date.now(); + let old = now - 70000; + for( let span of spans ) { + let id = span.getAttribute('last_seen'); + let s; + let lastOnline = online[id]; + if( !lastOnline ) { + s = ''; + } else if( lastOnline > old ) { + s = 'Active now'; + } else { + s = 'Last seen ' + ago(now - lastOnline); + } + console.log(`${id} ${s}`); + span.textContent = s; + } openModal(dialog); }