2023-07-28 14:39:38 +00:00
|
|
|
use crate::site::{application_question_check, site_default_post_listing_type_check};
|
2023-03-21 15:03:05 +00:00
|
|
|
use activitypub_federation::http_signatures::generate_actor_keypair;
|
2023-07-28 14:39:38 +00:00
|
|
|
use actix_web::web::{Data, Json};
|
2021-04-07 11:40:35 +00:00
|
|
|
use lemmy_api_common::{
|
2022-11-28 14:29:33 +00:00
|
|
|
context::LemmyContext,
|
2022-05-03 17:44:13 +00:00
|
|
|
site::{CreateSite, SiteResponse},
|
2024-01-25 14:22:11 +00:00
|
|
|
utils::{
|
|
|
|
generate_shared_inbox_url,
|
2024-03-15 11:03:29 +00:00
|
|
|
get_url_blocklist,
|
2024-01-25 14:22:11 +00:00
|
|
|
is_admin,
|
|
|
|
local_site_rate_limit_to_rate_limit_config,
|
|
|
|
local_site_to_slur_regex,
|
|
|
|
process_markdown_opt,
|
|
|
|
proxy_image_link_opt_api,
|
|
|
|
},
|
2021-04-07 11:40:35 +00:00
|
|
|
};
|
2021-10-16 13:33:38 +00:00
|
|
|
use lemmy_db_schema::{
|
2022-02-07 19:23:12 +00:00
|
|
|
newtypes::DbUrl,
|
2022-10-27 09:24:07 +00:00
|
|
|
source::{
|
|
|
|
local_site::{LocalSite, LocalSiteUpdateForm},
|
|
|
|
local_site_rate_limit::{LocalSiteRateLimit, LocalSiteRateLimitUpdateForm},
|
|
|
|
site::{Site, SiteUpdateForm},
|
2023-06-06 12:59:34 +00:00
|
|
|
tagline::Tagline,
|
2022-10-27 09:24:07 +00:00
|
|
|
},
|
2021-10-16 13:33:38 +00:00
|
|
|
traits::Crud,
|
2024-01-25 14:22:11 +00:00
|
|
|
utils::{diesel_option_overwrite, naive_now},
|
2021-04-07 11:40:35 +00:00
|
|
|
};
|
2023-09-21 10:42:28 +00:00
|
|
|
use lemmy_db_views::structs::{LocalUserView, SiteView};
|
2021-03-25 19:19:40 +00:00
|
|
|
use lemmy_utils::{
|
2023-07-10 14:50:07 +00:00
|
|
|
error::{LemmyError, LemmyErrorType, LemmyResult},
|
2023-04-15 14:45:11 +00:00
|
|
|
utils::{
|
|
|
|
slurs::{check_slurs, check_slurs_opt},
|
2023-06-27 11:03:30 +00:00
|
|
|
validation::{
|
|
|
|
build_and_check_regex,
|
|
|
|
check_site_visibility_valid,
|
|
|
|
is_valid_body_field,
|
|
|
|
site_description_length_check,
|
|
|
|
site_name_length_check,
|
|
|
|
},
|
2023-04-15 14:45:11 +00:00
|
|
|
},
|
2021-03-25 19:19:40 +00:00
|
|
|
};
|
2022-02-07 19:23:12 +00:00
|
|
|
use url::Url;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2023-07-28 14:39:38 +00:00
|
|
|
#[tracing::instrument(skip(context))]
|
|
|
|
pub async fn create_site(
|
|
|
|
data: Json<CreateSite>,
|
|
|
|
context: Data<LemmyContext>,
|
2023-09-21 10:42:28 +00:00
|
|
|
local_user_view: LocalUserView,
|
2023-07-28 14:39:38 +00:00
|
|
|
) -> Result<Json<SiteResponse>, LemmyError> {
|
|
|
|
let local_site = LocalSite::read(&mut context.pool()).await?;
|
|
|
|
|
|
|
|
// Make sure user is an admin; other types of users should not create site data...
|
|
|
|
is_admin(&local_user_view)?;
|
|
|
|
|
|
|
|
validate_create_payload(&local_site, &data)?;
|
|
|
|
|
|
|
|
let actor_id: DbUrl = Url::parse(&context.settings().get_protocol_and_hostname())?.into();
|
2023-11-16 13:22:40 +00:00
|
|
|
let inbox_url = Some(generate_shared_inbox_url(context.settings())?);
|
2023-07-28 14:39:38 +00:00
|
|
|
let keypair = generate_actor_keypair()?;
|
|
|
|
|
2024-01-25 14:22:11 +00:00
|
|
|
let slur_regex = local_site_to_slur_regex(&local_site);
|
2024-03-15 11:03:29 +00:00
|
|
|
let url_blocklist = get_url_blocklist(&context).await?;
|
|
|
|
let sidebar = process_markdown_opt(&data.sidebar, &slur_regex, &url_blocklist, &context).await?;
|
2024-01-25 14:22:11 +00:00
|
|
|
let icon = proxy_image_link_opt_api(&data.icon, &context).await?;
|
|
|
|
let banner = proxy_image_link_opt_api(&data.banner, &context).await?;
|
|
|
|
|
2023-08-08 09:41:41 +00:00
|
|
|
let site_form = SiteUpdateForm {
|
2023-10-11 14:48:19 +00:00
|
|
|
name: Some(data.name.clone()),
|
2024-01-25 14:22:11 +00:00
|
|
|
sidebar: diesel_option_overwrite(sidebar),
|
2023-10-11 14:48:19 +00:00
|
|
|
description: diesel_option_overwrite(data.description.clone()),
|
2024-01-25 14:22:11 +00:00
|
|
|
icon,
|
|
|
|
banner,
|
2023-08-08 09:41:41 +00:00
|
|
|
actor_id: Some(actor_id),
|
|
|
|
last_refreshed_at: Some(naive_now()),
|
|
|
|
inbox_url,
|
|
|
|
private_key: Some(Some(keypair.private_key)),
|
|
|
|
public_key: Some(keypair.public_key),
|
2024-02-16 12:24:35 +00:00
|
|
|
content_warning: diesel_option_overwrite(data.content_warning.clone()),
|
2023-08-08 09:41:41 +00:00
|
|
|
..Default::default()
|
|
|
|
};
|
2023-07-28 14:39:38 +00:00
|
|
|
|
|
|
|
let site_id = local_site.site_id;
|
|
|
|
|
|
|
|
Site::update(&mut context.pool(), site_id, &site_form).await?;
|
|
|
|
|
2023-08-08 09:41:41 +00:00
|
|
|
let local_site_form = LocalSiteUpdateForm {
|
2023-07-28 14:39:38 +00:00
|
|
|
// Set the site setup to true
|
2023-08-08 09:41:41 +00:00
|
|
|
site_setup: Some(true),
|
|
|
|
enable_downvotes: data.enable_downvotes,
|
|
|
|
registration_mode: data.registration_mode,
|
|
|
|
enable_nsfw: data.enable_nsfw,
|
|
|
|
community_creation_admin_only: data.community_creation_admin_only,
|
|
|
|
require_email_verification: data.require_email_verification,
|
2023-10-11 14:48:19 +00:00
|
|
|
application_question: diesel_option_overwrite(data.application_question.clone()),
|
2023-08-08 09:41:41 +00:00
|
|
|
private_instance: data.private_instance,
|
2023-10-11 14:48:19 +00:00
|
|
|
default_theme: data.default_theme.clone(),
|
2023-08-08 09:41:41 +00:00
|
|
|
default_post_listing_type: data.default_post_listing_type,
|
2024-02-16 14:36:46 +00:00
|
|
|
default_sort_type: data.default_sort_type,
|
2023-10-11 14:48:19 +00:00
|
|
|
legal_information: diesel_option_overwrite(data.legal_information.clone()),
|
2023-08-08 09:41:41 +00:00
|
|
|
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.clone()),
|
|
|
|
actor_name_max_length: data.actor_name_max_length,
|
|
|
|
federation_enabled: data.federation_enabled,
|
|
|
|
captcha_enabled: data.captcha_enabled,
|
|
|
|
captcha_difficulty: data.captcha_difficulty.clone(),
|
2024-02-16 12:24:35 +00:00
|
|
|
default_post_listing_mode: data.default_post_listing_mode,
|
2023-08-08 09:41:41 +00:00
|
|
|
..Default::default()
|
|
|
|
};
|
2023-07-28 14:39:38 +00:00
|
|
|
|
|
|
|
LocalSite::update(&mut context.pool(), &local_site_form).await?;
|
|
|
|
|
2023-08-08 09:41:41 +00:00
|
|
|
let local_site_rate_limit_form = LocalSiteRateLimitUpdateForm {
|
|
|
|
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,
|
|
|
|
..Default::default()
|
|
|
|
};
|
2023-07-28 14:39:38 +00:00
|
|
|
|
|
|
|
LocalSiteRateLimit::update(&mut context.pool(), &local_site_rate_limit_form).await?;
|
|
|
|
|
|
|
|
let site_view = SiteView::read_local(&mut context.pool()).await?;
|
|
|
|
|
|
|
|
let new_taglines = data.taglines.clone();
|
|
|
|
let taglines = Tagline::replace(&mut context.pool(), local_site.id, new_taglines).await?;
|
|
|
|
|
|
|
|
let rate_limit_config =
|
|
|
|
local_site_rate_limit_to_rate_limit_config(&site_view.local_site_rate_limit);
|
2023-10-19 13:31:51 +00:00
|
|
|
context.rate_limit_cell().set_config(rate_limit_config);
|
2023-07-28 14:39:38 +00:00
|
|
|
|
|
|
|
Ok(Json(SiteResponse {
|
|
|
|
site_view,
|
|
|
|
taglines,
|
|
|
|
}))
|
2021-03-25 19:19:40 +00:00
|
|
|
}
|
2023-06-27 11:03:30 +00:00
|
|
|
|
|
|
|
fn validate_create_payload(local_site: &LocalSite, create_site: &CreateSite) -> LemmyResult<()> {
|
|
|
|
// Make sure the site hasn't already been set up...
|
|
|
|
if local_site.site_setup {
|
2023-08-31 13:01:08 +00:00
|
|
|
Err(LemmyErrorType::SiteAlreadyExists)?
|
2023-06-27 11:03:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Check that the slur regex compiles, and returns the regex if valid...
|
|
|
|
// Prioritize using new slur regex from the request; if not provided, use the existing regex.
|
|
|
|
let slur_regex = build_and_check_regex(
|
|
|
|
&create_site
|
|
|
|
.slur_filter_regex
|
|
|
|
.as_deref()
|
|
|
|
.or(local_site.slur_filter_regex.as_deref()),
|
|
|
|
)?;
|
|
|
|
|
|
|
|
site_name_length_check(&create_site.name)?;
|
|
|
|
check_slurs(&create_site.name, &slur_regex)?;
|
|
|
|
|
|
|
|
if let Some(desc) = &create_site.description {
|
|
|
|
site_description_length_check(desc)?;
|
|
|
|
check_slurs_opt(&create_site.description, &slur_regex)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
site_default_post_listing_type_check(&create_site.default_post_listing_type)?;
|
|
|
|
|
|
|
|
check_site_visibility_valid(
|
|
|
|
local_site.private_instance,
|
|
|
|
local_site.federation_enabled,
|
|
|
|
&create_site.private_instance,
|
|
|
|
&create_site.federation_enabled,
|
|
|
|
)?;
|
|
|
|
|
|
|
|
// Ensure that the sidebar has fewer than the max num characters...
|
|
|
|
is_valid_body_field(&create_site.sidebar, false)?;
|
|
|
|
|
|
|
|
application_question_check(
|
|
|
|
&local_site.application_question,
|
|
|
|
&create_site.application_question,
|
|
|
|
create_site
|
|
|
|
.registration_mode
|
|
|
|
.unwrap_or(local_site.registration_mode),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
2024-03-26 09:17:42 +00:00
|
|
|
#[allow(clippy::unwrap_used)]
|
|
|
|
#[allow(clippy::indexing_slicing)]
|
2023-06-27 11:03:30 +00:00
|
|
|
mod tests {
|
2023-07-17 15:04:14 +00:00
|
|
|
|
2023-06-27 11:03:30 +00:00
|
|
|
use crate::site::create::validate_create_payload;
|
|
|
|
use lemmy_api_common::site::CreateSite;
|
2024-02-16 14:36:46 +00:00
|
|
|
use lemmy_db_schema::{source::local_site::LocalSite, ListingType, RegistrationMode, SortType};
|
2023-07-10 14:50:07 +00:00
|
|
|
use lemmy_utils::error::LemmyErrorType;
|
2023-06-27 11:03:30 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_validate_invalid_create_payload() {
|
|
|
|
let invalid_payloads = [
|
|
|
|
(
|
|
|
|
"CreateSite attempted on set up LocalSite",
|
2023-07-10 14:50:07 +00:00
|
|
|
LemmyErrorType::SiteAlreadyExists,
|
2023-06-27 11:03:30 +00:00
|
|
|
&generate_local_site(
|
|
|
|
true,
|
|
|
|
None::<String>,
|
|
|
|
true,
|
|
|
|
false,
|
|
|
|
None::<String>,
|
|
|
|
RegistrationMode::Open,
|
|
|
|
),
|
|
|
|
&generate_create_site(
|
|
|
|
String::from("site_name"),
|
|
|
|
None::<String>,
|
|
|
|
None::<String>,
|
|
|
|
None::<ListingType>,
|
2024-02-16 14:36:46 +00:00
|
|
|
None::<SortType>,
|
2023-06-27 11:03:30 +00:00
|
|
|
None::<String>,
|
|
|
|
None::<bool>,
|
|
|
|
None::<bool>,
|
|
|
|
None::<String>,
|
|
|
|
None::<RegistrationMode>,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"CreateSite name matches LocalSite slur filter",
|
2023-07-10 14:50:07 +00:00
|
|
|
LemmyErrorType::Slurs,
|
2023-06-27 11:03:30 +00:00
|
|
|
&generate_local_site(
|
|
|
|
false,
|
|
|
|
Some(String::from("(foo|bar)")),
|
|
|
|
true,
|
|
|
|
false,
|
|
|
|
None::<String>,
|
|
|
|
RegistrationMode::Open,
|
|
|
|
),
|
|
|
|
&generate_create_site(
|
|
|
|
String::from("foo site_name"),
|
|
|
|
None::<String>,
|
|
|
|
None::<String>,
|
|
|
|
None::<ListingType>,
|
2024-02-16 14:36:46 +00:00
|
|
|
None::<SortType>,
|
2023-06-27 11:03:30 +00:00
|
|
|
None::<String>,
|
|
|
|
None::<bool>,
|
|
|
|
None::<bool>,
|
|
|
|
None::<String>,
|
|
|
|
None::<RegistrationMode>,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"CreateSite name matches new slur filter",
|
2023-07-10 14:50:07 +00:00
|
|
|
LemmyErrorType::Slurs,
|
2023-06-27 11:03:30 +00:00
|
|
|
&generate_local_site(
|
|
|
|
false,
|
|
|
|
Some(String::from("(foo|bar)")),
|
|
|
|
true,
|
|
|
|
false,
|
|
|
|
None::<String>,
|
|
|
|
RegistrationMode::Open,
|
|
|
|
),
|
|
|
|
&generate_create_site(
|
|
|
|
String::from("zeta site_name"),
|
|
|
|
None::<String>,
|
|
|
|
None::<String>,
|
|
|
|
None::<ListingType>,
|
2024-02-16 14:36:46 +00:00
|
|
|
None::<SortType>,
|
2023-06-27 11:03:30 +00:00
|
|
|
Some(String::from("(zeta|alpha)")),
|
|
|
|
None::<bool>,
|
|
|
|
None::<bool>,
|
|
|
|
None::<String>,
|
|
|
|
None::<RegistrationMode>,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"CreateSite listing type is Subscribed, which is invalid",
|
2023-07-10 14:50:07 +00:00
|
|
|
LemmyErrorType::InvalidDefaultPostListingType,
|
2023-06-27 11:03:30 +00:00
|
|
|
&generate_local_site(
|
|
|
|
false,
|
|
|
|
None::<String>,
|
|
|
|
true,
|
|
|
|
false,
|
|
|
|
None::<String>,
|
|
|
|
RegistrationMode::Open,
|
|
|
|
),
|
|
|
|
&generate_create_site(
|
|
|
|
String::from("site_name"),
|
|
|
|
None::<String>,
|
|
|
|
None::<String>,
|
|
|
|
Some(ListingType::Subscribed),
|
2024-02-16 14:36:46 +00:00
|
|
|
None::<SortType>,
|
2023-06-27 11:03:30 +00:00
|
|
|
None::<String>,
|
|
|
|
None::<bool>,
|
|
|
|
None::<bool>,
|
|
|
|
None::<String>,
|
|
|
|
None::<RegistrationMode>,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"CreateSite is both private and federated",
|
2023-07-10 14:50:07 +00:00
|
|
|
LemmyErrorType::CantEnablePrivateInstanceAndFederationTogether,
|
2023-06-27 11:03:30 +00:00
|
|
|
&generate_local_site(
|
|
|
|
false,
|
|
|
|
None::<String>,
|
|
|
|
true,
|
|
|
|
false,
|
|
|
|
None::<String>,
|
|
|
|
RegistrationMode::Open,
|
|
|
|
),
|
|
|
|
&generate_create_site(
|
|
|
|
String::from("site_name"),
|
|
|
|
None::<String>,
|
|
|
|
None::<String>,
|
|
|
|
None::<ListingType>,
|
2024-02-16 14:36:46 +00:00
|
|
|
None::<SortType>,
|
2023-06-27 11:03:30 +00:00
|
|
|
None::<String>,
|
|
|
|
Some(true),
|
|
|
|
Some(true),
|
|
|
|
None::<String>,
|
|
|
|
None::<RegistrationMode>,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"LocalSite is private, but CreateSite also makes it federated",
|
2023-07-10 14:50:07 +00:00
|
|
|
LemmyErrorType::CantEnablePrivateInstanceAndFederationTogether,
|
2023-06-27 11:03:30 +00:00
|
|
|
&generate_local_site(
|
|
|
|
false,
|
|
|
|
None::<String>,
|
|
|
|
true,
|
|
|
|
false,
|
|
|
|
None::<String>,
|
|
|
|
RegistrationMode::Open,
|
|
|
|
),
|
|
|
|
&generate_create_site(
|
|
|
|
String::from("site_name"),
|
|
|
|
None::<String>,
|
|
|
|
None::<String>,
|
|
|
|
None::<ListingType>,
|
2024-02-16 14:36:46 +00:00
|
|
|
None::<SortType>,
|
2023-06-27 11:03:30 +00:00
|
|
|
None::<String>,
|
|
|
|
None::<bool>,
|
|
|
|
Some(true),
|
|
|
|
None::<String>,
|
|
|
|
None::<RegistrationMode>,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"CreateSite requires application, but neither it nor LocalSite has an application question",
|
2023-07-10 14:50:07 +00:00
|
|
|
LemmyErrorType::ApplicationQuestionRequired,
|
2023-06-27 11:03:30 +00:00
|
|
|
&generate_local_site(
|
|
|
|
false,
|
|
|
|
None::<String>,
|
|
|
|
true,
|
|
|
|
false,
|
|
|
|
None::<String>,
|
|
|
|
RegistrationMode::Open,
|
|
|
|
),
|
|
|
|
&generate_create_site(
|
|
|
|
String::from("site_name"),
|
|
|
|
None::<String>,
|
|
|
|
None::<String>,
|
|
|
|
None::<ListingType>,
|
2024-02-16 14:36:46 +00:00
|
|
|
None::<SortType>,
|
2023-06-27 11:03:30 +00:00
|
|
|
None::<String>,
|
|
|
|
None::<bool>,
|
|
|
|
None::<bool>,
|
|
|
|
None::<String>,
|
|
|
|
Some(RegistrationMode::RequireApplication),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
];
|
|
|
|
|
|
|
|
invalid_payloads.iter().enumerate().for_each(
|
|
|
|
|(
|
|
|
|
idx,
|
2023-07-10 14:50:07 +00:00
|
|
|
&(reason, ref expected_err, local_site, create_site),
|
2023-06-27 11:03:30 +00:00
|
|
|
)| {
|
|
|
|
match validate_create_payload(
|
|
|
|
local_site,
|
|
|
|
create_site,
|
|
|
|
) {
|
|
|
|
Ok(_) => {
|
|
|
|
panic!(
|
|
|
|
"Got Ok, but validation should have failed with error: {} for reason: {}. invalid_payloads.nth({})",
|
|
|
|
expected_err, reason, idx
|
|
|
|
)
|
|
|
|
}
|
|
|
|
Err(error) => {
|
|
|
|
assert!(
|
2023-07-10 20:44:14 +00:00
|
|
|
error.error_type.eq(&expected_err.clone()),
|
2023-06-27 11:03:30 +00:00
|
|
|
"Got Err {:?}, but should have failed with message: {} for reason: {}. invalid_payloads.nth({})",
|
2023-07-10 14:50:07 +00:00
|
|
|
error.error_type,
|
2023-06-27 11:03:30 +00:00
|
|
|
expected_err,
|
|
|
|
reason,
|
|
|
|
idx
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_validate_valid_create_payload() {
|
|
|
|
let valid_payloads = [
|
|
|
|
(
|
|
|
|
"No changes between LocalSite and CreateSite",
|
|
|
|
&generate_local_site(
|
|
|
|
false,
|
|
|
|
None::<String>,
|
|
|
|
true,
|
|
|
|
false,
|
|
|
|
None::<String>,
|
|
|
|
RegistrationMode::Open,
|
|
|
|
),
|
|
|
|
&generate_create_site(
|
|
|
|
String::from("site_name"),
|
|
|
|
None::<String>,
|
|
|
|
None::<String>,
|
|
|
|
None::<ListingType>,
|
2024-02-16 14:36:46 +00:00
|
|
|
None::<SortType>,
|
2023-06-27 11:03:30 +00:00
|
|
|
None::<String>,
|
|
|
|
None::<bool>,
|
|
|
|
None::<bool>,
|
|
|
|
None::<String>,
|
|
|
|
None::<RegistrationMode>,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"CreateSite allows clearing and changing values",
|
|
|
|
&generate_local_site(
|
|
|
|
false,
|
|
|
|
None::<String>,
|
|
|
|
true,
|
|
|
|
false,
|
|
|
|
None::<String>,
|
|
|
|
RegistrationMode::Open,
|
|
|
|
),
|
|
|
|
&generate_create_site(
|
|
|
|
String::from("site_name"),
|
|
|
|
Some(String::new()),
|
|
|
|
Some(String::new()),
|
|
|
|
Some(ListingType::All),
|
2024-02-16 14:36:46 +00:00
|
|
|
Some(SortType::Active),
|
2023-06-27 11:03:30 +00:00
|
|
|
Some(String::new()),
|
|
|
|
Some(false),
|
|
|
|
Some(true),
|
|
|
|
Some(String::new()),
|
|
|
|
Some(RegistrationMode::Open),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"CreateSite clears existing slur filter regex",
|
|
|
|
&generate_local_site(
|
|
|
|
false,
|
|
|
|
Some(String::from("(foo|bar)")),
|
|
|
|
true,
|
|
|
|
false,
|
|
|
|
None::<String>,
|
|
|
|
RegistrationMode::Open,
|
|
|
|
),
|
|
|
|
&generate_create_site(
|
|
|
|
String::from("foo site_name"),
|
|
|
|
None::<String>,
|
|
|
|
None::<String>,
|
|
|
|
None::<ListingType>,
|
2024-02-16 14:36:46 +00:00
|
|
|
None::<SortType>,
|
2023-06-27 11:03:30 +00:00
|
|
|
Some(String::new()),
|
|
|
|
None::<bool>,
|
|
|
|
None::<bool>,
|
|
|
|
None::<String>,
|
|
|
|
None::<RegistrationMode>,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"LocalSite has application question and CreateSite now requires applications,",
|
|
|
|
&generate_local_site(
|
|
|
|
false,
|
|
|
|
None::<String>,
|
|
|
|
true,
|
|
|
|
false,
|
|
|
|
Some(String::from("question")),
|
|
|
|
RegistrationMode::Open,
|
|
|
|
),
|
|
|
|
&generate_create_site(
|
|
|
|
String::from("site_name"),
|
|
|
|
None::<String>,
|
|
|
|
None::<String>,
|
|
|
|
None::<ListingType>,
|
2024-02-16 14:36:46 +00:00
|
|
|
None::<SortType>,
|
2023-06-27 11:03:30 +00:00
|
|
|
None::<String>,
|
|
|
|
None::<bool>,
|
|
|
|
None::<bool>,
|
|
|
|
None::<String>,
|
|
|
|
Some(RegistrationMode::RequireApplication),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
];
|
|
|
|
|
|
|
|
valid_payloads
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.for_each(|(idx, &(reason, local_site, edit_site))| {
|
|
|
|
assert!(
|
|
|
|
validate_create_payload(local_site, edit_site).is_ok(),
|
|
|
|
"Got Err, but should have got Ok for reason: {}. valid_payloads.nth({})",
|
|
|
|
reason,
|
|
|
|
idx
|
|
|
|
);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn generate_local_site(
|
|
|
|
site_setup: bool,
|
|
|
|
site_slur_filter_regex: Option<String>,
|
|
|
|
site_is_private: bool,
|
|
|
|
site_is_federated: bool,
|
|
|
|
site_application_question: Option<String>,
|
|
|
|
site_registration_mode: RegistrationMode,
|
|
|
|
) -> LocalSite {
|
|
|
|
LocalSite {
|
|
|
|
site_setup,
|
|
|
|
application_question: site_application_question,
|
|
|
|
private_instance: site_is_private,
|
|
|
|
slur_filter_regex: site_slur_filter_regex,
|
|
|
|
federation_enabled: site_is_federated,
|
|
|
|
registration_mode: site_registration_mode,
|
2023-11-06 21:02:01 +00:00
|
|
|
..Default::default()
|
2023-06-27 11:03:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allow the test helper function to have too many arguments.
|
|
|
|
// It's either this or generate the entire struct each time for testing.
|
|
|
|
#[allow(clippy::too_many_arguments)]
|
|
|
|
fn generate_create_site(
|
|
|
|
site_name: String,
|
|
|
|
site_description: Option<String>,
|
|
|
|
site_sidebar: Option<String>,
|
|
|
|
site_listing_type: Option<ListingType>,
|
2024-02-16 14:36:46 +00:00
|
|
|
site_sort_type: Option<SortType>,
|
2023-06-27 11:03:30 +00:00
|
|
|
site_slur_filter_regex: Option<String>,
|
|
|
|
site_is_private: Option<bool>,
|
|
|
|
site_is_federated: Option<bool>,
|
|
|
|
site_application_question: Option<String>,
|
|
|
|
site_registration_mode: Option<RegistrationMode>,
|
|
|
|
) -> CreateSite {
|
|
|
|
CreateSite {
|
|
|
|
name: site_name,
|
|
|
|
sidebar: site_sidebar,
|
|
|
|
description: site_description,
|
|
|
|
icon: None,
|
|
|
|
banner: None,
|
|
|
|
enable_downvotes: None,
|
|
|
|
enable_nsfw: None,
|
|
|
|
community_creation_admin_only: None,
|
|
|
|
require_email_verification: None,
|
|
|
|
application_question: site_application_question,
|
|
|
|
private_instance: site_is_private,
|
|
|
|
default_theme: None,
|
|
|
|
default_post_listing_type: site_listing_type,
|
2024-02-16 14:36:46 +00:00
|
|
|
default_sort_type: site_sort_type,
|
2023-06-27 11:03:30 +00:00
|
|
|
legal_information: None,
|
|
|
|
application_email_admins: None,
|
|
|
|
hide_modlog_mod_names: None,
|
|
|
|
discussion_languages: None,
|
|
|
|
slur_filter_regex: site_slur_filter_regex,
|
|
|
|
actor_name_max_length: None,
|
|
|
|
rate_limit_message: None,
|
|
|
|
rate_limit_message_per_second: None,
|
|
|
|
rate_limit_post: None,
|
|
|
|
rate_limit_post_per_second: None,
|
|
|
|
rate_limit_register: None,
|
|
|
|
rate_limit_register_per_second: None,
|
|
|
|
rate_limit_image: None,
|
|
|
|
rate_limit_image_per_second: None,
|
|
|
|
rate_limit_comment: None,
|
|
|
|
rate_limit_comment_per_second: None,
|
|
|
|
rate_limit_search: None,
|
|
|
|
rate_limit_search_per_second: None,
|
|
|
|
federation_enabled: site_is_federated,
|
|
|
|
federation_debug: None,
|
|
|
|
captcha_enabled: None,
|
|
|
|
captcha_difficulty: None,
|
|
|
|
allowed_instances: None,
|
|
|
|
blocked_instances: None,
|
|
|
|
taglines: None,
|
|
|
|
registration_mode: site_registration_mode,
|
2024-02-16 12:24:35 +00:00
|
|
|
content_warning: None,
|
|
|
|
default_post_listing_mode: None,
|
2023-06-27 11:03:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|