Changeset

5797:7b722955c59b

mod_lastlog2: Expose API to query the last active time of a user
author Matthew Wild <mwild1@gmail.com>
date Wed, 06 Dec 2023 12:12:37 +0000
parents 5796:3a7349aa95c7
children 5798:4c3216d9b118
files mod_lastlog2/mod_lastlog2.lua
diffstat 1 files changed, 18 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- 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 <user@host>]], [[Show when user last logged in or out]]);