changeset 16:bff178656073

delete function
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 06 Jul 2022 22:42:42 -0600
parents 0edde02b908c
children a602a5735a37
files src/delete.js.luan src/lib/Post.luan src/save_edit.js.luan src/site.css src/thread.html.luan
diffstat 5 files changed, 63 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
diff -r 0edde02b908c -r bff178656073 src/delete.js.luan
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/delete.js.luan	Wed Jul 06 22:42:42 2022 -0600
@@ -0,0 +1,22 @@
+local Luan = require "luan:Luan.luan"
+local error = Luan.error
+local Io = require "luan:Io.luan"
+local Http = require "luan:http/Http.luan"
+local Post = require "site:/lib/Post.luan"
+local Db = require "site:/lib/Db.luan"
+
+
+return function()
+	local post = Http.request.parameters.post or error()
+	Db.run_in_transaction( function()
+		post = Post.get_by_id(post) or error()
+		post.author_is_current() or error()
+		post.is_deleted = true
+		post.save()
+	end )
+	Io.stdout = Http.response.text_writer()
+%>
+let postDiv = document.querySelector('[post="<%=post.id%>"]');
+postDiv.querySelector('[output]').innerHTML = 'deleted';
+<%
+end
diff -r 0edde02b908c -r bff178656073 src/lib/Post.luan
--- a/src/lib/Post.luan	Tue Jul 05 23:27:54 2022 -0600
+++ b/src/lib/Post.luan	Wed Jul 06 22:42:42 2022 -0600
@@ -19,6 +19,7 @@
 		date = doc.date
 		author_name = doc.post_author_name
 		root_id = doc.post_root_id
+		is_deleted = doc.is_deleted == "true"
 
 		-- root only
 		subject = doc.subject
@@ -37,6 +38,7 @@
 		date = post.date or time_now()
 		post_author_name = post.author_name or error()
 		post_root_id = post.root_id
+		is_deleted = post.is_deleted and "true" or nil
 
 		-- root only
 		subject = post.subject
@@ -83,6 +85,12 @@
 		end )
 	end
 
+	function post.author_is_current()
+		local User = require "site:/lib/User.luan"
+		local user = User.current()
+		return user ~= nil and user.name == post.author_name
+	end
+
 	set_metatable(post,metatable)
 	return post
 end
diff -r 0edde02b908c -r bff178656073 src/save_edit.js.luan
--- a/src/save_edit.js.luan	Tue Jul 05 23:27:54 2022 -0600
+++ b/src/save_edit.js.luan	Wed Jul 06 22:42:42 2022 -0600
@@ -10,7 +10,6 @@
 local bbcode_preprocess = Bbcode.preprocess or error()
 local bbcode_to_html = Bbcode.to_html or error()
 local Db = require "site:/lib/Db.luan"
-local User = require "site:/lib/User.luan"
 
 
 return function()
@@ -19,8 +18,7 @@
 	text = bbcode_preprocess(text)
 	Db.run_in_transaction( function()
 		post = Post.get_by_id(post) or error()
-		local user = User.current() or error()
-		user.name = post.author_name or error()
+		post.author_is_current() or error()
 		post.content = text
 		post.save()
 	end )
diff -r 0edde02b908c -r bff178656073 src/site.css
--- a/src/site.css	Tue Jul 05 23:27:54 2022 -0600
+++ b/src/site.css	Wed Jul 06 22:42:42 2022 -0600
@@ -27,3 +27,7 @@
 	margin-right: 3%;
 	margin-bottom: 2em;
 }
+
+[hidden] {
+	display: none;
+}
diff -r 0edde02b908c -r bff178656073 src/thread.html.luan
--- a/src/thread.html.luan	Tue Jul 05 23:27:54 2022 -0600
+++ b/src/thread.html.luan	Wed Jul 06 22:42:42 2022 -0600
@@ -13,13 +13,20 @@
 local Post = require "site:/lib/Post.luan"
 local Bbcode = require "site:/lib/Bbcode.luan"
 local bbcode_to_html = Bbcode.to_html or error()
+local User = require "site:/lib/User.luan"
 
 
+local function deletePost()
+	%><a href="javascript:" onclick="deletePost(parentNode)">delete</a><%
+end
+
 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
+	local user = User.current()
+	local user_name = user and user.name
 	Io.stdout = Http.response.text_writer()
 %>
 <!doctype html>
@@ -38,27 +45,44 @@
 				postDiv.querySelector('[output]').style.display = 'block';
 				postDiv.querySelector('[edit]').innerHTML = '';
 			}
-
 			function saveEdit(post) {
 				let postDiv = document.querySelector('[post="'+post+'"]');
 				let text = postDiv.querySelector('textarea').value;
 				let postData = 'post=' + post + '&text=' + encodeURIComponent(text);
 				ajax("save_edit.js",postData);
 			}
+
+			function deletePost(span) {
+				span.innerHTML = document.querySelector('[hidden][delete]').innerHTML;
+			}
+			function deleteNo(span) {
+				span.innerHTML = document.querySelector('[hidden][undelete]').innerHTML;
+			}
+			function deleteYes(span) {
+				let post = span.getAttribute('delete');
+				ajax( '/delete.js?post=' + post );
+			}
 		</script>
 	</head>
 	<body>
 <%		header() %>
 		<div content>
 			<h1><%=subject_html%></h1>
-<%	for _, post in ipairs(posts) do %>
+<%	for _, post in ipairs(posts) do
+		if post.is_deleted then
+			continue
+		end
+%>
 			<hr>
 			<div post="<%=post.id%>">
 				<div output>
 					<% bbcode_to_html(post.content) %>
 					<p>
 						<a href="/reply.html?parent=<%=post.id%>">reply</a>
+<%	if post.author_name == user_name then %>
 						- <a href="javascript:ajax('/edit.js?post=<%=post.id%>')">edit</a>
+						- <span delete="<%=post.id%>"><%deletePost()%></span>
+<%	end %>
 					</p>
 				</div>
 				<div edit></div>
@@ -66,6 +90,8 @@
 <%	end %>
 		</div>
 <%		footer() %>
+		<span hidden delete>Delete? <a href="javascript:" onclick="deleteYes(parentNode)">yes</a> / <a href="javascript:" onclick="deleteNo(parentNode)">no</a></span>
+		<span hidden undelete><%deletePost()%></span>
 	</body>
 </html>
 <%