view src/add_post.js.luan @ 54:1d724f187cff

minor fix
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 03 Mar 2025 21:19:12 -0700
parents 9298b04607ae
children d4d154b404f8
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 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 = new_post{
			chat_id = chat.id
			author_id = user.id
			date = now
			content = content
		}
		post.save()
		chat.updated = now
		chat.save()
	end )
	chat.read(user)
	Notify.add(chat)
	local html = `post_html(post)`
	local js = "added("..json_string(html)..")"
	chat.http_push(js)
	js = "getChats('"..chat.id.."',"..now..")"
	http_push_to_users( chat.user_ids, js )
end