comparison src/heartbeat.js.luan @ 73:a63faf49e1d7

last seen
author Franklin Schmidt <fschmidt@gmail.com>
date Sat, 08 Mar 2025 20:26:19 -0700
parents 42b741a1d5c6
children 377bdda60f0b
comparison
equal deleted inserted replaced
72:bce0480721c1 73:a63faf49e1d7
1 local Luan = require "luan:Luan.luan" 1 local Luan = require "luan:Luan.luan"
2 local error = Luan.error 2 local error = Luan.error
3 local String = require "luan:String.luan" 3 local String = require "luan:String.luan"
4 local to_number = String.to_number or error() 4 local to_number = String.to_number or error()
5 local Parsers = require "luan:Parsers.luan"
6 local json_string = Parsers.json_string or error()
5 local Time = require "luan:Time.luan" 7 local Time = require "luan:Time.luan"
6 local time_now = Time.now or error() 8 local time_now = Time.now or error()
7 local Io = require "luan:Io.luan" 9 local Io = require "luan:Io.luan"
8 local Http = require "luan:http/Http.luan" 10 local Http = require "luan:http/Http.luan"
9 local User = require "site:/lib/User.luan" 11 local User = require "site:/lib/User.luan"
10 local current_user = User.current or error() 12 local current_user = User.current or error()
11 local Shared = require "site:/lib/Shared.luan" 13 local Shared = require "site:/lib/Shared.luan"
12 local http_push_to_users = Shared.http_push_to_users or error() 14 local compressed = Shared.compressed or error()
15 local Online = require "site:/lib/Online.luan"
16 local set_online = Online.set or error()
17 local get_online = Online.get or error()
13 local Logging = require "luan:logging/Logging.luan" 18 local Logging = require "luan:logging/Logging.luan"
14 local logger = Logging.logger "heartbeat.js" 19 local logger = Logging.logger "heartbeat.js"
15 20
16 21
17 return function() 22 return function()
18 local user = current_user() or error() 23 local user = current_user() or error()
19 local user_ids = user.chatting_with_ids() 24 set_online(user)
20 local js = "setOnline("..user.id..")" 25 Io.stdout = Http.response.text_writer()
21 http_push_to_users( user_ids, js )
22 26
23 local last_update = Http.request.parameters.last_update or error() 27 local last_update = Http.request.parameters.last_update or error()
24 last_update = to_number(last_update) or error(last_update) 28 last_update = to_number(last_update) or error(last_update)
25 local user_last_update = user.last_update() 29 local user_last_update = user.last_update()
26 local now = time_now() 30 if time_now() - user_last_update > 70000 and last_update < user_last_update then
27 if now - user_last_update < 10000 or last_update >= user_last_update then 31 logger.info "update"
28 -- logger.info "ok" 32 %>
33 location = '/';
34 <%
29 return 35 return
30 end 36 end
31 logger.info "update" 37
32 Io.stdout = Http.response.text_writer() 38 local online = get_online(user)
33 %> 39 %>
34 location = '/'; 40 online = <%=json_string(online,compressed)%>;
41 showOnline();
35 <% 42 <%
36 end 43 end