annotate src/add_post.js.luan @ 79:b5a316575e64

reply
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 10 Mar 2025 21:41:53 -0600
parents d4d154b404f8
children c41e200c3e76
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
15
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
3 local ipairs = Luan.ipairs or error()
10
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 local Time = require "luan:Time.luan"
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
5 local time_now = Time.now or error()
79
Franklin Schmidt <fschmidt@gmail.com>
parents: 61
diff changeset
6 local String = require "luan:String.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents: 61
diff changeset
7 local to_number = String.to_number or error()
12
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
8 local Parsers = require "luan:Parsers.luan"
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
9 local json_string = Parsers.json_string or error()
10
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 local Io = require "luan:Io.luan"
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11 local Http = require "luan:http/Http.luan"
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 local User = require "site:/lib/User.luan"
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13 local current_user = User.current or error()
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14 local Db = require "site:/lib/Db.luan"
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15 local run_in_transaction = Db.run_in_transaction or error()
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 local Chat = require "site:/lib/Chat.luan"
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 local get_chat_by_id = Chat.get_by_id or error()
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 local Post = require "site:/lib/Post.luan"
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19 local new_post = Post.new or error()
12
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
20 local Shared = require "site:/lib/Shared.luan"
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
21 local post_html = Shared.post_html or error()
16
82b55186a4a0 fix delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
22 local http_push_to_users = Shared.http_push_to_users or error()
40
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 30
diff changeset
23 local Notify = require "site:/lib/Notify.luan"
10
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 return function()
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 local user = current_user() or error()
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 local chat = Http.request.parameters.chat or error()
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 local content = Http.request.parameters.content or error()
12
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
30 local post
30
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents: 16
diff changeset
31 local now = time_now()
10
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32 run_in_transaction( function()
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33 chat = get_chat_by_id(chat) or error()
79
Franklin Schmidt <fschmidt@gmail.com>
parents: 61
diff changeset
34 post = {
10
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 chat_id = chat.id
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36 author_id = user.id
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37 date = now
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
38 content = content
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
39 }
79
Franklin Schmidt <fschmidt@gmail.com>
parents: 61
diff changeset
40 local reply = Http.request.parameters.reply
Franklin Schmidt <fschmidt@gmail.com>
parents: 61
diff changeset
41 if reply ~= nil then
Franklin Schmidt <fschmidt@gmail.com>
parents: 61
diff changeset
42 post.reply = to_number(reply) or error()
Franklin Schmidt <fschmidt@gmail.com>
parents: 61
diff changeset
43 end
Franklin Schmidt <fschmidt@gmail.com>
parents: 61
diff changeset
44 post = new_post(post)
10
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
45 post.save()
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
46 chat.updated = now
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47 chat.save()
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
48 end )
40
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 30
diff changeset
49 Notify.add(chat)
12
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
50 local html = `post_html(post)`
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
51 local js = "added("..json_string(html)..")"
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
52 chat.http_push(js)
30
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents: 16
diff changeset
53 js = "getChats('"..chat.id.."',"..now..")"
16
82b55186a4a0 fix delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
54 http_push_to_users( chat.user_ids, js )
10
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
55 end