1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
5 check_image_has_local_domain,
6 community::{CommunityResponse, EditCommunity},
7 get_local_user_view_from_jwt,
9 use lemmy_apub::protocol::activities::community::update::UpdateCommunity;
10 use lemmy_db_schema::{
11 diesel_option_overwrite_to_url,
14 source::community::{Community, CommunityForm},
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};
21 #[async_trait::async_trait(?Send)]
22 impl PerformCrud for EditCommunity {
23 type Response = CommunityResponse;
25 #[tracing::instrument(skip(context, websocket_id))]
28 context: &Data<LemmyContext>,
29 websocket_id: Option<ConnectionId>,
30 ) -> Result<CommunityResponse, LemmyError> {
31 let data: &EditCommunity = self;
33 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
35 let icon = diesel_option_overwrite_to_url(&data.icon)?;
36 let banner = diesel_option_overwrite_to_url(&data.banner)?;
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))?;
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())
50 if !mods.contains(&local_user_view.person.id) {
51 return Err(LemmyError::from_message("not_a_moderator"));
54 let community_id = data.community_id;
55 let read_community = blocking(context.pool(), move |conn| {
56 Community::read(conn, community_id)
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(),
67 updated: Some(naive_now()),
68 ..CommunityForm::default()
71 let community_id = data.community_id;
72 let updated_community = blocking(context.pool(), move |conn| {
73 Community::update(conn, community_id, &community_form)
76 .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_community"))?;
78 UpdateCommunity::send(
79 updated_community.into(),
80 &local_user_view.person.into(),
85 let op = UserOperationCrud::EditCommunity;
86 send_community_ws_message(data.community_id, op, websocket_id, None, context).await