]> Untitled Git - lemmy.git/blob - crates/api_crud/src/community/update.rs
Merge pull request #1566 from LemmyNet/additional_search_filters
[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::{utils::check_slurs_opt, ApiError, ConnectionId, LemmyError};
20 use lemmy_websocket::{LemmyContext, UserOperationCrud};
21
22 #[async_trait::async_trait(?Send)]
23 impl PerformCrud for EditCommunity {
24   type Response = CommunityResponse;
25
26   async fn perform(
27     &self,
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?;
33
34     check_slurs_opt(&data.title)?;
35     check_slurs_opt(&data.description)?;
36
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())
42     })
43     .await??;
44     if !mods.contains(&local_user_view.person.id) {
45       return Err(ApiError::err("not_a_moderator").into());
46     }
47
48     let community_id = data.community_id;
49     let read_community = blocking(context.pool(), move |conn| {
50       Community::read(conn, community_id)
51     })
52     .await??;
53
54     let icon = diesel_option_overwrite_to_url(&data.icon)?;
55     let banner = diesel_option_overwrite_to_url(&data.banner)?;
56
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(),
61       icon,
62       banner,
63       nsfw: data.nsfw,
64       updated: Some(naive_now()),
65       ..CommunityForm::default()
66     };
67
68     let community_id = data.community_id;
69     let updated_community = blocking(context.pool(), move |conn| {
70       Community::update(conn, community_id, &community_form)
71     })
72     .await?
73     .map_err(|_| ApiError::err("couldnt_update_community"))?;
74
75     updated_community
76       .send_update(local_user_view.person.to_owned(), context)
77       .await?;
78
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))
83     })
84     .await??;
85
86     let res = CommunityResponse { community_view };
87
88     send_community_websocket(
89       &res,
90       context,
91       websocket_id,
92       UserOperationCrud::EditCommunity,
93     );
94
95     Ok(res)
96   }
97 }