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

@@ -13,8 +13,8 @@ pub struct NotificationBuilder {
#[derive(Serialize, Deserialize)]
pub struct Notification {
pub id: NotificationId,
pub user_id: UserId,
pub id: DBNotificationId,
pub user_id: DBUserId,
pub body: NotificationBody,
pub read: bool,
pub created: DateTime<Utc>,
@@ -23,7 +23,7 @@ pub struct Notification {
#[derive(Serialize, Deserialize)]
pub struct NotificationAction {
pub id: NotificationActionId,
pub notification_id: NotificationId,
pub notification_id: DBNotificationId,
pub name: String,
pub action_route_method: String,
pub action_route: String,
@@ -32,7 +32,7 @@ pub struct NotificationAction {
impl NotificationBuilder {
pub async fn insert(
&self,
user: UserId,
user: DBUserId,
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
redis: &RedisPool,
) -> Result<(), DatabaseError> {
@@ -41,7 +41,7 @@ impl NotificationBuilder {
pub async fn insert_many(
&self,
users: Vec<UserId>,
users: Vec<DBUserId>,
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
redis: &RedisPool,
) -> Result<(), DatabaseError> {
@@ -80,7 +80,7 @@ impl NotificationBuilder {
impl Notification {
pub async fn get<'a, 'b, E>(
id: NotificationId,
id: DBNotificationId,
executor: E,
) -> Result<Option<Self>, sqlx::error::Error>
where
@@ -92,7 +92,7 @@ impl Notification {
}
pub async fn get_many<'a, E>(
notification_ids: &[NotificationId],
notification_ids: &[DBNotificationId],
exec: E,
) -> Result<Vec<Notification>, sqlx::Error>
where
@@ -114,11 +114,11 @@ impl Notification {
)
.fetch(exec)
.map_ok(|row| {
let id = NotificationId(row.id);
let id = DBNotificationId(row.id);
Notification {
id,
user_id: UserId(row.user_id),
user_id: DBUserId(row.user_id),
read: row.read,
created: row.created,
body: row.body.clone().and_then(|x| serde_json::from_value(x).ok()).unwrap_or_else(|| {
@@ -145,7 +145,7 @@ impl Notification {
}
pub async fn get_many_user<'a, E>(
user_id: UserId,
user_id: DBUserId,
exec: E,
redis: &RedisPool,
) -> Result<Vec<Notification>, DatabaseError>
@@ -174,15 +174,15 @@ impl Notification {
WHERE n.user_id = $1
GROUP BY n.id, n.user_id;
",
user_id as UserId
user_id as DBUserId
)
.fetch(exec)
.map_ok(|row| {
let id = NotificationId(row.id);
let id = DBNotificationId(row.id);
Notification {
id,
user_id: UserId(row.user_id),
user_id: DBUserId(row.user_id),
read: row.read,
created: row.created,
body: row.body.clone().and_then(|x| serde_json::from_value(x).ok()).unwrap_or_else(|| {
@@ -220,7 +220,7 @@ impl Notification {
}
pub async fn read(
id: NotificationId,
id: DBNotificationId,
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
redis: &RedisPool,
) -> Result<Option<()>, DatabaseError> {
@@ -228,7 +228,7 @@ impl Notification {
}
pub async fn read_many(
notification_ids: &[NotificationId],
notification_ids: &[DBNotificationId],
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
redis: &RedisPool,
) -> Result<Option<()>, DatabaseError> {
@@ -245,7 +245,7 @@ impl Notification {
&notification_ids_parsed
)
.fetch(&mut **transaction)
.map_ok(|x| UserId(x.user_id))
.map_ok(|x| DBUserId(x.user_id))
.try_collect::<Vec<_>>()
.await?;
@@ -259,7 +259,7 @@ impl Notification {
}
pub async fn remove(
id: NotificationId,
id: DBNotificationId,
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
redis: &RedisPool,
) -> Result<Option<()>, DatabaseError> {
@@ -267,7 +267,7 @@ impl Notification {
}
pub async fn remove_many(
notification_ids: &[NotificationId],
notification_ids: &[DBNotificationId],
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
redis: &RedisPool,
) -> Result<Option<()>, DatabaseError> {
@@ -293,7 +293,7 @@ impl Notification {
&notification_ids_parsed
)
.fetch(&mut **transaction)
.map_ok(|x| UserId(x.user_id))
.map_ok(|x| DBUserId(x.user_id))
.try_collect::<Vec<_>>()
.await?;
@@ -307,7 +307,7 @@ impl Notification {
}
pub async fn clear_user_notifications_cache(
user_ids: impl IntoIterator<Item = &UserId>,
user_ids: impl IntoIterator<Item = &DBUserId>,
redis: &RedisPool,
) -> Result<(), DatabaseError> {
let mut redis = redis.connect().await?;