]> Untitled Git - lemmy.git/blob - crates/api_crud/src/community/delete.rs
Split api crate into api_structs and api
[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, UserOperation};
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(&res, context, websocket_id, UserOperation::DeleteCommunity);
65
66     Ok(res)
67   }
68 }
69
70 #[async_trait::async_trait(?Send)]
71 impl PerformCrud for RemoveCommunity {
72   type Response = CommunityResponse;
73
74   async fn perform(
75     &self,
76     context: &Data<LemmyContext>,
77     websocket_id: Option<ConnectionId>,
78   ) -> Result<CommunityResponse, LemmyError> {
79     let data: &RemoveCommunity = &self;
80     let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
81
82     // Verify its an admin (only an admin can remove a community)
83     is_admin(&local_user_view)?;
84
85     // Do the remove
86     let community_id = data.community_id;
87     let removed = data.removed;
88     let updated_community = match blocking(context.pool(), move |conn| {
89       Community::update_removed(conn, community_id, removed)
90     })
91     .await?
92     {
93       Ok(community) => community,
94       Err(_e) => return Err(ApiError::err("couldnt_update_community").into()),
95     };
96
97     // Mod tables
98     let expires = match data.expires {
99       Some(time) => Some(naive_from_unix(time)),
100       None => None,
101     };
102     let form = ModRemoveCommunityForm {
103       mod_person_id: local_user_view.person.id,
104       community_id: data.community_id,
105       removed: Some(removed),
106       reason: data.reason.to_owned(),
107       expires,
108     };
109     blocking(context.pool(), move |conn| {
110       ModRemoveCommunity::create(conn, &form)
111     })
112     .await??;
113
114     // Apub messages
115     if removed {
116       updated_community.send_remove(context).await?;
117     } else {
118       updated_community.send_undo_remove(context).await?;
119     }
120
121     let community_id = data.community_id;
122     let person_id = local_user_view.person.id;
123     let community_view = blocking(context.pool(), move |conn| {
124       CommunityView::read(conn, community_id, Some(person_id))
125     })
126     .await??;
127
128     let res = CommunityResponse { community_view };
129
130     send_community_websocket(&res, context, websocket_id, UserOperation::RemoveCommunity);
131
132     Ok(res)
133   }
134 }