Files
thoughts/crates/adapters/postgres/src/remote_actor_connections.rs
Gabriel Kaszewski 314dad5451
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
Refactor database error handling across repositories to use IntoDbResult for improved error management
- 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.
2026-05-15 12:31:25 +02:00

112 lines
3.6 KiB
Rust

use crate::db_error::IntoDbResult;
use async_trait::async_trait;
use domain::{
errors::DomainError, models::actor_connection_summary::ActorConnectionSummary,
ports::RemoteActorConnectionRepository,
};
use sqlx::PgPool;
pub struct PgRemoteActorConnectionRepository {
pool: PgPool,
}
impl PgRemoteActorConnectionRepository {
pub fn new(pool: PgPool) -> Self {
Self { pool }
}
}
#[async_trait]
impl RemoteActorConnectionRepository for PgRemoteActorConnectionRepository {
async fn upsert_connections(
&self,
actor_url: &str,
connection_type: &str,
page: u32,
actors: &[ActorConnectionSummary],
) -> Result<(), DomainError> {
for actor in actors {
sqlx::query(
"INSERT INTO remote_actor_connections
(actor_url, connection_type, page, connected_actor_url,
connected_handle, connected_display_name, connected_avatar_url, fetched_at)
VALUES ($1, $2, $3, $4, $5, $6, $7, NOW())
ON CONFLICT(actor_url, connection_type, page, connected_actor_url)
DO UPDATE SET
connected_handle = EXCLUDED.connected_handle,
connected_display_name = EXCLUDED.connected_display_name,
connected_avatar_url = EXCLUDED.connected_avatar_url,
fetched_at = NOW()",
)
.bind(actor_url)
.bind(connection_type)
.bind(page as i32)
.bind(&actor.url)
.bind(&actor.handle)
.bind(&actor.display_name)
.bind(&actor.avatar_url)
.execute(&self.pool)
.await
.into_domain()?;
}
Ok(())
}
async fn list_connections(
&self,
actor_url: &str,
connection_type: &str,
page: u32,
) -> Result<Vec<ActorConnectionSummary>, DomainError> {
#[derive(sqlx::FromRow)]
struct Row {
connected_actor_url: String,
connected_handle: String,
connected_display_name: Option<String>,
connected_avatar_url: Option<String>,
}
let rows = sqlx::query_as::<_, Row>(
"SELECT connected_actor_url, connected_handle, connected_display_name, connected_avatar_url
FROM remote_actor_connections
WHERE actor_url = $1 AND connection_type = $2 AND page = $3
ORDER BY connected_handle",
)
.bind(actor_url)
.bind(connection_type)
.bind(page as i32)
.fetch_all(&self.pool)
.await
.into_domain()?;
Ok(rows
.into_iter()
.map(|r| ActorConnectionSummary {
url: r.connected_actor_url,
handle: r.connected_handle,
display_name: r.connected_display_name,
avatar_url: r.connected_avatar_url,
})
.collect())
}
async fn connection_page_age(
&self,
actor_url: &str,
connection_type: &str,
page: u32,
) -> Result<Option<chrono::DateTime<chrono::Utc>>, DomainError> {
let row: Option<(Option<chrono::DateTime<chrono::Utc>>,)> = sqlx::query_as(
"SELECT MAX(fetched_at) FROM remote_actor_connections
WHERE actor_url = $1 AND connection_type = $2 AND page = $3",
)
.bind(actor_url)
.bind(connection_type)
.bind(page as i32)
.fetch_optional(&self.pool)
.await
.into_domain()?;
Ok(row.and_then(|(ts,)| ts))
}
}