view src/save_post.js.luan @ 49:080eb027d93d

minor
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 28 Feb 2025 20:27:34 -0700
parents af41be2dcdec
children
line wrap: on
line source

local Luan = require "luan:Luan.luan"
local error = Luan.error
local Parsers = require "luan:Parsers.luan"
local json_string = Parsers.json_string or error()
local Io = require "luan:Io.luan"
local Http = require "luan:http/Http.luan"
local Post = require "site:/lib/Post.luan"
local get_post_by_id = Post.get_by_id or error()
local Db = require "site:/lib/Db.luan"
local run_in_transaction = Db.run_in_transaction or error()
local Chat = require "site:/lib/Chat.luan"
local get_chat_by_id = Chat.get_by_id or error()
local User = require "site:/lib/User.luan"
local current_user = User.current or error()
local Shared = require "site:/lib/Shared.luan"
local post_html = Shared.post_html 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()
	local content = Http.request.parameters.content 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()
	run_in_transaction( function()
		post = post.reload()
		post.content = content
		post.save()
	end )
	local html = `post_html(post)`
	local js = "edited( '"..post.id.."', "..json_string(html).." )"
	chat.http_push(js)
end