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:
Josiah Glosson
2025-05-22 03:34:36 -05:00
committed by GitHub
parent c6022ad977
commit 9e527ff141
111 changed files with 1477 additions and 1965 deletions

View File

@@ -71,7 +71,7 @@ pub async fn count_download(
.ok()
.flatten();
let project_id: crate::database::models::ids::ProjectId =
let project_id: crate::database::models::ids::DBProjectId =
download_body.project_id.into();
let id_option =
@@ -97,7 +97,7 @@ pub async fn count_download(
WHERE ((version_number = $1 OR id = $3) AND mod_id = $2)
",
download_body.version_name,
project_id as crate::database::models::ids::ProjectId,
project_id as crate::database::models::ids::DBProjectId,
id_option
)
.fetch_optional(pool.as_ref())
@@ -255,7 +255,7 @@ pub async fn delphi_result_ingest(
let mut transaction = pool.begin().await?;
ThreadMessageBuilder {
author_id: Some(crate::database::models::UserId(AUTOMOD_ID)),
author_id: Some(crate::database::models::DBUserId(AUTOMOD_ID)),
body: MessageBody::Text {
body: thread_header,
private: true,

View File

@@ -85,7 +85,7 @@ pub async fn products(
#[derive(Deserialize)]
struct SubscriptionsQuery {
pub user_id: Option<crate::models::ids::UserId>,
pub user_id: Option<ariadne::ids::UserId>,
}
#[get("subscriptions")]
@@ -600,7 +600,7 @@ pub async fn user_customer(
#[derive(Deserialize)]
pub struct ChargesQuery {
pub user_id: Option<crate::models::ids::UserId>,
pub user_id: Option<ariadne::ids::UserId>,
}
#[get("payments")]
@@ -944,7 +944,7 @@ pub async fn active_servers(
#[derive(Serialize)]
struct ActiveServer {
pub user_id: crate::models::ids::UserId,
pub user_id: ariadne::ids::UserId,
pub server_id: String,
pub price_id: crate::models::ids::ProductPriceId,
pub interval: PriceDuration,
@@ -1439,7 +1439,7 @@ pub async fn stripe_webhook(
let user_id = if let Some(user_id) = metadata
.get("modrinth_user_id")
.and_then(|x| parse_base62(x).ok())
.map(|x| crate::database::models::ids::UserId(x as i64))
.map(|x| crate::database::models::ids::DBUserId(x as i64))
{
user_id
} else {
@@ -1464,7 +1464,7 @@ pub async fn stripe_webhook(
let charge_id = if let Some(charge_id) = metadata
.get("modrinth_charge_id")
.and_then(|x| parse_base62(x).ok())
.map(|x| crate::database::models::ids::ChargeId(x as i64))
.map(|x| crate::database::models::ids::DBChargeId(x as i64))
{
charge_id
} else {
@@ -1557,7 +1557,7 @@ pub async fn stripe_webhook(
.get("modrinth_price_id")
.and_then(|x| parse_base62(x).ok())
.map(|x| {
crate::database::models::ids::ProductPriceId(
crate::database::models::ids::DBProductPriceId(
x as i64,
)
}) {
@@ -1601,7 +1601,7 @@ pub async fn stripe_webhook(
.get("modrinth_subscription_id")
.and_then(|x| parse_base62(x).ok())
.map(|x| {
crate::database::models::ids::UserSubscriptionId(x as i64)
crate::database::models::ids::DBUserSubscriptionId(x as i64)
}) {
subscription_id
} else {
@@ -1736,7 +1736,7 @@ pub async fn stripe_webhook(
",
badges.bits() as i64,
metadata.user_item.id
as crate::database::models::ids::UserId,
as crate::database::models::ids::DBUserId,
)
.execute(&mut *transaction)
.await?;
@@ -2061,7 +2061,7 @@ pub async fn stripe_webhook(
}
async fn get_or_create_customer(
user_id: crate::models::ids::UserId,
user_id: ariadne::ids::UserId,
stripe_customer_id: Option<&str>,
user_email: Option<&str>,
client: &stripe::Client,
@@ -2212,7 +2212,7 @@ pub async fn index_subscriptions(pool: PgPool, redis: RedisPool) {
WHERE (id = $2)
",
badges.bits() as i64,
user.id as crate::database::models::ids::UserId,
user.id as crate::database::models::ids::DBUserId,
)
.execute(&mut *transaction)
.await?;

View File

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

View File

@@ -143,7 +143,7 @@ pub async fn export(
.fetch_all(pool.as_ref())
.await?
.into_iter()
.map(|x| crate::database::models::ids::ThreadMessageId(x.id))
.map(|x| crate::database::models::ids::DBThreadMessageId(x.id))
.collect::<Vec<_>>();
let messages =
@@ -163,7 +163,7 @@ pub async fn export(
.fetch_all(pool.as_ref())
.await?
.into_iter()
.map(|x| crate::database::models::ids::ImageId(x.id))
.map(|x| crate::database::models::ids::DBImageId(x.id))
.collect::<Vec<_>>();
let uploaded_images = crate::database::models::image_item::Image::get_many(

View File

@@ -56,8 +56,8 @@ pub async fn get_projects(
count.count as i64
)
.fetch(&**pool)
.map_ok(|m| database::models::ProjectId(m.id))
.try_collect::<Vec<database::models::ProjectId>>()
.map_ok(|m| database::models::DBProjectId(m.id))
.try_collect::<Vec<database::models::DBProjectId>>()
.await?;
let projects: Vec<_> =

View File

@@ -1,5 +1,5 @@
use crate::auth::{AuthenticationError, get_user_from_headers};
use crate::database::models::UserId;
use crate::database::models::DBUserId;
use crate::database::models::session_item::Session as DBSession;
use crate::database::models::session_item::SessionBuilder;
use crate::database::redis::RedisPool;
@@ -85,7 +85,7 @@ pub async fn get_session_metadata(
pub async fn issue_session(
req: HttpRequest,
user_id: UserId,
user_id: DBUserId,
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
redis: &RedisPool,
) -> Result<DBSession, AuthenticationError> {