]> Untitled Git - lemmy.git/blob - crates/api_crud/src/community/update.rs
Making the chat server an actor. (#2793)
[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   context::LemmyContext,
6   utils::{get_local_user_view_from_jwt, local_site_to_slur_regex},
7   websocket::UserOperationCrud,
8 };
9 use lemmy_db_schema::{
10   newtypes::PersonId,
11   source::{
12     actor_language::{CommunityLanguage, SiteLanguage},
13     community::{Community, CommunityUpdateForm},
14     local_site::LocalSite,
15   },
16   traits::Crud,
17   utils::{diesel_option_overwrite, diesel_option_overwrite_to_url, naive_now},
18 };
19 use lemmy_db_views_actor::structs::CommunityModeratorView;
20 use lemmy_utils::{error::LemmyError, utils::slurs::check_slurs_opt, ConnectionId};
21
22 #[async_trait::async_trait(?Send)]
23 impl PerformCrud for EditCommunity {
24   type Response = CommunityResponse;
25
26   #[tracing::instrument(skip(context, websocket_id))]
27   async fn perform(
28     &self,
29     context: &Data<LemmyContext>,
30     websocket_id: Option<ConnectionId>,
31   ) -> Result<CommunityResponse, LemmyError> {
32     let data: &EditCommunity = self;
33     let local_user_view =
34       get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
35     let local_site = LocalSite::read(context.pool()).await?;
36
37     let icon = diesel_option_overwrite_to_url(&data.icon)?;
38     let banner = diesel_option_overwrite_to_url(&data.banner)?;
39     let description = diesel_option_overwrite(&data.description);
40
41     let slur_regex = local_site_to_slur_regex(&local_site);
42     check_slurs_opt(&data.title, &slur_regex)?;
43     check_slurs_opt(&data.description, &slur_regex)?;
44
45     // Verify its a mod (only mods can edit it)
46     let community_id = data.community_id;
47     let mods: Vec<PersonId> = CommunityModeratorView::for_community(context.pool(), community_id)
48       .await
49       .map(|v| v.into_iter().map(|m| m.moderator.id).collect())?;
50     if !mods.contains(&local_user_view.person.id) {
51       return Err(LemmyError::from_message("not_a_moderator"));
52     }
53
54     let community_id = data.community_id;
55     if let Some(languages) = data.discussion_languages.clone() {
56       let site_languages = SiteLanguage::read_local_raw(context.pool()).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));
60       if !is_subset {
61         return Err(LemmyError::from_message("language_not_allowed"));
62       }
63       CommunityLanguage::update(context.pool(), languages, community_id).await?;
64     }
65
66     let community_form = CommunityUpdateForm::builder()
67       .title(data.title.clone())
68       .description(description)
69       .icon(icon)
70       .banner(banner)
71       .nsfw(data.nsfw)
72       .posting_restricted_to_mods(data.posting_restricted_to_mods)
73       .updated(Some(Some(naive_now())))
74       .build();
75
76     let community_id = data.community_id;
77     Community::update(context.pool(), community_id, &community_form)
78       .await
79       .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_community"))?;
80
81     context
82       .send_community_ws_message(
83         &UserOperationCrud::EditCommunity,
84         data.community_id,
85         websocket_id,
86         None,
87       )
88       .await
89   }
90 }