]> Untitled Git - lemmy.git/blob - crates/api/src/community/hide.rs
Sanitize html (#3708)
[lemmy.git] / crates / api / src / community / hide.rs
1 use crate::Perform;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4   build_response::build_community_response,
5   community::{CommunityResponse, HideCommunity},
6   context::LemmyContext,
7   utils::{is_admin, local_user_view_from_jwt, sanitize_html_opt},
8 };
9 use lemmy_db_schema::{
10   source::{
11     community::{Community, CommunityUpdateForm},
12     moderator::{ModHideCommunity, ModHideCommunityForm},
13   },
14   traits::Crud,
15 };
16 use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
17
18 #[async_trait::async_trait(?Send)]
19 impl Perform for HideCommunity {
20   type Response = CommunityResponse;
21
22   #[tracing::instrument(skip(context))]
23   async fn perform(&self, context: &Data<LemmyContext>) -> Result<CommunityResponse, LemmyError> {
24     let data: &HideCommunity = self;
25
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)?;
29
30     let community_form = CommunityUpdateForm::builder()
31       .hidden(Some(data.hidden))
32       .build();
33
34     let mod_hide_community_form = ModHideCommunityForm {
35       community_id: data.community_id,
36       mod_person_id: local_user_view.person.id,
37       reason: sanitize_html_opt(&data.reason),
38       hidden: Some(data.hidden),
39     };
40
41     let community_id = data.community_id;
42     Community::update(&mut context.pool(), community_id, &community_form)
43       .await
44       .with_lemmy_type(LemmyErrorType::CouldntUpdateCommunityHiddenStatus)?;
45
46     ModHideCommunity::create(&mut context.pool(), &mod_hide_community_form).await?;
47
48     build_community_response(context, local_user_view, community_id).await
49   }
50 }