Cleanup remaining use of Result<bool, Error> (fixes #4862) (#5047)

This commit is contained in:
Nutomic 2024-09-24 19:25:33 +02:00 committed by GitHub
parent 0fab5bed24
commit 61a02482ff
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 12 additions and 43 deletions

View File

@ -107,9 +107,7 @@ pub async fn register(
check_slurs(&data.username, &slur_regex)?; check_slurs(&data.username, &slur_regex)?;
check_slurs_opt(&data.answer, &slur_regex)?; check_slurs_opt(&data.answer, &slur_regex)?;
if Person::is_username_taken(&mut context.pool(), &data.username).await? { Person::check_username_taken(&mut context.pool(), &data.username).await?;
return Err(LemmyErrorType::UsernameAlreadyExists)?;
}
if let Some(email) = &data.email { if let Some(email) = &data.email {
LocalUser::check_is_email_taken(&mut context.pool(), email).await?; LocalUser::check_is_email_taken(&mut context.pool(), email).await?;
@ -329,9 +327,7 @@ pub async fn authenticate_with_oauth(
check_slurs(username, &slur_regex)?; check_slurs(username, &slur_regex)?;
check_slurs_opt(&data.answer, &slur_regex)?; check_slurs_opt(&data.answer, &slur_regex)?;
if Person::is_username_taken(&mut context.pool(), username).await? { Person::check_username_taken(&mut context.pool(), username).await?;
return Err(LemmyErrorType::UsernameAlreadyExists)?;
}
// We have to create a person, a local_user, and an oauth_account // We have to create a person, a local_user, and an oauth_account
person = create_person( person = create_person(

View File

@ -1,32 +1,13 @@
use crate::{ use crate::{
newtypes::{LocalUserId, OAuthProviderId}, newtypes::LocalUserId,
schema::{oauth_account, oauth_account::dsl::local_user_id}, schema::{oauth_account, oauth_account::dsl::local_user_id},
source::oauth_account::{OAuthAccount, OAuthAccountInsertForm}, source::oauth_account::{OAuthAccount, OAuthAccountInsertForm},
utils::{get_conn, DbPool}, utils::{get_conn, DbPool},
}; };
use diesel::{ use diesel::{insert_into, result::Error, ExpressionMethods, QueryDsl};
dsl::{exists, insert_into},
result::Error,
select,
ExpressionMethods,
QueryDsl,
};
use diesel_async::RunQueryDsl; use diesel_async::RunQueryDsl;
impl OAuthAccount { impl OAuthAccount {
pub async fn read(
pool: &mut DbPool<'_>,
for_oauth_provider_id: OAuthProviderId,
for_local_user_id: LocalUserId,
) -> Result<bool, Error> {
let conn = &mut get_conn(pool).await?;
select(exists(
oauth_account::table.find((for_oauth_provider_id, for_local_user_id)),
))
.get_result(conn)
.await
}
pub async fn create(pool: &mut DbPool<'_>, form: &OAuthAccountInsertForm) -> Result<Self, Error> { pub async fn create(pool: &mut DbPool<'_>, form: &OAuthAccountInsertForm) -> Result<Self, Error> {
let conn = &mut get_conn(pool).await?; let conn = &mut get_conn(pool).await?;
insert_into(oauth_account::table) insert_into(oauth_account::table)
@ -35,17 +16,6 @@ impl OAuthAccount {
.await .await
} }
pub async fn delete(
pool: &mut DbPool<'_>,
for_oauth_provider_id: OAuthProviderId,
for_local_user_id: LocalUserId,
) -> Result<usize, Error> {
let conn = &mut get_conn(pool).await?;
diesel::delete(oauth_account::table.find((for_oauth_provider_id, for_local_user_id)))
.execute(conn)
.await
}
pub async fn delete_user_accounts( pub async fn delete_user_accounts(
pool: &mut DbPool<'_>, pool: &mut DbPool<'_>,
for_local_user_id: LocalUserId, for_local_user_id: LocalUserId,

View File

@ -21,6 +21,7 @@ use diesel::{
QueryDsl, QueryDsl,
}; };
use diesel_async::RunQueryDsl; use diesel_async::RunQueryDsl;
use lemmy_utils::{error::LemmyResult, LemmyErrorType};
#[async_trait] #[async_trait]
impl Crud for Person { impl Crud for Person {
@ -121,16 +122,18 @@ impl Person {
.await .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}; use diesel::dsl::{exists, select};
let conn = &mut get_conn(pool).await?; let conn = &mut get_conn(pool).await?;
select(exists( select(not(exists(
person::table person::table
.filter(lower(person::name).eq(username.to_lowercase())) .filter(lower(person::name).eq(username.to_lowercase()))
.filter(person::local.eq(true)), .filter(person::local.eq(true)),
)) )))
.get_result(conn) .get_result::<bool>(conn)
.await .await?
.then_some(())
.ok_or(LemmyErrorType::UsernameAlreadyExists.into())
} }
} }