view src/add_post.js.luan @ 93:d0566cc4a2ac default tip

uploa fix
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 03 Apr 2025 19:47:48 -0600
parents c41e200c3e76
children
line wrap: on
line source

local Luan = require "luan:Luan.luan"
local error = Luan.error
local ipairs = Luan.ipairs or error()
local Time = require "luan:Time.luan"
local time_now = Time.now or error()
local String = require "luan:String.luan"
local to_number = String.to_number or 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 User = require "site:/lib/User.luan"
local current_user = User.current 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 Post = require "site:/lib/Post.luan"
local new_post = Post.new or error()
local Shared = require "site:/lib/Shared.luan"
local post_html = Shared.post_html or error()
local http_push_to_users = Shared.http_push_to_users or error()
local Notify = require "site:/lib/Notify.luan"


return function()
	local user = current_user() or error()
	local chat = Http.request.parameters.chat or error()
	local content = Http.request.parameters.content or error()
	local post
	local now = time_now()
	run_in_transaction( function()
		chat = get_chat_by_id(chat) or error()
		post = {
			chat_id = chat.id
			author_id = user.id
			date = now
			content = content
		}
		local reply = Http.request.parameters.reply
		if reply ~= nil then
			post.reply = to_number(reply) or error()
		end
		post = new_post(post)
		post.save()
		chat.updated = now
		chat.save()
	end )
	Notify.add(chat)
	local html = `post_html(post)`
	local js = "added("..json_string(html)..","..now..")"
	chat.http_push(js)
	js = "getChats('"..chat.id.."',"..now..")"
	http_push_to_users( chat.user_ids, js )
end