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()
|
40
|
21 local Notify = require "site:/lib/Notify.luan"
|
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
|
30
|
29 local now = time_now()
|
10
|
30 run_in_transaction( function()
|
|
31 chat = get_chat_by_id(chat) or error()
|
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 )
|
53
|
42 chat.read(user)
|
40
|
43 Notify.add(chat)
|
12
|
44 local html = `post_html(post)`
|
|
45 local js = "added("..json_string(html)..")"
|
|
46 chat.http_push(js)
|
30
|
47 js = "getChats('"..chat.id.."',"..now..")"
|
16
|
48 http_push_to_users( chat.user_ids, js )
|
10
|
49 end
|