]> Untitled Git - lemmy.git/blob - crates/api_crud/src/community/update.rs
Merge pull request #1571 from guland2000/patch-3
[lemmy.git] / crates / api_crud / src / community / update.rs
1 use crate::{community::send_community_websocket, PerformCrud};
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4   blocking,
5   community::{CommunityResponse, EditCommunity},
6   get_local_user_view_from_jwt,
7 };
8 use lemmy_apub::CommunityType;
9 use lemmy_db_queries::{diesel_option_overwrite_to_url, Crud};
10 use lemmy_db_schema::{
11   naive_now,
12   source::community::{Community, CommunityForm},
13   PersonId,
14 };
15 use lemmy_db_views_actor::{
16   community_moderator_view::CommunityModeratorView,
17   community_view::CommunityView,
18 };
19 use lemmy_utils::{
20   utils::{check_slurs, check_slurs_opt},
21   ApiError,
22   ConnectionId,
23   LemmyError,
24 };
25 use lemmy_websocket::{LemmyContext, UserOperationCrud};
26
27 #[async_trait::async_trait(?Send)]
28 impl PerformCrud for EditCommunity {
29   type Response = CommunityResponse;
30
31   async fn perform(
32     &self,
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?;
38
39     check_slurs(&data.title)?;
40     check_slurs_opt(&data.description)?;
41
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())
47     })
48     .await??;
49     if !mods.contains(&local_user_view.person.id) {
50       return Err(ApiError::err("not_a_moderator").into());
51     }
52
53     let community_id = data.community_id;
54     let read_community = blocking(context.pool(), move |conn| {
55       Community::read(conn, community_id)
56     })
57     .await??;
58
59     let icon = diesel_option_overwrite_to_url(&data.icon)?;
60     let banner = diesel_option_overwrite_to_url(&data.banner)?;
61
62     let community_form = CommunityForm {
63       name: read_community.name,
64       title: data.title.to_owned(),
65       description: data.description.to_owned(),
66       icon,
67       banner,
68       nsfw: data.nsfw,
69       updated: Some(naive_now()),
70       ..CommunityForm::default()
71     };
72
73     let community_id = data.community_id;
74     let updated_community = blocking(context.pool(), move |conn| {
75       Community::update(conn, community_id, &community_form)
76     })
77     .await?
78     .map_err(|_| ApiError::err("couldnt_update_community"))?;
79
80     updated_community
81       .send_update(local_user_view.person.to_owned(), context)
82       .await?;
83
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))
88     })
89     .await??;
90
91     let res = CommunityResponse { community_view };
92
93     send_community_websocket(
94       &res,
95       context,
96       websocket_id,
97       UserOperationCrud::EditCommunity,
98     );
99
100     Ok(res)
101   }
102 }