]> Untitled Git - lemmy.git/blob - crates/api_crud/src/community/update.rs
Don't drop error context when adding a message to errors (#1958)
[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},
6   get_local_user_view_from_jwt,
7 };
8 use lemmy_apub::protocol::activities::community::update::UpdateCommunity;
9 use lemmy_db_schema::{
10   diesel_option_overwrite_to_url,
11   naive_now,
12   newtypes::PersonId,
13   source::community::{Community, CommunityForm},
14   traits::Crud,
15 };
16 use lemmy_db_views_actor::community_moderator_view::CommunityModeratorView;
17 use lemmy_utils::{utils::check_slurs_opt, ConnectionId, LemmyError};
18 use lemmy_websocket::{send::send_community_ws_message, LemmyContext, UserOperationCrud};
19
20 #[async_trait::async_trait(?Send)]
21 impl PerformCrud for EditCommunity {
22   type Response = CommunityResponse;
23
24   #[tracing::instrument(skip(context, websocket_id))]
25   async fn perform(
26     &self,
27     context: &Data<LemmyContext>,
28     websocket_id: Option<ConnectionId>,
29   ) -> Result<CommunityResponse, LemmyError> {
30     let data: &EditCommunity = self;
31     let local_user_view =
32       get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
33
34     check_slurs_opt(&data.title, &context.settings().slur_regex())?;
35     check_slurs_opt(&data.description, &context.settings().slur_regex())?;
36
37     // Verify its a mod (only mods can edit it)
38     let community_id = data.community_id;
39     let mods: Vec<PersonId> = blocking(context.pool(), move |conn| {
40       CommunityModeratorView::for_community(conn, community_id)
41         .map(|v| v.into_iter().map(|m| m.moderator.id).collect())
42     })
43     .await??;
44     if !mods.contains(&local_user_view.person.id) {
45       return Err(LemmyError::from_message("not_a_moderator"));
46     }
47
48     let community_id = data.community_id;
49     let read_community = blocking(context.pool(), move |conn| {
50       Community::read(conn, community_id)
51     })
52     .await??;
53
54     let icon = diesel_option_overwrite_to_url(&data.icon)?;
55     let banner = diesel_option_overwrite_to_url(&data.banner)?;
56
57     let community_form = CommunityForm {
58       name: read_community.name,
59       title: data.title.to_owned().unwrap_or(read_community.title),
60       description: data.description.to_owned(),
61       public_key: read_community.public_key,
62       icon,
63       banner,
64       nsfw: data.nsfw,
65       updated: Some(naive_now()),
66       ..CommunityForm::default()
67     };
68
69     let community_id = data.community_id;
70     let updated_community = blocking(context.pool(), move |conn| {
71       Community::update(conn, community_id, &community_form)
72     })
73     .await?
74     .map_err(LemmyError::from)
75     .map_err(|e| e.with_message("couldnt_update_community"))?;
76
77     UpdateCommunity::send(
78       updated_community.into(),
79       &local_user_view.person.into(),
80       context,
81     )
82     .await?;
83
84     let op = UserOperationCrud::EditCommunity;
85     send_community_ws_message(data.community_id, op, websocket_id, None, context).await
86   }
87 }