annotate src/delete_post.js.luan @ 38:436216d17a1b

fix edit and delete
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 12 Nov 2024 21:53:03 -0700
parents c54c806fcc6e
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
23
c54c806fcc6e add delete_post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1 local Luan = require "luan:Luan.luan"
c54c806fcc6e add delete_post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2 local error = Luan.error
c54c806fcc6e add delete_post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 local Http = require "luan:http/Http.luan"
c54c806fcc6e add delete_post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 local User = require "site:/lib/User.luan"
c54c806fcc6e add delete_post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
5 local current_user = User.current or error()
c54c806fcc6e add delete_post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6 local Chat = require "site:/lib/Chat.luan"
c54c806fcc6e add delete_post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7 local get_chat_by_id = Chat.get_by_id or error()
c54c806fcc6e add delete_post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8 local Post = require "site:/lib/Post.luan"
c54c806fcc6e add delete_post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9 local get_post_by_id = Post.get_by_id or error()
c54c806fcc6e add delete_post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 local Utils = require "site:/lib/Utils.luan"
c54c806fcc6e add delete_post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11 local is_in_list = Utils.is_in_list or error()
c54c806fcc6e add delete_post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12
c54c806fcc6e add delete_post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13
c54c806fcc6e add delete_post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14 return function()
c54c806fcc6e add delete_post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15 local post = Http.request.parameters.post or error()
c54c806fcc6e add delete_post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 post = get_post_by_id(post) or error()
c54c806fcc6e add delete_post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 local chat = get_chat_by_id(post.chat_id) or error()
c54c806fcc6e add delete_post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 local user_ids = chat.user_ids
c54c806fcc6e add delete_post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19 local user = current_user() or error()
c54c806fcc6e add delete_post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 is_in_list( user.id, user_ids ) or error()
c54c806fcc6e add delete_post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 post.delete()
c54c806fcc6e add delete_post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 local js = "deleted('"..post.id.."')"
c54c806fcc6e add delete_post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 chat.http_push(js)
c54c806fcc6e add delete_post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 end