]> Untitled Git - lemmy.git/blob - crates/api_crud/src/community/update.rs
Moving settings to Database. (#2492)
[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, local_site_to_slur_regex},
6 };
7 use lemmy_apub::protocol::activities::community::update::UpdateCommunity;
8 use lemmy_db_schema::{
9   newtypes::{LanguageId, PersonId},
10   source::{
11     actor_language::{CommunityLanguage, SiteLanguage},
12     community::{Community, CommunityUpdateForm},
13     local_site::LocalSite,
14   },
15   traits::Crud,
16   utils::{diesel_option_overwrite, diesel_option_overwrite_to_url},
17 };
18 use lemmy_db_views_actor::structs::CommunityModeratorView;
19 use lemmy_utils::{error::LemmyError, utils::check_slurs_opt, ConnectionId};
20 use lemmy_websocket::{send::send_community_ws_message, LemmyContext, UserOperationCrud};
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 = blocking(context.pool(), LocalSite::read).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> = blocking(context.pool(), move |conn| {
48       CommunityModeratorView::for_community(conn, community_id)
49         .map(|v| v.into_iter().map(|m| m.moderator.id).collect())
50     })
51     .await??;
52     if !mods.contains(&local_user_view.person.id) {
53       return Err(LemmyError::from_message("not_a_moderator"));
54     }
55
56     let community_id = data.community_id;
57     if let Some(languages) = data.discussion_languages.clone() {
58       let site_languages: Vec<LanguageId> =
59         blocking(context.pool(), SiteLanguage::read_local).await??;
60       // check that community languages are a subset of site languages
61       // https://stackoverflow.com/a/64227550
62       let is_subset = languages.iter().all(|item| site_languages.contains(item));
63       if !is_subset {
64         return Err(LemmyError::from_message("language_not_allowed"));
65       }
66       blocking(context.pool(), move |conn| {
67         CommunityLanguage::update(conn, languages, community_id)
68       })
69       .await??;
70     }
71
72     let community_form = CommunityUpdateForm::builder()
73       .title(data.title.to_owned())
74       .description(description)
75       .icon(icon)
76       .banner(banner)
77       .nsfw(data.nsfw)
78       .posting_restricted_to_mods(data.posting_restricted_to_mods)
79       .build();
80
81     let community_id = data.community_id;
82     let updated_community = blocking(context.pool(), move |conn| {
83       Community::update(conn, community_id, &community_form)
84     })
85     .await?
86     .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_community"))?;
87
88     UpdateCommunity::send(
89       updated_community.into(),
90       &local_user_view.person.into(),
91       context,
92     )
93     .await?;
94
95     let op = UserOperationCrud::EditCommunity;
96     send_community_ws_message(data.community_id, op, websocket_id, None, context).await
97   }
98 }