changeset 23:c54c806fcc6e

add delete_post
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 05 Nov 2024 16:09:10 -0700
parents 949914955bfe
children af41be2dcdec
files src/chat.js src/delete_post.js.luan src/index.html.luan src/lib/Post.luan src/lib/Shared.luan
diffstat 5 files changed, 65 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/src/chat.js	Tue Nov 05 14:29:16 2024 -0700
+++ b/src/chat.js	Tue Nov 05 16:09:10 2024 -0700
@@ -84,6 +84,25 @@
 	ajax(`delete_chat.js?chat=${currentChatId}`);
 }
 
+let currentPostId;
+
+function deletePost(postId) {
+	currentPostId = postId;
+	let dialog = document.querySelector('dialog[delete_post]');
+	openModal(dialog);
+}
+
+function doDeletePost(el) {
+	closeModal(el);
+	ajax(`delete_post.js?post=${currentPostId}`);
+}
+
+function deleted(postId) {
+	let div = document.querySelector(`div[post="${postId}"]`);
+	if( div )
+		div.outerHTML = '';
+}
+
 function added(html) {
 	let input = document.querySelector('div[input]');
 	input.insertAdjacentHTML('beforebegin',html);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/delete_post.js.luan	Tue Nov 05 16:09:10 2024 -0700
@@ -0,0 +1,24 @@
+local Luan = require "luan:Luan.luan"
+local error = Luan.error
+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 Post = require "site:/lib/Post.luan"
+local get_post_by_id = Post.get_by_id or error()
+local Utils = require "site:/lib/Utils.luan"
+local is_in_list = Utils.is_in_list or error()
+
+
+return function()
+	local post = Http.request.parameters.post or error()
+	post = get_post_by_id(post) or error()
+	local chat = get_chat_by_id(post.chat_id) or error()
+	local user_ids = chat.user_ids
+	local user = current_user() or error()
+	is_in_list( user.id, user_ids ) or error()
+	post.delete()
+	local js = "deleted('"..post.id.."')"
+	chat.http_push(js)
+end
--- a/src/index.html.luan	Tue Nov 05 14:29:16 2024 -0700
+++ b/src/index.html.luan	Tue Nov 05 16:09:10 2024 -0700
@@ -112,6 +112,14 @@
 				<button go onclick="doDeleteChat(this)">Delete</button>
 			</div>
 		</dialog>
+		<dialog delete_post>
+			<h2>Delete Message</h2>
+			<p>Are you sure that you want to delete this message?</p>
+			<div buttons>
+				<button cancel onclick="closeModal(this)">Cancel</button>
+				<button go onclick="doDeletePost(this)">Delete</button>
+			</div>
+		</dialog>
 		<script>
 <%
 	if selected ~= nil then
--- a/src/lib/Post.luan	Tue Nov 05 14:29:16 2024 -0700
+++ b/src/lib/Post.luan	Tue Nov 05 16:09:10 2024 -0700
@@ -4,6 +4,7 @@
 local Number = require "luan:Number.luan"
 local long = Number.long or error()
 local Db = require "site:/lib/Db.luan"
+local run_in_transaction = Db.run_in_transaction or error()
 
 
 local Post = {}
@@ -37,6 +38,13 @@
 		post.id = doc.id
 	end
 
+	function post.delete()
+		run_in_transaction( function()
+			local id = post.id
+			Db.delete("id:"..id)
+		end )
+	end
+
 	return post
 end
 
@@ -50,4 +58,9 @@
 	return posts
 end
 
+function Post.get_by_id(id)
+	local doc = Db.get_document("id:"..id)
+	return doc and doc.type=="post" and from_doc(doc) or nil
+end
+
 return Post
--- a/src/lib/Shared.luan	Tue Nov 05 14:29:16 2024 -0700
+++ b/src/lib/Shared.luan	Tue Nov 05 16:09:10 2024 -0700
@@ -107,7 +107,7 @@
 					<img onclick="clickMenu(this)" src="/images/more_vert.svg">
 					<div>
 						<span>Edit</span>
-						<span>Delete</span>
+						<span onclick="deletePost('<%=id%>')">Delete</span>
 					</div>
 				<span>
 <%	end %>