Mercurial > prosody-modules
annotate mod_nodeinfo2/mod_nodeinfo2.lua @ 3781:263f5f1ee71f
mod_nodeinfo2: Store a table instead of an integer (workaround for #905)
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Sun, 29 Dec 2019 19:53:23 +0100 |
parents | 7f5ce667d93e |
children | 908b5fd47634 |
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 |
3708
0a800463bc08
mod_nodeinfo2: Fetch active users from mod_lastlog.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3707
diff
changeset
|
10 local store = module:open_store("lastlog"); |
0a800463bc08
mod_nodeinfo2: Fetch active users from mod_lastlog.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3707
diff
changeset
|
11 |
3705
5d3d8b75dee9
mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
12 local total_users = 0; |
3708
0a800463bc08
mod_nodeinfo2: Fetch active users from mod_lastlog.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3707
diff
changeset
|
13 local half_year_users = 0; |
0a800463bc08
mod_nodeinfo2: Fetch active users from mod_lastlog.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3707
diff
changeset
|
14 local month_users = 0; |
0a800463bc08
mod_nodeinfo2: Fetch active users from mod_lastlog.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3707
diff
changeset
|
15 local week_users = 0; |
0a800463bc08
mod_nodeinfo2: Fetch active users from mod_lastlog.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3707
diff
changeset
|
16 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
|
17 total_users = total_users + 1; |
3708
0a800463bc08
mod_nodeinfo2: Fetch active users from mod_lastlog.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3707
diff
changeset
|
18 local lastlog = store:get(user); |
0a800463bc08
mod_nodeinfo2: Fetch active users from mod_lastlog.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3707
diff
changeset
|
19 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
|
20 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
|
21 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
|
22 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
|
23 end |
0a800463bc08
mod_nodeinfo2: Fetch active users from mod_lastlog.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3707
diff
changeset
|
24 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
|
25 month_users = month_users + 1; |
0a800463bc08
mod_nodeinfo2: Fetch active users from mod_lastlog.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3707
diff
changeset
|
26 end |
0a800463bc08
mod_nodeinfo2: Fetch active users from mod_lastlog.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3707
diff
changeset
|
27 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
|
28 week_users = week_users + 1; |
0a800463bc08
mod_nodeinfo2: Fetch active users from mod_lastlog.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3707
diff
changeset
|
29 end |
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 |
0a800463bc08
mod_nodeinfo2: Fetch active users from mod_lastlog.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3707
diff
changeset
|
33 -- 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
|
34 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
|
35 half_year_users = nil; |
0a800463bc08
mod_nodeinfo2: Fetch active users from mod_lastlog.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3707
diff
changeset
|
36 month_users = nil; |
0a800463bc08
mod_nodeinfo2: Fetch active users from mod_lastlog.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3707
diff
changeset
|
37 week_users = nil; |
3705
5d3d8b75dee9
mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
38 end |
5d3d8b75dee9
mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
39 |
3710
7f5ce667d93e
mod_nodeinfo2: Store and restore the total message count.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3708
diff
changeset
|
40 local message_count_store = module:open_store("message_count"); |
7f5ce667d93e
mod_nodeinfo2: Store and restore the total message count.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3708
diff
changeset
|
41 local message_count = message_count_store:get("message_count"); |
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 |
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
|
49 if stat == "/*/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; |
3781
263f5f1ee71f
mod_nodeinfo2: Store a table instead of an integer (workaround for #905)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3710
diff
changeset
|
51 if new_message_count ~= message_count[1] then |
263f5f1ee71f
mod_nodeinfo2: Store a table instead of an integer (workaround for #905)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3710
diff
changeset
|
52 message_count = { new_message_count }; |
3710
7f5ce667d93e
mod_nodeinfo2: Store and restore the total message count.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3708
diff
changeset
|
53 message_count_store:set("message_count", message_count); |
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 }; |
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
|
94 localPosts = message_count; |
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
|
95 localComments = message_count; |
3705
5d3d8b75dee9
mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
96 }; |
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 end; |
5d3d8b75dee9
mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
99 } |
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 |