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_apub::CommunityType;
9 use lemmy_db_queries::{diesel_option_overwrite_to_url, Crud};
10 use lemmy_db_schema::{
12 source::community::{Community, CommunityForm},
15 use lemmy_db_views_actor::{
16 community_moderator_view::CommunityModeratorView,
17 community_view::CommunityView,
20 utils::{check_slurs, check_slurs_opt},
25 use lemmy_websocket::{LemmyContext, UserOperationCrud};
27 #[async_trait::async_trait(?Send)]
28 impl PerformCrud for EditCommunity {
29 type Response = CommunityResponse;
33 context: &Data<LemmyContext>,
34 websocket_id: Option<ConnectionId>,
35 ) -> Result<CommunityResponse, LemmyError> {
36 let data: &EditCommunity = &self;
37 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
39 check_slurs(&data.title)?;
40 check_slurs_opt(&data.description)?;
42 // Verify its a mod (only mods can edit it)
43 let community_id = data.community_id;
44 let mods: Vec<PersonId> = blocking(context.pool(), move |conn| {
45 CommunityModeratorView::for_community(conn, community_id)
46 .map(|v| v.into_iter().map(|m| m.moderator.id).collect())
49 if !mods.contains(&local_user_view.person.id) {
50 return Err(ApiError::err("not_a_moderator").into());
53 let community_id = data.community_id;
54 let read_community = blocking(context.pool(), move |conn| {
55 Community::read(conn, community_id)
59 let icon = diesel_option_overwrite_to_url(&data.icon)?;
60 let banner = diesel_option_overwrite_to_url(&data.banner)?;
62 let community_form = CommunityForm {
63 name: read_community.name,
64 title: data.title.to_owned(),
65 description: data.description.to_owned(),
69 updated: Some(naive_now()),
70 ..CommunityForm::default()
73 let community_id = data.community_id;
74 let updated_community = blocking(context.pool(), move |conn| {
75 Community::update(conn, community_id, &community_form)
78 .map_err(|_| ApiError::err("couldnt_update_community"))?;
81 .send_update(local_user_view.person.to_owned(), context)
84 let community_id = data.community_id;
85 let person_id = local_user_view.person.id;
86 let community_view = blocking(context.pool(), move |conn| {
87 CommunityView::read(conn, community_id, Some(person_id))
91 let res = CommunityResponse { community_view };
93 send_community_websocket(
97 UserOperationCrud::EditCommunity,