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},
7 use lemmy_apub::protocol::activities::community::update::UpdateCommunity;
9 newtypes::{LanguageId, PersonId},
11 actor_language::{CommunityLanguage, SiteLanguage},
12 community::{Community, CommunityForm},
15 utils::{diesel_option_overwrite, diesel_option_overwrite_to_url, naive_now},
17 use lemmy_db_views_actor::structs::CommunityModeratorView;
18 use lemmy_utils::{error::LemmyError, utils::check_slurs_opt, ConnectionId};
19 use lemmy_websocket::{send::send_community_ws_message, LemmyContext, UserOperationCrud};
21 #[async_trait::async_trait(?Send)]
22 impl PerformCrud for EditCommunity {
23 type Response = CommunityResponse;
25 #[tracing::instrument(skip(context, websocket_id))]
28 context: &Data<LemmyContext>,
29 websocket_id: Option<ConnectionId>,
30 ) -> Result<CommunityResponse, LemmyError> {
31 let data: &EditCommunity = self;
33 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).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 check_slurs_opt(&data.title, &context.settings().slur_regex())?;
40 check_slurs_opt(&data.description, &context.settings().slur_regex())?;
42 // Verify its a mod (only mods can edit it)
43 let community_id = data.community_id;
44 let mods: Vec<PersonId> = blocking(context.pool(), move |conn| {
45 CommunityModeratorView::for_community(conn, community_id)
46 .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: Vec<LanguageId> =
56 blocking(context.pool(), SiteLanguage::read_local).await??;
57 // check that community languages are a subset of site languages
58 // https://stackoverflow.com/a/64227550
59 let is_subset = languages.iter().all(|item| site_languages.contains(item));
61 return Err(LemmyError::from_message("language_not_allowed"));
63 blocking(context.pool(), move |conn| {
64 CommunityLanguage::update(conn, languages, community_id)
69 let read_community = blocking(context.pool(), move |conn| {
70 Community::read(conn, community_id)
74 let community_form = CommunityForm {
75 name: read_community.name,
76 title: data.title.to_owned().unwrap_or(read_community.title),
81 posting_restricted_to_mods: data.posting_restricted_to_mods,
82 updated: Some(naive_now()),
83 ..CommunityForm::default()
86 let community_id = data.community_id;
87 let updated_community = blocking(context.pool(), move |conn| {
88 Community::update(conn, community_id, &community_form)
91 .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_community"))?;
93 UpdateCommunity::send(
94 updated_community.into(),
95 &local_user_view.person.into(),
100 let op = UserOperationCrud::EditCommunity;
101 send_community_ws_message(data.community_id, op, websocket_id, None, context).await