]> Untitled Git - lemmy.git/blob - crates/api_crud/src/community/update.rs
Merge branch 'remove_settings_and_secret_singletons_squashed'
[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   blocking,
5   community::{CommunityResponse, EditCommunity},
6   get_local_user_view_from_jwt,
7 };
8 use lemmy_apub::activities::community::update::UpdateCommunity;
9 use lemmy_db_queries::{diesel_option_overwrite_to_url, Crud};
10 use lemmy_db_schema::{
11   naive_now,
12   source::community::{Community, CommunityForm},
13   PersonId,
14 };
15 use lemmy_db_views_actor::community_moderator_view::CommunityModeratorView;
16 use lemmy_utils::{utils::check_slurs_opt, ApiError, ConnectionId, LemmyError};
17 use lemmy_websocket::{send::send_community_ws_message, LemmyContext, UserOperationCrud};
18
19 #[async_trait::async_trait(?Send)]
20 impl PerformCrud for EditCommunity {
21   type Response = CommunityResponse;
22
23   async fn perform(
24     &self,
25     context: &Data<LemmyContext>,
26     websocket_id: Option<ConnectionId>,
27   ) -> Result<CommunityResponse, LemmyError> {
28     let data: &EditCommunity = self;
29     let local_user_view =
30       get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
31
32     check_slurs_opt(&data.title, &context.settings().slur_regex())?;
33     check_slurs_opt(&data.description, &context.settings().slur_regex())?;
34
35     // Verify its a mod (only mods can edit it)
36     let community_id = data.community_id;
37     let mods: Vec<PersonId> = blocking(context.pool(), move |conn| {
38       CommunityModeratorView::for_community(conn, community_id)
39         .map(|v| v.into_iter().map(|m| m.moderator.id).collect())
40     })
41     .await??;
42     if !mods.contains(&local_user_view.person.id) {
43       return Err(ApiError::err("not_a_moderator").into());
44     }
45
46     let community_id = data.community_id;
47     let read_community = blocking(context.pool(), move |conn| {
48       Community::read(conn, community_id)
49     })
50     .await??;
51
52     let icon = diesel_option_overwrite_to_url(&data.icon)?;
53     let banner = diesel_option_overwrite_to_url(&data.banner)?;
54
55     let community_form = CommunityForm {
56       name: read_community.name,
57       title: data.title.to_owned().unwrap_or(read_community.title),
58       description: data.description.to_owned(),
59       icon,
60       banner,
61       nsfw: data.nsfw,
62       updated: Some(naive_now()),
63       ..CommunityForm::default()
64     };
65
66     let community_id = data.community_id;
67     let updated_community = blocking(context.pool(), move |conn| {
68       Community::update(conn, community_id, &community_form)
69     })
70     .await?
71     .map_err(|_| ApiError::err("couldnt_update_community"))?;
72
73     UpdateCommunity::send(&updated_community, &local_user_view.person, context).await?;
74
75     let op = UserOperationCrud::EditCommunity;
76     send_community_ws_message(data.community_id, op, websocket_id, None, context).await
77   }
78 }