refactor(domain): move DB string conversions out of domain enums

This commit is contained in:
2026-05-15 01:54:32 +02:00
parent a123c0b8cc
commit 344bcf34af
11 changed files with 124 additions and 74 deletions

View File

@@ -1,6 +1,16 @@
use async_trait::async_trait; use async_trait::async_trait;
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use domain::models::thought::Visibility;
fn visibility_from_str(s: &str) -> domain::models::thought::Visibility {
use domain::models::thought::Visibility;
match s {
"followers" => Visibility::Followers,
"unlisted" => Visibility::Unlisted,
"direct" => Visibility::Direct,
_ => Visibility::Public,
}
}
use domain::{ use domain::{
errors::DomainError, errors::DomainError,
models::{ models::{
@@ -81,7 +91,7 @@ fn row_to_entry(r: FeedRow) -> FeedEntry {
in_reply_to_id: r.in_reply_to_id.map(ThoughtId::from_uuid), in_reply_to_id: r.in_reply_to_id.map(ThoughtId::from_uuid),
in_reply_to_url: r.in_reply_to_url, in_reply_to_url: r.in_reply_to_url,
ap_id: r.t_ap_id, ap_id: r.t_ap_id,
visibility: Visibility::from_db_str(&r.visibility), visibility: visibility_from_str(&r.visibility),
content_warning: r.content_warning, content_warning: r.content_warning,
sensitive: r.sensitive, sensitive: r.sensitive,
local: r.t_local, local: r.t_local,

View File

@@ -1,6 +1,16 @@
use async_trait::async_trait; use async_trait::async_trait;
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use domain::models::thought::Visibility;
fn visibility_from_str(s: &str) -> domain::models::thought::Visibility {
use domain::models::thought::Visibility;
match s {
"followers" => Visibility::Followers,
"unlisted" => Visibility::Unlisted,
"direct" => Visibility::Direct,
_ => Visibility::Public,
}
}
use domain::{ use domain::{
errors::DomainError, errors::DomainError,
models::{ models::{
@@ -95,7 +105,7 @@ fn row_to_entry(r: FeedRow) -> FeedEntry {
in_reply_to_id: r.in_reply_to_id.map(ThoughtId::from_uuid), in_reply_to_id: r.in_reply_to_id.map(ThoughtId::from_uuid),
in_reply_to_url: r.in_reply_to_url, in_reply_to_url: r.in_reply_to_url,
ap_id: r.t_ap_id, ap_id: r.t_ap_id,
visibility: Visibility::from_db_str(&r.visibility), visibility: visibility_from_str(&r.visibility),
content_warning: r.content_warning, content_warning: r.content_warning,
sensitive: r.sensitive, sensitive: r.sensitive,
local: r.t_local, local: r.t_local,

View File

@@ -1,5 +1,24 @@
use async_trait::async_trait; use async_trait::async_trait;
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
fn follow_state_from_str(s: &str) -> domain::models::social::FollowState {
use domain::models::social::FollowState;
match s {
"pending" => FollowState::Pending,
"rejected" => FollowState::Rejected,
_ => FollowState::Accepted,
}
}
fn follow_state_as_str(state: &domain::models::social::FollowState) -> &'static str {
use domain::models::social::FollowState;
match state {
FollowState::Pending => "pending",
FollowState::Accepted => "accepted",
FollowState::Rejected => "rejected",
}
}
use domain::{ use domain::{
errors::DomainError, errors::DomainError,
models::{ models::{
@@ -31,7 +50,7 @@ impl FollowRepository for PgFollowRepository {
) )
.bind(f.follower_id.as_uuid()) .bind(f.follower_id.as_uuid())
.bind(f.following_id.as_uuid()) .bind(f.following_id.as_uuid())
.bind(f.state.as_str()) .bind(follow_state_as_str(&f.state))
.bind(&f.ap_id) .bind(&f.ap_id)
.bind(f.created_at) .bind(f.created_at)
.execute(&self.pool) .execute(&self.pool)
@@ -77,7 +96,7 @@ impl FollowRepository for PgFollowRepository {
.map(|o| o.map(|r| Follow { .map(|o| o.map(|r| Follow {
follower_id: UserId::from_uuid(r.follower_id), follower_id: UserId::from_uuid(r.follower_id),
following_id: UserId::from_uuid(r.following_id), following_id: UserId::from_uuid(r.following_id),
state: FollowState::from_db_str(&r.state), state: follow_state_from_str(&r.state),
ap_id: r.ap_id, ap_id: r.ap_id,
created_at: r.created_at, created_at: r.created_at,
})) }))
@@ -92,7 +111,7 @@ impl FollowRepository for PgFollowRepository {
sqlx::query("UPDATE follows SET state=$3 WHERE follower_id=$1 AND following_id=$2") sqlx::query("UPDATE follows SET state=$3 WHERE follower_id=$1 AND following_id=$2")
.bind(follower_id.as_uuid()) .bind(follower_id.as_uuid())
.bind(following_id.as_uuid()) .bind(following_id.as_uuid())
.bind(state.as_str()) .bind(follow_state_as_str(state))
.execute(&self.pool) .execute(&self.pool)
.await .await
.map_err(|e| DomainError::Internal(e.to_string())) .map_err(|e| DomainError::Internal(e.to_string()))

View File

@@ -1,10 +1,33 @@
use async_trait::async_trait; use async_trait::async_trait;
use chrono::{DateTime, Utc}; 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::{ use domain::{
errors::DomainError, errors::DomainError,
models::{ models::{
feed::{PageParams, Paginated}, feed::{PageParams, Paginated},
notification::{Notification, NotificationType}, notification::Notification,
}, },
ports::NotificationRepository, ports::NotificationRepository,
value_objects::{NotificationId, ThoughtId, UserId}, value_objects::{NotificationId, ThoughtId, UserId},
@@ -26,7 +49,7 @@ impl NotificationRepository for PgNotificationRepository {
sqlx::query( 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,type,from_user_id,thought_id,read,created_at) VALUES($1,$2,$3,$4,$5,$6,$7)"
) )
.bind(n.id.as_uuid()).bind(n.user_id.as_uuid()).bind(n.notification_type.as_str()) .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.from_user_id.as_ref().map(|u| u.as_uuid()))
.bind(n.thought_id.as_ref().map(|t| t.as_uuid())) .bind(n.thought_id.as_ref().map(|t| t.as_uuid()))
.bind(n.read).bind(n.created_at) .bind(n.read).bind(n.created_at)
@@ -62,7 +85,7 @@ impl NotificationRepository for PgNotificationRepository {
.map(|r| Notification { .map(|r| Notification {
id: NotificationId::from_uuid(r.id), id: NotificationId::from_uuid(r.id),
user_id: UserId::from_uuid(r.user_id), user_id: UserId::from_uuid(r.user_id),
notification_type: NotificationType::from_db_str(&r.r#type), notification_type: notif_type_from_str(&r.r#type),
from_user_id: r.from_user_id.map(UserId::from_uuid), from_user_id: r.from_user_id.map(UserId::from_uuid),
thought_id: r.thought_id.map(ThoughtId::from_uuid), thought_id: r.thought_id.map(ThoughtId::from_uuid),
read: r.read, read: r.read,

View File

@@ -1,10 +1,31 @@
use async_trait::async_trait; use async_trait::async_trait;
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
fn visibility_from_str(s: &str) -> domain::models::thought::Visibility {
use domain::models::thought::Visibility;
match s {
"followers" => Visibility::Followers,
"unlisted" => Visibility::Unlisted,
"direct" => Visibility::Direct,
_ => Visibility::Public,
}
}
fn visibility_as_str(v: &domain::models::thought::Visibility) -> &'static str {
use domain::models::thought::Visibility;
match v {
Visibility::Public => "public",
Visibility::Followers => "followers",
Visibility::Unlisted => "unlisted",
Visibility::Direct => "direct",
}
}
use domain::{ use domain::{
errors::DomainError, errors::DomainError,
models::{ models::{
feed::{PageParams, Paginated}, feed::{PageParams, Paginated},
thought::{Thought, Visibility}, thought::Thought,
}, },
ports::ThoughtRepository, ports::ThoughtRepository,
value_objects::{Content, ThoughtId, UserId}, value_objects::{Content, ThoughtId, UserId},
@@ -45,7 +66,7 @@ impl From<ThoughtRow> for Thought {
in_reply_to_id: r.in_reply_to_id.map(ThoughtId::from_uuid), in_reply_to_id: r.in_reply_to_id.map(ThoughtId::from_uuid),
in_reply_to_url: r.in_reply_to_url, in_reply_to_url: r.in_reply_to_url,
ap_id: r.ap_id, ap_id: r.ap_id,
visibility: Visibility::from_db_str(&r.visibility), visibility: visibility_from_str(&r.visibility),
content_warning: r.content_warning, content_warning: r.content_warning,
sensitive: r.sensitive, sensitive: r.sensitive,
local: r.local, local: r.local,
@@ -72,7 +93,7 @@ impl ThoughtRepository for PgThoughtRepository {
.bind(t.in_reply_to_id.as_ref().map(|x| x.as_uuid())) .bind(t.in_reply_to_id.as_ref().map(|x| x.as_uuid()))
.bind(&t.in_reply_to_url) .bind(&t.in_reply_to_url)
.bind(&t.ap_id) .bind(&t.ap_id)
.bind(t.visibility.as_str()) .bind(visibility_as_str(&t.visibility))
.bind(&t.content_warning) .bind(&t.content_warning)
.bind(t.sensitive) .bind(t.sensitive)
.bind(t.local) .bind(t.local)

View File

@@ -57,11 +57,12 @@ pub async fn create_thought(
input: CreateThoughtInput, input: CreateThoughtInput,
) -> Result<CreateThoughtOutput, DomainError> { ) -> Result<CreateThoughtOutput, DomainError> {
let content = Content::new_local(input.content)?; let content = Content::new_local(input.content)?;
let visibility = input let visibility = match input.visibility.as_deref() {
.visibility Some("followers") => Visibility::Followers,
.as_deref() Some("unlisted") => Visibility::Unlisted,
.map(Visibility::from_db_str) Some("direct") => Visibility::Direct,
.unwrap_or(Visibility::Public); _ => Visibility::Public,
};
let thought = Thought::new_local( let thought = Thought::new_local(
ThoughtId::new(), ThoughtId::new(),
input.user_id, input.user_id,

View File

@@ -9,26 +9,6 @@ pub enum NotificationType {
Mention, Mention,
Reply, Reply,
} }
impl NotificationType {
pub fn from_db_str(s: &str) -> Self {
match s {
"like" => Self::Like,
"boost" => Self::Boost,
"follow" => Self::Follow,
"mention" => Self::Mention,
_ => Self::Reply,
}
}
pub fn as_str(&self) -> &str {
match self {
Self::Like => "like",
Self::Boost => "boost",
Self::Follow => "follow",
Self::Mention => "mention",
Self::Reply => "reply",
}
}
}
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Notification { pub struct Notification {

View File

@@ -25,22 +25,6 @@ pub enum FollowState {
Accepted, Accepted,
Rejected, Rejected,
} }
impl FollowState {
pub fn from_db_str(s: &str) -> Self {
match s {
"pending" => Self::Pending,
"rejected" => Self::Rejected,
_ => Self::Accepted,
}
}
pub fn as_str(&self) -> &str {
match self {
Self::Pending => "pending",
Self::Accepted => "accepted",
Self::Rejected => "rejected",
}
}
}
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Follow { pub struct Follow {

View File

@@ -8,24 +8,6 @@ pub enum Visibility {
Unlisted, Unlisted,
Direct, Direct,
} }
impl Visibility {
pub fn from_db_str(s: &str) -> Self {
match s {
"followers" => Self::Followers,
"unlisted" => Self::Unlisted,
"direct" => Self::Direct,
_ => Self::Public,
}
}
pub fn as_str(&self) -> &str {
match self {
Self::Public => "public",
Self::Followers => "followers",
Self::Unlisted => "unlisted",
Self::Direct => "direct",
}
}
}
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Thought { pub struct Thought {

View File

@@ -21,13 +21,23 @@ use axum::{
use domain::models::feed::PageParams; use domain::models::feed::PageParams;
use domain::value_objects::UserId; use domain::value_objects::UserId;
fn visibility_as_str(v: &domain::models::thought::Visibility) -> &'static str {
use domain::models::thought::Visibility;
match v {
Visibility::Public => "public",
Visibility::Followers => "followers",
Visibility::Unlisted => "unlisted",
Visibility::Direct => "direct",
}
}
pub fn to_thought_response(e: &domain::models::feed::FeedEntry) -> ThoughtResponse { pub fn to_thought_response(e: &domain::models::feed::FeedEntry) -> ThoughtResponse {
ThoughtResponse { ThoughtResponse {
id: e.thought.id.as_uuid(), id: e.thought.id.as_uuid(),
content: e.thought.content.as_str().to_string(), content: e.thought.content.as_str().to_string(),
author: to_user_response(&e.author), author: to_user_response(&e.author),
in_reply_to_id: e.thought.in_reply_to_id.as_ref().map(|id| id.as_uuid()), in_reply_to_id: e.thought.in_reply_to_id.as_ref().map(|id| id.as_uuid()),
visibility: e.thought.visibility.as_str().to_string(), visibility: visibility_as_str(&e.thought.visibility).to_string(),
content_warning: e.thought.content_warning.clone(), content_warning: e.thought.content_warning.clone(),
sensitive: e.thought.sensitive, sensitive: e.thought.sensitive,
like_count: e.like_count, like_count: e.like_count,

View File

@@ -20,6 +20,16 @@ use axum::{
use domain::value_objects::ThoughtId; use domain::value_objects::ThoughtId;
use uuid::Uuid; use uuid::Uuid;
fn visibility_as_str(v: &domain::models::thought::Visibility) -> &'static str {
use domain::models::thought::Visibility;
match v {
Visibility::Public => "public",
Visibility::Followers => "followers",
Visibility::Unlisted => "unlisted",
Visibility::Direct => "direct",
}
}
fn thought_to_json( fn thought_to_json(
t: &domain::models::thought::Thought, t: &domain::models::thought::Thought,
author: &domain::models::user::User, author: &domain::models::user::User,
@@ -32,7 +42,7 @@ fn thought_to_json(
"content": t.content.as_str(), "content": t.content.as_str(),
"author": to_user_response(author), "author": to_user_response(author),
"replyToId": t.in_reply_to_id.as_ref().map(|x| x.as_uuid()), "replyToId": t.in_reply_to_id.as_ref().map(|x| x.as_uuid()),
"visibility": t.visibility.as_str(), "visibility": visibility_as_str(&t.visibility),
"contentWarning": t.content_warning, "contentWarning": t.content_warning,
"sensitive": t.sensitive, "sensitive": t.sensitive,
"likeCount": like_count, "likeCount": like_count,