Refactor database error handling across repositories to use IntoDbResult for improved error management
Some checks failed
lint / lint (push) Has been cancelled
test / unit (push) Has been cancelled
test / integration (push) Has been cancelled
lint / lint (pull_request) Failing after 9m30s
test / unit (pull_request) Successful in 16m10s
test / integration (pull_request) Failing after 16m44s

- Updated PgNotificationRepository to utilize IntoDbResult for error handling in various methods.
- Refactored PgRemoteActorRepository to replace manual error mapping with IntoDbResult.
- Modified PgRemoteActorConnectionRepository to implement IntoDbResult for error handling.
- Adjusted PgTagRepository to use IntoDbResult for consistent error management.
- Introduced test_helpers module for seeding users and thoughts in tests.
- Enhanced PgThoughtRepository to leverage IntoDbResult for error handling.
- Updated PgTopFriendRepository to utilize IntoDbResult for error management.
- Refactored PgUserRepository to implement IntoDbResult for error handling.
- Added constants for pagination defaults in requests.
- Introduced MAX_TOP_FRIENDS constant for top friends validation.
- Refactored JWT expiration time to use a constant.
- Improved rate limiter configuration with constants for better readability.
- Added utility methods for FollowState and Visibility enums for string conversions.
- Introduced maximum length constants for Username, Email, and Content value objects.
- Cleaned up test modules by removing redundant code and utilizing a shared testing state.
This commit is contained in:
2026-05-15 12:31:25 +02:00
parent a040a38036
commit 314dad5451
40 changed files with 456 additions and 690 deletions

View File

@@ -1,4 +1,8 @@
use crate::db_error::IntoDbResult;
use async_trait::async_trait;
const MAX_REMOTE_CONTENT_CHARS: usize = 500;
const THOUGHTS_PATH_PREFIX: &str = "/thoughts/";
use chrono::{DateTime, Utc};
use sqlx::PgPool;
use url::Url;
@@ -47,7 +51,7 @@ impl ActivityPubRepository for PgActivityPubRepository {
.bind(user_id.as_uuid())
.fetch_all(&self.pool)
.await
.map_err(|e| DomainError::Internal(e.to_string()))
.into_domain()
.map(|rows| {
rows.into_iter()
.map(|r| OutboxEntry {
@@ -113,7 +117,7 @@ impl ActivityPubRepository for PgActivityPubRepository {
.fetch_all(&self.pool)
.await
}
.map_err(|e| DomainError::Internal(e.to_string()))?;
.into_domain()?;
Ok(rows
.into_iter()
@@ -145,7 +149,7 @@ impl ActivityPubRepository for PgActivityPubRepository {
.bind(actor_ap_url.as_str())
.fetch_optional(&self.pool)
.await
.map_err(|e| DomainError::Internal(e.to_string()))
.into_domain()
.map(|o| o.map(UserId::from_uuid))
}
@@ -179,7 +183,7 @@ impl ActivityPubRepository for PgActivityPubRepository {
.bind(actor_ap_url.as_str())
.execute(&self.pool)
.await
.map_err(|e| DomainError::Internal(e.to_string()))?;
.into_domain()?;
// Re-fetch to get whichever id won the race
self.find_remote_actor_id(actor_ap_url)
.await?
@@ -205,7 +209,7 @@ impl ActivityPubRepository for PgActivityPubRepository {
.bind(user_id.as_uuid())
.execute(&self.pool)
.await
.map_err(|e| DomainError::Internal(e.to_string()))
.into_domain()
.map(|_| ())
}
@@ -220,13 +224,13 @@ impl ActivityPubRepository for PgActivityPubRepository {
visibility: &str,
in_reply_to: Option<&Url>,
) -> Result<(), DomainError> {
let capped: String = content.chars().take(500).collect();
let capped: String = content.chars().take(MAX_REMOTE_CONTENT_CHARS).collect();
let (in_reply_to_id, in_reply_to_url) = match in_reply_to {
Some(url) => {
// If the parent is a local thought, extract its UUID for in_reply_to_id.
let local_uuid = url
.path()
.strip_prefix("/thoughts/")
.strip_prefix(THOUGHTS_PATH_PREFIX)
.and_then(|s| s.split('/').next())
.and_then(|s| uuid::Uuid::parse_str(s).ok());
(local_uuid, Some(url.as_str().to_string()))
@@ -249,12 +253,12 @@ impl ActivityPubRepository for PgActivityPubRepository {
.bind(&in_reply_to_url)
.execute(&self.pool)
.await
.map_err(|e| DomainError::Internal(e.to_string()))
.into_domain()
.map(|_| ())
}
async fn apply_note_update(&self, ap_id: &Url, new_content: &str) -> Result<(), DomainError> {
let capped: String = new_content.chars().take(500).collect();
let capped: String = new_content.chars().take(MAX_REMOTE_CONTENT_CHARS).collect();
sqlx::query(
"UPDATE thoughts SET content=$2,updated_at=NOW() WHERE ap_id=$1 AND local=false",
)
@@ -262,7 +266,7 @@ impl ActivityPubRepository for PgActivityPubRepository {
.bind(&capped)
.execute(&self.pool)
.await
.map_err(|e| DomainError::Internal(e.to_string()))
.into_domain()
.map(|_| ())
}
@@ -271,7 +275,7 @@ impl ActivityPubRepository for PgActivityPubRepository {
.bind(ap_id.as_str())
.execute(&self.pool)
.await
.map_err(|e| DomainError::Internal(e.to_string()))
.into_domain()
.map(|_| ())
}
@@ -282,7 +286,7 @@ impl ActivityPubRepository for PgActivityPubRepository {
.bind(actor_ap_url.as_str())
.execute(&self.pool)
.await
.map_err(|e| DomainError::Internal(e.to_string()))
.into_domain()
.map(|_| ())
}
@@ -290,7 +294,7 @@ impl ActivityPubRepository for PgActivityPubRepository {
let n: i64 = sqlx::query_scalar("SELECT COUNT(*) FROM thoughts WHERE local=true")
.fetch_one(&self.pool)
.await
.map_err(|e| DomainError::Internal(e.to_string()))?;
.into_domain()?;
Ok(n as u64)
}
}