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