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()
|
12
|
6 local Parsers = require "luan:Parsers.luan"
|
|
7 local json_string = Parsers.json_string or error()
|
10
|
8 local Io = require "luan:Io.luan"
|
|
9 local Http = require "luan:http/Http.luan"
|
|
10 local User = require "site:/lib/User.luan"
|
|
11 local current_user = User.current or error()
|
|
12 local Db = require "site:/lib/Db.luan"
|
|
13 local run_in_transaction = Db.run_in_transaction or error()
|
|
14 local Chat = require "site:/lib/Chat.luan"
|
|
15 local get_chat_by_id = Chat.get_by_id or error()
|
|
16 local Post = require "site:/lib/Post.luan"
|
|
17 local new_post = Post.new or error()
|
12
|
18 local Shared = require "site:/lib/Shared.luan"
|
|
19 local post_html = Shared.post_html or error()
|
16
|
20 local http_push_to_users = Shared.http_push_to_users or error()
|
10
|
21
|
|
22
|
|
23 return function()
|
|
24 local user = current_user() or error()
|
|
25 local chat = Http.request.parameters.chat or error()
|
|
26 local content = Http.request.parameters.content or error()
|
12
|
27 local post
|
10
|
28 run_in_transaction( function()
|
|
29 chat = get_chat_by_id(chat) or error()
|
|
30 local now = time_now()
|
12
|
31 post = new_post{
|
10
|
32 chat_id = chat.id
|
|
33 author_id = user.id
|
|
34 date = now
|
|
35 content = content
|
|
36 }
|
|
37 post.save()
|
|
38 chat.updated = now
|
|
39 chat.save()
|
|
40 end )
|
12
|
41 local html = `post_html(post)`
|
|
42 local js = "added("..json_string(html)..")"
|
|
43 chat.http_push(js)
|
15
|
44 js = "getChats('"..chat.id.."')"
|
16
|
45 http_push_to_users( chat.user_ids, js )
|
10
|
46 end
|