]> Untitled Git - lemmy.git/blob - crates/api_crud/src/community/update.rs
Add restricted community field to CreateCommunity, UpdateCommunity (ref #2235) (...
[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   check_image_has_local_domain,
6   community::{CommunityResponse, EditCommunity},
7   get_local_user_view_from_jwt,
8 };
9 use lemmy_apub::protocol::activities::community::update::UpdateCommunity;
10 use lemmy_db_schema::{
11   diesel_option_overwrite_to_url,
12   naive_now,
13   newtypes::PersonId,
14   source::community::{Community, CommunityForm},
15   traits::Crud,
16 };
17 use lemmy_db_views_actor::community_moderator_view::CommunityModeratorView;
18 use lemmy_utils::{utils::check_slurs_opt, ConnectionId, LemmyError};
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
38     check_slurs_opt(&data.title, &context.settings().slur_regex())?;
39     check_slurs_opt(&data.description, &context.settings().slur_regex())?;
40     check_image_has_local_domain(icon.as_ref().unwrap_or(&None))?;
41     check_image_has_local_domain(banner.as_ref().unwrap_or(&None))?;
42
43     // Verify its a mod (only mods can edit it)
44     let community_id = data.community_id;
45     let mods: Vec<PersonId> = blocking(context.pool(), move |conn| {
46       CommunityModeratorView::for_community(conn, community_id)
47         .map(|v| v.into_iter().map(|m| m.moderator.id).collect())
48     })
49     .await??;
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     let read_community = blocking(context.pool(), move |conn| {
56       Community::read(conn, community_id)
57     })
58     .await??;
59
60     let community_form = CommunityForm {
61       name: read_community.name,
62       title: data.title.to_owned().unwrap_or(read_community.title),
63       description: data.description.to_owned(),
64       icon,
65       banner,
66       nsfw: data.nsfw,
67       posting_restricted_to_mods: data.posting_restricted_to_mods,
68       updated: Some(naive_now()),
69       ..CommunityForm::default()
70     };
71
72     let community_id = data.community_id;
73     let updated_community = blocking(context.pool(), move |conn| {
74       Community::update(conn, community_id, &community_form)
75     })
76     .await?
77     .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_community"))?;
78
79     UpdateCommunity::send(
80       updated_community.into(),
81       &local_user_view.person.into(),
82       context,
83     )
84     .await?;
85
86     let op = UserOperationCrud::EditCommunity;
87     send_community_ws_message(data.community_id, op, websocket_id, None, context).await
88   }
89 }