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"; |