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