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},
9 use lemmy_apub::protocol::activities::community::update::UpdateCommunity;
10 use lemmy_db_schema::{
12 community::{Community, CommunityUpdateForm},
13 moderator::{ModHideCommunity, ModHideCommunityForm},
17 use lemmy_utils::{error::LemmyError, ConnectionId};
19 #[async_trait::async_trait(?Send)]
20 impl Perform for HideCommunity {
21 type Response = CommunityResponse;
23 #[tracing::instrument(skip(context, websocket_id))]
26 context: &Data<LemmyContext>,
27 websocket_id: Option<ConnectionId>,
28 ) -> Result<CommunityResponse, LemmyError> {
29 let data: &HideCommunity = self;
31 // Verify its a admin (only admin can hide or unhide it)
33 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
34 is_admin(&local_user_view)?;
36 let community_form = CommunityUpdateForm::builder()
37 .hidden(Some(data.hidden))
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),
47 let community_id = data.community_id;
48 let updated_community = Community::update(context.pool(), community_id, &community_form)
50 .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_community_hidden_status"))?;
52 ModHideCommunity::create(context.pool(), &mod_hide_community_form).await?;
54 UpdateCommunity::send(
55 updated_community.into(),
56 &local_user_view.person.into(),
61 let op = UserOperationCrud::EditCommunity;
62 send_community_ws_message(data.community_id, op, websocket_id, None, context).await