annotate src/add_post.js.luan @ 16:82b55186a4a0

fix delete_chat
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 03 Nov 2024 21:22:06 -0700
parents 8b8905f63d80
children f4708943f29e
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()
12
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
6 local Parsers = require "luan:Parsers.luan"
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
7 local json_string = Parsers.json_string or error()
10
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8 local Io = require "luan:Io.luan"
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9 local Http = require "luan:http/Http.luan"
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 local User = require "site:/lib/User.luan"
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11 local current_user = User.current or error()
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 local Db = require "site:/lib/Db.luan"
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13 local run_in_transaction = Db.run_in_transaction or error()
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14 local Chat = require "site:/lib/Chat.luan"
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15 local get_chat_by_id = Chat.get_by_id or error()
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 local Post = require "site:/lib/Post.luan"
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 local new_post = Post.new or error()
12
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
18 local Shared = require "site:/lib/Shared.luan"
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
19 local post_html = Shared.post_html or error()
16
82b55186a4a0 fix delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
20 local http_push_to_users = Shared.http_push_to_users or error()
10
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 return function()
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 local user = current_user() or error()
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 local chat = Http.request.parameters.chat or error()
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 local content = Http.request.parameters.content or error()
12
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
27 local post
10
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 run_in_transaction( function()
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 chat = get_chat_by_id(chat) or error()
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 local now = time_now()
12
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
31 post = new_post{
10
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32 chat_id = chat.id
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33 author_id = user.id
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34 date = now
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 content = content
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36 }
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37 post.save()
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
38 chat.updated = now
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
39 chat.save()
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
40 end )
12
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
41 local html = `post_html(post)`
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
42 local js = "added("..json_string(html)..")"
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
43 chat.http_push(js)
15
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
44 js = "getChats('"..chat.id.."')"
16
82b55186a4a0 fix delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
45 http_push_to_users( chat.user_ids, js )
10
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
46 end