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