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(|e| LemmyError::from_error_message(e, "couldnt_update_community"))?;
81 UpdateCommunity::send(
82 updated_community.into(),
83 &local_user_view.person.into(),
88 let op = UserOperationCrud::EditCommunity;
89 send_community_ws_message(data.community_id, op, websocket_id, None, context).await
93 #[async_trait::async_trait(?Send)]
94 impl PerformCrud for HideCommunity {
95 type Response = CommunityResponse;
97 #[tracing::instrument(skip(context, websocket_id))]
100 context: &Data<LemmyContext>,
101 websocket_id: Option<ConnectionId>,
102 ) -> Result<CommunityResponse, LemmyError> {
103 let data: &HideCommunity = self;
105 // Verify its a admin (only admin can hide or unhide it)
106 let local_user_view =
107 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
108 is_admin(&local_user_view)?;
110 let community_id = data.community_id;
111 let read_community = blocking(context.pool(), move |conn| {
112 Community::read(conn, community_id)
116 let community_form = CommunityForm {
117 name: read_community.name,
118 title: read_community.title,
119 description: read_community.description.to_owned(),
120 public_key: read_community.public_key,
121 icon: Some(read_community.icon),
122 banner: Some(read_community.banner),
123 nsfw: Some(read_community.nsfw),
124 updated: Some(naive_now()),
125 hidden: Some(data.hidden),
126 ..CommunityForm::default()
129 let mod_hide_community_form = ModHideCommunityForm {
130 community_id: data.community_id,
131 mod_person_id: local_user_view.person.id,
132 reason: data.reason.clone(),
133 hidden: Some(data.hidden),
136 let community_id = data.community_id;
137 let updated_community = blocking(context.pool(), move |conn| {
138 Community::update(conn, community_id, &community_form)
141 .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_community_hidden_status"))?;
143 blocking(context.pool(), move |conn| {
144 ModHideCommunity::create(conn, &mod_hide_community_form)
148 UpdateCommunity::send(
149 updated_community.into(),
150 &local_user_view.person.into(),
155 let op = UserOperationCrud::EditCommunity;
156 send_community_ws_message(data.community_id, op, websocket_id, None, context).await