Skip to content

Commit

Permalink
Cleanup remaining use of Result<bool, Error> (fixes #4862)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nutomic committed Sep 24, 2024
1 parent 9eee61d commit 5d46dd1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
8 changes: 2 additions & 6 deletions crates/api_crud/src/user/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,7 @@ pub async fn register(
check_slurs(&data.username, &slur_regex)?;
check_slurs_opt(&data.answer, &slur_regex)?;

if Person::is_username_taken(&mut context.pool(), &data.username).await? {
return Err(LemmyErrorType::UsernameAlreadyExists)?;
}
Person::check_username_taken(&mut context.pool(), &data.username).await?;

if let Some(email) = &data.email {
LocalUser::check_is_email_taken(&mut context.pool(), email).await?;
Expand Down Expand Up @@ -329,9 +327,7 @@ pub async fn authenticate_with_oauth(
check_slurs(username, &slur_regex)?;
check_slurs_opt(&data.answer, &slur_regex)?;

if Person::is_username_taken(&mut context.pool(), username).await? {
return Err(LemmyErrorType::UsernameAlreadyExists)?;
}
Person::check_username_taken(&mut context.pool(), username).await?;

// We have to create a person, a local_user, and an oauth_account
person = create_person(
Expand Down
13 changes: 8 additions & 5 deletions crates/db_schema/src/impls/person.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use diesel::{
QueryDsl,
};
use diesel_async::RunQueryDsl;
use lemmy_utils::{error::LemmyResult, LemmyErrorType};

#[async_trait]
impl Crud for Person {
Expand Down Expand Up @@ -121,16 +122,18 @@ impl Person {
.await
}

pub async fn is_username_taken(pool: &mut DbPool<'_>, username: &str) -> Result<bool, Error> {
pub async fn check_username_taken(pool: &mut DbPool<'_>, username: &str) -> LemmyResult<()> {
use diesel::dsl::{exists, select};
let conn = &mut get_conn(pool).await?;
select(exists(
select(not(exists(
person::table
.filter(lower(person::name).eq(username.to_lowercase()))
.filter(person::local.eq(true)),
))
.get_result(conn)
.await
)))
.get_result::<bool>(conn)
.await?
.then_some(())
.ok_or(LemmyErrorType::UsernameAlreadyExists.into())
}
}

Expand Down

0 comments on commit 5d46dd1

Please sign in to comment.