1 use crate::{community::send_community_websocket, 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_db_queries::{diesel_option_overwrite_to_url, Crud};
11 source::community::{Community, CommunityForm},
14 use lemmy_db_views_actor::{
15 community_moderator_view::CommunityModeratorView,
16 community_view::CommunityView,
19 utils::{check_slurs, check_slurs_opt},
24 use lemmy_websocket::{LemmyContext, UserOperation};
26 #[async_trait::async_trait(?Send)]
27 impl PerformCrud for EditCommunity {
28 type Response = CommunityResponse;
32 context: &Data<LemmyContext>,
33 websocket_id: Option<ConnectionId>,
34 ) -> Result<CommunityResponse, LemmyError> {
35 let data: &EditCommunity = &self;
36 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
38 check_slurs(&data.title)?;
39 check_slurs_opt(&data.description)?;
41 // Verify its a mod (only mods can edit it)
42 let community_id = data.community_id;
43 let mods: Vec<PersonId> = blocking(context.pool(), move |conn| {
44 CommunityModeratorView::for_community(conn, community_id)
45 .map(|v| v.into_iter().map(|m| m.moderator.id).collect())
48 if !mods.contains(&local_user_view.person.id) {
49 return Err(ApiError::err("not_a_moderator").into());
52 let community_id = data.community_id;
53 let read_community = blocking(context.pool(), move |conn| {
54 Community::read(conn, community_id)
58 let icon = diesel_option_overwrite_to_url(&data.icon)?;
59 let banner = diesel_option_overwrite_to_url(&data.banner)?;
61 let community_form = CommunityForm {
62 name: read_community.name,
63 title: data.title.to_owned(),
64 description: data.description.to_owned(),
67 creator_id: read_community.creator_id,
68 removed: Some(read_community.removed),
69 deleted: Some(read_community.deleted),
71 updated: Some(naive_now()),
72 actor_id: Some(read_community.actor_id),
73 local: read_community.local,
74 private_key: read_community.private_key,
75 public_key: read_community.public_key,
76 last_refreshed_at: None,
80 shared_inbox_url: None,
83 let community_id = data.community_id;
84 match blocking(context.pool(), move |conn| {
85 Community::update(conn, community_id, &community_form)
89 Ok(community) => community,
90 Err(_e) => return Err(ApiError::err("couldnt_update_community").into()),
93 // TODO there needs to be some kind of an apub update
94 // process for communities and users
96 let community_id = data.community_id;
97 let person_id = local_user_view.person.id;
98 let community_view = blocking(context.pool(), move |conn| {
99 CommunityView::read(conn, community_id, Some(person_id))
103 let res = CommunityResponse { community_view };
105 send_community_websocket(&res, context, websocket_id, UserOperation::EditCommunity);