Mercurial Hosting > chat
comparison src/save_post.js.luan @ 24:af41be2dcdec
add edit_post
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 05 Nov 2024 19:45:08 -0700 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
23:c54c806fcc6e | 24:af41be2dcdec |
---|---|
1 local Luan = require "luan:Luan.luan" | |
2 local error = Luan.error | |
3 local Parsers = require "luan:Parsers.luan" | |
4 local json_string = Parsers.json_string or error() | |
5 local Io = require "luan:Io.luan" | |
6 local Http = require "luan:http/Http.luan" | |
7 local Post = require "site:/lib/Post.luan" | |
8 local get_post_by_id = Post.get_by_id or error() | |
9 local Db = require "site:/lib/Db.luan" | |
10 local run_in_transaction = Db.run_in_transaction or error() | |
11 local Chat = require "site:/lib/Chat.luan" | |
12 local get_chat_by_id = Chat.get_by_id or error() | |
13 local User = require "site:/lib/User.luan" | |
14 local current_user = User.current or error() | |
15 local Shared = require "site:/lib/Shared.luan" | |
16 local post_html = Shared.post_html or error() | |
17 local Utils = require "site:/lib/Utils.luan" | |
18 local is_in_list = Utils.is_in_list or error() | |
19 | |
20 | |
21 return function() | |
22 local post = Http.request.parameters.post or error() | |
23 local content = Http.request.parameters.content or error() | |
24 post = get_post_by_id(post) or error() | |
25 local chat = get_chat_by_id(post.chat_id) or error() | |
26 local user_ids = chat.user_ids | |
27 local user = current_user() or error() | |
28 is_in_list( user.id, user_ids ) or error() | |
29 run_in_transaction( function() | |
30 post = post.reload() | |
31 post.content = content | |
32 post.save() | |
33 end ) | |
34 local html = `post_html(post)` | |
35 local js = "edited( '"..post.id.."', "..json_string(html).." )" | |
36 chat.http_push(js) | |
37 end |