annotate src/heartbeat.js.luan @ 46:42b741a1d5c6

add username
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 28 Feb 2025 19:25:12 -0700
parents 62d04ca486dd
children a63faf49e1d7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
30
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1 local Luan = require "luan:Luan.luan"
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2 local error = Luan.error
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 local String = require "luan:String.luan"
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 local to_number = String.to_number or error()
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
5 local Time = require "luan:Time.luan"
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6 local time_now = Time.now or error()
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7 local Io = require "luan:Io.luan"
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8 local Http = require "luan:http/Http.luan"
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9 local User = require "site:/lib/User.luan"
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 local current_user = User.current or error()
33
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 30
diff changeset
11 local Shared = require "site:/lib/Shared.luan"
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 30
diff changeset
12 local http_push_to_users = Shared.http_push_to_users or error()
30
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13 local Logging = require "luan:logging/Logging.luan"
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14 local logger = Logging.logger "heartbeat.js"
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 return function()
33
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 30
diff changeset
18 local user = current_user() or error()
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 30
diff changeset
19 local user_ids = user.chatting_with_ids()
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 30
diff changeset
20 local js = "setOnline("..user.id..")"
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 30
diff changeset
21 http_push_to_users( user_ids, js )
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 30
diff changeset
22
30
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 local last_update = Http.request.parameters.last_update or error()
46
42b741a1d5c6 add username
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
24 last_update = to_number(last_update) or error(last_update)
30
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 local user_last_update = user.last_update()
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 local now = time_now()
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 if now - user_last_update < 10000 or last_update >= user_last_update then
33
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 30
diff changeset
28 -- logger.info "ok"
30
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 return
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 end
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31 logger.info "update"
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32 Io.stdout = Http.response.text_writer()
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33 %>
34
Franklin Schmidt <fschmidt@gmail.com>
parents: 33
diff changeset
34 location = '/';
30
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 <%
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36 end