Mercurial Hosting > freedit
diff src/new_post.js.luan @ 42:0c1b820fff34
use push
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 08 Nov 2022 14:02:28 -0700 |
parents | |
children | 9f8ebc757814 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/new_post.js.luan Tue Nov 08 14:02:28 2022 -0700 @@ -0,0 +1,43 @@ +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 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 post = Post.new{ + root_id = to_number(root_id) or error() + content = text + author_name = user.name + } + 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)%> ); +console.log(<%=post.id%>); +<% + end) + local url = base_url().."/thread.html?root="..root_id + Http.push(url,js) +end