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