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, UserOperationCrud};
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(),
68 updated: Some(naive_now()),
69 ..CommunityForm::default()
72 let community_id = data.community_id;
73 match blocking(context.pool(), move |conn| {
74 Community::update(conn, community_id, &community_form)
78 Ok(community) => community,
79 Err(_e) => return Err(ApiError::err("couldnt_update_community").into()),
82 // TODO there needs to be some kind of an apub update
83 // process for communities and users
85 let community_id = data.community_id;
86 let person_id = local_user_view.person.id;
87 let community_view = blocking(context.pool(), move |conn| {
88 CommunityView::read(conn, community_id, Some(person_id))
92 let res = CommunityResponse { community_view };
94 send_community_websocket(
98 UserOperationCrud::EditCommunity,