changeset 9:9674275019bb

reply and edit
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 30 Jun 2022 00:02:28 -0600
parents be36282b556a
children de0cbf515ef5
files src/edit.html.luan src/index.html.luan src/lib/Post.luan src/new_thread.html.luan src/reply.html.luan src/thread.html.luan
diffstat 6 files changed, 206 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/edit.html.luan	Thu Jun 30 00:02:28 2022 -0600
@@ -0,0 +1,50 @@
+local Luan = require "luan:Luan.luan"
+local error = Luan.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 footer = Shared.footer or error()
+local Forum = require "site:/lib/Forum.luan"
+local forum_title = Forum.title or error()
+local Post = require "site:/lib/Post.luan"
+local User = require "site:/lib/User.luan"
+
+
+return function()
+	local user = User.current_required()
+	if user==nil then return end
+	local post_id = Http.request.parameters.post or error()
+	local post = Post.get_by_id(post_id) or error()
+	if Http.request.method == "POST" then
+		post.content = Http.request.parameters.content or error()
+		post.save()
+		Http.response.send_redirect("/thread.html?root="..post.root_id)
+		return
+	end
+	Io.stdout = Http.response.text_writer()
+%>
+<!doctype html>
+<html>
+	<head>
+<%		head() %>
+		<title><%=forum_title%>: edit</title>
+	</head>
+	<body>
+<%		header() %>
+		<div content>
+			<h1>edit: <%=post.root.subject_html%></h1>
+			<p>This page will almost certainly be replaced by ajax, but whatever.  Hack for now.</p>
+			<form method=post>
+				<p><textarea name=content><%=html_encode(post.content)%></textarea></p>
+				<p><input type=submit></p>
+			</form>
+		</div>
+<%		footer() %>
+	</body>
+</html>
+<%
+end
--- a/src/index.html.luan	Wed Jun 29 00:04:09 2022 -0600
+++ b/src/index.html.luan	Thu Jun 30 00:02:28 2022 -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"
@@ -8,9 +9,13 @@
 local footer = Shared.footer or error()
 local Forum = require "site:/lib/Forum.luan"
 local forum_title = Forum.title or error()
+local Db = require "site:/lib/Db.luan"
+local Post = require "site:/lib/Post.luan"
 
 
 return function()
+	local docs, total_hits = Db.search("post_is_root:true",1,1000,{sort="id desc"})
+	local posts = Post.from_docs(docs)
 	Io.stdout = Http.response.text_writer()
 %>
 <!doctype html>
@@ -23,6 +28,9 @@
 <%		header() %>
 		<div content>
 			<p><a href="/new_thread.html">New Thread</a></p>
+<%	for _, post in ipairs(posts) do %>
+			<p><a href="/thread.html?root=<%=post.id%>"><%=post.subject_html%></a></p>
+<%	end %>
 		</div>
 <%		footer() %>
 	</body>
--- a/src/lib/Post.luan	Wed Jun 29 00:04:09 2022 -0600
+++ b/src/lib/Post.luan	Thu Jun 30 00:02:28 2022 -0600
@@ -1,10 +1,13 @@
 local Luan = require "luan:Luan.luan"
 local error = Luan.error
+local ipairs = Luan.ipairs or error()
 local set_metatable = Luan.set_metatable or error()
 local Number = require "luan:Number.luan"
 local long = Number.long or error()
 local Time = require "luan:Time.luan"
 local time_now = Time.now or error()
+local Html = require "luan:Html.luan"
+local html_encode = Html.encode or error()
 local Db = require "site:/lib/Db.luan"
 
 
@@ -14,13 +17,18 @@
 	doc.type == "post" or error "wrong type"
 	return Post.new {
 		id = doc.id
-		subject = doc.subject
 		content = doc.content
 		date = doc.date
 		author_name = doc.post_author_name
 		author_id = doc.post_author_id
+		root_id = doc.post_root_id
+
+		-- root only
+		subject = doc.subject
 		is_root = doc.post_is_root == "true"
-		root_id = doc.post_root_id
+
+		-- replies only
+		parent_id = doc.parent_id
 	}
 end
 
@@ -28,13 +36,18 @@
 	return {
 		type = "post"
 		id = post.id
-		subject = post.subject or error()
 		content = post.content or error()
 		date = post.date or time_now()
 		post_author_name = post.author_name or error()
 		post_author_id = long(post.author_id)
+		post_root_id = post.root_id and long(post.root_id)
+
+		-- root only
+		subject = post.subject
 		post_is_root = post.is_root and "true" or nil
-		post_root_id = post.root_id and long(post.root_id)
+
+		-- replies only
+		parent_id = post.parent_id
 	}
 end
 
@@ -44,6 +57,12 @@
 		local User = require "site:/lib/User.luan"
 		return User.get_by_id(post.author_id)
 	end
+	if key == "subject_html" then
+		return post.subject and html_encode(post.subject)
+	end
+	if key == "root" then
+		return post.is_root and post or Post.get_by_id(post.root_id)
+	end
 	return nil
 end
 
@@ -55,19 +74,33 @@
 		post.id = doc.id
 	end
 
+	function post.reply(author,content)
+		return Db.run_in_transaction( function()
+			local post = Post.new{
+				root_id = post.root_id
+				parent_id = post.id
+				content = content
+				author_name = author.name
+				author_id = author.id
+			}
+			post.save()
+			return post
+		end )
+	end
+
 	set_metatable(post,metatable)
 	return post
 end
 
 function Post.get_by_id(id)
 	local doc = Db.get_document("id:"..id)
-	return doc and Post.from_doc(doc)
+	return doc and from_doc(doc)
 end
 
 function Post.new_thread(author,subject,content)
 	return Db.run_in_transaction( function()
 		local post = Post.new{
-			subject = subject
+			subject = subject or error()
 			content = content
 			author_name = author.name
 			author_id = author.id
@@ -80,4 +113,13 @@
 	end )
 end
 
+function Post.from_docs(docs)
+	local posts = {}
+	for _, doc in ipairs(docs) do
+		local post = from_doc(doc)
+		posts[#posts+1] = post
+	end
+	return posts
+end
+
 return Post
--- a/src/new_thread.html.luan	Wed Jun 29 00:04:09 2022 -0600
+++ b/src/new_thread.html.luan	Thu Jun 30 00:02:28 2022 -0600
@@ -28,7 +28,7 @@
 <html>
 	<head>
 <%		head() %>
-		<title><%=forum_title%></title>
+		<title><%=forum_title%>: new thread</title>
 	</head>
 	<body>
 <%		header() %>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/reply.html.luan	Thu Jun 30 00:02:28 2022 -0600
@@ -0,0 +1,48 @@
+local Luan = require "luan:Luan.luan"
+local error = Luan.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 footer = Shared.footer or error()
+local Forum = require "site:/lib/Forum.luan"
+local forum_title = Forum.title or error()
+local Post = require "site:/lib/Post.luan"
+local User = require "site:/lib/User.luan"
+
+
+return function()
+	local user = User.current_required()
+	if user==nil then return end
+	local parent_id = Http.request.parameters.parent or error()
+	local parent = Post.get_by_id(parent_id) or error()
+	if Http.request.method == "POST" then
+		local content = Http.request.parameters.content or error()
+		local post = parent.reply(user,content)
+		Http.response.send_redirect("/thread.html?root="..post.root_id)
+		return
+	end
+	Io.stdout = Http.response.text_writer()
+%>
+<!doctype html>
+<html>
+	<head>
+<%		head() %>
+		<title><%=forum_title%>: reply</title>
+	</head>
+	<body>
+<%		header() %>
+		<div content>
+			<h1>reply to: <%=parent.root.subject_html%></h1>
+			<p>This page will almost certainly be replaced by ajax, but whatever.  Hack for now.</p>
+			<form method=post>
+				<p><textarea name=content></textarea></p>
+				<p><input type=submit></p>
+			</form>
+		</div>
+<%		footer() %>
+	</body>
+</html>
+<%
+end
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/thread.html.luan	Thu Jun 30 00:02:28 2022 -0600
@@ -0,0 +1,51 @@
+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"
+local head = Shared.head or error()
+local header = Shared.header or error()
+local footer = Shared.footer or error()
+local Forum = require "site:/lib/Forum.luan"
+local forum_title = Forum.title or error()
+local Db = require "site:/lib/Db.luan"
+local Post = require "site:/lib/Post.luan"
+
+
+return function()
+	local root_id = Http.request.parameters.root or error()
+	local docs, total_hits = Db.search("post_root_id:"..root_id,1,1000,{sort="id"})
+	local posts = Post.from_docs(docs)
+	local subject_html = posts[1].subject_html
+	Io.stdout = Http.response.text_writer()
+%>
+<!doctype html>
+<html>
+	<head>
+<%		head() %>
+		<title><%=forum_title%>: <%=subject_html%></title>
+		<style>
+			[post] {
+				white-space: pre-wrap;
+			}
+		</style>
+	</head>
+	<body>
+<%		header() %>
+		<div content>
+			<h1><%=subject_html%></h1>
+<%	for _, post in ipairs(posts) do %>
+			<hr>
+			<p post><%=post.content%></p>
+			<p>
+				<a href="/reply.html?parent=<%=post.id%>">reply</a>
+				- <a href="/edit.html?post=<%=post.id%>">edit</a>
+			</p>
+<%	end %>
+		</div>
+<%		footer() %>
+	</body>
+</html>
+<%
+end