Skip to content

Commit 9e527ff

Browse files
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>
1 parent c6022ad commit 9e527ff

File tree

111 files changed

+1453
-1941
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

111 files changed

+1453
-1941
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apps/labrinth/.sqlx/query-06bf1b34b70f5e61bf619c4d7706d07d6db413751ecab86896a708c8539e38b6.json renamed to apps/labrinth/.sqlx/query-0fcbb92442bdab97fd1b44a0eecb28c05a4c6d482b3fa7874902ee8e846d20b0.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apps/labrinth/.sqlx/query-1243d13d622a9970240c8f26b5031b4c68d08607f7a0142b662b53eb05b4723a.json renamed to apps/labrinth/.sqlx/query-282ec0ee4a4ef739fc24af40d2c9be2a335ea9a32a1d19a450aef02f1c58334e.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apps/labrinth/.sqlx/query-9abdd9a2018e7bfe26836dd5463ba0923ef0a76c32ca258faf55fc3301c567bf.json renamed to apps/labrinth/.sqlx/query-2d2cb03a63c4692080dd58f88f34ac63b20f89c9e7d6f520865133de60b9e911.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apps/labrinth/.sqlx/query-46c32719ad68b667e816f58f55b5b4829bff8f0fbb039a0eda7bf08a430ba466.json

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apps/labrinth/.sqlx/query-061a3e43df9464263aaf1555a27c1f4b6a0f381282f4fa75cc13b1d354857578.json renamed to apps/labrinth/.sqlx/query-a91d793778d92844cfe338007e8f25daca0d711e0569b99f14d820ddb1685405.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apps/labrinth/.sqlx/query-cdb2f18f826097f0f17a1f7295d7c45eb1987b63c1a21666c6ca60c52217ba4d.json renamed to apps/labrinth/.sqlx/query-ab9b4b383ce8431214eb26abde734d36eff6902ae55b7ed4d89a10c4d69506a9.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apps/labrinth/.sqlx/query-8d7746fedec4c2339352a3acd934b13c351b8a1fdb05bf982bab1a5b7ed17f3b.json renamed to apps/labrinth/.sqlx/query-e5c4275ebfa60938505ef8b32b54efa569c6b516b547faec9a10943ddef6266e.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apps/labrinth/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ futures.workspace = true
3131
futures-util.workspace = true
3232
async-trait.workspace = true
3333
dashmap.workspace = true
34+
paste.workspace = true
3435

3536
meilisearch-sdk = { workspace = true, features = ["reqwest"] }
3637
rust-s3.workspace = true

apps/labrinth/src/auth/checks.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ pub async fn filter_visible_project_ids(
9090
user_option: &Option<User>,
9191
pool: &PgPool,
9292
hide_unlisted: bool,
93-
) -> Result<Vec<crate::database::models::ProjectId>, ApiError> {
93+
) -> Result<Vec<crate::database::models::DBProjectId>, ApiError> {
9494
let mut return_projects = Vec::new();
9595
let mut check_projects = Vec::new();
9696

@@ -129,11 +129,11 @@ pub async fn filter_enlisted_projects_ids(
129129
projects: Vec<&Project>,
130130
user_option: &Option<User>,
131131
pool: &PgPool,
132-
) -> Result<Vec<crate::database::models::ProjectId>, ApiError> {
132+
) -> Result<Vec<crate::database::models::DBProjectId>, ApiError> {
133133
let mut return_projects = vec![];
134134

135135
if let Some(user) = user_option {
136-
let user_id: models::ids::UserId = user.id.into();
136+
let user_id: models::ids::DBUserId = user.id.into();
137137

138138
use futures::TryStreamExt;
139139

@@ -154,7 +154,7 @@ pub async fn filter_enlisted_projects_ids(
154154
.iter()
155155
.filter_map(|x| x.organization_id.map(|x| x.0))
156156
.collect::<Vec<_>>(),
157-
user_id as database::models::ids::UserId,
157+
user_id as database::models::ids::DBUserId,
158158
)
159159
.fetch(pool)
160160
.map_ok(|row| {
@@ -236,7 +236,7 @@ pub async fn filter_visible_version_ids(
236236
user_option: &Option<User>,
237237
pool: &PgPool,
238238
redis: &RedisPool,
239-
) -> Result<Vec<crate::database::models::VersionId>, ApiError> {
239+
) -> Result<Vec<crate::database::models::DBVersionId>, ApiError> {
240240
let mut return_versions = Vec::new();
241241
let mut check_versions = Vec::new();
242242

@@ -291,7 +291,7 @@ pub async fn filter_enlisted_version_ids(
291291
user_option: &Option<User>,
292292
pool: &PgPool,
293293
redis: &RedisPool,
294-
) -> Result<Vec<crate::database::models::VersionId>, ApiError> {
294+
) -> Result<Vec<crate::database::models::DBVersionId>, ApiError> {
295295
let mut return_versions = Vec::new();
296296

297297
// Get project ids of versions

apps/labrinth/src/auth/oauth/errors.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ pub enum OAuthErrorType {
116116
AuthenticationError(#[from] AuthenticationError),
117117
#[error("Client {} has no redirect URIs specified", .client_id.0)]
118118
ClientMissingRedirectURI {
119-
client_id: crate::database::models::OAuthClientId,
119+
client_id: crate::database::models::DBOAuthClientId,
120120
},
121121
#[error(
122122
"The provided redirect URI did not match any configured in the client"
@@ -133,7 +133,7 @@ pub enum OAuthErrorType {
133133
#[error("The provided flow id was invalid")]
134134
InvalidAcceptFlowId,
135135
#[error("The provided client id was invalid")]
136-
InvalidClientId(crate::database::models::OAuthClientId),
136+
InvalidClientId(crate::database::models::DBOAuthClientId),
137137
#[error("The provided ID could not be decoded: {0}")]
138138
MalformedId(#[from] DecodingError),
139139
#[error("Failed to authenticate client")]

apps/labrinth/src/auth/oauth/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::database::models::oauth_client_authorization_item::OAuthClientAuthori
66
use crate::database::models::oauth_client_item::OAuthClient as DBOAuthClient;
77
use crate::database::models::oauth_token_item::OAuthAccessToken;
88
use crate::database::models::{
9-
OAuthClientAuthorizationId, generate_oauth_access_token_id,
9+
DBOAuthClientAuthorizationId, generate_oauth_access_token_id,
1010
generate_oauth_client_authorization_id,
1111
};
1212
use crate::database::redis::RedisPool;
@@ -417,9 +417,9 @@ fn generate_access_token() -> String {
417417
}
418418

419419
async fn init_oauth_code_flow(
420-
user_id: crate::database::models::UserId,
420+
user_id: crate::database::models::DBUserId,
421421
client_id: OAuthClientId,
422-
authorization_id: OAuthClientAuthorizationId,
422+
authorization_id: DBOAuthClientAuthorizationId,
423423
scopes: Scopes,
424424
redirect_uris: OAuthRedirectUris,
425425
state: Option<String>,

apps/labrinth/src/auth/oauth/uris.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::errors::OAuthError;
22
use crate::auth::oauth::OAuthErrorType;
3-
use crate::database::models::OAuthClientId;
3+
use crate::database::models::DBOAuthClientId;
44
use serde::{Deserialize, Serialize};
55

66
#[derive(Serialize, Deserialize)]
@@ -16,7 +16,7 @@ impl ValidatedRedirectUri {
1616
pub fn validate<'a>(
1717
to_validate: &Option<String>,
1818
validate_against: impl IntoIterator<Item = &'a str> + Clone,
19-
client_id: OAuthClientId,
19+
client_id: DBOAuthClientId,
2020
) -> Result<Self, OAuthError> {
2121
if let Some(first_client_redirect_uri) =
2222
validate_against.clone().into_iter().next()
@@ -61,7 +61,7 @@ mod tests {
6161
let validated = ValidatedRedirectUri::validate(
6262
&None,
6363
validate_against.clone(),
64-
OAuthClientId(0),
64+
DBOAuthClientId(0),
6565
)
6666
.unwrap();
6767

@@ -82,7 +82,7 @@ mod tests {
8282
let validated = ValidatedRedirectUri::validate(
8383
&Some(to_validate.clone()),
8484
validate_against,
85-
OAuthClientId(0),
85+
DBOAuthClientId(0),
8686
)
8787
.unwrap();
8888

@@ -97,7 +97,7 @@ mod tests {
9797
let validated = ValidatedRedirectUri::validate(
9898
&Some(to_validate),
9999
validate_against,
100-
OAuthClientId(0),
100+
DBOAuthClientId(0),
101101
);
102102

103103
assert!(validated.is_err_and(|e| matches!(

apps/labrinth/src/database/models/charge_item.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::database::models::{
2-
ChargeId, DatabaseError, ProductPriceId, UserId, UserSubscriptionId,
2+
DBChargeId, DBProductPriceId, DBUserId, DBUserSubscriptionId, DatabaseError,
33
};
44
use crate::models::billing::{
55
ChargeStatus, ChargeType, PaymentPlatform, PriceDuration,
@@ -8,23 +8,23 @@ use chrono::{DateTime, Utc};
88
use std::convert::{TryFrom, TryInto};
99

1010
pub struct ChargeItem {
11-
pub id: ChargeId,
12-
pub user_id: UserId,
13-
pub price_id: ProductPriceId,
11+
pub id: DBChargeId,
12+
pub user_id: DBUserId,
13+
pub price_id: DBProductPriceId,
1414
pub amount: i64,
1515
pub currency_code: String,
1616
pub status: ChargeStatus,
1717
pub due: DateTime<Utc>,
1818
pub last_attempt: Option<DateTime<Utc>>,
1919

2020
pub type_: ChargeType,
21-
pub subscription_id: Option<UserSubscriptionId>,
21+
pub subscription_id: Option<DBUserSubscriptionId>,
2222
pub subscription_interval: Option<PriceDuration>,
2323

2424
pub payment_platform: PaymentPlatform,
2525
pub payment_platform_id: Option<String>,
2626

27-
pub parent_charge_id: Option<ChargeId>,
27+
pub parent_charge_id: Option<DBChargeId>,
2828

2929
// Net is always in USD
3030
pub net: Option<i64>,
@@ -53,22 +53,22 @@ impl TryFrom<ChargeResult> for ChargeItem {
5353

5454
fn try_from(r: ChargeResult) -> Result<Self, Self::Error> {
5555
Ok(ChargeItem {
56-
id: ChargeId(r.id),
57-
user_id: UserId(r.user_id),
58-
price_id: ProductPriceId(r.price_id),
56+
id: DBChargeId(r.id),
57+
user_id: DBUserId(r.user_id),
58+
price_id: DBProductPriceId(r.price_id),
5959
amount: r.amount,
6060
currency_code: r.currency_code,
6161
status: ChargeStatus::from_string(&r.status),
6262
due: r.due,
6363
last_attempt: r.last_attempt,
6464
type_: ChargeType::from_string(&r.charge_type),
65-
subscription_id: r.subscription_id.map(UserSubscriptionId),
65+
subscription_id: r.subscription_id.map(DBUserSubscriptionId),
6666
subscription_interval: r
6767
.subscription_interval
6868
.map(|x| PriceDuration::from_string(&x)),
6969
payment_platform: PaymentPlatform::from_string(&r.payment_platform),
7070
payment_platform_id: r.payment_platform_id,
71-
parent_charge_id: r.parent_charge_id.map(ChargeId),
71+
parent_charge_id: r.parent_charge_id.map(DBChargeId),
7272
net: r.net,
7373
})
7474
}
@@ -100,7 +100,7 @@ impl ChargeItem {
100100
pub async fn upsert(
101101
&self,
102102
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
103-
) -> Result<ChargeId, DatabaseError> {
103+
) -> Result<DBChargeId, DatabaseError> {
104104
sqlx::query!(
105105
r#"
106106
INSERT INTO charges (id, user_id, price_id, amount, currency_code, charge_type, status, due, last_attempt, subscription_id, subscription_interval, payment_platform, payment_platform_id, parent_charge_id, net)
@@ -144,7 +144,7 @@ impl ChargeItem {
144144
}
145145

146146
pub async fn get(
147-
id: ChargeId,
147+
id: DBChargeId,
148148
exec: impl sqlx::Executor<'_, Database = sqlx::Postgres>,
149149
) -> Result<Option<ChargeItem>, DatabaseError> {
150150
let id = id.0;
@@ -156,7 +156,7 @@ impl ChargeItem {
156156
}
157157

158158
pub async fn get_from_user(
159-
user_id: UserId,
159+
user_id: DBUserId,
160160
exec: impl sqlx::Executor<'_, Database = sqlx::Postgres>,
161161
) -> Result<Vec<ChargeItem>, DatabaseError> {
162162
let user_id = user_id.0;
@@ -174,7 +174,7 @@ impl ChargeItem {
174174
}
175175

176176
pub async fn get_children(
177-
charge_id: ChargeId,
177+
charge_id: DBChargeId,
178178
exec: impl sqlx::Executor<'_, Database = sqlx::Postgres>,
179179
) -> Result<Vec<ChargeItem>, DatabaseError> {
180180
let charge_id = charge_id.0;
@@ -192,7 +192,7 @@ impl ChargeItem {
192192
}
193193

194194
pub async fn get_open_subscription(
195-
user_subscription_id: UserSubscriptionId,
195+
user_subscription_id: DBUserSubscriptionId,
196196
exec: impl sqlx::Executor<'_, Database = sqlx::Postgres>,
197197
) -> Result<Option<ChargeItem>, DatabaseError> {
198198
let user_subscription_id = user_subscription_id.0;
@@ -255,7 +255,7 @@ impl ChargeItem {
255255
}
256256

257257
pub async fn remove(
258-
id: ChargeId,
258+
id: DBChargeId,
259259
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
260260
) -> Result<(), DatabaseError> {
261261
sqlx::query!(

0 commit comments

Comments
 (0)