annotate src/new_post.js.luan @ 51:78b2d6995244

add preview
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 22 Nov 2022 22:57:43 -0700
parents 0c1b820fff34
children 9f8ebc757814
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
42
0c1b820fff34 use push
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1 local Luan = require "luan:Luan.luan"
0c1b820fff34 use push
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2 local error = Luan.error
0c1b820fff34 use push
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 local String = require "luan:String.luan"
0c1b820fff34 use push
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 local to_number = String.to_number or error()
0c1b820fff34 use push
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
5 local Time = require "luan:Time.luan"
0c1b820fff34 use push
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6 local time_now = Time.now or error()
0c1b820fff34 use push
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7 local Parsers = require "luan:Parsers.luan"
0c1b820fff34 use push
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8 local json_string = Parsers.json_string or error()
0c1b820fff34 use push
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9 local Io = require "luan:Io.luan"
0c1b820fff34 use push
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 local output_of = Io.output_of or error()
0c1b820fff34 use push
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11 local Http = require "luan:http/Http.luan"
0c1b820fff34 use push
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 local User = require "site:/lib/User.luan"
0c1b820fff34 use push
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13 local Post = require "site:/lib/Post.luan"
0c1b820fff34 use push
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14 local Db = require "site:/lib/Db.luan"
0c1b820fff34 use push
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15 local Shared = require "site:/lib/Shared.luan"
0c1b820fff34 use push
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 local base_url = Shared.base_url or error()
0c1b820fff34 use push
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 local show_post = Shared.show_post or error()
0c1b820fff34 use push
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 local Logging = require "luan:logging/Logging.luan"
0c1b820fff34 use push
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19 local logger = Logging.logger "new_post.js"
0c1b820fff34 use push
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20
0c1b820fff34 use push
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21
0c1b820fff34 use push
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 return function()
0c1b820fff34 use push
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 local user = User.current() or error()
0c1b820fff34 use push
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 local root_id = Http.request.parameters.root or error()
0c1b820fff34 use push
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 local text = Http.request.parameters.text or error()
0c1b820fff34 use push
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 local post = Post.new{
0c1b820fff34 use push
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 root_id = to_number(root_id) or error()
0c1b820fff34 use push
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 content = text
0c1b820fff34 use push
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 author_name = user.name
0c1b820fff34 use push
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 }
0c1b820fff34 use push
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31 Db.run_in_transaction( function()
0c1b820fff34 use push
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32 post.save()
0c1b820fff34 use push
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33 end )
0c1b820fff34 use push
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34 local html = output_of(function() show_post(post,time_now()) end)
0c1b820fff34 use push
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 local js = output_of(function()
0c1b820fff34 use push
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36 %>
0c1b820fff34 use push
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37 added( '<%=post.id%>', <%= json_string(html) %>, <%=Post.thread_size(root_id)%> );
0c1b820fff34 use push
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
38 console.log(<%=post.id%>);
0c1b820fff34 use push
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
39 <%
0c1b820fff34 use push
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
40 end)
0c1b820fff34 use push
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
41 local url = base_url().."/thread.html?root="..root_id
0c1b820fff34 use push
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
42 Http.push(url,js)
0c1b820fff34 use push
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
43 end