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()
|
15
|
20 local Utils = require "site:/lib/Utils.luan"
|
|
21 local base_url = Utils.base_url or error()
|
10
|
22
|
|
23
|
|
24 return function()
|
|
25 local user = current_user() or error()
|
|
26 local chat = Http.request.parameters.chat or error()
|
|
27 local content = Http.request.parameters.content or error()
|
12
|
28 local post
|
10
|
29 run_in_transaction( function()
|
|
30 chat = get_chat_by_id(chat) or error()
|
|
31 local now = time_now()
|
12
|
32 post = new_post{
|
10
|
33 chat_id = chat.id
|
|
34 author_id = user.id
|
|
35 date = now
|
|
36 content = content
|
|
37 }
|
|
38 post.save()
|
|
39 chat.updated = now
|
|
40 chat.save()
|
|
41 end )
|
12
|
42 local html = `post_html(post)`
|
|
43 local js = "added("..json_string(html)..")"
|
|
44 chat.http_push(js)
|
15
|
45 js = "getChats('"..chat.id.."')"
|
|
46 local base = base_url().."/user/"
|
|
47 for _, user_id in ipairs(chat.user_ids) do
|
|
48 local url = base..user_id
|
|
49 Http.push(url,js)
|
|
50 end
|
10
|
51 end
|