comparison src/add_post.js.luan @ 12:9f45d32670ae

server push
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 31 Oct 2024 21:40:57 -0600
parents f9e6a4cc4f7d
children 8b8905f63d80
comparison
equal deleted inserted replaced
11:563a5358f2ee 12:9f45d32670ae
1 local Luan = require "luan:Luan.luan" 1 local Luan = require "luan:Luan.luan"
2 local error = Luan.error 2 local error = Luan.error
3 local Time = require "luan:Time.luan" 3 local Time = require "luan:Time.luan"
4 local time_now = Time.now or error() 4 local time_now = Time.now or error()
5 local Parsers = require "luan:Parsers.luan"
6 local json_string = Parsers.json_string or error()
5 local Io = require "luan:Io.luan" 7 local Io = require "luan:Io.luan"
6 local Http = require "luan:http/Http.luan" 8 local Http = require "luan:http/Http.luan"
7 local User = require "site:/lib/User.luan" 9 local User = require "site:/lib/User.luan"
8 local current_user = User.current or error() 10 local current_user = User.current or error()
9 local Db = require "site:/lib/Db.luan" 11 local Db = require "site:/lib/Db.luan"
10 local run_in_transaction = Db.run_in_transaction or error() 12 local run_in_transaction = Db.run_in_transaction or error()
11 local Chat = require "site:/lib/Chat.luan" 13 local Chat = require "site:/lib/Chat.luan"
12 local get_chat_by_id = Chat.get_by_id or error() 14 local get_chat_by_id = Chat.get_by_id or error()
13 local Post = require "site:/lib/Post.luan" 15 local Post = require "site:/lib/Post.luan"
14 local new_post = Post.new or error() 16 local new_post = Post.new or error()
17 local Shared = require "site:/lib/Shared.luan"
18 local post_html = Shared.post_html or error()
15 19
16 20
17 return function() 21 return function()
18 local user = current_user() or error() 22 local user = current_user() or error()
19 local chat = Http.request.parameters.chat or error() 23 local chat = Http.request.parameters.chat or error()
20 local content = Http.request.parameters.content or error() 24 local content = Http.request.parameters.content or error()
25 local post
21 run_in_transaction( function() 26 run_in_transaction( function()
22 chat = get_chat_by_id(chat) or error() 27 chat = get_chat_by_id(chat) or error()
23 local now = time_now() 28 local now = time_now()
24 local post = new_post{ 29 post = new_post{
25 chat_id = chat.id 30 chat_id = chat.id
26 author_id = user.id 31 author_id = user.id
27 date = now 32 date = now
28 content = content 33 content = content
29 } 34 }
30 post.save() 35 post.save()
31 chat.updated = now 36 chat.updated = now
32 chat.save() 37 chat.save()
33 end ) 38 end )
39 local html = `post_html(post)`
40 local js = "added("..json_string(html)..")"
41 chat.http_push(js)
34 end 42 end