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,
19 use lemmy_utils::{utils::check_slurs_opt, ApiError, ConnectionId, LemmyError};
20 use lemmy_websocket::{LemmyContext, UserOperationCrud};
22 #[async_trait::async_trait(?Send)]
23 impl PerformCrud for EditCommunity {
24 type Response = CommunityResponse;
28 context: &Data<LemmyContext>,
29 websocket_id: Option<ConnectionId>,
30 ) -> Result<CommunityResponse, LemmyError> {
31 let data: &EditCommunity = self;
32 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
34 check_slurs_opt(&data.title)?;
35 check_slurs_opt(&data.description)?;
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(ApiError::err("not_a_moderator").into());
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(),
64 updated: Some(naive_now()),
65 ..CommunityForm::default()
68 let community_id = data.community_id;
69 let updated_community = blocking(context.pool(), move |conn| {
70 Community::update(conn, community_id, &community_form)
73 .map_err(|_| ApiError::err("couldnt_update_community"))?;
76 .send_update(local_user_view.person.to_owned(), context)
79 let community_id = data.community_id;
80 let person_id = local_user_view.person.id;
81 let community_view = blocking(context.pool(), move |conn| {
82 CommunityView::read(conn, community_id, Some(person_id))
86 let res = CommunityResponse { community_view };
88 send_community_websocket(
92 UserOperationCrud::EditCommunity,