]> Untitled Git - lemmy.git/blob - crates/api_crud/src/community/delete.rs
Use .map_err in api code (fixes #1573) (#1575)
[lemmy.git] / crates / api_crud / src / community / delete.rs
1 use crate::{community::send_community_websocket, PerformCrud};
2 use actix_web::web::Data;
3 use lemmy_api_common::{blocking, community::*, get_local_user_view_from_jwt, is_admin};
4 use lemmy_apub::CommunityType;
5 use lemmy_db_queries::{source::community::Community_, Crud};
6 use lemmy_db_schema::source::{
7   community::*,
8   moderator::{ModRemoveCommunity, ModRemoveCommunityForm},
9 };
10 use lemmy_db_views_actor::{
11   community_moderator_view::CommunityModeratorView,
12   community_view::CommunityView,
13 };
14 use lemmy_utils::{utils::naive_from_unix, ApiError, ConnectionId, LemmyError};
15 use lemmy_websocket::{LemmyContext, UserOperationCrud};
16
17 #[async_trait::async_trait(?Send)]
18 impl PerformCrud for DeleteCommunity {
19   type Response = CommunityResponse;
20
21   async fn perform(
22     &self,
23     context: &Data<LemmyContext>,
24     websocket_id: Option<ConnectionId>,
25   ) -> Result<CommunityResponse, LemmyError> {
26     let data: &DeleteCommunity = &self;
27     let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
28
29     // Fetch the community mods
30     let community_id = data.community_id;
31     let community_mods = blocking(context.pool(), move |conn| {
32       CommunityModeratorView::for_community(conn, community_id)
33     })
34     .await??;
35
36     // Make sure deleter is the top mod
37     if local_user_view.person.id != community_mods[0].moderator.id {
38       return Err(ApiError::err("no_community_edit_allowed").into());
39     }
40
41     // Do the delete
42     let community_id = data.community_id;
43     let deleted = data.deleted;
44     let updated_community = blocking(context.pool(), move |conn| {
45       Community::update_deleted(conn, community_id, deleted)
46     })
47     .await?
48     .map_err(|_| ApiError::err("couldnt_update_community"))?;
49
50     // Send apub messages
51     if deleted {
52       updated_community
53         .send_delete(local_user_view.person.to_owned(), context)
54         .await?;
55     } else {
56       updated_community
57         .send_undo_delete(local_user_view.person.to_owned(), context)
58         .await?;
59     }
60
61     let community_id = data.community_id;
62     let person_id = local_user_view.person.id;
63     let community_view = blocking(context.pool(), move |conn| {
64       CommunityView::read(conn, community_id, Some(person_id))
65     })
66     .await??;
67
68     let res = CommunityResponse { community_view };
69
70     send_community_websocket(
71       &res,
72       context,
73       websocket_id,
74       UserOperationCrud::DeleteCommunity,
75     );
76
77     Ok(res)
78   }
79 }
80
81 #[async_trait::async_trait(?Send)]
82 impl PerformCrud for RemoveCommunity {
83   type Response = CommunityResponse;
84
85   async fn perform(
86     &self,
87     context: &Data<LemmyContext>,
88     websocket_id: Option<ConnectionId>,
89   ) -> Result<CommunityResponse, LemmyError> {
90     let data: &RemoveCommunity = &self;
91     let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
92
93     // Verify its an admin (only an admin can remove a community)
94     is_admin(&local_user_view)?;
95
96     // Do the remove
97     let community_id = data.community_id;
98     let removed = data.removed;
99     let updated_community = blocking(context.pool(), move |conn| {
100       Community::update_removed(conn, community_id, removed)
101     })
102     .await?
103     .map_err(|_| ApiError::err("couldnt_update_community"))?;
104
105     // Mod tables
106     let expires = data.expires.map(naive_from_unix);
107     let form = ModRemoveCommunityForm {
108       mod_person_id: local_user_view.person.id,
109       community_id: data.community_id,
110       removed: Some(removed),
111       reason: data.reason.to_owned(),
112       expires,
113     };
114     blocking(context.pool(), move |conn| {
115       ModRemoveCommunity::create(conn, &form)
116     })
117     .await??;
118
119     // Apub messages
120     if removed {
121       updated_community.send_remove(context).await?;
122     } else {
123       updated_community.send_undo_remove(context).await?;
124     }
125
126     let community_id = data.community_id;
127     let person_id = local_user_view.person.id;
128     let community_view = blocking(context.pool(), move |conn| {
129       CommunityView::read(conn, community_id, Some(person_id))
130     })
131     .await??;
132
133     let res = CommunityResponse { community_view };
134
135     send_community_websocket(
136       &res,
137       context,
138       websocket_id,
139       UserOperationCrud::RemoveCommunity,
140     );
141
142     Ok(res)
143   }
144 }