]> Untitled Git - lemmy.git/blob - crates/api_crud/src/community/update.rs
implement language tags for site/community in db and api (#2434)
[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},
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, CommunityForm},
13   },
14   traits::Crud,
15   utils::{diesel_option_overwrite, diesel_option_overwrite_to_url, naive_now},
16 };
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};
20
21 #[async_trait::async_trait(?Send)]
22 impl PerformCrud for EditCommunity {
23   type Response = CommunityResponse;
24
25   #[tracing::instrument(skip(context, websocket_id))]
26   async fn perform(
27     &self,
28     context: &Data<LemmyContext>,
29     websocket_id: Option<ConnectionId>,
30   ) -> Result<CommunityResponse, LemmyError> {
31     let data: &EditCommunity = self;
32     let local_user_view =
33       get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
34
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);
38
39     check_slurs_opt(&data.title, &context.settings().slur_regex())?;
40     check_slurs_opt(&data.description, &context.settings().slur_regex())?;
41
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())
47     })
48     .await??;
49     if !mods.contains(&local_user_view.person.id) {
50       return Err(LemmyError::from_message("not_a_moderator"));
51     }
52
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));
60       if !is_subset {
61         return Err(LemmyError::from_message("language_not_allowed"));
62       }
63       blocking(context.pool(), move |conn| {
64         CommunityLanguage::update(conn, languages, community_id)
65       })
66       .await??;
67     }
68
69     let read_community = blocking(context.pool(), move |conn| {
70       Community::read(conn, community_id)
71     })
72     .await??;
73
74     let community_form = CommunityForm {
75       name: read_community.name,
76       title: data.title.to_owned().unwrap_or(read_community.title),
77       description,
78       icon,
79       banner,
80       nsfw: data.nsfw,
81       posting_restricted_to_mods: data.posting_restricted_to_mods,
82       updated: Some(naive_now()),
83       ..CommunityForm::default()
84     };
85
86     let community_id = data.community_id;
87     let updated_community = blocking(context.pool(), move |conn| {
88       Community::update(conn, community_id, &community_form)
89     })
90     .await?
91     .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_community"))?;
92
93     UpdateCommunity::send(
94       updated_community.into(),
95       &local_user_view.person.into(),
96       context,
97     )
98     .await?;
99
100     let op = UserOperationCrud::EditCommunity;
101     send_community_ws_message(data.community_id, op, websocket_id, None, context).await
102   }
103 }