]> Untitled Git - lemmy.git/blob - crates/api/src/community/hide.rs
54a08135af2069d243981a2a83f99dfcbe49ecab
[lemmy.git] / crates / api / src / community / hide.rs
1 use crate::Perform;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4   community::{CommunityResponse, HideCommunity},
5   utils::{get_local_user_view_from_jwt, is_admin},
6   websocket::{send::send_community_ws_message, UserOperationCrud},
7   LemmyContext,
8 };
9 use lemmy_apub::protocol::activities::community::update::UpdateCommunity;
10 use lemmy_db_schema::{
11   source::{
12     community::{Community, CommunityUpdateForm},
13     moderator::{ModHideCommunity, ModHideCommunityForm},
14   },
15   traits::Crud,
16 };
17 use lemmy_utils::{error::LemmyError, ConnectionId};
18
19 #[async_trait::async_trait(?Send)]
20 impl Perform for HideCommunity {
21   type Response = CommunityResponse;
22
23   #[tracing::instrument(skip(context, websocket_id))]
24   async fn perform(
25     &self,
26     context: &Data<LemmyContext>,
27     websocket_id: Option<ConnectionId>,
28   ) -> Result<CommunityResponse, LemmyError> {
29     let data: &HideCommunity = self;
30
31     // Verify its a admin (only admin can hide or unhide it)
32     let local_user_view =
33       get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
34     is_admin(&local_user_view)?;
35
36     let community_form = CommunityUpdateForm::builder()
37       .hidden(Some(data.hidden))
38       .build();
39
40     let mod_hide_community_form = ModHideCommunityForm {
41       community_id: data.community_id,
42       mod_person_id: local_user_view.person.id,
43       reason: data.reason.clone(),
44       hidden: Some(data.hidden),
45     };
46
47     let community_id = data.community_id;
48     let updated_community = Community::update(context.pool(), community_id, &community_form)
49       .await
50       .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_community_hidden_status"))?;
51
52     ModHideCommunity::create(context.pool(), &mod_hide_community_form).await?;
53
54     UpdateCommunity::send(
55       updated_community.into(),
56       &local_user_view.person.into(),
57       context,
58     )
59     .await?;
60
61     let op = UserOperationCrud::EditCommunity;
62     send_community_ws_message(data.community_id, op, websocket_id, None, context).await
63   }
64 }