Mercurial > prosody-modules
annotate mod_nodeinfo2/mod_nodeinfo2.lua @ 3786:58e484426d13
mod_nodeinfo2: Fix traceback on non-existing data
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Sun, 29 Dec 2019 23:41:32 +0100 |
parents | 741a1e400de4 |
children | e3b673df3906 |
rev | line source |
---|---|
3705
5d3d8b75dee9
mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
1 local json = require "util.json"; |
5d3d8b75dee9
mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
2 local array = require "util.array"; |
3707
3248295e1b16
mod_nodeinfo2: Retrieve message count from mod_measure_message_e2ee for localPosts and localComments.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3706
diff
changeset
|
3 local get_stats = require "core.statsmanager".get_stats; |
3708
0a800463bc08
mod_nodeinfo2: Fetch active users from mod_lastlog.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3707
diff
changeset
|
4 local os_time = os.time; |
3705
5d3d8b75dee9
mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
5 |
5d3d8b75dee9
mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
6 module:depends("http"); |
3708
0a800463bc08
mod_nodeinfo2: Fetch active users from mod_lastlog.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3707
diff
changeset
|
7 module:depends("lastlog"); |
3707
3248295e1b16
mod_nodeinfo2: Retrieve message count from mod_measure_message_e2ee for localPosts and localComments.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3706
diff
changeset
|
8 module:depends("measure_message_e2ee"); |
3705
5d3d8b75dee9
mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
9 |
3784
741a1e400de4
mod_nodeinfo2: Count messages per host
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3783
diff
changeset
|
10 local main_store = module:open_store(); |
741a1e400de4
mod_nodeinfo2: Count messages per host
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3783
diff
changeset
|
11 local lastlog_store = module:open_store("lastlog"); |
3708
0a800463bc08
mod_nodeinfo2: Fetch active users from mod_lastlog.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3707
diff
changeset
|
12 |
3705
5d3d8b75dee9
mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
13 local total_users = 0; |
3708
0a800463bc08
mod_nodeinfo2: Fetch active users from mod_lastlog.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3707
diff
changeset
|
14 local half_year_users = 0; |
0a800463bc08
mod_nodeinfo2: Fetch active users from mod_lastlog.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3707
diff
changeset
|
15 local month_users = 0; |
0a800463bc08
mod_nodeinfo2: Fetch active users from mod_lastlog.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3707
diff
changeset
|
16 local week_users = 0; |
0a800463bc08
mod_nodeinfo2: Fetch active users from mod_lastlog.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3707
diff
changeset
|
17 for user in require "core.usermanager".users(module.host) do -- TODO refresh at some interval? |
3705
5d3d8b75dee9
mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
18 total_users = total_users + 1; |
3784
741a1e400de4
mod_nodeinfo2: Count messages per host
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3783
diff
changeset
|
19 local lastlog = lastlog_store:get(user); |
3708
0a800463bc08
mod_nodeinfo2: Fetch active users from mod_lastlog.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3707
diff
changeset
|
20 if lastlog and lastlog.timestamp then |
0a800463bc08
mod_nodeinfo2: Fetch active users from mod_lastlog.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3707
diff
changeset
|
21 local delta = os_time() - lastlog.timestamp; |
0a800463bc08
mod_nodeinfo2: Fetch active users from mod_lastlog.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3707
diff
changeset
|
22 if delta < 6 * 30 * 24 * 60 * 60 then |
0a800463bc08
mod_nodeinfo2: Fetch active users from mod_lastlog.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3707
diff
changeset
|
23 half_year_users = half_year_users + 1; |
0a800463bc08
mod_nodeinfo2: Fetch active users from mod_lastlog.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3707
diff
changeset
|
24 end |
0a800463bc08
mod_nodeinfo2: Fetch active users from mod_lastlog.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3707
diff
changeset
|
25 if delta < 30 * 24 * 60 * 60 then |
0a800463bc08
mod_nodeinfo2: Fetch active users from mod_lastlog.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3707
diff
changeset
|
26 month_users = month_users + 1; |
0a800463bc08
mod_nodeinfo2: Fetch active users from mod_lastlog.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3707
diff
changeset
|
27 end |
0a800463bc08
mod_nodeinfo2: Fetch active users from mod_lastlog.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3707
diff
changeset
|
28 if delta < 7 * 24 * 60 * 60 then |
0a800463bc08
mod_nodeinfo2: Fetch active users from mod_lastlog.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3707
diff
changeset
|
29 week_users = week_users + 1; |
0a800463bc08
mod_nodeinfo2: Fetch active users from mod_lastlog.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3707
diff
changeset
|
30 end |
0a800463bc08
mod_nodeinfo2: Fetch active users from mod_lastlog.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3707
diff
changeset
|
31 end |
0a800463bc08
mod_nodeinfo2: Fetch active users from mod_lastlog.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3707
diff
changeset
|
32 end |
0a800463bc08
mod_nodeinfo2: Fetch active users from mod_lastlog.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3707
diff
changeset
|
33 |
0a800463bc08
mod_nodeinfo2: Fetch active users from mod_lastlog.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3707
diff
changeset
|
34 -- Remove the properties if we couldn’t find a single active user. It most likely means mod_lastlog isn’t in use. |
0a800463bc08
mod_nodeinfo2: Fetch active users from mod_lastlog.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3707
diff
changeset
|
35 if half_year_users == 0 and month_users == 0 and week_users == 0 then |
0a800463bc08
mod_nodeinfo2: Fetch active users from mod_lastlog.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3707
diff
changeset
|
36 half_year_users = nil; |
0a800463bc08
mod_nodeinfo2: Fetch active users from mod_lastlog.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3707
diff
changeset
|
37 month_users = nil; |
0a800463bc08
mod_nodeinfo2: Fetch active users from mod_lastlog.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3707
diff
changeset
|
38 week_users = nil; |
3705
5d3d8b75dee9
mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
39 end |
5d3d8b75dee9
mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
40 |
3786
58e484426d13
mod_nodeinfo2: Fix traceback on non-existing data
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3784
diff
changeset
|
41 local data = main_store:get("nodeinfo2") or { message_count = 0 }; |
3710
7f5ce667d93e
mod_nodeinfo2: Store and restore the total message count.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3708
diff
changeset
|
42 |
3705
5d3d8b75dee9
mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
43 module:provides("http", { |
5d3d8b75dee9
mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
44 default_path = "/.well-known/x-nodeinfo2"; |
5d3d8b75dee9
mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
45 route = { |
5d3d8b75dee9
mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
46 GET = function (event) |
3707
3248295e1b16
mod_nodeinfo2: Retrieve message count from mod_measure_message_e2ee for localPosts and localComments.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3706
diff
changeset
|
47 local stats, changed_only, extras = get_stats(); |
3708
0a800463bc08
mod_nodeinfo2: Fetch active users from mod_lastlog.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3707
diff
changeset
|
48 for stat, _ in pairs(stats) do |
3784
741a1e400de4
mod_nodeinfo2: Count messages per host
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3783
diff
changeset
|
49 if stat == "/"..module.host.."/mod_measure_message_e2ee/message:rate" then |
3710
7f5ce667d93e
mod_nodeinfo2: Store and restore the total message count.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3708
diff
changeset
|
50 local new_message_count = extras[stat].total; |
3786
58e484426d13
mod_nodeinfo2: Fix traceback on non-existing data
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3784
diff
changeset
|
51 if new_message_count ~= data.message_count then |
3784
741a1e400de4
mod_nodeinfo2: Count messages per host
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3783
diff
changeset
|
52 data = { message_count = new_message_count }; |
741a1e400de4
mod_nodeinfo2: Count messages per host
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3783
diff
changeset
|
53 main_store:set("nodeinfo2", data); |
3710
7f5ce667d93e
mod_nodeinfo2: Store and restore the total message count.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3708
diff
changeset
|
54 end |
3707
3248295e1b16
mod_nodeinfo2: Retrieve message count from mod_measure_message_e2ee for localPosts and localComments.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3706
diff
changeset
|
55 end |
3248295e1b16
mod_nodeinfo2: Retrieve message count from mod_measure_message_e2ee for localPosts and localComments.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3706
diff
changeset
|
56 end |
3248295e1b16
mod_nodeinfo2: Retrieve message count from mod_measure_message_e2ee for localPosts and localComments.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3706
diff
changeset
|
57 |
3705
5d3d8b75dee9
mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
58 event.response.headers.content_type = "application/json"; |
5d3d8b75dee9
mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
59 return json.encode({ |
5d3d8b75dee9
mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
60 version = "1.0"; |
5d3d8b75dee9
mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
61 server = { |
5d3d8b75dee9
mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
62 baseUrl = module:http_url("","/"); |
5d3d8b75dee9
mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
63 name = module.host; |
5d3d8b75dee9
mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
64 software = "Prosody"; |
5d3d8b75dee9
mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
65 version = prosody.version; |
5d3d8b75dee9
mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
66 }; |
3706 | 67 --[[ TODO re-use data from mod_server_contact_info ? |
3705
5d3d8b75dee9
mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
68 organization = { |
5d3d8b75dee9
mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
69 name = ""; |
5d3d8b75dee9
mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
70 contact = ""; |
5d3d8b75dee9
mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
71 account = ""; |
5d3d8b75dee9
mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
72 }; |
5d3d8b75dee9
mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
73 --]] |
5d3d8b75dee9
mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
74 protocols = array { |
5d3d8b75dee9
mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
75 "xmpp", |
5d3d8b75dee9
mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
76 }; |
3706 | 77 --[[ TODO would be cool to identify local transports |
3705
5d3d8b75dee9
mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
78 services = { |
5d3d8b75dee9
mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
79 inbound = array { |
5d3d8b75dee9
mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
80 "irc"; |
5d3d8b75dee9
mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
81 }; |
5d3d8b75dee9
mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
82 outbound = array { |
5d3d8b75dee9
mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
83 }; |
5d3d8b75dee9
mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
84 }; |
5d3d8b75dee9
mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
85 --]] |
5d3d8b75dee9
mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
86 openRegistrations = module:get_option_boolean("allow_registration", false); |
5d3d8b75dee9
mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
87 usage = { |
5d3d8b75dee9
mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
88 users = { |
5d3d8b75dee9
mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
89 total = total_users; |
3708
0a800463bc08
mod_nodeinfo2: Fetch active users from mod_lastlog.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3707
diff
changeset
|
90 activeHalfyear = half_year_users; |
0a800463bc08
mod_nodeinfo2: Fetch active users from mod_lastlog.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3707
diff
changeset
|
91 activeMonth = month_users; |
0a800463bc08
mod_nodeinfo2: Fetch active users from mod_lastlog.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3707
diff
changeset
|
92 activeWeek = week_users; |
3705
5d3d8b75dee9
mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
93 }; |
3784
741a1e400de4
mod_nodeinfo2: Count messages per host
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3783
diff
changeset
|
94 localPosts = data.message_count; |
3783
908b5fd47634
mod_nodeinfo2: Remove comments count
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3781
diff
changeset
|
95 -- TODO: also count PubSub replies here. |
908b5fd47634
mod_nodeinfo2: Remove comments count
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3781
diff
changeset
|
96 localComments = 0; |
3705
5d3d8b75dee9
mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
97 }; |
5d3d8b75dee9
mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
98 }); |
5d3d8b75dee9
mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
99 end; |
5d3d8b75dee9
mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
100 } |
5d3d8b75dee9
mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
101 }); |
5d3d8b75dee9
mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
102 |