]> Untitled Git - lemmy.git/blob - crates/api_crud/src/community/update.rs
Change to_apub and from_apub to take by value and avoid cloning
[lemmy.git] / crates / api_crud / src / community / update.rs
1 use crate::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::protocol::activities::community::update::UpdateCommunity;
9 use lemmy_db_schema::{
10   diesel_option_overwrite_to_url,
11   naive_now,
12   newtypes::PersonId,
13   source::community::{Community, CommunityForm},
14   traits::Crud,
15 };
16 use lemmy_db_views_actor::community_moderator_view::CommunityModeratorView;
17 use lemmy_utils::{utils::check_slurs_opt, ApiError, ConnectionId, LemmyError};
18 use lemmy_websocket::{send::send_community_ws_message, LemmyContext, UserOperationCrud};
19
20 #[async_trait::async_trait(?Send)]
21 impl PerformCrud for EditCommunity {
22   type Response = CommunityResponse;
23
24   async fn perform(
25     &self,
26     context: &Data<LemmyContext>,
27     websocket_id: Option<ConnectionId>,
28   ) -> Result<CommunityResponse, LemmyError> {
29     let data: &EditCommunity = self;
30     let local_user_view =
31       get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
32
33     check_slurs_opt(&data.title, &context.settings().slur_regex())?;
34     check_slurs_opt(&data.description, &context.settings().slur_regex())?;
35
36     // Verify its a mod (only mods can edit it)
37     let community_id = data.community_id;
38     let mods: Vec<PersonId> = blocking(context.pool(), move |conn| {
39       CommunityModeratorView::for_community(conn, community_id)
40         .map(|v| v.into_iter().map(|m| m.moderator.id).collect())
41     })
42     .await??;
43     if !mods.contains(&local_user_view.person.id) {
44       return Err(ApiError::err_plain("not_a_moderator").into());
45     }
46
47     let community_id = data.community_id;
48     let read_community = blocking(context.pool(), move |conn| {
49       Community::read(conn, community_id)
50     })
51     .await??;
52
53     let icon = diesel_option_overwrite_to_url(&data.icon)?;
54     let banner = diesel_option_overwrite_to_url(&data.banner)?;
55
56     let community_form = CommunityForm {
57       name: read_community.name,
58       title: data.title.to_owned().unwrap_or(read_community.title),
59       description: data.description.to_owned(),
60       icon,
61       banner,
62       nsfw: data.nsfw,
63       updated: Some(naive_now()),
64       ..CommunityForm::default()
65     };
66
67     let community_id = data.community_id;
68     let updated_community = blocking(context.pool(), move |conn| {
69       Community::update(conn, community_id, &community_form)
70     })
71     .await?
72     .map_err(|e| ApiError::err("couldnt_update_community", e))?;
73
74     UpdateCommunity::send(
75       updated_community.into(),
76       &local_user_view.person.into(),
77       context,
78     )
79     .await?;
80
81     let op = UserOperationCrud::EditCommunity;
82     send_community_ws_message(data.community_id, op, websocket_id, None, context).await
83   }
84 }