Software /
code /
prosody-modules
Comparison
mod_nodeinfo2/mod_nodeinfo2.lua @ 3789:e3b673df3906
mod_nodeinfo2: Only expose message stats if nodeinfo2_expose_posts isn’t set to false
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Mon, 30 Dec 2019 22:20:11 +0100 |
parent | 3786:58e484426d13 |
child | 3790:352f3efe1b67 |
comparison
equal
deleted
inserted
replaced
3788:14028430638b | 3789:e3b673df3906 |
---|---|
3 local get_stats = require "core.statsmanager".get_stats; | 3 local get_stats = require "core.statsmanager".get_stats; |
4 local os_time = os.time; | 4 local os_time = os.time; |
5 | 5 |
6 module:depends("http"); | 6 module:depends("http"); |
7 module:depends("lastlog"); | 7 module:depends("lastlog"); |
8 module:depends("measure_message_e2ee"); | 8 |
9 local expose_posts = module:get_option_boolean("nodeinfo2_expose_posts", true); | |
10 if expose_posts then | |
11 module:depends("measure_message_e2ee"); | |
12 end | |
9 | 13 |
10 local main_store = module:open_store(); | 14 local main_store = module:open_store(); |
11 local lastlog_store = module:open_store("lastlog"); | 15 local lastlog_store = module:open_store("lastlog"); |
12 | 16 |
13 local total_users = 0; | 17 local total_users = 0; |
36 half_year_users = nil; | 40 half_year_users = nil; |
37 month_users = nil; | 41 month_users = nil; |
38 week_users = nil; | 42 week_users = nil; |
39 end | 43 end |
40 | 44 |
41 local data = main_store:get("nodeinfo2") or { message_count = 0 }; | 45 local data; |
46 if expose_posts then | |
47 data = main_store:get("nodeinfo2") or { message_count = 0 }; | |
48 end | |
42 | 49 |
43 module:provides("http", { | 50 module:provides("http", { |
44 default_path = "/.well-known/x-nodeinfo2"; | 51 default_path = "/.well-known/x-nodeinfo2"; |
45 route = { | 52 route = { |
46 GET = function (event) | 53 GET = function (event) |
47 local stats, changed_only, extras = get_stats(); | 54 local usage = { |
48 for stat, _ in pairs(stats) do | 55 users = { |
49 if stat == "/"..module.host.."/mod_measure_message_e2ee/message:rate" then | 56 total = total_users; |
50 local new_message_count = extras[stat].total; | 57 activeHalfyear = half_year_users; |
51 if new_message_count ~= data.message_count then | 58 activeMonth = month_users; |
52 data = { message_count = new_message_count }; | 59 activeWeek = week_users; |
53 main_store:set("nodeinfo2", data); | 60 }; |
61 }; | |
62 | |
63 if expose_posts then | |
64 local stats, changed_only, extras = get_stats(); | |
65 for stat, _ in pairs(stats) do | |
66 if stat == "/"..module.host.."/mod_measure_message_e2ee/message:rate" then | |
67 local new_message_count = extras[stat].total; | |
68 if new_message_count ~= data.message_count then | |
69 data = { message_count = new_message_count }; | |
70 main_store:set("nodeinfo2", data); | |
71 end | |
54 end | 72 end |
55 end | 73 end |
74 usage.localPosts = data.message_count; | |
75 -- TODO: also count PubSub replies here. | |
76 usage.localComments = 0; | |
56 end | 77 end |
57 | 78 |
58 event.response.headers.content_type = "application/json"; | 79 event.response.headers.content_type = "application/json"; |
59 return json.encode({ | 80 return json.encode({ |
60 version = "1.0"; | 81 version = "1.0"; |
82 outbound = array { | 103 outbound = array { |
83 }; | 104 }; |
84 }; | 105 }; |
85 --]] | 106 --]] |
86 openRegistrations = module:get_option_boolean("allow_registration", false); | 107 openRegistrations = module:get_option_boolean("allow_registration", false); |
87 usage = { | 108 usage = usage; |
88 users = { | |
89 total = total_users; | |
90 activeHalfyear = half_year_users; | |
91 activeMonth = month_users; | |
92 activeWeek = week_users; | |
93 }; | |
94 localPosts = data.message_count; | |
95 -- TODO: also count PubSub replies here. | |
96 localComments = 0; | |
97 }; | |
98 }); | 109 }); |
99 end; | 110 end; |
100 } | 111 } |
101 }); | 112 }); |
102 | 113 |