1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
5 community::{CommunityResponse, EditCommunity, HideCommunity},
6 get_local_user_view_from_jwt,
9 use lemmy_apub::protocol::activities::community::update::UpdateCommunity;
10 use lemmy_db_schema::{
11 diesel_option_overwrite_to_url,
15 community::{Community, CommunityForm},
16 moderator::{ModHideCommunity, ModHideCommunityForm},
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};
24 #[async_trait::async_trait(?Send)]
25 impl PerformCrud for EditCommunity {
26 type Response = CommunityResponse;
28 #[tracing::instrument(skip(context, websocket_id))]
31 context: &Data<LemmyContext>,
32 websocket_id: Option<ConnectionId>,
33 ) -> Result<CommunityResponse, LemmyError> {
34 let data: &EditCommunity = self;
36 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
38 check_slurs_opt(&data.title, &context.settings().slur_regex())?;
39 check_slurs_opt(&data.description, &context.settings().slur_regex())?;
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())
48 if !mods.contains(&local_user_view.person.id) {
49 return Err(LemmyError::from_message("not_a_moderator"));
52 let community_id = data.community_id;
53 let read_community = blocking(context.pool(), move |conn| {
54 Community::read(conn, community_id)
58 let icon = diesel_option_overwrite_to_url(&data.icon)?;
59 let banner = diesel_option_overwrite_to_url(&data.banner)?;
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,
69 hidden: Some(read_community.hidden),
70 updated: Some(naive_now()),
71 ..CommunityForm::default()
74 let community_id = data.community_id;
75 let updated_community = blocking(context.pool(), move |conn| {
76 Community::update(conn, community_id, &community_form)
79 .map_err(LemmyError::from)
80 .map_err(|e| e.with_message("couldnt_update_community"))?;
82 UpdateCommunity::send(
83 updated_community.into(),
84 &local_user_view.person.into(),
89 let op = UserOperationCrud::EditCommunity;
90 send_community_ws_message(data.community_id, op, websocket_id, None, context).await
94 #[async_trait::async_trait(?Send)]
95 impl PerformCrud for HideCommunity {
96 type Response = CommunityResponse;
98 #[tracing::instrument(skip(context, websocket_id))]
101 context: &Data<LemmyContext>,
102 websocket_id: Option<ConnectionId>,
103 ) -> Result<CommunityResponse, LemmyError> {
104 let data: &HideCommunity = self;
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)?;
111 let community_id = data.community_id;
112 let read_community = blocking(context.pool(), move |conn| {
113 Community::read(conn, community_id)
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()
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),
137 let community_id = data.community_id;
138 let updated_community = blocking(context.pool(), move |conn| {
139 Community::update(conn, community_id, &community_form)
142 .map_err(LemmyError::from)
143 .map_err(|e| e.with_message("couldnt_update_community_hidden_status"))?;
145 blocking(context.pool(), move |conn| {
146 ModHideCommunity::create(conn, &mod_hide_community_form)
150 UpdateCommunity::send(
151 updated_community.into(),
152 &local_user_view.person.into(),
157 let op = UserOperationCrud::EditCommunity;
158 send_community_ws_message(data.community_id, op, websocket_id, None, context).await