refactor(domain): algebraic NotificationKind — invalid states now unrepresentable
This commit is contained in:
@@ -2,33 +2,11 @@ use crate::db_error::IntoDbResult;
|
||||
use async_trait::async_trait;
|
||||
use chrono::{DateTime, Utc};
|
||||
|
||||
fn notif_type_from_str(s: &str) -> domain::models::notification::NotificationType {
|
||||
use domain::models::notification::NotificationType;
|
||||
match s {
|
||||
"like" => NotificationType::Like,
|
||||
"boost" => NotificationType::Boost,
|
||||
"follow" => NotificationType::Follow,
|
||||
"mention" => NotificationType::Mention,
|
||||
_ => NotificationType::Reply,
|
||||
}
|
||||
}
|
||||
|
||||
fn notif_type_as_str(t: &domain::models::notification::NotificationType) -> &'static str {
|
||||
use domain::models::notification::NotificationType;
|
||||
match t {
|
||||
NotificationType::Like => "like",
|
||||
NotificationType::Boost => "boost",
|
||||
NotificationType::Follow => "follow",
|
||||
NotificationType::Mention => "mention",
|
||||
NotificationType::Reply => "reply",
|
||||
}
|
||||
}
|
||||
|
||||
use domain::{
|
||||
errors::DomainError,
|
||||
models::{
|
||||
feed::{PageParams, Paginated},
|
||||
notification::Notification,
|
||||
notification::{Notification, NotificationKind},
|
||||
},
|
||||
ports::NotificationRepository,
|
||||
value_objects::{NotificationId, ThoughtId, UserId},
|
||||
@@ -44,17 +22,83 @@ impl PgNotificationRepository {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(sqlx::FromRow)]
|
||||
struct NotificationRow {
|
||||
id: uuid::Uuid,
|
||||
user_id: uuid::Uuid,
|
||||
notification_type: String,
|
||||
from_user_id: Option<uuid::Uuid>,
|
||||
thought_id: Option<uuid::Uuid>,
|
||||
read: bool,
|
||||
created_at: DateTime<Utc>,
|
||||
}
|
||||
|
||||
fn row_to_notification(r: NotificationRow) -> Result<Notification, DomainError> {
|
||||
let from_user_id = r
|
||||
.from_user_id
|
||||
.map(UserId::from_uuid)
|
||||
.ok_or_else(|| DomainError::Internal("notification missing from_user_id".into()))?;
|
||||
|
||||
let kind = match r.notification_type.as_str() {
|
||||
"follow" => NotificationKind::Follow { from_user_id },
|
||||
other => {
|
||||
let thought_id = r.thought_id.map(ThoughtId::from_uuid).ok_or_else(|| {
|
||||
DomainError::Internal(format!("notification type '{other}' missing thought_id"))
|
||||
})?;
|
||||
match other {
|
||||
"like" => NotificationKind::Like {
|
||||
thought_id,
|
||||
from_user_id,
|
||||
},
|
||||
"boost" => NotificationKind::Boost {
|
||||
thought_id,
|
||||
from_user_id,
|
||||
},
|
||||
"reply" => NotificationKind::Reply {
|
||||
thought_id,
|
||||
from_user_id,
|
||||
},
|
||||
"mention" => NotificationKind::Mention {
|
||||
thought_id,
|
||||
from_user_id,
|
||||
},
|
||||
_ => {
|
||||
return Err(DomainError::Internal(format!(
|
||||
"unknown notification type: {other}"
|
||||
)))
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Ok(Notification {
|
||||
id: NotificationId::from_uuid(r.id),
|
||||
user_id: UserId::from_uuid(r.user_id),
|
||||
kind,
|
||||
read: r.read,
|
||||
created_at: r.created_at,
|
||||
})
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl NotificationRepository for PgNotificationRepository {
|
||||
async fn save(&self, n: &Notification) -> Result<(), DomainError> {
|
||||
sqlx::query(
|
||||
"INSERT INTO notifications(id,user_id,type,from_user_id,thought_id,read,created_at) VALUES($1,$2,$3,$4,$5,$6,$7)"
|
||||
"INSERT INTO notifications(id,user_id,notification_type,from_user_id,thought_id,read,created_at)
|
||||
VALUES($1,$2,$3,$4,$5,$6,$7)
|
||||
ON CONFLICT(id) DO NOTHING"
|
||||
)
|
||||
.bind(n.id.as_uuid()).bind(n.user_id.as_uuid()).bind(notif_type_as_str(&n.notification_type))
|
||||
.bind(n.from_user_id.as_ref().map(|u| u.as_uuid()))
|
||||
.bind(n.thought_id.as_ref().map(|t| t.as_uuid()))
|
||||
.bind(n.read).bind(n.created_at)
|
||||
.execute(&self.pool).await.into_domain().map(|_| ())
|
||||
.bind(n.id.as_uuid())
|
||||
.bind(n.user_id.as_uuid())
|
||||
.bind(n.kind.kind_str())
|
||||
.bind(n.kind.from_user_id().as_uuid())
|
||||
.bind(n.kind.thought_id().map(|t| t.as_uuid()))
|
||||
.bind(n.read)
|
||||
.bind(n.created_at)
|
||||
.execute(&self.pool)
|
||||
.await
|
||||
.into_domain()
|
||||
.map(|_| ())
|
||||
}
|
||||
|
||||
async fn list_for_user(
|
||||
@@ -67,32 +111,14 @@ impl NotificationRepository for PgNotificationRepository {
|
||||
.fetch_one(&self.pool)
|
||||
.await
|
||||
.into_domain()?;
|
||||
#[derive(sqlx::FromRow)]
|
||||
struct Row {
|
||||
id: uuid::Uuid,
|
||||
user_id: uuid::Uuid,
|
||||
r#type: String,
|
||||
from_user_id: Option<uuid::Uuid>,
|
||||
thought_id: Option<uuid::Uuid>,
|
||||
read: bool,
|
||||
created_at: DateTime<Utc>,
|
||||
}
|
||||
let rows = sqlx::query_as::<_, Row>(
|
||||
"SELECT id,user_id,type,from_user_id,thought_id,read,created_at FROM notifications WHERE user_id=$1 ORDER BY created_at DESC LIMIT $2 OFFSET $3"
|
||||
let rows = sqlx::query_as::<_, NotificationRow>(
|
||||
"SELECT id,user_id,notification_type,from_user_id,thought_id,read,created_at FROM notifications WHERE user_id=$1 ORDER BY created_at DESC LIMIT $2 OFFSET $3"
|
||||
).bind(user_id.as_uuid()).bind(page.limit()).bind(page.offset())
|
||||
.fetch_all(&self.pool).await.into_domain()?;
|
||||
let items = rows
|
||||
.into_iter()
|
||||
.map(|r| Notification {
|
||||
id: NotificationId::from_uuid(r.id),
|
||||
user_id: UserId::from_uuid(r.user_id),
|
||||
notification_type: notif_type_from_str(&r.r#type),
|
||||
from_user_id: r.from_user_id.map(UserId::from_uuid),
|
||||
thought_id: r.thought_id.map(ThoughtId::from_uuid),
|
||||
read: r.read,
|
||||
created_at: r.created_at,
|
||||
})
|
||||
.collect();
|
||||
.map(row_to_notification)
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
Ok(Paginated {
|
||||
items,
|
||||
total,
|
||||
@@ -139,7 +165,7 @@ mod tests {
|
||||
use chrono::Utc;
|
||||
use domain::ports::UserWriter;
|
||||
use domain::{
|
||||
models::{notification::NotificationType, user::User},
|
||||
models::{notification::NotificationKind, user::User},
|
||||
value_objects::*,
|
||||
};
|
||||
|
||||
@@ -158,14 +184,15 @@ mod tests {
|
||||
#[sqlx::test(migrations = "./migrations")]
|
||||
async fn save_and_list(pool: sqlx::PgPool) {
|
||||
let user = seed_user(&pool).await;
|
||||
let from_user = seed_user(&pool).await;
|
||||
let repo = PgNotificationRepository::new(pool);
|
||||
use domain::models::feed::PageParams;
|
||||
let n = Notification {
|
||||
id: NotificationId::new(),
|
||||
user_id: user.id.clone(),
|
||||
notification_type: NotificationType::Like,
|
||||
from_user_id: None,
|
||||
thought_id: None,
|
||||
kind: NotificationKind::Follow {
|
||||
from_user_id: from_user.id.clone(),
|
||||
},
|
||||
read: false,
|
||||
created_at: Utc::now(),
|
||||
};
|
||||
@@ -187,14 +214,15 @@ mod tests {
|
||||
#[sqlx::test(migrations = "./migrations")]
|
||||
async fn mark_all_read(pool: sqlx::PgPool) {
|
||||
let user = seed_user(&pool).await;
|
||||
let from_user = seed_user(&pool).await;
|
||||
let repo = PgNotificationRepository::new(pool);
|
||||
use domain::models::feed::PageParams;
|
||||
let n = Notification {
|
||||
id: NotificationId::new(),
|
||||
user_id: user.id.clone(),
|
||||
notification_type: NotificationType::Follow,
|
||||
from_user_id: None,
|
||||
thought_id: None,
|
||||
kind: NotificationKind::Follow {
|
||||
from_user_id: from_user.id.clone(),
|
||||
},
|
||||
read: false,
|
||||
created_at: Utc::now(),
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user