changeset 7:41d35b72c774

chat page
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 29 Oct 2024 22:11:40 -0600
parents e22a1ba4b2ed
children d654e3471132
files src/chat.html.luan src/chat.js.luan src/lib/Chat.luan
diffstat 3 files changed, 109 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/src/chat.html.luan	Tue Oct 29 16:47:11 2024 -0600
+++ b/src/chat.html.luan	Tue Oct 29 22:11:40 2024 -0600
@@ -13,7 +13,6 @@
 local header = Shared.header or error()
 local User = require "site:/lib/User.luan"
 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"
 local to_set = Utils.to_set or error()
@@ -23,18 +22,6 @@
 local chat_search = Chat.search or error()
 
 
-local function other_users(user,chat)
-	local my_id = user.id
-	local t = {}
-	for _, user_id in ipairs(chat.user_ids) do
-		if user_id ~= my_id then
-			local other_user = get_user_by_id(user_id) or error()
-			t[#t+1] = other_user.email
-		end
-	end
-	return concat( t, ", " )
-end
-
 local function get_chat(with)
 	local t = {}
 	local ids = {}
@@ -91,14 +78,69 @@
 <html>
 	<head>
 <%		head() %>
+		<style>
+			body {
+				height: 100vh;
+				display: flex;
+				flex-direction: column;
+			}
+			div[content] {
+				margin-bottom: 0;
+				display: flex;
+				height: 100%;
+				margin-left: calc(3% - 8px);
+			}
+			div[chats] {
+				width: 250px;
+				padding-right: 8px;
+			}
+			div[chats] > div {
+				margin-top: 2px;
+				margin-bottom: 2px;
+				padding-top: 16px;
+				padding-bottom: 16px;
+				padding-left: 8px;
+				padding-right: 8px;
+				border-radius: 4px;
+			}
+			div[chats] > div:hover,
+			div[chats] > div[selected] {
+				background-color: LightBlue;
+			}
+			div[posts] {
+				padding-left: 8px;
+				border-left: 1px solid;
+			}
+		</style>
+		<script>
+			'use strict';
+
+			let currentChatId = null;
+
+			function selectChat(div) {
+				let chatId = div.getAttribute('chat');
+				if( chatId === currentChatId )
+					return;
+				let selected = div.parentNode.querySelector('[selected]');
+				if( selected )  selected.removeAttribute('selected');
+				div.setAttribute('selected','');
+				ajax(`chat.js?chat=${chatId}`);
+			}
+		</script>
 	</head>
 	<body>
 <%		header() %>
 		<div content>
-			<h1>Chat</h1>
-<%	for _, chat in ipairs(chats) do %>
-			<p><%= other_users(user,chat) %></p>
-<%	end %>
+			<div chats>
+<%
+	for _, chat in ipairs(chats) do
+%>
+				<div chat="<%=chat.id%>" onclick="selectChat(this)"><%= chat.other_users_email(user) %></div>
+<%
+	end
+%>
+			</div>
+			<div posts></div>
 		</div>
 	</body>
 </html>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/chat.js.luan	Tue Oct 29 22:11:40 2024 -0600
@@ -0,0 +1,27 @@
+local Luan = require "luan:Luan.luan"
+local error = Luan.error
+local Parsers = require "luan:Parsers.luan"
+local json_string = Parsers.json_string or error()
+local Io = require "luan:Io.luan"
+local Http = require "luan:http/Http.luan"
+local User = require "site:/lib/User.luan"
+local current_user = User.current or error()
+local Chat = require "site:/lib/Chat.luan"
+local get_chat_by_id = Chat.get_by_id or error()
+
+
+local function html()
+	local user = current_user() or error()
+	local chat = Http.request.parameters.chat or error()
+	chat = get_chat_by_id(chat) or error()
+%>
+	<p><%= chat.other_users_email(user) %></p>
+<%
+end
+
+return function()
+	Io.stdout = Http.response.text_writer()
+%>
+	document.querySelector('div[posts]').innerHTML = <%=json_string(`html()`)%>;
+<%
+end
--- a/src/lib/Chat.luan	Tue Oct 29 16:47:11 2024 -0600
+++ b/src/lib/Chat.luan	Tue Oct 29 22:11:40 2024 -0600
@@ -1,6 +1,9 @@
 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 is_empty = Table.is_empty or error()
 local Time = require "luan:Time.luan"
 local time_now = Time.now or error()
 local Db = require "site:/lib/Db.luan"
@@ -44,6 +47,21 @@
 		end )
 	end
 
+	function chat.other_users_email(user)
+		local User = require "site:/lib/User.luan"
+		local get_user_by_id = User.get_by_id or error()
+
+		local my_id = user.id
+		local t = {}
+		for _, user_id in ipairs(chat.user_ids) do
+			if user_id ~= my_id then
+				local other_user = get_user_by_id(user_id) or error()
+				t[#t+1] = other_user.email
+			end
+		end
+		return concat( t, ", " )
+	end
+
 	return chat
 end
 
@@ -57,4 +75,9 @@
 	return chats
 end
 
+function Chat.get_by_id(id)
+	local doc = Db.get_document("id:"..id)
+	return doc and doc.type=="chat" and from_doc(doc) or nil
+end
+
 return Chat