changeset 2:ee1f91e67509

add Db
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 24 Oct 2024 15:37:35 -0600
parents b116b6ef07bf
children 2c63b10781e1
files src/index.html.luan src/lib/Db.luan src/lib/Shared.luan src/lib/User.luan src/private/tools/index.html.luan src/private/tools/lucene.html.luan src/site.css
diffstat 7 files changed, 163 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/src/index.html.luan	Wed Oct 23 21:18:04 2024 -0600
+++ b/src/index.html.luan	Thu Oct 24 15:37:35 2024 -0600
@@ -14,7 +14,7 @@
 <html>
 	<head>
 <%		head() %>
-		<title>Reactionary Chat</title>
+		<title>Web Chat</title>
 		<style>
 			h1 {
 				margin-bottom: 0;
@@ -27,7 +27,7 @@
 	<body>
 <%		header() %>
 		<div content>
-			<h1>Reactionary Chat</h1>
+			<h1>Web Chat</h1>
 			<h3>A free web-based instant messaging service by <a href="https://www.reactionary.software/">Reactionary Software</a></h3>
 		</div>
 	</body>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/lib/Db.luan	Thu Oct 24 15:37:35 2024 -0600
@@ -0,0 +1,35 @@
+local Luan = require "luan:Luan.luan"
+local error = Luan.error
+local new_error = Luan.new_error or error()
+local Lucene = require "luan:lucene/Lucene.luan"
+local Io = require "luan:Io.luan"
+local uri = Io.uri or error()
+local Http = require "luan:http/Http.luan"
+local Thread = require "luan:Thread.luan"
+local Time = require "luan:Time.luan"
+local Logging = require "luan:logging/Logging.luan"
+local logger = Logging.logger "Db"
+
+
+local dir = uri("site:/private/local/lucene")
+
+local Db = Lucene.index( dir, {
+	log_dir = uri("site:/private/local/lucene_log")
+	name = "lucene"
+	version = 1
+} )
+
+Db.indexed_fields.user_email = Lucene.type.lowercase
+
+function Db.not_in_transaction()
+	logger.error(new_error("not in transaction"))
+end
+
+Db.update{
+}
+
+if Http.is_serving then
+	Thread.schedule( Db.check, { delay=0, repeating_delay=Time.period{hours=1} } )
+end
+
+return Db
--- a/src/lib/Shared.luan	Wed Oct 23 21:18:04 2024 -0600
+++ b/src/lib/Shared.luan	Thu Oct 24 15:37:35 2024 -0600
@@ -4,6 +4,8 @@
 local parse = Luan.parse or error()
 local Io = require "luan:Io.luan"
 local uri = Io.uri or error()
+local User = require "site:/lib/User.luan"
+local current_user = User.current or error()
 
 
 local Shared = {}
@@ -18,12 +20,22 @@
 end
 
 local function header(crumbs)
+	local user = current_user()
 %>
 		<div header>
-			<a href="/">Reactionary Chat</a>
+			<span>
+				<a href="/">Web Chat</a>
 <%	for _, crumb in ipairs(crumbs or {}) do %>
-			/ <%=crumb%>
+				/ <%=crumb%>
 <%	end %>
+			</span>
+			<span>
+<%	if user == nil then %>
+				<a href="/login.html">Login / Register</a>
+<%	else %>
+				<a href="/account.html"><%= user.email %></a>
+<%	end %>
+			</span>
 		</div>
 <%
 end
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/lib/User.luan	Thu Oct 24 15:37:35 2024 -0600
@@ -0,0 +1,103 @@
+local Luan = require "luan:Luan.luan"
+local error = Luan.error
+local ipairs = Luan.ipairs or error()
+local to_string = Luan.to_string or error()
+local get_local_only = Luan.get_local_only or error()
+local set_local_only = Luan.set_local_only or error()
+local Lucene = require "luan:lucene/Lucene.luan"
+local lucene_quote = Lucene.quote or error()
+local Http = require "luan:http/Http.luan"
+local Db = require "site:/lib/Db.luan"
+local Logging = require "luan:logging/Logging.luan"
+local logger = Logging.logger "User"
+
+
+local User = {}
+
+local function from_doc(doc)
+	doc.type == "user" or error "wrong type"
+	return User.new {
+		id = doc.id
+		email = doc.user_email
+		password = doc.password
+	}
+end
+
+local function to_doc(user)
+	return {
+		type = "user"
+		id = user.id
+		user_email = user.email
+		password = user.password
+	}
+end
+
+function User.new(user)
+
+	function user.save()
+		local doc = to_doc(user)
+		Db.save(doc)
+		user.id = doc.id
+	end
+
+	function user.reload()
+		return User.get_by_id(user.id) or error(user.id)
+	end
+
+	function user.login()
+		local id = to_string(user.id)
+		Http.response.set_persistent_cookie("user",id)
+		Http.response.set_persistent_cookie("password",user.password)
+		Http.request.cookies.user = id
+		Http.request.cookies.password = user.password or error()
+	end
+
+	return user
+end
+
+local function get_by_id(id)
+	local doc = Db.get_document("id:"..id)
+	return doc and doc.type=="user" and from_doc(doc) or nil
+end
+User.get_by_id = get_by_id
+
+function User.get_by_email(email)
+	local doc = Db.get_document("user_email:"..lucene_quote(email))
+	return doc and from_doc(doc)
+end
+
+function User.search(query,sort,rows)
+	rows = rows or 1000000
+	local users = {}
+	local docs = Db.search(query,1,rows,{sort=sort})
+	for _, doc in ipairs(docs) do
+		users[#users+1] = from_doc(doc)
+	end
+	return users
+end
+
+function User.current()
+	local user = get_local_only(User,"current")
+	if user == nil then
+		local id = Http.request.cookies.user
+		local password = Http.request.cookies.password
+		if id == nil or password == nil then
+			user = "nil"
+		else
+			user = get_by_id(id)
+			if user == nil or user.registered == nil or user.password ~= password then
+				user = "nil"
+			end
+		end
+		set_local_only(User,"current",user)
+	end
+	return user ~= "nil" and user or nil
+end
+
+function User.current_required()
+	local user = User.current()
+	user or Http.response.send_redirect "/login.html"
+	return user
+end
+
+return User
--- a/src/private/tools/index.html.luan	Wed Oct 23 21:18:04 2024 -0600
+++ b/src/private/tools/index.html.luan	Thu Oct 24 15:37:35 2024 -0600
@@ -21,6 +21,7 @@
 		<div content>
 			<h1>Private Tools</h1>
 			<p><a href="config.html">configure</a></p>
+			<p><a href="lucene.html">lucene</a></p>
 		</div>
 	</body>
 </html>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/private/tools/lucene.html.luan	Thu Oct 24 15:37:35 2024 -0600
@@ -0,0 +1,6 @@
+local Luan = require "luan:Luan.luan"
+local error = Luan.error
+local Web_search = require "luan:lucene/Web_search.luan"
+local Db = require "site:/lib/Db.luan"
+
+return Web_search.of(Db)
--- a/src/site.css	Wed Oct 23 21:18:04 2024 -0600
+++ b/src/site.css	Thu Oct 24 15:37:35 2024 -0600
@@ -18,6 +18,8 @@
 	font-size: 14px;
 	background-color: #ddd;
 	padding: 8px 3%;
+	display: flex;
+	justify-content: space-between;
 }
 
 [content] {