fix(domain): from_db_str returns Result — unknown DB values are errors not silent defaults
This commit is contained in:
@@ -67,13 +67,13 @@ const FEED_SELECT: &str = "
|
|||||||
(SELECT COUNT(*) FROM thoughts r WHERE r.in_reply_to_id=t.id) AS reply_count
|
(SELECT COUNT(*) FROM thoughts r WHERE r.in_reply_to_id=t.id) AS reply_count
|
||||||
FROM thoughts t JOIN users u ON u.id=t.user_id";
|
FROM thoughts t JOIN users u ON u.id=t.user_id";
|
||||||
|
|
||||||
fn row_to_entry(r: FeedRow) -> FeedEntry {
|
fn row_to_entry(r: FeedRow) -> Result<FeedEntry, DomainError> {
|
||||||
let thought = Thought {
|
let thought = Thought {
|
||||||
id: ThoughtId::from_uuid(r.thought_id),
|
id: ThoughtId::from_uuid(r.thought_id),
|
||||||
user_id: UserId::from_uuid(r.t_user_id),
|
user_id: UserId::from_uuid(r.t_user_id),
|
||||||
content: Content::new_remote(r.content),
|
content: Content::new_remote(r.content),
|
||||||
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),
|
||||||
visibility: Visibility::from_db_str(&r.visibility),
|
visibility: Visibility::from_db_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,
|
||||||
@@ -94,7 +94,7 @@ fn row_to_entry(r: FeedRow) -> FeedEntry {
|
|||||||
created_at: r.author_created_at,
|
created_at: r.author_created_at,
|
||||||
updated_at: r.author_updated_at,
|
updated_at: r.author_updated_at,
|
||||||
};
|
};
|
||||||
FeedEntry {
|
Ok(FeedEntry {
|
||||||
thought,
|
thought,
|
||||||
author,
|
author,
|
||||||
like_count: r.like_count,
|
like_count: r.like_count,
|
||||||
@@ -102,7 +102,7 @@ fn row_to_entry(r: FeedRow) -> FeedEntry {
|
|||||||
reply_count: r.reply_count,
|
reply_count: r.reply_count,
|
||||||
liked_by_viewer: false,
|
liked_by_viewer: false,
|
||||||
boosted_by_viewer: false,
|
boosted_by_viewer: false,
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
@@ -137,7 +137,10 @@ impl SearchPort for PgSearchRepository {
|
|||||||
.map_err(|e| DomainError::Internal(e.to_string()))?;
|
.map_err(|e| DomainError::Internal(e.to_string()))?;
|
||||||
|
|
||||||
Ok(Paginated {
|
Ok(Paginated {
|
||||||
items: rows.into_iter().map(row_to_entry).collect(),
|
items: rows
|
||||||
|
.into_iter()
|
||||||
|
.map(row_to_entry)
|
||||||
|
.collect::<Result<Vec<_>, _>>()?,
|
||||||
total,
|
total,
|
||||||
page: page.page,
|
page: page.page,
|
||||||
per_page: page.per_page,
|
per_page: page.per_page,
|
||||||
|
|||||||
@@ -106,13 +106,13 @@ fn feed_select(viewer: Option<uuid::Uuid>) -> String {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn row_to_entry(r: FeedRow) -> FeedEntry {
|
fn row_to_entry(r: FeedRow) -> Result<FeedEntry, DomainError> {
|
||||||
let thought = Thought {
|
let thought = Thought {
|
||||||
id: ThoughtId::from_uuid(r.thought_id),
|
id: ThoughtId::from_uuid(r.thought_id),
|
||||||
user_id: UserId::from_uuid(r.t_user_id),
|
user_id: UserId::from_uuid(r.t_user_id),
|
||||||
content: Content::new_remote(r.content),
|
content: Content::new_remote(r.content),
|
||||||
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),
|
||||||
visibility: Visibility::from_db_str(&r.visibility),
|
visibility: Visibility::from_db_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,
|
||||||
@@ -133,7 +133,7 @@ fn row_to_entry(r: FeedRow) -> FeedEntry {
|
|||||||
created_at: r.author_created_at,
|
created_at: r.author_created_at,
|
||||||
updated_at: r.author_updated_at,
|
updated_at: r.author_updated_at,
|
||||||
};
|
};
|
||||||
FeedEntry {
|
Ok(FeedEntry {
|
||||||
thought,
|
thought,
|
||||||
author,
|
author,
|
||||||
like_count: r.like_count,
|
like_count: r.like_count,
|
||||||
@@ -141,7 +141,7 @@ fn row_to_entry(r: FeedRow) -> FeedEntry {
|
|||||||
reply_count: r.reply_count,
|
reply_count: r.reply_count,
|
||||||
liked_by_viewer: r.liked_by_viewer,
|
liked_by_viewer: r.liked_by_viewer,
|
||||||
boosted_by_viewer: r.boosted_by_viewer,
|
boosted_by_viewer: r.boosted_by_viewer,
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
@@ -176,7 +176,10 @@ impl FeedRepository for PgFeedRepository {
|
|||||||
.into_domain()?;
|
.into_domain()?;
|
||||||
|
|
||||||
Ok(Paginated {
|
Ok(Paginated {
|
||||||
items: rows.into_iter().map(row_to_entry).collect(),
|
items: rows
|
||||||
|
.into_iter()
|
||||||
|
.map(row_to_entry)
|
||||||
|
.collect::<Result<Vec<_>, _>>()?,
|
||||||
total,
|
total,
|
||||||
page: page.page,
|
page: page.page,
|
||||||
per_page: page.per_page,
|
per_page: page.per_page,
|
||||||
@@ -206,7 +209,10 @@ impl FeedRepository for PgFeedRepository {
|
|||||||
.into_domain()?;
|
.into_domain()?;
|
||||||
|
|
||||||
Ok(Paginated {
|
Ok(Paginated {
|
||||||
items: rows.into_iter().map(row_to_entry).collect(),
|
items: rows
|
||||||
|
.into_iter()
|
||||||
|
.map(row_to_entry)
|
||||||
|
.collect::<Result<Vec<_>, _>>()?,
|
||||||
total,
|
total,
|
||||||
page: page.page,
|
page: page.page,
|
||||||
per_page: page.per_page,
|
per_page: page.per_page,
|
||||||
@@ -239,7 +245,10 @@ impl FeedRepository for PgFeedRepository {
|
|||||||
.into_domain()?;
|
.into_domain()?;
|
||||||
|
|
||||||
Ok(Paginated {
|
Ok(Paginated {
|
||||||
items: rows.into_iter().map(row_to_entry).collect(),
|
items: rows
|
||||||
|
.into_iter()
|
||||||
|
.map(row_to_entry)
|
||||||
|
.collect::<Result<Vec<_>, _>>()?,
|
||||||
total,
|
total,
|
||||||
page: page.page,
|
page: page.page,
|
||||||
per_page: page.per_page,
|
per_page: page.per_page,
|
||||||
@@ -281,7 +290,10 @@ impl FeedRepository for PgFeedRepository {
|
|||||||
.into_domain()?;
|
.into_domain()?;
|
||||||
|
|
||||||
Ok(Paginated {
|
Ok(Paginated {
|
||||||
items: rows.into_iter().map(row_to_entry).collect(),
|
items: rows
|
||||||
|
.into_iter()
|
||||||
|
.map(row_to_entry)
|
||||||
|
.collect::<Result<Vec<_>, _>>()?,
|
||||||
total,
|
total,
|
||||||
page: page.page,
|
page: page.page,
|
||||||
per_page: page.per_page,
|
per_page: page.per_page,
|
||||||
@@ -321,7 +333,10 @@ impl FeedRepository for PgFeedRepository {
|
|||||||
.into_domain()?;
|
.into_domain()?;
|
||||||
|
|
||||||
Ok(Paginated {
|
Ok(Paginated {
|
||||||
items: rows.into_iter().map(row_to_entry).collect(),
|
items: rows
|
||||||
|
.into_iter()
|
||||||
|
.map(row_to_entry)
|
||||||
|
.collect::<Result<Vec<_>, _>>()?,
|
||||||
total,
|
total,
|
||||||
page: page.page,
|
page: page.page,
|
||||||
per_page: page.per_page,
|
per_page: page.per_page,
|
||||||
|
|||||||
@@ -76,13 +76,18 @@ impl FollowRepository for PgFollowRepository {
|
|||||||
.fetch_optional(&self.pool)
|
.fetch_optional(&self.pool)
|
||||||
.await
|
.await
|
||||||
.into_domain()
|
.into_domain()
|
||||||
.map(|o| o.map(|r| Follow {
|
.and_then(|o| {
|
||||||
follower_id: UserId::from_uuid(r.follower_id),
|
o.map(|r| {
|
||||||
following_id: UserId::from_uuid(r.following_id),
|
Ok(Follow {
|
||||||
state: FollowState::from_db_str(&r.state),
|
follower_id: UserId::from_uuid(r.follower_id),
|
||||||
ap_id: r.ap_id,
|
following_id: UserId::from_uuid(r.following_id),
|
||||||
created_at: r.created_at,
|
state: FollowState::from_db_str(&r.state)?,
|
||||||
}))
|
ap_id: r.ap_id,
|
||||||
|
created_at: r.created_at,
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.transpose()
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn update_state(
|
async fn update_state(
|
||||||
|
|||||||
@@ -105,7 +105,10 @@ impl TagRepository for PgTagRepository {
|
|||||||
.fetch_all(&self.pool).await.into_domain()?;
|
.fetch_all(&self.pool).await.into_domain()?;
|
||||||
|
|
||||||
Ok(Paginated {
|
Ok(Paginated {
|
||||||
items: rows.into_iter().map(Thought::from).collect(),
|
items: rows
|
||||||
|
.into_iter()
|
||||||
|
.map(Thought::try_from)
|
||||||
|
.collect::<Result<Vec<_>, _>>()?,
|
||||||
total,
|
total,
|
||||||
page: page.page,
|
page: page.page,
|
||||||
per_page: page.per_page,
|
per_page: page.per_page,
|
||||||
|
|||||||
@@ -36,20 +36,21 @@ pub(crate) struct ThoughtRow {
|
|||||||
pub updated_at: Option<DateTime<Utc>>,
|
pub updated_at: Option<DateTime<Utc>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<ThoughtRow> for Thought {
|
impl TryFrom<ThoughtRow> for Thought {
|
||||||
fn from(r: ThoughtRow) -> Self {
|
type Error = DomainError;
|
||||||
Thought {
|
fn try_from(r: ThoughtRow) -> Result<Self, DomainError> {
|
||||||
|
Ok(Thought {
|
||||||
id: ThoughtId::from_uuid(r.id),
|
id: ThoughtId::from_uuid(r.id),
|
||||||
user_id: UserId::from_uuid(r.user_id),
|
user_id: UserId::from_uuid(r.user_id),
|
||||||
content: Content::new_remote(r.content),
|
content: Content::new_remote(r.content),
|
||||||
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),
|
||||||
visibility: Visibility::from_db_str(&r.visibility),
|
visibility: Visibility::from_db_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,
|
||||||
created_at: r.created_at,
|
created_at: r.created_at,
|
||||||
updated_at: r.updated_at,
|
updated_at: r.updated_at,
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -85,7 +86,7 @@ impl ThoughtRepository for PgThoughtRepository {
|
|||||||
.fetch_optional(&self.pool)
|
.fetch_optional(&self.pool)
|
||||||
.await
|
.await
|
||||||
.into_domain()
|
.into_domain()
|
||||||
.map(|o| o.map(Thought::from))
|
.and_then(|o| o.map(Thought::try_from).transpose())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn delete(&self, id: &ThoughtId, user_id: &UserId) -> Result<(), DomainError> {
|
async fn delete(&self, id: &ThoughtId, user_id: &UserId) -> Result<(), DomainError> {
|
||||||
@@ -129,7 +130,7 @@ impl ThoughtRepository for PgThoughtRepository {
|
|||||||
.fetch_all(&self.pool)
|
.fetch_all(&self.pool)
|
||||||
.await
|
.await
|
||||||
.into_domain()
|
.into_domain()
|
||||||
.map(|rows| rows.into_iter().map(Thought::from).collect())
|
.and_then(|rows| rows.into_iter().map(Thought::try_from).collect())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn list_by_user(
|
async fn list_by_user(
|
||||||
@@ -155,7 +156,10 @@ impl ThoughtRepository for PgThoughtRepository {
|
|||||||
.into_domain()?;
|
.into_domain()?;
|
||||||
|
|
||||||
Ok(Paginated {
|
Ok(Paginated {
|
||||||
items: rows.into_iter().map(Thought::from).collect(),
|
items: rows
|
||||||
|
.into_iter()
|
||||||
|
.map(Thought::try_from)
|
||||||
|
.collect::<Result<Vec<_>, _>>()?,
|
||||||
total,
|
total,
|
||||||
page: page.page,
|
page: page.page,
|
||||||
per_page: page.per_page,
|
per_page: page.per_page,
|
||||||
|
|||||||
@@ -35,11 +35,14 @@ impl FollowState {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_db_str(s: &str) -> Self {
|
pub fn from_db_str(s: &str) -> Result<Self, crate::errors::DomainError> {
|
||||||
match s {
|
match s {
|
||||||
"pending" => Self::Pending,
|
"pending" => Ok(Self::Pending),
|
||||||
"rejected" => Self::Rejected,
|
"accepted" => Ok(Self::Accepted),
|
||||||
_ => Self::Accepted,
|
"rejected" => Ok(Self::Rejected),
|
||||||
|
other => Err(crate::errors::DomainError::Internal(format!(
|
||||||
|
"unknown follow_state: '{other}'"
|
||||||
|
))),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,12 +33,15 @@ impl Visibility {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_db_str(s: &str) -> Self {
|
pub fn from_db_str(s: &str) -> Result<Self, crate::errors::DomainError> {
|
||||||
match s {
|
match s {
|
||||||
"followers" => Self::Followers,
|
"public" => Ok(Self::Public),
|
||||||
"unlisted" => Self::Unlisted,
|
"followers" => Ok(Self::Followers),
|
||||||
"direct" => Self::Direct,
|
"unlisted" => Ok(Self::Unlisted),
|
||||||
_ => Self::Public,
|
"direct" => Ok(Self::Direct),
|
||||||
|
other => Err(crate::errors::DomainError::Internal(format!(
|
||||||
|
"unknown visibility: '{other}'"
|
||||||
|
))),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user