]> Untitled Git - lemmy.git/blob - crates/api_crud/src/community/update.rs
Rework error handling (fixes #1714) (#2135)
[lemmy.git] / crates / api_crud / src / community / update.rs
1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4   blocking,
5   community::{CommunityResponse, EditCommunity, HideCommunity},
6   get_local_user_view_from_jwt,
7   is_admin,
8 };
9 use lemmy_apub::protocol::activities::community::update::UpdateCommunity;
10 use lemmy_db_schema::{
11   diesel_option_overwrite_to_url,
12   naive_now,
13   newtypes::PersonId,
14   source::{
15     community::{Community, CommunityForm},
16     moderator::{ModHideCommunity, ModHideCommunityForm},
17   },
18   traits::Crud,
19 };
20 use lemmy_db_views_actor::community_moderator_view::CommunityModeratorView;
21 use lemmy_utils::{utils::check_slurs_opt, ConnectionId, LemmyError};
22 use lemmy_websocket::{send::send_community_ws_message, LemmyContext, UserOperationCrud};
23
24 #[async_trait::async_trait(?Send)]
25 impl PerformCrud for EditCommunity {
26   type Response = CommunityResponse;
27
28   #[tracing::instrument(skip(context, websocket_id))]
29   async fn perform(
30     &self,
31     context: &Data<LemmyContext>,
32     websocket_id: Option<ConnectionId>,
33   ) -> Result<CommunityResponse, LemmyError> {
34     let data: &EditCommunity = self;
35     let local_user_view =
36       get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
37
38     check_slurs_opt(&data.title, &context.settings().slur_regex())?;
39     check_slurs_opt(&data.description, &context.settings().slur_regex())?;
40
41     // Verify its a mod (only mods can edit it)
42     let community_id = data.community_id;
43     let mods: Vec<PersonId> = blocking(context.pool(), move |conn| {
44       CommunityModeratorView::for_community(conn, community_id)
45         .map(|v| v.into_iter().map(|m| m.moderator.id).collect())
46     })
47     .await??;
48     if !mods.contains(&local_user_view.person.id) {
49       return Err(LemmyError::from_message("not_a_moderator"));
50     }
51
52     let community_id = data.community_id;
53     let read_community = blocking(context.pool(), move |conn| {
54       Community::read(conn, community_id)
55     })
56     .await??;
57
58     let icon = diesel_option_overwrite_to_url(&data.icon)?;
59     let banner = diesel_option_overwrite_to_url(&data.banner)?;
60
61     let community_form = CommunityForm {
62       name: read_community.name,
63       title: data.title.to_owned().unwrap_or(read_community.title),
64       description: data.description.to_owned(),
65       public_key: read_community.public_key,
66       icon,
67       banner,
68       nsfw: data.nsfw,
69       hidden: Some(read_community.hidden),
70       updated: Some(naive_now()),
71       ..CommunityForm::default()
72     };
73
74     let community_id = data.community_id;
75     let updated_community = blocking(context.pool(), move |conn| {
76       Community::update(conn, community_id, &community_form)
77     })
78     .await?
79     .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_community"))?;
80
81     UpdateCommunity::send(
82       updated_community.into(),
83       &local_user_view.person.into(),
84       context,
85     )
86     .await?;
87
88     let op = UserOperationCrud::EditCommunity;
89     send_community_ws_message(data.community_id, op, websocket_id, None, context).await
90   }
91 }
92
93 #[async_trait::async_trait(?Send)]
94 impl PerformCrud for HideCommunity {
95   type Response = CommunityResponse;
96
97   #[tracing::instrument(skip(context, websocket_id))]
98   async fn perform(
99     &self,
100     context: &Data<LemmyContext>,
101     websocket_id: Option<ConnectionId>,
102   ) -> Result<CommunityResponse, LemmyError> {
103     let data: &HideCommunity = self;
104
105     // Verify its a admin (only admin can hide or unhide it)
106     let local_user_view =
107       get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
108     is_admin(&local_user_view)?;
109
110     let community_id = data.community_id;
111     let read_community = blocking(context.pool(), move |conn| {
112       Community::read(conn, community_id)
113     })
114     .await??;
115
116     let community_form = CommunityForm {
117       name: read_community.name,
118       title: read_community.title,
119       description: read_community.description.to_owned(),
120       public_key: read_community.public_key,
121       icon: Some(read_community.icon),
122       banner: Some(read_community.banner),
123       nsfw: Some(read_community.nsfw),
124       updated: Some(naive_now()),
125       hidden: Some(data.hidden),
126       ..CommunityForm::default()
127     };
128
129     let mod_hide_community_form = ModHideCommunityForm {
130       community_id: data.community_id,
131       mod_person_id: local_user_view.person.id,
132       reason: data.reason.clone(),
133       hidden: Some(data.hidden),
134     };
135
136     let community_id = data.community_id;
137     let updated_community = blocking(context.pool(), move |conn| {
138       Community::update(conn, community_id, &community_form)
139     })
140     .await?
141     .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_community_hidden_status"))?;
142
143     blocking(context.pool(), move |conn| {
144       ModHideCommunity::create(conn, &mod_hide_community_form)
145     })
146     .await??;
147
148     UpdateCommunity::send(
149       updated_community.into(),
150       &local_user_view.person.into(),
151       context,
152     )
153     .await?;
154
155     let op = UserOperationCrud::EditCommunity;
156     send_community_ws_message(data.community_id, op, websocket_id, None, context).await
157   }
158 }