annotate src/save_post.js.luan @ 49:080eb027d93d

minor
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 28 Feb 2025 20:27:34 -0700
parents af41be2dcdec
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
24
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1 local Luan = require "luan:Luan.luan"
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2 local error = Luan.error
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 local Parsers = require "luan:Parsers.luan"
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 local json_string = Parsers.json_string or error()
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
5 local Io = require "luan:Io.luan"
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6 local Http = require "luan:http/Http.luan"
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7 local Post = require "site:/lib/Post.luan"
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8 local get_post_by_id = Post.get_by_id or error()
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9 local Db = require "site:/lib/Db.luan"
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 local run_in_transaction = Db.run_in_transaction or error()
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11 local Chat = require "site:/lib/Chat.luan"
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 local get_chat_by_id = Chat.get_by_id or error()
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13 local User = require "site:/lib/User.luan"
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14 local current_user = User.current or error()
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15 local Shared = require "site:/lib/Shared.luan"
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 local post_html = Shared.post_html or error()
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 local Utils = require "site:/lib/Utils.luan"
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 local is_in_list = Utils.is_in_list or error()
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 return function()
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 local post = Http.request.parameters.post or error()
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 local content = Http.request.parameters.content or error()
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 post = get_post_by_id(post) or error()
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 local chat = get_chat_by_id(post.chat_id) or error()
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 local user_ids = chat.user_ids
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 local user = current_user() or error()
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 is_in_list( user.id, user_ids ) or error()
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 run_in_transaction( function()
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 post = post.reload()
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31 post.content = content
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32 post.save()
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33 end )
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34 local html = `post_html(post)`
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 local js = "edited( '"..post.id.."', "..json_string(html).." )"
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36 chat.http_push(js)
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37 end