]> Untitled Git - lemmy.git/blob - crates/api/src/post/lock.rs
Dont return error in case optional auth is invalid (#2879)
[lemmy.git] / crates / api / src / post / lock.rs
1 use crate::Perform;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4   context::LemmyContext,
5   post::{LockPost, PostResponse},
6   utils::{
7     check_community_ban,
8     check_community_deleted_or_removed,
9     is_mod_or_admin,
10     local_user_view_from_jwt,
11   },
12   websocket::UserOperation,
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, ConnectionId};
22
23 #[async_trait::async_trait(?Send)]
24 impl Perform for LockPost {
25   type Response = PostResponse;
26
27   #[tracing::instrument(skip(context, websocket_id))]
28   async fn perform(
29     &self,
30     context: &Data<LemmyContext>,
31     websocket_id: Option<ConnectionId>,
32   ) -> Result<PostResponse, LemmyError> {
33     let data: &LockPost = self;
34     let local_user_view = local_user_view_from_jwt(&data.auth, context).await?;
35
36     let post_id = data.post_id;
37     let orig_post = Post::read(context.pool(), post_id).await?;
38
39     check_community_ban(
40       local_user_view.person.id,
41       orig_post.community_id,
42       context.pool(),
43     )
44     .await?;
45     check_community_deleted_or_removed(orig_post.community_id, context.pool()).await?;
46
47     // Verify that only the mods can lock
48     is_mod_or_admin(
49       context.pool(),
50       local_user_view.person.id,
51       orig_post.community_id,
52     )
53     .await?;
54
55     // Update the post
56     let post_id = data.post_id;
57     let locked = data.locked;
58     Post::update(
59       context.pool(),
60       post_id,
61       &PostUpdateForm::builder().locked(Some(locked)).build(),
62     )
63     .await?;
64
65     // Mod tables
66     let form = ModLockPostForm {
67       mod_person_id: local_user_view.person.id,
68       post_id: data.post_id,
69       locked: Some(locked),
70     };
71     ModLockPost::create(context.pool(), &form).await?;
72
73     context
74       .send_post_ws_message(
75         &UserOperation::LockPost,
76         data.post_id,
77         websocket_id,
78         Some(local_user_view.person.id),
79       )
80       .await
81   }
82 }