};
use lemmy_db_views::structs::SiteView;
use lemmy_utils::{
- error::{LemmyError, LemmyResult},
+ error::{LemmyError, LemmyErrorType, LemmyResult},
utils::{
slurs::{check_slurs, check_slurs_opt},
validation::{
async fn perform(&self, context: &Data<LemmyContext>) -> Result<SiteResponse, LemmyError> {
let data: &CreateSite = self;
let local_user_view = local_user_view_from_jwt(&data.auth, context).await?;
- let local_site = LocalSite::read(context.pool()).await?;
+ 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)?;
let site_id = local_site.site_id;
- Site::update(context.pool(), site_id, &site_form).await?;
+ Site::update(&mut context.pool(), site_id, &site_form).await?;
let local_site_form = LocalSiteUpdateForm::builder()
// Set the site setup to true
.captcha_difficulty(data.captcha_difficulty.clone())
.build();
- LocalSite::update(context.pool(), &local_site_form).await?;
+ LocalSite::update(&mut context.pool(), &local_site_form).await?;
let local_site_rate_limit_form = LocalSiteRateLimitUpdateForm::builder()
.message(data.rate_limit_message)
.search_per_second(data.rate_limit_search_per_second)
.build();
- LocalSiteRateLimit::update(context.pool(), &local_site_rate_limit_form).await?;
+ LocalSiteRateLimit::update(&mut context.pool(), &local_site_rate_limit_form).await?;
- let site_view = SiteView::read_local(context.pool()).await?;
+ let site_view = SiteView::read_local(&mut context.pool()).await?;
let new_taglines = data.taglines.clone();
- let taglines = Tagline::replace(context.pool(), local_site.id, new_taglines).await?;
+ 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);
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 {
- return Err(LemmyError::from_message("site_already_exists"));
+ return Err(LemmyErrorType::SiteAlreadyExists)?;
};
// Check that the slur regex compiles, and returns the regex if valid...
use crate::site::create::validate_create_payload;
use lemmy_api_common::site::CreateSite;
use lemmy_db_schema::{source::local_site::LocalSite, ListingType, RegistrationMode};
+ use lemmy_utils::error::LemmyErrorType;
#[test]
fn test_validate_invalid_create_payload() {
let invalid_payloads = [
(
"CreateSite attempted on set up LocalSite",
- "site_already_exists",
+ LemmyErrorType::SiteAlreadyExists,
&generate_local_site(
true,
None::<String>,
),
(
"CreateSite name matches LocalSite slur filter",
- "slurs",
+ LemmyErrorType::Slurs,
&generate_local_site(
false,
Some(String::from("(foo|bar)")),
),
(
"CreateSite name matches new slur filter",
- "slurs",
+ LemmyErrorType::Slurs,
&generate_local_site(
false,
Some(String::from("(foo|bar)")),
),
(
"CreateSite listing type is Subscribed, which is invalid",
- "invalid_default_post_listing_type",
+ LemmyErrorType::InvalidDefaultPostListingType,
&generate_local_site(
false,
None::<String>,
),
(
"CreateSite is both private and federated",
- "cant_enable_private_instance_and_federation_together",
+ LemmyErrorType::CantEnablePrivateInstanceAndFederationTogether,
&generate_local_site(
false,
None::<String>,
),
(
"LocalSite is private, but CreateSite also makes it federated",
- "cant_enable_private_instance_and_federation_together",
+ LemmyErrorType::CantEnablePrivateInstanceAndFederationTogether,
&generate_local_site(
false,
None::<String>,
),
(
"CreateSite requires application, but neither it nor LocalSite has an application question",
- "application_question_required",
+ LemmyErrorType::ApplicationQuestionRequired,
&generate_local_site(
false,
None::<String>,
invalid_payloads.iter().enumerate().for_each(
|(
idx,
- &(reason, expected_err, local_site, create_site),
+ &(reason, ref expected_err, local_site, create_site),
)| {
match validate_create_payload(
local_site,
}
Err(error) => {
assert!(
- error.message.eq(&Some(String::from(expected_err))),
+ error.error_type.eq(&expected_err.clone()),
"Got Err {:?}, but should have failed with message: {} for reason: {}. invalid_payloads.nth({})",
- error.message,
+ error.error_type,
expected_err,
reason,
idx