1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4 build_response::build_community_response,
5 community::{CommunityResponse, EditCommunity},
7 utils::{local_site_to_slur_regex, local_user_view_from_jwt},
12 actor_language::{CommunityLanguage, SiteLanguage},
13 community::{Community, CommunityUpdateForm},
14 local_site::LocalSite,
17 utils::{diesel_option_overwrite, diesel_option_overwrite_to_url, naive_now},
19 use lemmy_db_views_actor::structs::CommunityModeratorView;
22 utils::{slurs::check_slurs_opt, validation::is_valid_body_field},
25 #[async_trait::async_trait(?Send)]
26 impl PerformCrud for EditCommunity {
27 type Response = CommunityResponse;
29 #[tracing::instrument(skip(context))]
30 async fn perform(&self, context: &Data<LemmyContext>) -> Result<CommunityResponse, LemmyError> {
31 let data: &EditCommunity = self;
32 let local_user_view = local_user_view_from_jwt(&data.auth, context).await?;
33 let local_site = LocalSite::read(context.pool()).await?;
35 let icon = diesel_option_overwrite_to_url(&data.icon)?;
36 let banner = diesel_option_overwrite_to_url(&data.banner)?;
37 let description = diesel_option_overwrite(&data.description);
39 let slur_regex = local_site_to_slur_regex(&local_site);
40 check_slurs_opt(&data.title, &slur_regex)?;
41 check_slurs_opt(&data.description, &slur_regex)?;
42 is_valid_body_field(&data.description)?;
44 // Verify its a mod (only mods can edit it)
45 let community_id = data.community_id;
46 let mods: Vec<PersonId> = CommunityModeratorView::for_community(context.pool(), community_id)
48 .map(|v| v.into_iter().map(|m| m.moderator.id).collect())?;
49 if !mods.contains(&local_user_view.person.id) {
50 return Err(LemmyError::from_message("not_a_moderator"));
53 let community_id = data.community_id;
54 if let Some(languages) = data.discussion_languages.clone() {
55 let site_languages = SiteLanguage::read_local_raw(context.pool()).await?;
56 // check that community languages are a subset of site languages
57 // https://stackoverflow.com/a/64227550
58 let is_subset = languages.iter().all(|item| site_languages.contains(item));
60 return Err(LemmyError::from_message("language_not_allowed"));
62 CommunityLanguage::update(context.pool(), languages, community_id).await?;
65 let community_form = CommunityUpdateForm::builder()
66 .title(data.title.clone())
67 .description(description)
71 .posting_restricted_to_mods(data.posting_restricted_to_mods)
72 .updated(Some(Some(naive_now())))
75 let community_id = data.community_id;
76 Community::update(context.pool(), community_id, &community_form)
78 .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_community"))?;
80 build_community_response(context, local_user_view, community_id).await