1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
5 community::{CommunityResponse, EditCommunity},
6 get_local_user_view_from_jwt,
8 use lemmy_apub::protocol::activities::community::update::UpdateCommunity;
10 diesel_option_overwrite_to_url,
13 source::community::{Community, CommunityForm},
16 use lemmy_db_views_actor::community_moderator_view::CommunityModeratorView;
17 use lemmy_utils::{utils::check_slurs_opt, ConnectionId, LemmyError};
18 use lemmy_websocket::{send::send_community_ws_message, LemmyContext, UserOperationCrud};
20 #[async_trait::async_trait(?Send)]
21 impl PerformCrud for EditCommunity {
22 type Response = CommunityResponse;
24 #[tracing::instrument(skip(context, websocket_id))]
27 context: &Data<LemmyContext>,
28 websocket_id: Option<ConnectionId>,
29 ) -> Result<CommunityResponse, LemmyError> {
30 let data: &EditCommunity = self;
32 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
34 check_slurs_opt(&data.title, &context.settings().slur_regex())?;
35 check_slurs_opt(&data.description, &context.settings().slur_regex())?;
37 // Verify its a mod (only mods can edit it)
38 let community_id = data.community_id;
39 let mods: Vec<PersonId> = blocking(context.pool(), move |conn| {
40 CommunityModeratorView::for_community(conn, community_id)
41 .map(|v| v.into_iter().map(|m| m.moderator.id).collect())
44 if !mods.contains(&local_user_view.person.id) {
45 return Err(LemmyError::from_message("not_a_moderator"));
48 let community_id = data.community_id;
49 let read_community = blocking(context.pool(), move |conn| {
50 Community::read(conn, community_id)
54 let icon = diesel_option_overwrite_to_url(&data.icon)?;
55 let banner = diesel_option_overwrite_to_url(&data.banner)?;
57 let community_form = CommunityForm {
58 name: read_community.name,
59 title: data.title.to_owned().unwrap_or(read_community.title),
60 description: data.description.to_owned(),
61 public_key: read_community.public_key,
65 updated: Some(naive_now()),
66 ..CommunityForm::default()
69 let community_id = data.community_id;
70 let updated_community = blocking(context.pool(), move |conn| {
71 Community::update(conn, community_id, &community_form)
74 .map_err(LemmyError::from)
75 .map_err(|e| e.with_message("couldnt_update_community"))?;
77 UpdateCommunity::send(
78 updated_community.into(),
79 &local_user_view.person.into(),
84 let op = UserOperationCrud::EditCommunity;
85 send_community_ws_message(data.community_id, op, websocket_id, None, context).await