2023-01-05 01:42:30 +00:00
|
|
|
use crate::{site::check_application_question, PerformCrud};
|
2021-03-25 19:19:40 +00:00
|
|
|
use actix_web::web::Data;
|
|
|
|
use lemmy_api_common::{
|
2022-11-28 14:29:33 +00:00
|
|
|
context::LemmyContext,
|
2021-03-25 19:19:40 +00:00
|
|
|
site::{EditSite, SiteResponse},
|
2022-10-27 09:24:07 +00:00
|
|
|
utils::{
|
|
|
|
is_admin,
|
2022-11-16 19:06:22 +00:00
|
|
|
local_site_rate_limit_to_rate_limit_config,
|
2022-10-27 09:24:07 +00:00
|
|
|
local_site_to_slur_regex,
|
2023-05-25 14:50:07 +00:00
|
|
|
local_user_view_from_jwt,
|
2022-10-27 09:24:07 +00:00
|
|
|
site_description_length_check,
|
|
|
|
},
|
2022-12-09 15:31:47 +00:00
|
|
|
websocket::UserOperationCrud,
|
2021-04-07 11:40:35 +00:00
|
|
|
};
|
2021-10-16 13:33:38 +00:00
|
|
|
use lemmy_db_schema::{
|
2021-12-15 19:49:59 +00:00
|
|
|
source::{
|
2022-10-06 18:27:58 +00:00
|
|
|
actor_language::SiteLanguage,
|
2022-10-27 09:24:07 +00:00
|
|
|
federation_allowlist::FederationAllowList,
|
|
|
|
federation_blocklist::FederationBlockList,
|
2023-04-17 19:19:51 +00:00
|
|
|
local_site::{LocalSite, LocalSiteUpdateForm},
|
2022-10-27 09:24:07 +00:00
|
|
|
local_site_rate_limit::{LocalSiteRateLimit, LocalSiteRateLimitUpdateForm},
|
2021-12-15 19:49:59 +00:00
|
|
|
local_user::LocalUser,
|
2022-10-27 09:24:07 +00:00
|
|
|
site::{Site, SiteUpdateForm},
|
2022-11-19 14:48:29 +00:00
|
|
|
tagline::Tagline,
|
2021-12-15 19:49:59 +00:00
|
|
|
},
|
2021-10-16 13:33:38 +00:00
|
|
|
traits::Crud,
|
2022-05-06 20:55:07 +00:00
|
|
|
utils::{diesel_option_overwrite, diesel_option_overwrite_to_url, naive_now},
|
|
|
|
ListingType,
|
2023-04-17 19:19:51 +00:00
|
|
|
RegistrationMode,
|
2021-03-25 19:19:40 +00:00
|
|
|
};
|
2022-05-03 17:44:13 +00:00
|
|
|
use lemmy_db_views::structs::SiteView;
|
2023-04-15 14:45:11 +00:00
|
|
|
use lemmy_utils::{
|
|
|
|
error::LemmyError,
|
|
|
|
utils::{slurs::check_slurs_opt, validation::is_valid_body_field},
|
|
|
|
ConnectionId,
|
|
|
|
};
|
2021-03-25 19:19:40 +00:00
|
|
|
|
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
impl PerformCrud for EditSite {
|
|
|
|
type Response = SiteResponse;
|
2021-12-06 14:54:47 +00:00
|
|
|
|
|
|
|
#[tracing::instrument(skip(context, websocket_id))]
|
2021-03-25 19:19:40 +00:00
|
|
|
async fn perform(
|
|
|
|
&self,
|
|
|
|
context: &Data<LemmyContext>,
|
|
|
|
websocket_id: Option<ConnectionId>,
|
|
|
|
) -> Result<SiteResponse, LemmyError> {
|
2021-07-05 16:07:26 +00:00
|
|
|
let data: &EditSite = self;
|
2023-05-25 14:50:07 +00:00
|
|
|
let local_user_view = local_user_view_from_jwt(&data.auth, context).await?;
|
2023-02-22 01:22:54 +00:00
|
|
|
let site_view = SiteView::read_local(context.pool()).await?;
|
|
|
|
let local_site = site_view.local_site;
|
|
|
|
let site = site_view.site;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
|
|
|
// Make sure user is an admin
|
|
|
|
is_admin(&local_user_view)?;
|
|
|
|
|
2022-10-27 09:24:07 +00:00
|
|
|
let slur_regex = local_site_to_slur_regex(&local_site);
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2022-10-27 09:24:07 +00:00
|
|
|
check_slurs_opt(&data.name, &slur_regex)?;
|
|
|
|
check_slurs_opt(&data.description, &slur_regex)?;
|
2022-04-04 20:23:18 +00:00
|
|
|
|
2022-10-27 09:24:07 +00:00
|
|
|
if let Some(desc) = &data.description {
|
2021-04-07 11:40:35 +00:00
|
|
|
site_description_length_check(desc)?;
|
|
|
|
}
|
|
|
|
|
2023-04-15 14:45:11 +00:00
|
|
|
is_valid_body_field(&data.sidebar)?;
|
|
|
|
|
2022-10-27 09:24:07 +00:00
|
|
|
let application_question = diesel_option_overwrite(&data.application_question);
|
2022-12-23 01:31:39 +00:00
|
|
|
check_application_question(
|
|
|
|
&application_question,
|
2023-01-05 01:42:30 +00:00
|
|
|
data
|
|
|
|
.registration_mode
|
|
|
|
.unwrap_or(local_site.registration_mode),
|
2022-12-23 01:31:39 +00:00
|
|
|
)?;
|
2022-04-04 20:23:18 +00:00
|
|
|
|
2023-04-17 19:19:51 +00:00
|
|
|
if let Some(listing_type) = &data.default_post_listing_type {
|
2022-04-19 19:05:08 +00:00
|
|
|
// only allow all or local as default listing types
|
2023-04-17 19:19:51 +00:00
|
|
|
if listing_type != &ListingType::All && listing_type != &ListingType::Local {
|
2022-04-19 19:05:08 +00:00
|
|
|
return Err(LemmyError::from_message(
|
|
|
|
"invalid_default_post_listing_type",
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-06 18:27:58 +00:00
|
|
|
if let Some(discussion_languages) = data.discussion_languages.clone() {
|
2022-11-09 10:05:00 +00:00
|
|
|
SiteLanguage::update(context.pool(), discussion_languages.clone(), &site).await?;
|
2022-10-06 18:27:58 +00:00
|
|
|
}
|
|
|
|
|
2022-11-19 04:33:54 +00:00
|
|
|
let name = data.name.clone();
|
2022-10-27 09:24:07 +00:00
|
|
|
let site_form = SiteUpdateForm::builder()
|
|
|
|
.name(name)
|
|
|
|
.sidebar(diesel_option_overwrite(&data.sidebar))
|
|
|
|
.description(diesel_option_overwrite(&data.description))
|
|
|
|
.icon(diesel_option_overwrite_to_url(&data.icon)?)
|
|
|
|
.banner(diesel_option_overwrite_to_url(&data.banner)?)
|
|
|
|
.updated(Some(Some(naive_now())))
|
|
|
|
.build();
|
|
|
|
|
2023-02-22 01:22:54 +00:00
|
|
|
Site::update(context.pool(), site.id, &site_form)
|
2022-11-09 10:05:00 +00:00
|
|
|
.await
|
|
|
|
// Ignore errors for all these, so as to not throw errors if no update occurs
|
|
|
|
// Diesel will throw an error for empty update forms
|
|
|
|
.ok();
|
2022-10-27 09:24:07 +00:00
|
|
|
|
|
|
|
let local_site_form = LocalSiteUpdateForm::builder()
|
|
|
|
.enable_downvotes(data.enable_downvotes)
|
2023-01-05 01:42:30 +00:00
|
|
|
.registration_mode(data.registration_mode)
|
2022-10-27 09:24:07 +00:00
|
|
|
.enable_nsfw(data.enable_nsfw)
|
|
|
|
.community_creation_admin_only(data.community_creation_admin_only)
|
|
|
|
.require_email_verification(data.require_email_verification)
|
|
|
|
.application_question(application_question)
|
|
|
|
.private_instance(data.private_instance)
|
|
|
|
.default_theme(data.default_theme.clone())
|
2023-04-17 19:19:51 +00:00
|
|
|
.default_post_listing_type(data.default_post_listing_type)
|
2022-10-27 09:24:07 +00:00
|
|
|
.legal_information(diesel_option_overwrite(&data.legal_information))
|
|
|
|
.application_email_admins(data.application_email_admins)
|
|
|
|
.hide_modlog_mod_names(data.hide_modlog_mod_names)
|
|
|
|
.updated(Some(Some(naive_now())))
|
|
|
|
.slur_filter_regex(diesel_option_overwrite(&data.slur_filter_regex))
|
|
|
|
.actor_name_max_length(data.actor_name_max_length)
|
|
|
|
.federation_enabled(data.federation_enabled)
|
|
|
|
.federation_debug(data.federation_debug)
|
|
|
|
.federation_worker_count(data.federation_worker_count)
|
|
|
|
.captcha_enabled(data.captcha_enabled)
|
2022-11-19 04:33:54 +00:00
|
|
|
.captcha_difficulty(data.captcha_difficulty.clone())
|
2023-02-14 15:57:08 +00:00
|
|
|
.reports_email_admins(data.reports_email_admins)
|
2022-10-27 09:24:07 +00:00
|
|
|
.build();
|
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
let update_local_site = LocalSite::update(context.pool(), &local_site_form)
|
|
|
|
.await
|
|
|
|
.ok();
|
2022-10-27 09:24:07 +00:00
|
|
|
|
|
|
|
let local_site_rate_limit_form = LocalSiteRateLimitUpdateForm::builder()
|
|
|
|
.message(data.rate_limit_message)
|
|
|
|
.message_per_second(data.rate_limit_message_per_second)
|
|
|
|
.post(data.rate_limit_post)
|
|
|
|
.post_per_second(data.rate_limit_post_per_second)
|
|
|
|
.register(data.rate_limit_register)
|
|
|
|
.register_per_second(data.rate_limit_register_per_second)
|
|
|
|
.image(data.rate_limit_image)
|
|
|
|
.image_per_second(data.rate_limit_image_per_second)
|
|
|
|
.comment(data.rate_limit_comment)
|
|
|
|
.comment_per_second(data.rate_limit_comment_per_second)
|
|
|
|
.search(data.rate_limit_search)
|
|
|
|
.search_per_second(data.rate_limit_search_per_second)
|
|
|
|
.build();
|
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
LocalSiteRateLimit::update(context.pool(), &local_site_rate_limit_form)
|
|
|
|
.await
|
|
|
|
.ok();
|
2022-10-27 09:24:07 +00:00
|
|
|
|
|
|
|
// Replace the blocked and allowed instances
|
2022-11-19 04:33:54 +00:00
|
|
|
let allowed = data.allowed_instances.clone();
|
2022-11-09 10:05:00 +00:00
|
|
|
FederationAllowList::replace(context.pool(), allowed).await?;
|
2022-11-19 04:33:54 +00:00
|
|
|
let blocked = data.blocked_instances.clone();
|
2022-11-09 10:05:00 +00:00
|
|
|
FederationBlockList::replace(context.pool(), blocked).await?;
|
2021-12-15 19:49:59 +00:00
|
|
|
|
|
|
|
// TODO can't think of a better way to do this.
|
|
|
|
// If the server suddenly requires email verification, or required applications, no old users
|
|
|
|
// will be able to log in. It really only wants this to be a requirement for NEW signups.
|
|
|
|
// So if it was set from false, to true, you need to update all current users columns to be verified.
|
|
|
|
|
2023-01-05 01:42:30 +00:00
|
|
|
let old_require_application =
|
|
|
|
local_site.registration_mode == RegistrationMode::RequireApplication;
|
2022-10-27 09:24:07 +00:00
|
|
|
let new_require_application = update_local_site
|
|
|
|
.as_ref()
|
2023-01-05 01:42:30 +00:00
|
|
|
.map(|ols| ols.registration_mode == RegistrationMode::RequireApplication)
|
2022-10-27 09:24:07 +00:00
|
|
|
.unwrap_or(false);
|
2023-01-05 01:42:30 +00:00
|
|
|
if !old_require_application && new_require_application {
|
2022-11-09 10:05:00 +00:00
|
|
|
LocalUser::set_all_users_registration_applications_accepted(context.pool())
|
|
|
|
.await
|
|
|
|
.map_err(|e| LemmyError::from_error_message(e, "couldnt_set_all_registrations_accepted"))?;
|
2021-12-15 19:49:59 +00:00
|
|
|
}
|
|
|
|
|
2022-10-27 09:24:07 +00:00
|
|
|
let new_require_email_verification = update_local_site
|
|
|
|
.as_ref()
|
2022-11-09 10:05:00 +00:00
|
|
|
.map(|ols| ols.require_email_verification)
|
2022-10-27 09:24:07 +00:00
|
|
|
.unwrap_or(false);
|
|
|
|
if !local_site.require_email_verification && new_require_email_verification {
|
2022-11-09 10:05:00 +00:00
|
|
|
LocalUser::set_all_users_email_verified(context.pool())
|
|
|
|
.await
|
|
|
|
.map_err(|e| LemmyError::from_error_message(e, "couldnt_set_all_email_verified"))?;
|
2021-12-15 19:49:59 +00:00
|
|
|
}
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2023-06-06 12:59:34 +00:00
|
|
|
let new_taglines = data.taglines.clone();
|
|
|
|
let taglines = Tagline::replace(context.pool(), local_site.id, new_taglines).await?;
|
2022-11-19 14:48:29 +00:00
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
let site_view = SiteView::read_local(context.pool()).await?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2022-11-16 19:06:22 +00:00
|
|
|
let rate_limit_config =
|
|
|
|
local_site_rate_limit_to_rate_limit_config(&site_view.local_site_rate_limit);
|
|
|
|
context
|
|
|
|
.settings_updated_channel()
|
|
|
|
.send(rate_limit_config)
|
|
|
|
.await?;
|
|
|
|
|
2023-06-06 12:59:34 +00:00
|
|
|
let res = SiteResponse {
|
|
|
|
site_view,
|
|
|
|
taglines,
|
|
|
|
};
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2023-04-13 10:53:55 +00:00
|
|
|
context.send_all_ws_message(&UserOperationCrud::EditSite, &res, websocket_id)?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
}
|