annotate src/heartbeat.js.luan @ 75:377bdda60f0b

read up to
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 09 Mar 2025 18:26:49 -0600
parents a63faf49e1d7
children 2158a5950fcd
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
75
377bdda60f0b read up to
Franklin Schmidt <fschmidt@gmail.com>
parents: 73
diff changeset
3 local ipairs = Luan.ipairs or error()
30
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 local String = require "luan:String.luan"
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
5 local to_number = String.to_number or error()
73
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 46
diff changeset
6 local Parsers = require "luan:Parsers.luan"
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 46
diff changeset
7 local json_string = Parsers.json_string or error()
30
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8 local Time = require "luan:Time.luan"
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9 local time_now = Time.now or error()
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 local Io = require "luan:Io.luan"
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11 local Http = require "luan:http/Http.luan"
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 local User = require "site:/lib/User.luan"
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13 local current_user = User.current or error()
75
377bdda60f0b read up to
Franklin Schmidt <fschmidt@gmail.com>
parents: 73
diff changeset
14 local get_user_by_id = User.get_by_id or error()
33
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 30
diff changeset
15 local Shared = require "site:/lib/Shared.luan"
73
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 46
diff changeset
16 local compressed = Shared.compressed or error()
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 46
diff changeset
17 local Online = require "site:/lib/Online.luan"
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 46
diff changeset
18 local set_online = Online.set or error()
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 46
diff changeset
19 local get_online = Online.get or error()
75
377bdda60f0b read up to
Franklin Schmidt <fschmidt@gmail.com>
parents: 73
diff changeset
20 local Chat = require "site:/lib/Chat.luan"
377bdda60f0b read up to
Franklin Schmidt <fschmidt@gmail.com>
parents: 73
diff changeset
21 local get_chat_by_id = Chat.get_by_id or error()
30
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 local Logging = require "luan:logging/Logging.luan"
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 local logger = Logging.logger "heartbeat.js"
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 return function()
33
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 30
diff changeset
27 local user = current_user() or error()
73
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 46
diff changeset
28 set_online(user)
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 46
diff changeset
29 Io.stdout = Http.response.text_writer()
33
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 30
diff changeset
30
30
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31 local last_update = Http.request.parameters.last_update or error()
46
42b741a1d5c6 add username
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
32 last_update = to_number(last_update) or error(last_update)
30
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33 local user_last_update = user.last_update()
73
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 46
diff changeset
34 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
35 logger.info "update"
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 46
diff changeset
36 %>
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 46
diff changeset
37 location = '/';
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 46
diff changeset
38 <%
30
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
39 return
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
40 end
73
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 46
diff changeset
41
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 46
diff changeset
42 local online = get_online(user)
30
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
43 %>
73
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 46
diff changeset
44 online = <%=json_string(online,compressed)%>;
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 46
diff changeset
45 showOnline();
30
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
46 <%
75
377bdda60f0b read up to
Franklin Schmidt <fschmidt@gmail.com>
parents: 73
diff changeset
47 local chat = Http.request.parameters.chat
377bdda60f0b read up to
Franklin Schmidt <fschmidt@gmail.com>
parents: 73
diff changeset
48 if chat ~= nil then
377bdda60f0b read up to
Franklin Schmidt <fschmidt@gmail.com>
parents: 73
diff changeset
49 chat = get_chat_by_id(chat) or error()
377bdda60f0b read up to
Franklin Schmidt <fschmidt@gmail.com>
parents: 73
diff changeset
50 local my_user_id = user.id
377bdda60f0b read up to
Franklin Schmidt <fschmidt@gmail.com>
parents: 73
diff changeset
51 for _, user_id in ipairs(chat.user_ids) do
377bdda60f0b read up to
Franklin Schmidt <fschmidt@gmail.com>
parents: 73
diff changeset
52 if user_id == my_user_id then continue end
377bdda60f0b read up to
Franklin Schmidt <fschmidt@gmail.com>
parents: 73
diff changeset
53 local user = get_user_by_id(user_id)
377bdda60f0b read up to
Franklin Schmidt <fschmidt@gmail.com>
parents: 73
diff changeset
54 %>
377bdda60f0b read up to
Franklin Schmidt <fschmidt@gmail.com>
parents: 73
diff changeset
55 readUpTo(<%=user_id%>,<%=json_string(user.name_html())%>,<%=chat.unread(user)%>);
377bdda60f0b read up to
Franklin Schmidt <fschmidt@gmail.com>
parents: 73
diff changeset
56 <%
377bdda60f0b read up to
Franklin Schmidt <fschmidt@gmail.com>
parents: 73
diff changeset
57 end
377bdda60f0b read up to
Franklin Schmidt <fschmidt@gmail.com>
parents: 73
diff changeset
58 end
30
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
59 end