view src/new_post.js.luan @ 56:7ce54f6d93f2

add change name
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 28 Nov 2022 22:00:43 -0700
parents 9f8ebc757814
children
line wrap: on
line source

local Luan = require "luan:Luan.luan"
local error = Luan.error
local String = require "luan:String.luan"
local to_number = String.to_number 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 output_of = Io.output_of or error()
local Http = require "luan:http/Http.luan"
local User = require "site:/lib/User.luan"
local Post = require "site:/lib/Post.luan"
local Db = require "site:/lib/Db.luan"
local Shared = require "site:/lib/Shared.luan"
local base_url = Shared.base_url or error()
local show_post = Shared.show_post or error()
local Bbcode = require "site:/bbcode/Bbcode.luan"
local preprocess = Bbcode.preprocess or error()
local Logging = require "luan:logging/Logging.luan"
local logger = Logging.logger "new_post.js"


return function()
	local user = User.current() or error()
	local root_id = Http.request.parameters.root or error()
	local text = Http.request.parameters.text or error()
	local convert_urls = Http.request.parameters.convert_urls or error()
	if convert_urls == "true" then
		text = preprocess(text)
	end
	local post = Post.new{
		root_id = to_number(root_id) or error()
		content = text
		author_id = user.id
	}
	Db.run_in_transaction( function()
		post.save()
	end )
	local html = output_of(function() show_post(post,time_now()) end)
	local js = output_of(function()
%>
added( '<%=post.id%>', <%= json_string(html) %>, <%=Post.thread_size(root_id)%> );
<%
	end)
	local url = base_url().."/thread.html?root="..root_id
	Http.push(url,js)
end