changeset 5:a49866b52cc2

login to chat
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 29 Oct 2024 13:10:47 -0600
parents 2da10ece826f
children e22a1ba4b2ed
files src/account.html.luan src/chat.html.luan src/do_login.html.luan src/login.html.luan src/login.js.luan
diffstat 5 files changed, 61 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/src/account.html.luan	Sun Oct 27 20:39:18 2024 -0600
+++ b/src/account.html.luan	Tue Oct 29 13:10:47 2024 -0600
@@ -5,9 +5,15 @@
 local Shared = require "site:/lib/Shared.luan"
 local head = Shared.head or error()
 local header = Shared.header or error()
+local Utils = require "site:/lib/Utils.luan"
+local base_url = Utils.base_url or error()
+local User = require "site:/lib/User.luan"
+local current_user = User.current_required or error()
 
 
 return function()
+	local user = current_user()
+	if user == nil then return end
 	Io.stdout = Http.response.text_writer()
 %>
 <!doctype html>
@@ -20,6 +26,7 @@
 		<div content>
 			<h1>Your Account</h1>
 			<p><a href="chat.html">Your Chats</a></p>
+			<p>Your URL: <%= base_url() %>/chat.html?with=<%=user.email%></p>
 			<p><a href="javascript:logout()">Logout</a></p>
 		</div>
 	</body>
--- a/src/chat.html.luan	Sun Oct 27 20:39:18 2024 -0600
+++ b/src/chat.html.luan	Tue Oct 29 13:10:47 2024 -0600
@@ -4,13 +4,15 @@
 local pairs = Luan.pairs or error()
 local Table = require "luan:Table.luan"
 local concat = Table.concat or error()
+local is_empty = Table.is_empty or error()
+local size = Table.size 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.header or error()
 local User = require "site:/lib/User.luan"
-local current_user = User.current_required or error()
+local current_user = User.current or error()
 local get_user_by_id = User.get_by_id or error()
 local get_user_by_email = User.get_by_email or error()
 local Utils = require "site:/lib/Utils.luan"
@@ -61,15 +63,26 @@
 end
 
 return function()
+	local with = Http.request.parameters.with
+	with = to_set(with)
 	local user = current_user()
 	if user == nil then
+		local url = "/login.html"
+		if not is_empty(with) then
+			local t = {}
+			for email in pairs(with) do
+				t[#t+1] = "with="..email
+			end
+			url = url.."?"..concat(t,"&")
+		end
+		Http.response.send_redirect(url)
 		return
 	end
-	local with = Http.request.parameters.with
-	if with ~= nil then
-		with = to_set(with)
+	if not is_empty(with) then
 		with[user.email] = true
-		get_chat(with)
+		if size(with) > 1 then
+			get_chat(with)
+		end
 	end
 	local chats = user.get_chats()
 	Io.stdout = Http.response.text_writer()
--- a/src/do_login.html.luan	Sun Oct 27 20:39:18 2024 -0600
+++ b/src/do_login.html.luan	Tue Oct 29 13:10:47 2024 -0600
@@ -1,11 +1,16 @@
 local Luan = require "luan:Luan.luan"
 local error = Luan.error
+local ipairs = Luan.ipairs or error()
+local Table = require "luan:Table.luan"
+local concat = Table.concat 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.header or error()
 local User = require "site:/lib/User.luan"
+local Utils = require "site:/lib/Utils.luan"
+local to_list = Utils.to_list or error()
 
 
 return function()
@@ -28,8 +33,20 @@
 <%
 	else
 		user.login()
+		local with = Http.request.parameters.with
+		local location
+		if with == nil then
+			location = "/"
+		else
+			with = to_list(with)
+			local t = {}
+			for _, email in ipairs(with) do
+				t[#t+1] = "with="..email
+			end
+			location = "/chat.html?"..concat(t,"&")
+		end
 %>
-			<script> location = '/'; </script>
+			<script> location = '<%=location%>'; </script>
 <%
 	end
 %>
--- a/src/login.html.luan	Sun Oct 27 20:39:18 2024 -0600
+++ b/src/login.html.luan	Tue Oct 29 13:10:47 2024 -0600
@@ -1,13 +1,20 @@
 local Luan = require "luan:Luan.luan"
 local error = Luan.error
+local ipairs = Luan.ipairs or error()
+local Html = require "luan:Html.luan"
+local html_encode = Html.encode 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.header or error()
+local Utils = require "site:/lib/Utils.luan"
+local to_list = Utils.to_list or error()
 
 
 return function()
+	local with = Http.request.parameters.with
+	with = to_list(with)
 	Io.stdout = Http.response.text_writer()
 %>
 <!doctype html>
@@ -21,6 +28,9 @@
 			<h1>Login / Register</h1>
 			<p>A link to login will be emailed to you.</p>
 			<form page onsubmit="ajaxForm('/login.js',this)" action="javascript:">
+<%	for _, email in ipairs(with) do %>
+				<input type=hidden name=with value="<%=html_encode(email)%>">
+<%	end %>
 				<p>
 					<label prompt>Your email address</label>
 					<input type=email name=email required autofocus>
--- a/src/login.js.luan	Sun Oct 27 20:39:18 2024 -0600
+++ b/src/login.js.luan	Tue Oct 29 13:10:47 2024 -0600
@@ -1,5 +1,6 @@
 local Luan = require "luan:Luan.luan"
 local error = Luan.error
+local ipairs = Luan.ipairs or error()
 local Io = require "luan:Io.luan"
 local Http = require "luan:http/Http.luan"
 local Shared = require "site:/lib/Shared.luan"
@@ -7,12 +8,19 @@
 local Utils = require "site:/lib/Utils.luan"
 local base_url = Utils.base_url or error()
 local User = require "site:/lib/User.luan"
+local Utils = require "site:/lib/Utils.luan"
+local to_list = Utils.to_list or error()
 
 
 return function()
 	local email = Http.request.parameters.email or error()
 	local user = User.get_or_create_by_email(email)
 	local url = base_url().."/do_login.html?user="..user.id.."&password="..user.password
+	local with = Http.request.parameters.with
+	with = to_list(with)
+	for _, email in ipairs(with) do
+		url = url.."&with="..email
+	end
 	send_mail_async {
 		From = "Web Chat <chat@reactionary.software>"
 		To = email