# HG changeset patch # User Matthew Wild # Date 1701864757 0 # Node ID 7b722955c59b8460bc0b31ab06b3bd4190c6e6b3 # Parent 3a7349aa95c747a8e20d71dc7efd5219c8219fc9 mod_lastlog2: Expose API to query the last active time of a user diff -r 3a7349aa95c7 -r 7b722955c59b mod_lastlog2/mod_lastlog2.lua --- a/mod_lastlog2/mod_lastlog2.lua Tue Dec 05 12:39:00 2023 +0000 +++ b/mod_lastlog2/mod_lastlog2.lua Wed Dec 06 12:12:37 2023 +0000 @@ -47,6 +47,24 @@ end); end +do + local user_sessions = prosody.hosts[module.host].sessions; + local kv_store = module:open_store(); + function get_last_active(username) --luacheck: ignore 131/get_last_active + if user_sessions[username] then + return os.time(); -- Currently connected + else + local last_activity = kv_store:get(username); + if not last_activity then return nil; end + local latest = math.max(last_activity.login or 0, last_activity.logout or 0); + if latest == 0 then + return nil; -- Never logged in + end + return latest; + end + end +end + function module.command(arg) if not arg[1] or arg[1] == "--help" then require"util.prosodyctl".show_usage([[mod_lastlog ]], [[Show when user last logged in or out]]);