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},
7 use lemmy_apub::protocol::activities::community::update::UpdateCommunity;
10 source::community::{Community, CommunityForm},
12 utils::{diesel_option_overwrite_to_url, naive_now},
14 use lemmy_db_views_actor::structs::CommunityModeratorView;
15 use lemmy_utils::{utils::check_slurs_opt, ConnectionId, LemmyError};
16 use lemmy_websocket::{send::send_community_ws_message, LemmyContext, UserOperationCrud};
18 #[async_trait::async_trait(?Send)]
19 impl PerformCrud for EditCommunity {
20 type Response = CommunityResponse;
22 #[tracing::instrument(skip(context, websocket_id))]
25 context: &Data<LemmyContext>,
26 websocket_id: Option<ConnectionId>,
27 ) -> Result<CommunityResponse, LemmyError> {
28 let data: &EditCommunity = self;
30 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
32 let icon = diesel_option_overwrite_to_url(&data.icon)?;
33 let banner = diesel_option_overwrite_to_url(&data.banner)?;
35 check_slurs_opt(&data.title, &context.settings().slur_regex())?;
36 check_slurs_opt(&data.description, &context.settings().slur_regex())?;
38 // Verify its a mod (only mods can edit it)
39 let community_id = data.community_id;
40 let mods: Vec<PersonId> = blocking(context.pool(), move |conn| {
41 CommunityModeratorView::for_community(conn, community_id)
42 .map(|v| v.into_iter().map(|m| m.moderator.id).collect())
45 if !mods.contains(&local_user_view.person.id) {
46 return Err(LemmyError::from_message("not_a_moderator"));
49 let community_id = data.community_id;
50 let read_community = blocking(context.pool(), move |conn| {
51 Community::read(conn, community_id)
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(),
62 posting_restricted_to_mods: data.posting_restricted_to_mods,
63 updated: Some(naive_now()),
64 ..CommunityForm::default()
67 let community_id = data.community_id;
68 let updated_community = blocking(context.pool(), move |conn| {
69 Community::update(conn, community_id, &community_form)
72 .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_community"))?;
74 UpdateCommunity::send(
75 updated_community.into(),
76 &local_user_view.person.into(),
81 let op = UserOperationCrud::EditCommunity;
82 send_community_ws_message(data.community_id, op, websocket_id, None, context).await