Skip to content

Commit 4e4a7be

Browse files
authored
Commonize and distinguish a lot of struct names in labrinth::database::models (#3691)
1 parent 9c1bdf1 commit 4e4a7be

Some content is hidden

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

78 files changed

+1075
-1009
lines changed

apps/labrinth/src/auth/checks.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use crate::database;
2-
use crate::database::models::Collection;
3-
use crate::database::models::project_item::QueryProject;
4-
use crate::database::models::version_item::QueryVersion;
2+
use crate::database::models::DBCollection;
3+
use crate::database::models::project_item::ProjectQueryResult;
4+
use crate::database::models::version_item::VersionQueryResult;
55
use crate::database::redis::RedisPool;
6-
use crate::database::{Project, Version, models};
6+
use crate::database::{DBProject, DBVersion, models};
77
use crate::models::users::User;
88
use crate::routes::ApiError;
99
use itertools::Itertools;
@@ -38,7 +38,7 @@ where
3838
}
3939

4040
pub async fn is_visible_project(
41-
project_data: &Project,
41+
project_data: &DBProject,
4242
user_option: &Option<User>,
4343
pool: &PgPool,
4444
hide_unlisted: bool,
@@ -54,7 +54,7 @@ pub async fn is_visible_project(
5454
}
5555

5656
pub async fn is_team_member_project(
57-
project_data: &Project,
57+
project_data: &DBProject,
5858
user_option: &Option<User>,
5959
pool: &PgPool,
6060
) -> Result<bool, ApiError> {
@@ -64,7 +64,7 @@ pub async fn is_team_member_project(
6464
}
6565

6666
pub async fn filter_visible_projects(
67-
mut projects: Vec<QueryProject>,
67+
mut projects: Vec<ProjectQueryResult>,
6868
user_option: &Option<User>,
6969
pool: &PgPool,
7070
hide_unlisted: bool,
@@ -86,7 +86,7 @@ pub async fn filter_visible_projects(
8686
// - the user is a mod
8787
// This is essentially whether you can know of the project's existence
8888
pub async fn filter_visible_project_ids(
89-
projects: Vec<&Project>,
89+
projects: Vec<&DBProject>,
9090
user_option: &Option<User>,
9191
pool: &PgPool,
9292
hide_unlisted: bool,
@@ -126,7 +126,7 @@ pub async fn filter_visible_project_ids(
126126
// These are projects we have internal access to and can potentially see even if they are hidden
127127
// This is useful for getting visibility of versions, or seeing analytics or sensitive team-restricted data of a project
128128
pub async fn filter_enlisted_projects_ids(
129-
projects: Vec<&Project>,
129+
projects: Vec<&DBProject>,
130130
user_option: &Option<User>,
131131
pool: &PgPool,
132132
) -> Result<Vec<crate::database::models::DBProjectId>, ApiError> {
@@ -173,7 +173,7 @@ pub async fn filter_enlisted_projects_ids(
173173
}
174174

175175
pub async fn is_visible_version(
176-
version_data: &Version,
176+
version_data: &DBVersion,
177177
user_option: &Option<User>,
178178
pool: &PgPool,
179179
redis: &RedisPool,
@@ -184,7 +184,7 @@ pub async fn is_visible_version(
184184
}
185185

186186
pub async fn is_team_member_version(
187-
version_data: &Version,
187+
version_data: &DBVersion,
188188
user_option: &Option<User>,
189189
pool: &PgPool,
190190
redis: &RedisPool,
@@ -195,7 +195,7 @@ pub async fn is_team_member_version(
195195
}
196196

197197
pub async fn filter_visible_versions(
198-
mut versions: Vec<QueryVersion>,
198+
mut versions: Vec<VersionQueryResult>,
199199
user_option: &Option<User>,
200200
pool: &PgPool,
201201
redis: &RedisPool,
@@ -211,7 +211,7 @@ pub async fn filter_visible_versions(
211211
Ok(versions.into_iter().map(|x| x.into()).collect())
212212
}
213213

214-
impl ValidateAuthorized for models::OAuthClient {
214+
impl ValidateAuthorized for models::DBOAuthClient {
215215
fn validate_authorized(
216216
&self,
217217
user_option: Option<&User>,
@@ -232,7 +232,7 @@ impl ValidateAuthorized for models::OAuthClient {
232232
}
233233

234234
pub async fn filter_visible_version_ids(
235-
versions: Vec<&Version>,
235+
versions: Vec<&DBVersion>,
236236
user_option: &Option<User>,
237237
pool: &PgPool,
238238
redis: &RedisPool,
@@ -247,7 +247,7 @@ pub async fn filter_visible_version_ids(
247247

248248
// Get visible projects- ones we are allowed to see public versions for.
249249
let visible_project_ids = filter_visible_project_ids(
250-
Project::get_many_ids(&project_ids, pool, redis)
250+
DBProject::get_many_ids(&project_ids, pool, redis)
251251
.await?
252252
.iter()
253253
.map(|x| &x.inner)
@@ -287,7 +287,7 @@ pub async fn filter_visible_version_ids(
287287
}
288288

289289
pub async fn filter_enlisted_version_ids(
290-
versions: Vec<&Version>,
290+
versions: Vec<&DBVersion>,
291291
user_option: &Option<User>,
292292
pool: &PgPool,
293293
redis: &RedisPool,
@@ -299,7 +299,7 @@ pub async fn filter_enlisted_version_ids(
299299

300300
// Get enlisted projects- ones we are allowed to see hidden versions for.
301301
let authorized_project_ids = filter_enlisted_projects_ids(
302-
Project::get_many_ids(&project_ids, pool, redis)
302+
DBProject::get_many_ids(&project_ids, pool, redis)
303303
.await?
304304
.iter()
305305
.map(|x| &x.inner)
@@ -325,7 +325,7 @@ pub async fn filter_enlisted_version_ids(
325325
}
326326

327327
pub async fn is_visible_collection(
328-
collection_data: &Collection,
328+
collection_data: &DBCollection,
329329
user_option: &Option<User>,
330330
) -> Result<bool, ApiError> {
331331
let mut authorized = !collection_data.status.is_hidden()
@@ -341,7 +341,7 @@ pub async fn is_visible_collection(
341341
}
342342

343343
pub async fn filter_visible_collections(
344-
collections: Vec<Collection>,
344+
collections: Vec<DBCollection>,
345345
user_option: &Option<User>,
346346
) -> Result<Vec<crate::models::collections::Collection>, ApiError> {
347347
let mut return_collections = Vec::new();

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

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use crate::auth::get_user_from_headers;
22
use crate::auth::oauth::uris::{OAuthRedirectUris, ValidatedRedirectUri};
33
use crate::auth::validate::extract_authorization_header;
4-
use crate::database::models::flow_item::Flow;
5-
use crate::database::models::oauth_client_authorization_item::OAuthClientAuthorization;
6-
use crate::database::models::oauth_client_item::OAuthClient as DBOAuthClient;
7-
use crate::database::models::oauth_token_item::OAuthAccessToken;
4+
use crate::database::models::flow_item::DBFlow;
5+
use crate::database::models::oauth_client_authorization_item::DBOAuthClientAuthorization;
6+
use crate::database::models::oauth_client_item::DBOAuthClient;
7+
use crate::database::models::oauth_token_item::DBOAuthAccessToken;
88
use crate::database::models::{
99
DBOAuthClientAuthorizationId, generate_oauth_access_token_id,
1010
generate_oauth_client_authorization_id,
@@ -106,7 +106,7 @@ pub async fn init_oauth(
106106
}
107107

108108
let existing_authorization =
109-
OAuthClientAuthorization::get(client.id, user.id.into(), &**pool)
109+
DBOAuthClientAuthorization::get(client.id, user.id.into(), &**pool)
110110
.await
111111
.map_err(|e| {
112112
OAuthError::redirect(e, &oauth_info.state, &redirect_uri)
@@ -131,7 +131,7 @@ pub async fn init_oauth(
131131
.await
132132
}
133133
_ => {
134-
let flow_id = Flow::InitOAuthAppApproval {
134+
let flow_id = DBFlow::InitOAuthAppApproval {
135135
user_id: user.id.into(),
136136
client_id: client.id,
137137
existing_authorization_id: existing_authorization
@@ -231,13 +231,13 @@ pub async fn request_token(
231231

232232
// Ensure auth code is single use
233233
// per IETF RFC6749 Section 10.5 (https://datatracker.ietf.org/doc/html/rfc6749#section-10.5)
234-
let flow = Flow::take_if(
234+
let flow = DBFlow::take_if(
235235
&req_params.code,
236-
|f| matches!(f, Flow::OAuthAuthorizationCodeSupplied { .. }),
236+
|f| matches!(f, DBFlow::OAuthAuthorizationCodeSupplied { .. }),
237237
&redis,
238238
)
239239
.await?;
240-
if let Some(Flow::OAuthAuthorizationCodeSupplied {
240+
if let Some(DBFlow::OAuthAuthorizationCodeSupplied {
241241
user_id,
242242
client_id,
243243
authorization_id,
@@ -274,8 +274,8 @@ pub async fn request_token(
274274
let token_id =
275275
generate_oauth_access_token_id(&mut transaction).await?;
276276
let token = generate_access_token();
277-
let token_hash = OAuthAccessToken::hash_token(&token);
278-
let time_until_expiration = OAuthAccessToken {
277+
let token_hash = DBOAuthAccessToken::hash_token(&token);
278+
let time_until_expiration = DBOAuthAccessToken {
279279
id: token_id,
280280
authorization_id,
281281
token_hash,
@@ -328,13 +328,13 @@ pub async fn accept_or_reject_client_scopes(
328328
.await?
329329
.1;
330330

331-
let flow = Flow::take_if(
331+
let flow = DBFlow::take_if(
332332
&body.flow,
333-
|f| matches!(f, Flow::InitOAuthAppApproval { .. }),
333+
|f| matches!(f, DBFlow::InitOAuthAppApproval { .. }),
334334
&redis,
335335
)
336336
.await?;
337-
if let Some(Flow::InitOAuthAppApproval {
337+
if let Some(DBFlow::InitOAuthAppApproval {
338338
user_id,
339339
client_id,
340340
existing_authorization_id,
@@ -359,7 +359,7 @@ pub async fn accept_or_reject_client_scopes(
359359
.await?
360360
}
361361
};
362-
OAuthClientAuthorization::upsert(
362+
DBOAuthClientAuthorization::upsert(
363363
auth_id,
364364
client_id,
365365
user_id,
@@ -425,7 +425,7 @@ async fn init_oauth_code_flow(
425425
state: Option<String>,
426426
redis: &RedisPool,
427427
) -> Result<HttpResponse, OAuthError> {
428-
let code = Flow::OAuthAuthorizationCodeSupplied {
428+
let code = DBFlow::OAuthAuthorizationCodeSupplied {
429429
user_id,
430430
client_id: client_id.into(),
431431
authorization_id,

apps/labrinth/src/auth/validate.rs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ pub async fn get_user_record_from_bearer_token<'a, 'b, E>(
5050
executor: E,
5151
redis: &RedisPool,
5252
session_queue: &AuthQueue,
53-
) -> Result<Option<(Scopes, user_item::User)>, AuthenticationError>
53+
) -> Result<Option<(Scopes, user_item::DBUser)>, AuthenticationError>
5454
where
5555
E: sqlx::Executor<'a, Database = sqlx::Postgres> + Copy,
5656
{
@@ -63,7 +63,7 @@ where
6363
let possible_user = match token.split_once('_') {
6464
Some(("mrp", _)) => {
6565
let pat =
66-
crate::database::models::pat_item::PersonalAccessToken::get(
66+
crate::database::models::pat_item::DBPersonalAccessToken::get(
6767
token, executor, redis,
6868
)
6969
.await?
@@ -74,25 +74,26 @@ where
7474
}
7575

7676
let user =
77-
user_item::User::get_id(pat.user_id, executor, redis).await?;
77+
user_item::DBUser::get_id(pat.user_id, executor, redis).await?;
7878

7979
session_queue.add_pat(pat.id).await;
8080

8181
user.map(|x| (pat.scopes, x))
8282
}
8383
Some(("mra", _)) => {
84-
let session = crate::database::models::session_item::Session::get(
85-
token, executor, redis,
86-
)
87-
.await?
88-
.ok_or_else(|| AuthenticationError::InvalidCredentials)?;
84+
let session =
85+
crate::database::models::session_item::DBSession::get(
86+
token, executor, redis,
87+
)
88+
.await?
89+
.ok_or_else(|| AuthenticationError::InvalidCredentials)?;
8990

9091
if session.expires < Utc::now() {
9192
return Err(AuthenticationError::InvalidCredentials);
9293
}
9394

9495
let user =
95-
user_item::User::get_id(session.user_id, executor, redis)
96+
user_item::DBUser::get_id(session.user_id, executor, redis)
9697
.await?;
9798

9899
let rate_limit_ignore = dotenvy::var("RATE_LIMIT_IGNORE_KEY")?;
@@ -110,21 +111,24 @@ where
110111
user.map(|x| (Scopes::all(), x))
111112
}
112113
Some(("mro", _)) => {
113-
use crate::database::models::oauth_token_item::OAuthAccessToken;
114+
use crate::database::models::oauth_token_item::DBOAuthAccessToken;
114115

115-
let hash = OAuthAccessToken::hash_token(token);
116+
let hash = DBOAuthAccessToken::hash_token(token);
116117
let access_token =
117-
crate::database::models::oauth_token_item::OAuthAccessToken::get(hash, executor)
118+
crate::database::models::oauth_token_item::DBOAuthAccessToken::get(hash, executor)
118119
.await?
119120
.ok_or(AuthenticationError::InvalidCredentials)?;
120121

121122
if access_token.expires < Utc::now() {
122123
return Err(AuthenticationError::InvalidCredentials);
123124
}
124125

125-
let user =
126-
user_item::User::get_id(access_token.user_id, executor, redis)
127-
.await?;
126+
let user = user_item::DBUser::get_id(
127+
access_token.user_id,
128+
executor,
129+
redis,
130+
)
131+
.await?;
128132

129133
session_queue.add_oauth_access_token(access_token.id).await;
130134

@@ -135,7 +139,7 @@ where
135139
let id =
136140
AuthProvider::GitHub.get_user_id(&user.id, executor).await?;
137141

138-
let user = user_item::User::get_id(
142+
let user = user_item::DBUser::get_id(
139143
id.ok_or_else(|| AuthenticationError::InvalidCredentials)?,
140144
executor,
141145
redis,

apps/labrinth/src/database/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
pub mod models;
22
mod postgres_database;
33
pub mod redis;
4-
pub use models::Image;
5-
pub use models::Project;
6-
pub use models::Version;
4+
pub use models::DBImage;
5+
pub use models::DBProject;
6+
pub use models::DBVersion;
77
pub use postgres_database::check_for_migrations;
88
pub use postgres_database::connect;
99
pub use postgres_database::register_and_set_metrics;

0 commit comments

Comments
 (0)