--- a/mod_nodeinfo2/mod_nodeinfo2.lua Mon Dec 30 22:20:11 2019 +0100
+++ b/mod_nodeinfo2/mod_nodeinfo2.lua Mon Dec 30 22:39:17 2019 +0100
@@ -1,6 +1,8 @@
local json = require "util.json";
local array = require "util.array";
+local add_task = require "util.timer".add_task;
local get_stats = require "core.statsmanager".get_stats;
+local list_users = require "core.usermanager".users;
local os_time = os.time;
module:depends("http");
@@ -14,39 +16,45 @@
local main_store = module:open_store();
local lastlog_store = module:open_store("lastlog");
-local total_users = 0;
-local half_year_users = 0;
-local month_users = 0;
-local week_users = 0;
-for user in require "core.usermanager".users(module.host) do -- TODO refresh at some interval?
- total_users = total_users + 1;
- local lastlog = lastlog_store:get(user);
- if lastlog and lastlog.timestamp then
- local delta = os_time() - lastlog.timestamp;
- if delta < 6 * 30 * 24 * 60 * 60 then
- half_year_users = half_year_users + 1;
- end
- if delta < 30 * 24 * 60 * 60 then
- month_users = month_users + 1;
- end
- if delta < 7 * 24 * 60 * 60 then
- week_users = week_users + 1;
- end
- end
-end
-
--- Remove the properties if we couldn’t find a single active user. It most likely means mod_lastlog isn’t in use.
-if half_year_users == 0 and month_users == 0 and week_users == 0 then
- half_year_users = nil;
- month_users = nil;
- week_users = nil;
-end
-
local data;
if expose_posts then
data = main_store:get("nodeinfo2") or { message_count = 0 };
end
+local total_users = 0;
+local week_users = 0;
+local month_users = 0;
+local half_year_users = 0;
+
+local function update_user_list()
+ for user in list_users(module.host) do
+ total_users = total_users + 1;
+ local lastlog = lastlog_store:get(user);
+ if lastlog and lastlog.timestamp then
+ local delta = os_time() - lastlog.timestamp;
+ if delta < 7 * 86400 then
+ week_users = week_users + 1;
+ end
+ if delta < 30 * 86400 then
+ month_users = month_users + 1;
+ end
+ if delta < 6 * 30 * 86400 then
+ half_year_users = half_year_users + 1;
+ end
+ end
+ end
+
+ -- Remove the properties if we couldn’t find a single active user. It most likely means mod_lastlog isn’t in use.
+ if half_year_users == 0 and month_users == 0 and week_users == 0 then
+ week_users = nil;
+ month_users = nil;
+ half_year_users = nil;
+ end
+end
+
+add_task(86400, update_user_list);
+update_user_list();
+
module:provides("http", {
default_path = "/.well-known/x-nodeinfo2";
route = {
@@ -54,9 +62,9 @@
local usage = {
users = {
total = total_users;
- activeHalfyear = half_year_users;
+ activeWeek = week_users;
activeMonth = month_users;
- activeWeek = week_users;
+ activeHalfyear = half_year_users;
};
};