1
0

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

@@ -9,20 +9,20 @@ const IMAGES_NAMESPACE: &str = "images";
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Image {
pub id: ImageId,
pub id: DBImageId,
pub url: String,
pub raw_url: String,
pub size: u64,
pub created: DateTime<Utc>,
pub owner_id: UserId,
pub owner_id: DBUserId,
// context it is associated with
pub context: String,
pub project_id: Option<ProjectId>,
pub version_id: Option<VersionId>,
pub thread_message_id: Option<ThreadMessageId>,
pub report_id: Option<ReportId>,
pub project_id: Option<DBProjectId>,
pub version_id: Option<DBVersionId>,
pub thread_message_id: Option<DBThreadMessageId>,
pub report_id: Option<DBReportId>,
}
impl Image {
@@ -39,12 +39,12 @@ impl Image {
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11
);
",
self.id as ImageId,
self.id as DBImageId,
self.url,
self.raw_url,
self.size as i64,
self.created,
self.owner_id as UserId,
self.owner_id as DBUserId,
self.context,
self.project_id.map(|x| x.0),
self.version_id.map(|x| x.0),
@@ -58,7 +58,7 @@ impl Image {
}
pub async fn remove(
id: ImageId,
id: DBImageId,
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
redis: &RedisPool,
) -> Result<Option<()>, DatabaseError> {
@@ -70,7 +70,7 @@ impl Image {
DELETE FROM uploaded_images
WHERE id = $1
",
id as ImageId,
id as DBImageId,
)
.execute(&mut **transaction)
.await?;
@@ -98,22 +98,22 @@ impl Image {
ImageContext::Project {
project_id: Some(id),
} => {
project_id = Some(ProjectId::from(id));
project_id = Some(DBProjectId::from(id));
}
ImageContext::Version {
version_id: Some(id),
} => {
version_id = Some(VersionId::from(id));
version_id = Some(DBVersionId::from(id));
}
ImageContext::ThreadMessage {
thread_message_id: Some(id),
} => {
thread_message_id = Some(ThreadMessageId::from(id));
thread_message_id = Some(DBThreadMessageId::from(id));
}
ImageContext::Report {
report_id: Some(id),
} => {
report_id = Some(ReportId::from(id));
report_id = Some(DBReportId::from(id));
}
_ => {}
}
@@ -139,7 +139,7 @@ impl Image {
)
.fetch(&mut **transaction)
.map_ok(|row| {
let id = ImageId(row.id);
let id = DBImageId(row.id);
Image {
id,
@@ -147,12 +147,12 @@ impl Image {
raw_url: row.raw_url,
size: row.size as u64,
created: row.created,
owner_id: UserId(row.owner_id),
owner_id: DBUserId(row.owner_id),
context: row.context,
project_id: row.mod_id.map(ProjectId),
version_id: row.version_id.map(VersionId),
thread_message_id: row.thread_message_id.map(ThreadMessageId),
report_id: row.report_id.map(ReportId),
project_id: row.mod_id.map(DBProjectId),
version_id: row.version_id.map(DBVersionId),
thread_message_id: row.thread_message_id.map(DBThreadMessageId),
report_id: row.report_id.map(DBReportId),
}
})
.try_collect::<Vec<Image>>()
@@ -160,7 +160,7 @@ impl Image {
}
pub async fn get<'a, 'b, E>(
id: ImageId,
id: DBImageId,
executor: E,
redis: &RedisPool,
) -> Result<Option<Image>, DatabaseError>
@@ -173,7 +173,7 @@ impl Image {
}
pub async fn get_many<'a, E>(
image_ids: &[ImageId],
image_ids: &[DBImageId],
exec: E,
redis: &RedisPool,
) -> Result<Vec<Image>, DatabaseError>
@@ -198,17 +198,17 @@ impl Image {
.fetch(exec)
.try_fold(DashMap::new(), |acc, i| {
let img = Image {
id: ImageId(i.id),
id: DBImageId(i.id),
url: i.url,
raw_url: i.raw_url,
size: i.size as u64,
created: i.created,
owner_id: UserId(i.owner_id),
owner_id: DBUserId(i.owner_id),
context: i.context,
project_id: i.mod_id.map(ProjectId),
version_id: i.version_id.map(VersionId),
thread_message_id: i.thread_message_id.map(ThreadMessageId),
report_id: i.report_id.map(ReportId),
project_id: i.mod_id.map(DBProjectId),
version_id: i.version_id.map(DBVersionId),
thread_message_id: i.thread_message_id.map(DBThreadMessageId),
report_id: i.report_id.map(DBReportId),
};
acc.insert(i.id, img);
@@ -224,7 +224,7 @@ impl Image {
}
pub async fn clear_cache(
id: ImageId,
id: DBImageId,
redis: &RedisPool,
) -> Result<(), DatabaseError> {
let mut redis = redis.connect().await?;