]> Untitled Git - lemmy.git/blob - crates/api/src/post/lock.rs
Remove chatserver (#2919)
[lemmy.git] / crates / api / src / post / lock.rs
1 use crate::Perform;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4   build_response::build_post_response,
5   context::LemmyContext,
6   post::{LockPost, PostResponse},
7   utils::{
8     check_community_ban,
9     check_community_deleted_or_removed,
10     is_mod_or_admin,
11     local_user_view_from_jwt,
12   },
13 };
14 use lemmy_db_schema::{
15   source::{
16     moderator::{ModLockPost, ModLockPostForm},
17     post::{Post, PostUpdateForm},
18   },
19   traits::Crud,
20 };
21 use lemmy_utils::error::LemmyError;
22
23 #[async_trait::async_trait(?Send)]
24 impl Perform for LockPost {
25   type Response = PostResponse;
26
27   #[tracing::instrument(skip(context))]
28   async fn perform(&self, context: &Data<LemmyContext>) -> Result<PostResponse, LemmyError> {
29     let data: &LockPost = self;
30     let local_user_view = local_user_view_from_jwt(&data.auth, context).await?;
31
32     let post_id = data.post_id;
33     let orig_post = Post::read(context.pool(), post_id).await?;
34
35     check_community_ban(
36       local_user_view.person.id,
37       orig_post.community_id,
38       context.pool(),
39     )
40     .await?;
41     check_community_deleted_or_removed(orig_post.community_id, context.pool()).await?;
42
43     // Verify that only the mods can lock
44     is_mod_or_admin(
45       context.pool(),
46       local_user_view.person.id,
47       orig_post.community_id,
48     )
49     .await?;
50
51     // Update the post
52     let post_id = data.post_id;
53     let locked = data.locked;
54     Post::update(
55       context.pool(),
56       post_id,
57       &PostUpdateForm::builder().locked(Some(locked)).build(),
58     )
59     .await?;
60
61     // Mod tables
62     let form = ModLockPostForm {
63       mod_person_id: local_user_view.person.id,
64       post_id: data.post_id,
65       locked: Some(locked),
66     };
67     ModLockPost::create(context.pool(), &form).await?;
68
69     build_post_response(
70       context,
71       orig_post.community_id,
72       local_user_view.person.id,
73       post_id,
74     )
75     .await
76   }
77 }