You've already forked AstralRinth
forked from didirus/AstralRinth
Labrinth ID cleanup (#3681)
* Put all ID types in the labrinth::models::ids, and reduce code duplication with them * Rewrite labrinth::database::models::ids and rename most DB interface ID structs to be prefixed with DB * Run sqlx prepare --------- Co-authored-by: Alejandro González <7822554+AlexTMjugador@users.noreply.github.com>
This commit is contained in:
@@ -74,7 +74,7 @@ impl TempUser {
|
||||
client: &PgPool,
|
||||
file_host: &Arc<dyn FileHost + Send + Sync>,
|
||||
redis: &RedisPool,
|
||||
) -> Result<crate::database::models::UserId, AuthenticationError> {
|
||||
) -> Result<crate::database::models::DBUserId, AuthenticationError> {
|
||||
if let Some(email) = &self.email {
|
||||
if crate::database::models::User::get_email(email, client)
|
||||
.await?
|
||||
@@ -115,47 +115,45 @@ impl TempUser {
|
||||
}
|
||||
}
|
||||
|
||||
let (avatar_url, raw_avatar_url) =
|
||||
if let Some(avatar_url) = self.avatar_url {
|
||||
let res = reqwest::get(&avatar_url).await?;
|
||||
let headers = res.headers().clone();
|
||||
let (avatar_url, raw_avatar_url) = if let Some(avatar_url) =
|
||||
self.avatar_url
|
||||
{
|
||||
let res = reqwest::get(&avatar_url).await?;
|
||||
let headers = res.headers().clone();
|
||||
|
||||
let img_data = if let Some(content_type) = headers
|
||||
.get(reqwest::header::CONTENT_TYPE)
|
||||
.and_then(|ct| ct.to_str().ok())
|
||||
{
|
||||
get_image_ext(content_type)
|
||||
} else {
|
||||
avatar_url.rsplit('.').next()
|
||||
};
|
||||
let img_data = if let Some(content_type) = headers
|
||||
.get(reqwest::header::CONTENT_TYPE)
|
||||
.and_then(|ct| ct.to_str().ok())
|
||||
{
|
||||
get_image_ext(content_type)
|
||||
} else {
|
||||
avatar_url.rsplit('.').next()
|
||||
};
|
||||
|
||||
if let Some(ext) = img_data {
|
||||
let bytes = res.bytes().await?;
|
||||
if let Some(ext) = img_data {
|
||||
let bytes = res.bytes().await?;
|
||||
|
||||
let upload_result = upload_image_optimized(
|
||||
&format!(
|
||||
"user/{}",
|
||||
crate::models::users::UserId::from(user_id)
|
||||
),
|
||||
bytes,
|
||||
ext,
|
||||
Some(96),
|
||||
Some(1.0),
|
||||
&**file_host,
|
||||
)
|
||||
.await;
|
||||
let upload_result = upload_image_optimized(
|
||||
&format!("user/{}", ariadne::ids::UserId::from(user_id)),
|
||||
bytes,
|
||||
ext,
|
||||
Some(96),
|
||||
Some(1.0),
|
||||
&**file_host,
|
||||
)
|
||||
.await;
|
||||
|
||||
if let Ok(upload_result) = upload_result {
|
||||
(Some(upload_result.url), Some(upload_result.raw_url))
|
||||
} else {
|
||||
(None, None)
|
||||
}
|
||||
if let Ok(upload_result) = upload_result {
|
||||
(Some(upload_result.url), Some(upload_result.raw_url))
|
||||
} else {
|
||||
(None, None)
|
||||
}
|
||||
} else {
|
||||
(None, None)
|
||||
};
|
||||
}
|
||||
} else {
|
||||
(None, None)
|
||||
};
|
||||
|
||||
if let Some(username) = username {
|
||||
crate::database::models::User {
|
||||
@@ -823,7 +821,7 @@ impl AuthProvider {
|
||||
&self,
|
||||
id: &str,
|
||||
executor: E,
|
||||
) -> Result<Option<crate::database::models::UserId>, AuthenticationError>
|
||||
) -> Result<Option<crate::database::models::DBUserId>, AuthenticationError>
|
||||
where
|
||||
E: sqlx::Executor<'a, Database = sqlx::Postgres>,
|
||||
{
|
||||
@@ -837,7 +835,7 @@ impl AuthProvider {
|
||||
.fetch_optional(executor)
|
||||
.await?;
|
||||
|
||||
value.map(|x| crate::database::models::UserId(x.id))
|
||||
value.map(|x| crate::database::models::DBUserId(x.id))
|
||||
}
|
||||
AuthProvider::Discord => {
|
||||
let value = sqlx::query!(
|
||||
@@ -848,7 +846,7 @@ impl AuthProvider {
|
||||
.fetch_optional(executor)
|
||||
.await?;
|
||||
|
||||
value.map(|x| crate::database::models::UserId(x.id))
|
||||
value.map(|x| crate::database::models::DBUserId(x.id))
|
||||
}
|
||||
AuthProvider::Microsoft => {
|
||||
let value = sqlx::query!(
|
||||
@@ -858,7 +856,7 @@ impl AuthProvider {
|
||||
.fetch_optional(executor)
|
||||
.await?;
|
||||
|
||||
value.map(|x| crate::database::models::UserId(x.id))
|
||||
value.map(|x| crate::database::models::DBUserId(x.id))
|
||||
}
|
||||
AuthProvider::GitLab => {
|
||||
let value = sqlx::query!(
|
||||
@@ -869,7 +867,7 @@ impl AuthProvider {
|
||||
.fetch_optional(executor)
|
||||
.await?;
|
||||
|
||||
value.map(|x| crate::database::models::UserId(x.id))
|
||||
value.map(|x| crate::database::models::DBUserId(x.id))
|
||||
}
|
||||
AuthProvider::Google => {
|
||||
let value = sqlx::query!(
|
||||
@@ -879,7 +877,7 @@ impl AuthProvider {
|
||||
.fetch_optional(executor)
|
||||
.await?;
|
||||
|
||||
value.map(|x| crate::database::models::UserId(x.id))
|
||||
value.map(|x| crate::database::models::DBUserId(x.id))
|
||||
}
|
||||
AuthProvider::Steam => {
|
||||
let value = sqlx::query!(
|
||||
@@ -890,7 +888,7 @@ impl AuthProvider {
|
||||
.fetch_optional(executor)
|
||||
.await?;
|
||||
|
||||
value.map(|x| crate::database::models::UserId(x.id))
|
||||
value.map(|x| crate::database::models::DBUserId(x.id))
|
||||
}
|
||||
AuthProvider::PayPal => {
|
||||
let value = sqlx::query!(
|
||||
@@ -900,14 +898,14 @@ impl AuthProvider {
|
||||
.fetch_optional(executor)
|
||||
.await?;
|
||||
|
||||
value.map(|x| crate::database::models::UserId(x.id))
|
||||
value.map(|x| crate::database::models::DBUserId(x.id))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn update_user_id(
|
||||
&self,
|
||||
user_id: crate::database::models::UserId,
|
||||
user_id: crate::database::models::DBUserId,
|
||||
id: Option<&str>,
|
||||
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
|
||||
) -> Result<(), AuthenticationError> {
|
||||
@@ -919,7 +917,7 @@ impl AuthProvider {
|
||||
SET github_id = $2
|
||||
WHERE (id = $1)
|
||||
",
|
||||
user_id as crate::database::models::UserId,
|
||||
user_id as crate::database::models::DBUserId,
|
||||
id.and_then(|x| x.parse::<i64>().ok())
|
||||
)
|
||||
.execute(&mut **transaction)
|
||||
@@ -932,7 +930,7 @@ impl AuthProvider {
|
||||
SET discord_id = $2
|
||||
WHERE (id = $1)
|
||||
",
|
||||
user_id as crate::database::models::UserId,
|
||||
user_id as crate::database::models::DBUserId,
|
||||
id.and_then(|x| x.parse::<i64>().ok())
|
||||
)
|
||||
.execute(&mut **transaction)
|
||||
@@ -945,7 +943,7 @@ impl AuthProvider {
|
||||
SET microsoft_id = $2
|
||||
WHERE (id = $1)
|
||||
",
|
||||
user_id as crate::database::models::UserId,
|
||||
user_id as crate::database::models::DBUserId,
|
||||
id,
|
||||
)
|
||||
.execute(&mut **transaction)
|
||||
@@ -958,7 +956,7 @@ impl AuthProvider {
|
||||
SET gitlab_id = $2
|
||||
WHERE (id = $1)
|
||||
",
|
||||
user_id as crate::database::models::UserId,
|
||||
user_id as crate::database::models::DBUserId,
|
||||
id.and_then(|x| x.parse::<i64>().ok())
|
||||
)
|
||||
.execute(&mut **transaction)
|
||||
@@ -971,7 +969,7 @@ impl AuthProvider {
|
||||
SET google_id = $2
|
||||
WHERE (id = $1)
|
||||
",
|
||||
user_id as crate::database::models::UserId,
|
||||
user_id as crate::database::models::DBUserId,
|
||||
id,
|
||||
)
|
||||
.execute(&mut **transaction)
|
||||
@@ -984,7 +982,7 @@ impl AuthProvider {
|
||||
SET steam_id = $2
|
||||
WHERE (id = $1)
|
||||
",
|
||||
user_id as crate::database::models::UserId,
|
||||
user_id as crate::database::models::DBUserId,
|
||||
id.and_then(|x| x.parse::<i64>().ok())
|
||||
)
|
||||
.execute(&mut **transaction)
|
||||
@@ -998,7 +996,7 @@ impl AuthProvider {
|
||||
SET paypal_country = NULL, paypal_email = NULL, paypal_id = NULL
|
||||
WHERE (id = $1)
|
||||
",
|
||||
user_id as crate::database::models::UserId,
|
||||
user_id as crate::database::models::DBUserId,
|
||||
)
|
||||
.execute(&mut **transaction)
|
||||
.await?;
|
||||
@@ -1009,7 +1007,7 @@ impl AuthProvider {
|
||||
SET paypal_id = $2
|
||||
WHERE (id = $1)
|
||||
",
|
||||
user_id as crate::database::models::UserId,
|
||||
user_id as crate::database::models::DBUserId,
|
||||
id,
|
||||
)
|
||||
.execute(&mut **transaction)
|
||||
@@ -1152,7 +1150,7 @@ pub async fn auth_callback(
|
||||
oauth_user.country,
|
||||
oauth_user.email,
|
||||
oauth_user.id,
|
||||
id as crate::database::models::ids::UserId,
|
||||
id as crate::database::models::ids::DBUserId,
|
||||
)
|
||||
.execute(&mut *transaction)
|
||||
.await?;
|
||||
@@ -1527,7 +1525,7 @@ async fn validate_2fa_code(
|
||||
input: String,
|
||||
secret: String,
|
||||
allow_backup: bool,
|
||||
user_id: crate::database::models::UserId,
|
||||
user_id: crate::database::models::DBUserId,
|
||||
redis: &RedisPool,
|
||||
pool: &PgPool,
|
||||
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
|
||||
@@ -1583,7 +1581,7 @@ async fn validate_2fa_code(
|
||||
DELETE FROM user_backup_codes
|
||||
WHERE user_id = $1 AND code = $2
|
||||
",
|
||||
user_id as crate::database::models::ids::UserId,
|
||||
user_id as crate::database::models::ids::DBUserId,
|
||||
code as i64,
|
||||
)
|
||||
.execute(&mut **transaction)
|
||||
@@ -1746,7 +1744,7 @@ pub async fn finish_2fa_flow(
|
||||
WHERE (id = $2)
|
||||
",
|
||||
secret,
|
||||
user_id as crate::database::models::ids::UserId,
|
||||
user_id as crate::database::models::ids::DBUserId,
|
||||
)
|
||||
.execute(&mut *transaction)
|
||||
.await?;
|
||||
@@ -1756,7 +1754,7 @@ pub async fn finish_2fa_flow(
|
||||
DELETE FROM user_backup_codes
|
||||
WHERE user_id = $1
|
||||
",
|
||||
user_id as crate::database::models::ids::UserId,
|
||||
user_id as crate::database::models::ids::DBUserId,
|
||||
)
|
||||
.execute(&mut *transaction)
|
||||
.await?;
|
||||
@@ -1776,7 +1774,7 @@ pub async fn finish_2fa_flow(
|
||||
$1, $2
|
||||
)
|
||||
",
|
||||
user_id as crate::database::models::ids::UserId,
|
||||
user_id as crate::database::models::ids::DBUserId,
|
||||
val as i64,
|
||||
)
|
||||
.execute(&mut *transaction)
|
||||
@@ -1869,7 +1867,7 @@ pub async fn remove_2fa(
|
||||
SET totp_secret = NULL
|
||||
WHERE (id = $1)
|
||||
",
|
||||
user.id as crate::database::models::ids::UserId,
|
||||
user.id as crate::database::models::ids::DBUserId,
|
||||
)
|
||||
.execute(&mut *transaction)
|
||||
.await?;
|
||||
@@ -1879,7 +1877,7 @@ pub async fn remove_2fa(
|
||||
DELETE FROM user_backup_codes
|
||||
WHERE user_id = $1
|
||||
",
|
||||
user.id as crate::database::models::ids::UserId,
|
||||
user.id as crate::database::models::ids::DBUserId,
|
||||
)
|
||||
.execute(&mut *transaction)
|
||||
.await?;
|
||||
@@ -2081,7 +2079,7 @@ pub async fn change_password(
|
||||
WHERE (id = $2)
|
||||
",
|
||||
update_password,
|
||||
user.id as crate::database::models::ids::UserId,
|
||||
user.id as crate::database::models::ids::DBUserId,
|
||||
)
|
||||
.execute(&mut *transaction)
|
||||
.await?;
|
||||
@@ -2291,7 +2289,7 @@ pub async fn verify_email(
|
||||
SET email_verified = TRUE
|
||||
WHERE (id = $1)
|
||||
",
|
||||
user.id as crate::database::models::ids::UserId,
|
||||
user.id as crate::database::models::ids::DBUserId,
|
||||
)
|
||||
.execute(&mut *transaction)
|
||||
.await?;
|
||||
|
||||
Reference in New Issue
Block a user