2023-08-02 16:52:41 +00:00
|
|
|
use activitypub_federation::config::Data;
|
|
|
|
use actix_web::web::Json;
|
2021-03-25 19:19:40 +00:00
|
|
|
use lemmy_api_common::{
|
2023-06-06 16:27:22 +00:00
|
|
|
build_response::build_community_response,
|
2022-04-28 20:32:32 +00:00
|
|
|
community::{CommunityResponse, EditCommunity},
|
2022-11-28 14:29:33 +00:00
|
|
|
context::LemmyContext,
|
2023-08-02 16:52:41 +00:00
|
|
|
send_activity::{ActivityChannel, SendActivityData},
|
2023-10-13 13:48:18 +00:00
|
|
|
utils::{check_community_mod_action, local_site_to_slur_regex},
|
2021-03-25 19:19:40 +00:00
|
|
|
};
|
|
|
|
use lemmy_db_schema::{
|
2022-10-06 18:27:58 +00:00
|
|
|
source::{
|
|
|
|
actor_language::{CommunityLanguage, SiteLanguage},
|
2022-10-27 09:24:07 +00:00
|
|
|
community::{Community, CommunityUpdateForm},
|
|
|
|
local_site::LocalSite,
|
2022-10-06 18:27:58 +00:00
|
|
|
},
|
2021-10-16 13:33:38 +00:00
|
|
|
traits::Crud,
|
2023-02-14 21:41:22 +00:00
|
|
|
utils::{diesel_option_overwrite, diesel_option_overwrite_to_url, naive_now},
|
2021-03-25 19:19:40 +00:00
|
|
|
};
|
2023-09-21 10:42:28 +00:00
|
|
|
use lemmy_db_views::structs::LocalUserView;
|
2023-04-15 14:45:11 +00:00
|
|
|
use lemmy_utils::{
|
2023-07-10 14:50:07 +00:00
|
|
|
error::{LemmyError, LemmyErrorExt, LemmyErrorType},
|
2023-04-15 14:45:11 +00:00
|
|
|
utils::{slurs::check_slurs_opt, validation::is_valid_body_field},
|
|
|
|
};
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2023-08-02 16:52:41 +00:00
|
|
|
#[tracing::instrument(skip(context))]
|
|
|
|
pub async fn update_community(
|
|
|
|
data: Json<EditCommunity>,
|
|
|
|
context: Data<LemmyContext>,
|
2023-09-21 10:42:28 +00:00
|
|
|
local_user_view: LocalUserView,
|
2023-08-02 16:52:41 +00:00
|
|
|
) -> Result<Json<CommunityResponse>, LemmyError> {
|
|
|
|
let local_site = LocalSite::read(&mut context.pool()).await?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2023-08-02 16:52:41 +00:00
|
|
|
let slur_regex = local_site_to_slur_regex(&local_site);
|
|
|
|
check_slurs_opt(&data.title, &slur_regex)?;
|
|
|
|
check_slurs_opt(&data.description, &slur_regex)?;
|
|
|
|
is_valid_body_field(&data.description, false)?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2023-08-02 16:52:41 +00:00
|
|
|
let icon = diesel_option_overwrite_to_url(&data.icon)?;
|
|
|
|
let banner = diesel_option_overwrite_to_url(&data.banner)?;
|
2023-10-11 14:48:19 +00:00
|
|
|
let description = diesel_option_overwrite(data.description.clone());
|
2023-07-26 18:01:15 +00:00
|
|
|
|
2023-08-02 16:52:41 +00:00
|
|
|
// Verify its a mod (only mods can edit it)
|
2023-10-13 13:48:18 +00:00
|
|
|
check_community_mod_action(
|
|
|
|
&local_user_view.person,
|
|
|
|
data.community_id,
|
|
|
|
false,
|
|
|
|
&mut context.pool(),
|
|
|
|
)
|
|
|
|
.await?;
|
2023-07-26 18:01:15 +00:00
|
|
|
|
2023-08-02 16:52:41 +00:00
|
|
|
let community_id = data.community_id;
|
|
|
|
if let Some(languages) = data.discussion_languages.clone() {
|
|
|
|
let site_languages = SiteLanguage::read_local_raw(&mut context.pool()).await?;
|
|
|
|
// check that community languages are a subset of site languages
|
|
|
|
// https://stackoverflow.com/a/64227550
|
|
|
|
let is_subset = languages.iter().all(|item| site_languages.contains(item));
|
|
|
|
if !is_subset {
|
2023-08-31 13:01:08 +00:00
|
|
|
Err(LemmyErrorType::LanguageNotAllowed)?
|
2021-03-25 19:19:40 +00:00
|
|
|
}
|
2023-08-02 16:52:41 +00:00
|
|
|
CommunityLanguage::update(&mut context.pool(), languages, community_id).await?;
|
|
|
|
}
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2023-08-08 09:41:41 +00:00
|
|
|
let community_form = CommunityUpdateForm {
|
2023-10-11 14:48:19 +00:00
|
|
|
title: data.title.clone(),
|
2023-08-08 09:41:41 +00:00
|
|
|
description,
|
|
|
|
icon,
|
|
|
|
banner,
|
|
|
|
nsfw: data.nsfw,
|
|
|
|
posting_restricted_to_mods: data.posting_restricted_to_mods,
|
|
|
|
updated: Some(Some(naive_now())),
|
|
|
|
..Default::default()
|
|
|
|
};
|
2022-10-06 18:27:58 +00:00
|
|
|
|
2023-08-02 16:52:41 +00:00
|
|
|
let community_id = data.community_id;
|
|
|
|
let community = Community::update(&mut context.pool(), community_id, &community_form)
|
|
|
|
.await
|
|
|
|
.with_lemmy_type(LemmyErrorType::CouldntUpdateCommunity)?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2023-08-02 16:52:41 +00:00
|
|
|
ActivityChannel::submit_activity(
|
|
|
|
SendActivityData::UpdateCommunity(local_user_view.person.clone(), community),
|
|
|
|
&context,
|
|
|
|
)
|
|
|
|
.await?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2023-08-02 16:52:41 +00:00
|
|
|
build_community_response(&context, local_user_view, community_id).await
|
2021-03-25 19:19:40 +00:00
|
|
|
}
|