annotate 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
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()
73
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 46
diff changeset
5 local Parsers = require "luan:Parsers.luan"
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 46
diff changeset
6 local json_string = Parsers.json_string or error()
30
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7 local Time = require "luan:Time.luan"
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8 local time_now = Time.now or error()
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9 local Io = require "luan:Io.luan"
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 local Http = require "luan:http/Http.luan"
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11 local User = require "site:/lib/User.luan"
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 local current_user = User.current or error()
33
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 30
diff changeset
13 local Shared = require "site:/lib/Shared.luan"
73
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 46
diff changeset
14 local compressed = Shared.compressed or error()
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 46
diff changeset
15 local Online = require "site:/lib/Online.luan"
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 46
diff changeset
16 local set_online = Online.set or error()
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 46
diff changeset
17 local get_online = Online.get or error()
30
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 local Logging = require "luan:logging/Logging.luan"
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19 local logger = Logging.logger "heartbeat.js"
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 return function()
33
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 30
diff changeset
23 local user = current_user() or error()
73
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 46
diff changeset
24 set_online(user)
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 46
diff changeset
25 Io.stdout = Http.response.text_writer()
33
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 30
diff changeset
26
30
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 local last_update = Http.request.parameters.last_update or error()
46
42b741a1d5c6 add username
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
28 last_update = to_number(last_update) or error(last_update)
30
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 local user_last_update = user.last_update()
73
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 46
diff changeset
30 if time_now() - user_last_update > 70000 and last_update < user_last_update then
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 46
diff changeset
31 logger.info "update"
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 46
diff changeset
32 %>
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 46
diff changeset
33 location = '/';
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 46
diff changeset
34 <%
30
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 return
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36 end
73
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 46
diff changeset
37
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 46
diff changeset
38 local online = get_online(user)
30
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
39 %>
73
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 46
diff changeset
40 online = <%=json_string(online,compressed)%>;
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 46
diff changeset
41 showOnline();
30
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
42 <%
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
43 end