1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
5 check_image_has_local_domain,
6 community::{CommunityResponse, EditCommunity, HideCommunity},
7 get_local_user_view_from_jwt,
10 use lemmy_apub::protocol::activities::community::update::UpdateCommunity;
11 use lemmy_db_schema::{
12 diesel_option_overwrite_to_url,
16 community::{Community, CommunityForm},
17 moderator::{ModHideCommunity, ModHideCommunityForm},
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};
25 #[async_trait::async_trait(?Send)]
26 impl PerformCrud for EditCommunity {
27 type Response = CommunityResponse;
29 #[tracing::instrument(skip(context, websocket_id))]
32 context: &Data<LemmyContext>,
33 websocket_id: Option<ConnectionId>,
34 ) -> Result<CommunityResponse, LemmyError> {
35 let data: &EditCommunity = self;
37 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
39 let icon = diesel_option_overwrite_to_url(&data.icon)?;
40 let banner = diesel_option_overwrite_to_url(&data.banner)?;
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))?;
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())
54 if !mods.contains(&local_user_view.person.id) {
55 return Err(LemmyError::from_message("not_a_moderator"));
58 let community_id = data.community_id;
59 let read_community = blocking(context.pool(), move |conn| {
60 Community::read(conn, community_id)
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,
72 hidden: Some(read_community.hidden),
73 updated: Some(naive_now()),
74 ..CommunityForm::default()
77 let community_id = data.community_id;
78 let updated_community = blocking(context.pool(), move |conn| {
79 Community::update(conn, community_id, &community_form)
82 .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_community"))?;
84 UpdateCommunity::send(
85 updated_community.into(),
86 &local_user_view.person.into(),
91 let op = UserOperationCrud::EditCommunity;
92 send_community_ws_message(data.community_id, op, websocket_id, None, context).await
96 #[async_trait::async_trait(?Send)]
97 impl PerformCrud for HideCommunity {
98 type Response = CommunityResponse;
100 #[tracing::instrument(skip(context, websocket_id))]
103 context: &Data<LemmyContext>,
104 websocket_id: Option<ConnectionId>,
105 ) -> Result<CommunityResponse, LemmyError> {
106 let data: &HideCommunity = self;
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)?;
113 let community_id = data.community_id;
114 let read_community = blocking(context.pool(), move |conn| {
115 Community::read(conn, community_id)
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()
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),
139 let community_id = data.community_id;
140 let updated_community = blocking(context.pool(), move |conn| {
141 Community::update(conn, community_id, &community_form)
144 .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_community_hidden_status"))?;
146 blocking(context.pool(), move |conn| {
147 ModHideCommunity::create(conn, &mod_hide_community_form)
151 UpdateCommunity::send(
152 updated_community.into(),
153 &local_user_view.person.into(),
158 let op = UserOperationCrud::EditCommunity;
159 send_community_ws_message(data.community_id, op, websocket_id, None, context).await