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