]> Untitled Git - lemmy.git/blob - crates/api_crud/src/community/update.rs
Merge branch 'main' into mandatory_questionnaire
[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   check_image_has_local_domain,
6   community::{CommunityResponse, EditCommunity, HideCommunity},
7   get_local_user_view_from_jwt,
8   is_admin,
9 };
10 use lemmy_apub::protocol::activities::community::update::UpdateCommunity;
11 use lemmy_db_schema::{
12   diesel_option_overwrite_to_url,
13   naive_now,
14   newtypes::PersonId,
15   source::{
16     community::{Community, CommunityForm},
17     moderator::{ModHideCommunity, ModHideCommunityForm},
18   },
19   traits::Crud,
20 };
21 use lemmy_db_views_actor::community_moderator_view::CommunityModeratorView;
22 use lemmy_utils::{utils::check_slurs_opt, ConnectionId, LemmyError};
23 use lemmy_websocket::{send::send_community_ws_message, LemmyContext, UserOperationCrud};
24
25 #[async_trait::async_trait(?Send)]
26 impl PerformCrud for EditCommunity {
27   type Response = CommunityResponse;
28
29   #[tracing::instrument(skip(context, websocket_id))]
30   async fn perform(
31     &self,
32     context: &Data<LemmyContext>,
33     websocket_id: Option<ConnectionId>,
34   ) -> Result<CommunityResponse, LemmyError> {
35     let data: &EditCommunity = self;
36     let local_user_view =
37       get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
38
39     let icon = diesel_option_overwrite_to_url(&data.icon)?;
40     let banner = diesel_option_overwrite_to_url(&data.banner)?;
41
42     check_slurs_opt(&data.title, &context.settings().slur_regex())?;
43     check_slurs_opt(&data.description, &context.settings().slur_regex())?;
44     check_image_has_local_domain(icon.as_ref().unwrap_or(&None))?;
45     check_image_has_local_domain(banner.as_ref().unwrap_or(&None))?;
46
47     // Verify its a mod (only mods can edit it)
48     let community_id = data.community_id;
49     let mods: Vec<PersonId> = blocking(context.pool(), move |conn| {
50       CommunityModeratorView::for_community(conn, community_id)
51         .map(|v| v.into_iter().map(|m| m.moderator.id).collect())
52     })
53     .await??;
54     if !mods.contains(&local_user_view.person.id) {
55       return Err(LemmyError::from_message("not_a_moderator"));
56     }
57
58     let community_id = data.community_id;
59     let read_community = blocking(context.pool(), move |conn| {
60       Community::read(conn, community_id)
61     })
62     .await??;
63
64     let community_form = CommunityForm {
65       name: read_community.name,
66       title: data.title.to_owned().unwrap_or(read_community.title),
67       description: data.description.to_owned(),
68       public_key: read_community.public_key,
69       icon,
70       banner,
71       nsfw: data.nsfw,
72       hidden: Some(read_community.hidden),
73       updated: Some(naive_now()),
74       ..CommunityForm::default()
75     };
76
77     let community_id = data.community_id;
78     let updated_community = blocking(context.pool(), move |conn| {
79       Community::update(conn, community_id, &community_form)
80     })
81     .await?
82     .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_community"))?;
83
84     UpdateCommunity::send(
85       updated_community.into(),
86       &local_user_view.person.into(),
87       context,
88     )
89     .await?;
90
91     let op = UserOperationCrud::EditCommunity;
92     send_community_ws_message(data.community_id, op, websocket_id, None, context).await
93   }
94 }
95
96 #[async_trait::async_trait(?Send)]
97 impl PerformCrud for HideCommunity {
98   type Response = CommunityResponse;
99
100   #[tracing::instrument(skip(context, websocket_id))]
101   async fn perform(
102     &self,
103     context: &Data<LemmyContext>,
104     websocket_id: Option<ConnectionId>,
105   ) -> Result<CommunityResponse, LemmyError> {
106     let data: &HideCommunity = self;
107
108     // Verify its a admin (only admin can hide or unhide it)
109     let local_user_view =
110       get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
111     is_admin(&local_user_view)?;
112
113     let community_id = data.community_id;
114     let read_community = blocking(context.pool(), move |conn| {
115       Community::read(conn, community_id)
116     })
117     .await??;
118
119     let community_form = CommunityForm {
120       name: read_community.name,
121       title: read_community.title,
122       description: read_community.description.to_owned(),
123       public_key: read_community.public_key,
124       icon: Some(read_community.icon),
125       banner: Some(read_community.banner),
126       nsfw: Some(read_community.nsfw),
127       updated: Some(naive_now()),
128       hidden: Some(data.hidden),
129       ..CommunityForm::default()
130     };
131
132     let mod_hide_community_form = ModHideCommunityForm {
133       community_id: data.community_id,
134       mod_person_id: local_user_view.person.id,
135       reason: data.reason.clone(),
136       hidden: Some(data.hidden),
137     };
138
139     let community_id = data.community_id;
140     let updated_community = blocking(context.pool(), move |conn| {
141       Community::update(conn, community_id, &community_form)
142     })
143     .await?
144     .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_community_hidden_status"))?;
145
146     blocking(context.pool(), move |conn| {
147       ModHideCommunity::create(conn, &mod_hide_community_form)
148     })
149     .await??;
150
151     UpdateCommunity::send(
152       updated_community.into(),
153       &local_user_view.person.into(),
154       context,
155     )
156     .await?;
157
158     let op = UserOperationCrud::EditCommunity;
159     send_community_ws_message(data.community_id, op, websocket_id, None, context).await
160   }
161 }