Mercurial Hosting > chat
comparison src/chat.js @ 33:e2b7f6393dab
add online
| author | Franklin Schmidt <fschmidt@gmail.com> |
|---|---|
| date | Sun, 10 Nov 2024 19:57:14 -0700 |
| parents | 0f40501b0b56 |
| children | 62d04ca486dd |
comparison
equal
deleted
inserted
replaced
| 32:0f40501b0b56 | 33:e2b7f6393dab |
|---|---|
| 171 } | 171 } |
| 172 } | 172 } |
| 173 } | 173 } |
| 174 | 174 |
| 175 window.onfocus = function() { | 175 window.onfocus = function() { |
| 176 // console.log('onfocus'); | |
| 176 document.title = title; | 177 document.title = title; |
| 177 hasUnseen = false; | 178 hasUnseen = false; |
| 178 }; | 179 }; |
| 179 | 180 |
| 180 let urlRegex = /(^|\s)(https?:\/\/\S+)/g; | 181 let urlRegex = /(^|\s)(https?:\/\/\S+)/g; |
| 205 pulldown.scrollIntoViewIfNeeded(false); | 206 pulldown.scrollIntoViewIfNeeded(false); |
| 206 } | 207 } |
| 207 } | 208 } |
| 208 | 209 |
| 209 setInterval(function(){ | 210 setInterval(function(){ |
| 211 showOnline(); | |
| 210 ajax(`heartbeat.js?last_update=${lastUpdate}`); | 212 ajax(`heartbeat.js?last_update=${lastUpdate}`); |
| 211 }, 60000 ); | 213 }, 10000 ); |
| 212 | 214 |
| 213 function resync(updated) { | 215 function resync(updated) { |
| 214 lastUpdate = updated; | 216 lastUpdate = updated; |
| 215 currentChatId = null; | 217 currentChatId = null; |
| 216 document.querySelector('div[posts]').innerHTML = ''; | 218 document.querySelector('div[posts]').innerHTML = ''; |
| 221 let sound = new Audio('/images/notify.mp3'); | 223 let sound = new Audio('/images/notify.mp3'); |
| 222 function notify() { | 224 function notify() { |
| 223 sound.play(); | 225 sound.play(); |
| 224 } | 226 } |
| 225 | 227 |
| 228 let online = {}; | |
| 229 | |
| 230 function setOnline(userId) { | |
| 231 online[userId] = Date.now(); | |
| 232 } | |
| 233 | |
| 234 function showOnline() { | |
| 235 let old = Date.now() - 20000; | |
| 236 for( let id of Object.keys(online) ) { | |
| 237 if( online[id] < old ) | |
| 238 delete online[id]; | |
| 239 } | |
| 240 let a = []; | |
| 241 for( let id in online ) { | |
| 242 a.push( `span[online="${id}"]` ); | |
| 243 } | |
| 244 let style = document.querySelector('style[online]'); | |
| 245 if( a.length === 0 ) { | |
| 246 style.innerHTML = ''; | |
| 247 } else { | |
| 248 style.innerHTML = ` | |
| 249 ${a.join(', ')} { | |
| 250 background-color: green; | |
| 251 } | |
| 252 ` ; | |
| 253 } | |
| 254 } |
