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