]> Untitled Git - lemmy.git/blobdiff - crates/api_crud/src/community/delete.rs
Make functions work with both connection and pool (#3420)
[lemmy.git] / crates / api_crud / src / community / delete.rs
index 7ce685d8ba871e4d06b91b827ce319e127267f44..d3e58d56ce22e7f029da91b009890e43dcaaeeb8 100644 (file)
@@ -1,36 +1,31 @@
 use crate::PerformCrud;
 use actix_web::web::Data;
 use lemmy_api_common::{
+  build_response::build_community_response,
   community::{CommunityResponse, DeleteCommunity},
   context::LemmyContext,
-  utils::{get_local_user_view_from_jwt, is_top_mod},
-  websocket::UserOperationCrud,
+  utils::{is_top_mod, local_user_view_from_jwt},
 };
 use lemmy_db_schema::{
   source::community::{Community, CommunityUpdateForm},
   traits::Crud,
 };
 use lemmy_db_views_actor::structs::CommunityModeratorView;
-use lemmy_utils::{error::LemmyError, ConnectionId};
+use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
 
 #[async_trait::async_trait(?Send)]
 impl PerformCrud for DeleteCommunity {
   type Response = CommunityResponse;
 
-  #[tracing::instrument(skip(context, websocket_id))]
-  async fn perform(
-    &self,
-    context: &Data<LemmyContext>,
-    websocket_id: Option<ConnectionId>,
-  ) -> Result<CommunityResponse, LemmyError> {
+  #[tracing::instrument(skip(context))]
+  async fn perform(&self, context: &Data<LemmyContext>) -> Result<CommunityResponse, LemmyError> {
     let data: &DeleteCommunity = self;
-    let local_user_view =
-      get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
+    let local_user_view = local_user_view_from_jwt(&data.auth, context).await?;
 
     // Fetch the community mods
     let community_id = data.community_id;
     let community_mods =
-      CommunityModeratorView::for_community(context.pool(), community_id).await?;
+      CommunityModeratorView::for_community(&mut context.pool(), community_id).await?;
 
     // Make sure deleter is the top mod
     is_top_mod(&local_user_view, &community_mods)?;
@@ -39,24 +34,15 @@ impl PerformCrud for DeleteCommunity {
     let community_id = data.community_id;
     let deleted = data.deleted;
     Community::update(
-      context.pool(),
+      &mut context.pool(),
       community_id,
       &CommunityUpdateForm::builder()
         .deleted(Some(deleted))
         .build(),
     )
     .await
-    .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_community"))?;
+    .with_lemmy_type(LemmyErrorType::CouldntUpdateCommunity)?;
 
-    let res = context
-      .send_community_ws_message(
-        &UserOperationCrud::DeleteCommunity,
-        data.community_id,
-        websocket_id,
-        Some(local_user_view.person.id),
-      )
-      .await?;
-
-    Ok(res)
+    build_community_response(context, local_user_view, community_id).await
   }
 }