2 use actix_web::web::Data;
3 use lemmy_api_common::{
4 build_response::build_community_response,
5 community::{CommunityResponse, HideCommunity},
7 utils::{is_admin, local_user_view_from_jwt},
11 community::{Community, CommunityUpdateForm},
12 moderator::{ModHideCommunity, ModHideCommunityForm},
16 use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
18 #[async_trait::async_trait(?Send)]
19 impl Perform for HideCommunity {
20 type Response = CommunityResponse;
22 #[tracing::instrument(skip(context))]
23 async fn perform(&self, context: &Data<LemmyContext>) -> Result<CommunityResponse, LemmyError> {
24 let data: &HideCommunity = self;
26 // Verify its a admin (only admin can hide or unhide it)
27 let local_user_view = local_user_view_from_jwt(&data.auth, context).await?;
28 is_admin(&local_user_view)?;
30 let community_form = CommunityUpdateForm::builder()
31 .hidden(Some(data.hidden))
34 let mod_hide_community_form = ModHideCommunityForm {
35 community_id: data.community_id,
36 mod_person_id: local_user_view.person.id,
37 reason: data.reason.clone(),
38 hidden: Some(data.hidden),
41 let community_id = data.community_id;
42 Community::update(&mut context.pool(), community_id, &community_form)
44 .with_lemmy_type(LemmyErrorType::CouldntUpdateCommunityHiddenStatus)?;
46 ModHideCommunity::create(&mut context.pool(), &mod_hide_community_form).await?;
48 build_community_response(context, local_user_view, community_id).await