30
|
1 local Luan = require "luan:Luan.luan"
|
|
2 local error = Luan.error
|
75
|
3 local ipairs = Luan.ipairs or error()
|
30
|
4 local String = require "luan:String.luan"
|
|
5 local to_number = String.to_number or error()
|
73
|
6 local Parsers = require "luan:Parsers.luan"
|
|
7 local json_string = Parsers.json_string or error()
|
30
|
8 local Time = require "luan:Time.luan"
|
|
9 local time_now = Time.now or error()
|
|
10 local Io = require "luan:Io.luan"
|
|
11 local Http = require "luan:http/Http.luan"
|
|
12 local User = require "site:/lib/User.luan"
|
|
13 local current_user = User.current or error()
|
75
|
14 local get_user_by_id = User.get_by_id or error()
|
33
|
15 local Shared = require "site:/lib/Shared.luan"
|
73
|
16 local compressed = Shared.compressed or error()
|
|
17 local Online = require "site:/lib/Online.luan"
|
|
18 local set_online = Online.set or error()
|
|
19 local get_online = Online.get or error()
|
75
|
20 local Chat = require "site:/lib/Chat.luan"
|
|
21 local get_chat_by_id = Chat.get_by_id or error()
|
30
|
22 local Logging = require "luan:logging/Logging.luan"
|
|
23 local logger = Logging.logger "heartbeat.js"
|
|
24
|
|
25
|
|
26 return function()
|
33
|
27 local user = current_user() or error()
|
89
|
28 local spy = Http.request.cookies.spy ~= nil
|
|
29 if not spy then
|
|
30 set_online(user)
|
|
31 end
|
73
|
32 Io.stdout = Http.response.text_writer()
|
33
|
33
|
30
|
34 local last_update = Http.request.parameters.last_update or error()
|
46
|
35 last_update = to_number(last_update) or error(last_update)
|
30
|
36 local user_last_update = user.last_update()
|
92
|
37 if time_now() - user_last_update > 10000 and last_update < user_last_update then
|
73
|
38 logger.info "update"
|
|
39 %>
|
|
40 location = '/';
|
|
41 <%
|
30
|
42 return
|
|
43 end
|
73
|
44
|
|
45 local online = get_online(user)
|
30
|
46 %>
|
73
|
47 online = <%=json_string(online,compressed)%>;
|
|
48 showOnline();
|
30
|
49 <%
|
75
|
50 local chat = Http.request.parameters.chat
|
|
51 if chat ~= nil then
|
85
|
52 chat = get_chat_by_id(chat)
|
|
53 if chat == nil then
|
|
54 %>
|
|
55 location = '/';
|
|
56 <%
|
|
57 return
|
|
58 end
|
75
|
59 local my_user_id = user.id
|
|
60 for _, user_id in ipairs(chat.user_ids) do
|
|
61 if user_id == my_user_id then continue end
|
|
62 local user = get_user_by_id(user_id)
|
|
63 %>
|
|
64 readUpTo(<%=user_id%>,<%=json_string(user.name_html())%>,<%=chat.unread(user)%>);
|
|
65 <%
|
|
66 end
|
|
67 end
|
30
|
68 end
|