annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1 local Luan = require "luan:Luan.luan"
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2 local error = Luan.error
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 local Time = require "luan:Time.luan"
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 local time_now = Time.now or error()
12
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
5 local Parsers = require "luan:Parsers.luan"
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
6 local json_string = Parsers.json_string or error()
10
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7 local Io = require "luan:Io.luan"
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8 local Http = require "luan:http/Http.luan"
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9 local User = require "site:/lib/User.luan"
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 local current_user = User.current or error()
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11 local Db = require "site:/lib/Db.luan"
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 local run_in_transaction = Db.run_in_transaction or error()
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13 local Chat = require "site:/lib/Chat.luan"
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14 local get_chat_by_id = Chat.get_by_id or error()
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15 local Post = require "site:/lib/Post.luan"
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 local new_post = Post.new or error()
12
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
17 local Shared = require "site:/lib/Shared.luan"
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
18 local post_html = Shared.post_html or error()
10
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 return function()
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 local user = current_user() or error()
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 local chat = Http.request.parameters.chat or error()
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 local content = Http.request.parameters.content or error()
12
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
25 local post
10
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 run_in_transaction( function()
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 chat = get_chat_by_id(chat) or error()
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 local now = time_now()
12
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
29 post = new_post{
10
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 chat_id = chat.id
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31 author_id = user.id
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32 date = now
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33 content = content
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34 }
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 post.save()
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36 chat.updated = now
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37 chat.save()
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
38 end )
12
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
39 local html = `post_html(post)`
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
40 local js = "added("..json_string(html)..")"
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
41 chat.http_push(js)
10
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
42 end