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_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?;
@ -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(

View file

@ -1,32 +1,13 @@
use crate::{
newtypes::{LocalUserId, OAuthProviderId},
newtypes::LocalUserId,
schema::{oauth_account, oauth_account::dsl::local_user_id},
source::oauth_account::{OAuthAccount, OAuthAccountInsertForm},
utils::{get_conn, DbPool},
};
use diesel::{
dsl::{exists, insert_into},
result::Error,
select,
ExpressionMethods,
QueryDsl,
};
use diesel::{insert_into, result::Error, ExpressionMethods, QueryDsl};
use diesel_async::RunQueryDsl;
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> {
let conn = &mut get_conn(pool).await?;
insert_into(oauth_account::table)
@ -35,17 +16,6 @@ impl OAuthAccount {
.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(
pool: &mut DbPool<'_>,
for_local_user_id: LocalUserId,

View file

@ -21,6 +21,7 @@ use diesel::{
QueryDsl,
};
use diesel_async::RunQueryDsl;
use lemmy_utils::{error::LemmyResult, LemmyErrorType};
#[async_trait]
impl Crud for Person {
@ -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())
}
}