]> Untitled Git - lemmy.git/blob - crates/api_crud/src/community/delete.rs
62fb0f38fa2224f4a4d3a71c03b5861a3115571d
[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, DeleteableOrRemoveable};
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         .blank_out_deleted_or_removed_info()
54         .send_delete(local_user_view.person.to_owned(), context)
55         .await?;
56     } else {
57       updated_community
58         .send_undo_delete(local_user_view.person.to_owned(), context)
59         .await?;
60     }
61
62     let community_id = data.community_id;
63     let person_id = local_user_view.person.id;
64     let mut community_view = blocking(context.pool(), move |conn| {
65       CommunityView::read(conn, community_id, Some(person_id))
66     })
67     .await??;
68
69     // Blank out deleted or removed info
70     if deleted {
71       community_view.community = community_view.community.blank_out_deleted_or_removed_info();
72     }
73
74     let res = CommunityResponse { community_view };
75
76     send_community_websocket(
77       &res,
78       context,
79       websocket_id,
80       UserOperationCrud::DeleteCommunity,
81     );
82
83     Ok(res)
84   }
85 }
86
87 #[async_trait::async_trait(?Send)]
88 impl PerformCrud for RemoveCommunity {
89   type Response = CommunityResponse;
90
91   async fn perform(
92     &self,
93     context: &Data<LemmyContext>,
94     websocket_id: Option<ConnectionId>,
95   ) -> Result<CommunityResponse, LemmyError> {
96     let data: &RemoveCommunity = self;
97     let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
98
99     // Verify its an admin (only an admin can remove a community)
100     is_admin(&local_user_view)?;
101
102     // Do the remove
103     let community_id = data.community_id;
104     let removed = data.removed;
105     let updated_community = blocking(context.pool(), move |conn| {
106       Community::update_removed(conn, community_id, removed)
107     })
108     .await?
109     .map_err(|_| ApiError::err("couldnt_update_community"))?;
110
111     // Mod tables
112     let expires = data.expires.map(naive_from_unix);
113     let form = ModRemoveCommunityForm {
114       mod_person_id: local_user_view.person.id,
115       community_id: data.community_id,
116       removed: Some(removed),
117       reason: data.reason.to_owned(),
118       expires,
119     };
120     blocking(context.pool(), move |conn| {
121       ModRemoveCommunity::create(conn, &form)
122     })
123     .await??;
124
125     // Apub messages
126     if removed {
127       updated_community
128         .blank_out_deleted_or_removed_info()
129         .send_remove(context)
130         .await?;
131     } else {
132       updated_community.send_undo_remove(context).await?;
133     }
134
135     let community_id = data.community_id;
136     let person_id = local_user_view.person.id;
137     let mut community_view = blocking(context.pool(), move |conn| {
138       CommunityView::read(conn, community_id, Some(person_id))
139     })
140     .await??;
141
142     // Blank out deleted or removed info
143     if removed {
144       community_view.community = community_view.community.blank_out_deleted_or_removed_info();
145     }
146
147     let res = CommunityResponse { community_view };
148
149     send_community_websocket(
150       &res,
151       context,
152       websocket_id,
153       UserOperationCrud::RemoveCommunity,
154     );
155
156     Ok(res)
157   }
158 }