]> Untitled Git - lemmy.git/blob - crates/api/src/post/lock.rs
Split apart api files (#2216)
[lemmy.git] / crates / api / src / post / lock.rs
1 use crate::Perform;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4   blocking,
5   check_community_ban,
6   check_community_deleted_or_removed,
7   get_local_user_view_from_jwt,
8   is_mod_or_admin,
9   post::{LockPost, PostResponse},
10 };
11 use lemmy_apub::{
12   objects::post::ApubPost,
13   protocol::activities::{create_or_update::post::CreateOrUpdatePost, CreateOrUpdateType},
14 };
15 use lemmy_db_schema::{
16   source::{
17     moderator::{ModLockPost, ModLockPostForm},
18     post::Post,
19   },
20   traits::Crud,
21 };
22 use lemmy_utils::{ConnectionId, LemmyError};
23 use lemmy_websocket::{send::send_post_ws_message, LemmyContext, UserOperation};
24
25 #[async_trait::async_trait(?Send)]
26 impl Perform for LockPost {
27   type Response = PostResponse;
28
29   #[tracing::instrument(skip(context, websocket_id))]
30   async fn perform(
31     &self,
32     context: &Data<LemmyContext>,
33     websocket_id: Option<ConnectionId>,
34   ) -> Result<PostResponse, LemmyError> {
35     let data: &LockPost = self;
36     let local_user_view =
37       get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
38
39     let post_id = data.post_id;
40     let orig_post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
41
42     check_community_ban(
43       local_user_view.person.id,
44       orig_post.community_id,
45       context.pool(),
46     )
47     .await?;
48     check_community_deleted_or_removed(orig_post.community_id, context.pool()).await?;
49
50     // Verify that only the mods can lock
51     is_mod_or_admin(
52       context.pool(),
53       local_user_view.person.id,
54       orig_post.community_id,
55     )
56     .await?;
57
58     // Update the post
59     let post_id = data.post_id;
60     let locked = data.locked;
61     let updated_post: ApubPost = blocking(context.pool(), move |conn| {
62       Post::update_locked(conn, post_id, locked)
63     })
64     .await??
65     .into();
66
67     // Mod tables
68     let form = ModLockPostForm {
69       mod_person_id: local_user_view.person.id,
70       post_id: data.post_id,
71       locked: Some(locked),
72     };
73     blocking(context.pool(), move |conn| ModLockPost::create(conn, &form)).await??;
74
75     // apub updates
76     CreateOrUpdatePost::send(
77       updated_post,
78       &local_user_view.person.clone().into(),
79       CreateOrUpdateType::Update,
80       context,
81     )
82     .await?;
83
84     send_post_ws_message(
85       data.post_id,
86       UserOperation::LockPost,
87       websocket_id,
88       Some(local_user_view.person.id),
89       context,
90     )
91     .await
92   }
93 }