|
10
|
1 local Luan = require "luan:Luan.luan"
|
|
|
2 local error = Luan.error
|
|
15
|
3 local ipairs = Luan.ipairs or error()
|
|
10
|
4 local Time = require "luan:Time.luan"
|
|
|
5 local time_now = Time.now or error()
|
|
79
|
6 local String = require "luan:String.luan"
|
|
|
7 local to_number = String.to_number or error()
|
|
12
|
8 local Parsers = require "luan:Parsers.luan"
|
|
|
9 local json_string = Parsers.json_string or error()
|
|
10
|
10 local Io = require "luan:Io.luan"
|
|
|
11 local Http = require "luan:http/Http.luan"
|
|
|
12 local User = require "site:/lib/User.luan"
|
|
|
13 local current_user = User.current or error()
|
|
|
14 local Db = require "site:/lib/Db.luan"
|
|
|
15 local run_in_transaction = Db.run_in_transaction or error()
|
|
|
16 local Chat = require "site:/lib/Chat.luan"
|
|
|
17 local get_chat_by_id = Chat.get_by_id or error()
|
|
|
18 local Post = require "site:/lib/Post.luan"
|
|
|
19 local new_post = Post.new or error()
|
|
12
|
20 local Shared = require "site:/lib/Shared.luan"
|
|
|
21 local post_html = Shared.post_html or error()
|
|
16
|
22 local http_push_to_users = Shared.http_push_to_users or error()
|
|
40
|
23 local Notify = require "site:/lib/Notify.luan"
|
|
110
|
24 local notify = Notify.notify or error()
|
|
10
|
25
|
|
|
26
|
|
|
27 return function()
|
|
|
28 local user = current_user() or error()
|
|
|
29 local chat = Http.request.parameters.chat or error()
|
|
|
30 local content = Http.request.parameters.content or error()
|
|
12
|
31 local post
|
|
30
|
32 local now = time_now()
|
|
10
|
33 run_in_transaction( function()
|
|
|
34 chat = get_chat_by_id(chat) or error()
|
|
79
|
35 post = {
|
|
10
|
36 chat_id = chat.id
|
|
|
37 author_id = user.id
|
|
|
38 date = now
|
|
|
39 content = content
|
|
|
40 }
|
|
79
|
41 local reply = Http.request.parameters.reply
|
|
|
42 if reply ~= nil then
|
|
|
43 post.reply = to_number(reply) or error()
|
|
|
44 end
|
|
|
45 post = new_post(post)
|
|
10
|
46 post.save()
|
|
|
47 chat.updated = now
|
|
|
48 chat.save()
|
|
|
49 end )
|
|
110
|
50 notify(chat,post,user)
|
|
12
|
51 local html = `post_html(post)`
|
|
92
|
52 local js = "added("..json_string(html)..","..now..")"
|
|
12
|
53 chat.http_push(js)
|
|
30
|
54 js = "getChats('"..chat.id.."',"..now..")"
|
|
16
|
55 http_push_to_users( chat.user_ids, js )
|
|
10
|
56 end
|