diff src/delete_post.js.luan @ 23:c54c806fcc6e

add delete_post
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 05 Nov 2024 16:09:10 -0700
parents
children
line wrap: on
line diff
--- /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