annotate src/heartbeat.js.luan @ 92:c41e200c3e76

try to fix lost posts
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 31 Mar 2025 15:43:14 -0600
parents 3053a4fc10be
children
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()
89
Franklin Schmidt <fschmidt@gmail.com>
parents: 85
diff changeset
28 local spy = Http.request.cookies.spy ~= nil
Franklin Schmidt <fschmidt@gmail.com>
parents: 85
diff changeset
29 if not spy then
Franklin Schmidt <fschmidt@gmail.com>
parents: 85
diff changeset
30 set_online(user)
Franklin Schmidt <fschmidt@gmail.com>
parents: 85
diff changeset
31 end
73
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 46
diff changeset
32 Io.stdout = Http.response.text_writer()
33
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 30
diff changeset
33
30
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34 local last_update = Http.request.parameters.last_update or error()
46
42b741a1d5c6 add username
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
35 last_update = to_number(last_update) or error(last_update)
30
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36 local user_last_update = user.last_update()
92
c41e200c3e76 try to fix lost posts
Franklin Schmidt <fschmidt@gmail.com>
parents: 89
diff changeset
37 if time_now() - user_last_update > 10000 and last_update < user_last_update then
73
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 46
diff changeset
38 logger.info "update"
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 46
diff changeset
39 %>
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 46
diff changeset
40 location = '/';
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 46
diff changeset
41 <%
30
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
42 return
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
43 end
73
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 46
diff changeset
44
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 46
diff changeset
45 local online = get_online(user)
30
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
46 %>
73
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 46
diff changeset
47 online = <%=json_string(online,compressed)%>;
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 46
diff changeset
48 showOnline();
30
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
49 <%
75
377bdda60f0b read up to
Franklin Schmidt <fschmidt@gmail.com>
parents: 73
diff changeset
50 local chat = Http.request.parameters.chat
377bdda60f0b read up to
Franklin Schmidt <fschmidt@gmail.com>
parents: 73
diff changeset
51 if chat ~= nil then
85
Franklin Schmidt <fschmidt@gmail.com>
parents: 75
diff changeset
52 chat = get_chat_by_id(chat)
Franklin Schmidt <fschmidt@gmail.com>
parents: 75
diff changeset
53 if chat == nil then
Franklin Schmidt <fschmidt@gmail.com>
parents: 75
diff changeset
54 %>
Franklin Schmidt <fschmidt@gmail.com>
parents: 75
diff changeset
55 location = '/';
Franklin Schmidt <fschmidt@gmail.com>
parents: 75
diff changeset
56 <%
Franklin Schmidt <fschmidt@gmail.com>
parents: 75
diff changeset
57 return
Franklin Schmidt <fschmidt@gmail.com>
parents: 75
diff changeset
58 end
75
377bdda60f0b read up to
Franklin Schmidt <fschmidt@gmail.com>
parents: 73
diff changeset
59 local my_user_id = user.id
377bdda60f0b read up to
Franklin Schmidt <fschmidt@gmail.com>
parents: 73
diff changeset
60 for _, user_id in ipairs(chat.user_ids) do
377bdda60f0b read up to
Franklin Schmidt <fschmidt@gmail.com>
parents: 73
diff changeset
61 if user_id == my_user_id then continue end
377bdda60f0b read up to
Franklin Schmidt <fschmidt@gmail.com>
parents: 73
diff changeset
62 local user = get_user_by_id(user_id)
377bdda60f0b read up to
Franklin Schmidt <fschmidt@gmail.com>
parents: 73
diff changeset
63 %>
377bdda60f0b read up to
Franklin Schmidt <fschmidt@gmail.com>
parents: 73
diff changeset
64 readUpTo(<%=user_id%>,<%=json_string(user.name_html())%>,<%=chat.unread(user)%>);
377bdda60f0b read up to
Franklin Schmidt <fschmidt@gmail.com>
parents: 73
diff changeset
65 <%
377bdda60f0b read up to
Franklin Schmidt <fschmidt@gmail.com>
parents: 73
diff changeset
66 end
377bdda60f0b read up to
Franklin Schmidt <fschmidt@gmail.com>
parents: 73
diff changeset
67 end
30
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
68 end