changeset 88:7b339b1ccd11

add tools/users.html
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 13 Mar 2025 16:18:39 -0600
parents 246a792adedf
children 3053a4fc10be
files src/account.html.luan src/chat.js src/invite.js.luan src/lib/Online.luan src/lib/User.luan src/login.js.luan src/private/tools/index.html.luan src/private/tools/users.html.luan src/site.js
diffstat 9 files changed, 137 insertions(+), 39 deletions(-) [+]
line wrap: on
line diff
--- a/src/account.html.luan	Wed Mar 12 14:20:10 2025 -0600
+++ b/src/account.html.luan	Thu Mar 13 16:18:39 2025 -0600
@@ -4,6 +4,7 @@
 local json_string = Parsers.json_string or error()
 local Html = require "luan:Html.luan"
 local html_encode = Html.encode or error()
+local url_encode = Html.url_encode or error()
 local Io = require "luan:Io.luan"
 local Http = require "luan:http/Http.luan"
 local Shared = require "site:/lib/Shared.luan"
@@ -216,7 +217,7 @@
 		</dialog>
 		<dialog url>
 			<h2>Your chat URL</h2>
-			<p><%= base_url() %>/chat?with=<%=html_encode(user.email)%></p>
+			<p><%= base_url() %>/chat?with=<%=url_encode(user.email)%></p>
 			<p>Use this URL to link to your chat so that other people can chat with you by clicking on the link.</p>
 			<div buttons>
 				<button onclick="closeModal(this)">Close</button>
--- a/src/chat.js	Wed Mar 12 14:20:10 2025 -0600
+++ b/src/chat.js	Thu Mar 13 16:18:39 2025 -0600
@@ -353,35 +353,6 @@
 	reader.readAsArrayBuffer(file);
 }
 
-let times = [
-	{
-		time: 1000*60*60*24,
-		unit: 'day'
-	},
-	{
-		time: 1000*60*60,
-		unit: 'hour'
-	},
-	{
-		time: 1000*60,
-		unit: 'minute'
-	}
-];
-
-function ago(time) {
-	for( let t of times ) {
-		let n = Math.floor(time / t.time);
-		if( n > 0 ) {
-			let s = `${n} ${t.unit}`;
-			if( n > 1 )
-				s = s + 's';
-			return s + ' ago';
-		}
-	}
-	return 'just now';
-end
-}
-
 function openPeople() {
 	let dialog = document.querySelector('dialog[people]');
 	let spans = dialog.querySelectorAll('span[last_seen]');
--- a/src/invite.js.luan	Wed Mar 12 14:20:10 2025 -0600
+++ b/src/invite.js.luan	Thu Mar 13 16:18:39 2025 -0600
@@ -12,8 +12,6 @@
 local current_user = User.current or error()
 local Shared = require "site:/lib/Shared.luan"
 local send_mail = Shared.send_mail or error()
-local Utils = require "site:/lib/Utils.luan"
-local base_url = Utils.base_url or error()
 
 
 return function()
@@ -27,8 +25,7 @@
 		return
 	end
 	local invitee = get_or_create_user_by_email(email)
-	local password = invitee.password
-	local url = base_url().."/do_login.html?user="..invitee.id.."&password="..password.."&with="..url_encode(user.email)
+	local url = invitee.login_url().."&with="..url_encode(user.email)
 	local who = user.name or user.email
 	send_mail {
 		To = email
@@ -40,7 +37,7 @@
 
 <%= url %>
 
-Or login with your email and the password: <%=password%>
+Or login with your email and the password: <%=invitee.password%>
 <%		`
 	}
 %>
--- a/src/lib/Online.luan	Wed Mar 12 14:20:10 2025 -0600
+++ b/src/lib/Online.luan	Thu Mar 13 16:18:39 2025 -0600
@@ -26,4 +26,8 @@
 	return online
 end
 
+function Online.get_user(user)
+	return glob[user.id]
+end
+
 return Online
--- a/src/lib/User.luan	Wed Mar 12 14:20:10 2025 -0600
+++ b/src/lib/User.luan	Thu Mar 13 16:18:39 2025 -0600
@@ -23,6 +23,7 @@
 local Utils = require "site:/lib/Utils.luan"
 local list_to_set = Utils.list_to_set or error()
 local set_to_list = Utils.set_to_list or error()
+local base_url = Utils.base_url or error()
 local Logging = require "luan:logging/Logging.luan"
 local logger = Logging.logger "User"
 
@@ -115,6 +116,10 @@
 		return html_encode(user.name or user.email)
 	end
 
+	function user.login_url()
+		return base_url().."/do_login.html?user="..user.id.."&password="..user.password
+	end
+
 	return user
 end
 
--- a/src/login.js.luan	Wed Mar 12 14:20:10 2025 -0600
+++ b/src/login.js.luan	Thu Mar 13 16:18:39 2025 -0600
@@ -8,7 +8,6 @@
 local Shared = require "site:/lib/Shared.luan"
 local send_mail_async = Shared.send_mail_async or error()
 local Utils = require "site:/lib/Utils.luan"
-local base_url = Utils.base_url or error()
 local to_list = Utils.to_list or error()
 local User = require "site:/lib/User.luan"
 
@@ -16,8 +15,7 @@
 return function()
 	local email = Http.request.parameters.email or error()
 	local user = User.get_or_create_by_email(email)
-	local password = user.password
-	local url = base_url().."/do_login.html?user="..user.id.."&password="..password
+	local url = user.login_url()
 	local with = Http.request.parameters.with
 	with = to_list(with)
 	for _, email in ipairs(with) do
@@ -31,7 +29,7 @@
 
 <%= url %>
 
-Or login with your email and the password: <%=password%>
+Or login with your email and the password: <%=user.password%>
 <%		`
 	}
 	Io.stdout = Http.response.text_writer()
--- a/src/private/tools/index.html.luan	Wed Mar 12 14:20:10 2025 -0600
+++ b/src/private/tools/index.html.luan	Thu Mar 13 16:18:39 2025 -0600
@@ -19,6 +19,7 @@
 <%		header() %>
 		<div content>
 			<h1>Private Tools</h1>
+			<p><a href="users.html">users</a></p>
 			<p><a href="config.html">configure</a></p>
 			<p>
 				public tools:
@@ -26,6 +27,7 @@
 				- <a href="/tools/cookies.html">cookies</a>
 				- <a href="/tools/request.txt">HTTP request</a>
 			</p>
+			<p><a href="../local/logs/">logs</a></p>
 			<p><a href="lucene.html">lucene</a></p>
 			<p><a href="shell.html">luan shell</a></p>
 			<p><a href="run">luan batch</a></p>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/private/tools/users.html.luan	Thu Mar 13 16:18:39 2025 -0600
@@ -0,0 +1,88 @@
+local Luan = require "luan:Luan.luan"
+local error = Luan.error
+local ipairs = Luan.ipairs or error()
+local Time = require "luan:Time.luan"
+local time_now = Time.now or error()
+local Table = require "luan:Table.luan"
+local sort = Table.sort or error()
+local Io = require "luan:Io.luan"
+local Http = require "luan:http/Http.luan"
+local Shared = require "site:/lib/Shared.luan"
+local head = Shared.head or error()
+local header = Shared.private_header or error()
+local User = require "site:/lib/User.luan"
+local Online = require "site:/lib/Online.luan"
+local get_online = Online.get_user or error()
+local Logging = require "luan:logging/Logging.luan"
+local logger = Logging.logger "tools/users.html"
+
+
+local function sorter(u1,u2)
+	local o1 = u1.online
+	local o2 = u2.online
+	if o1 == nil then
+		return false
+	elseif o2 == nil then
+		return true
+	else
+		return o1 > o2
+	end
+end
+
+return function()
+	local users = User.search("type:user")
+	for _, user in ipairs(users) do
+		user.online = get_online(user)
+	end
+	sort(users,sorter)
+	local now = time_now()
+	Io.stdout = Http.response.text_writer()
+%>
+<!doctype html>
+<html>
+	<head>
+<%		head() %>
+		<style>
+			table {
+				border-collapse: collapse;
+			}
+			th, td {
+				border: 1px solid;
+				padding: 4px;
+			}
+		</style>
+	</head>
+	<body>
+<%		header() %>
+		<div content>
+			<h1>users</h1>
+			<table>
+				<tr>
+					<th>ID</th>
+					<th>name</th>
+					<th>last seen</th>
+				</tr>
+<%
+	for _, user in ipairs(users) do
+		local online = user.online
+		local ago
+		if online == nil then
+			ago = ""
+		else
+			ago = `%><script>document.write(ago(<%= now - online %>))</script><%`
+		end
+%>
+				<tr>
+					<td><%= user.id %></td>
+					<td><a href="<%=user.login_url()%>"><%= user.name_html() %></a></td>
+					<td><%= ago %></td>
+				</tr>
+<%
+	end
+%>
+			</table>
+		</div>
+	</body>
+</html>
+<%
+end
--- a/src/site.js	Wed Mar 12 14:20:10 2025 -0600
+++ b/src/site.js	Thu Mar 13 16:18:39 2025 -0600
@@ -90,3 +90,35 @@
 function closeModal(el) {
 	getEnclosingDialog(el).close();
 }
+
+let times = [
+	{
+		time: 1000*60*60*24,
+		unit: 'day'
+	},
+	{
+		time: 1000*60*60,
+		unit: 'hour'
+	},
+	{
+		time: 1000*60,
+		unit: 'minute'
+	},
+	{
+		time: 1000,
+		unit: 'second'
+	}
+];
+
+function ago(time) {
+	for( let t of times ) {
+		let n = Math.floor(time / t.time);
+		if( n > 0 ) {
+			let s = `${n} ${t.unit}`;
+			if( n > 1 )
+				s = s + 's';
+			return s + ' ago';
+		}
+	}
+	return 'just now';
+}