feat: atomic social local writes, remote review enrichment
SocialCommand methods take events. Local follow ops = one FollowCommand::apply tx (rows + events). k_ap path (remote, block/unblock): best-effort publish in CompositeSocialAdapter after k_ap write; not atomic, k_ap owns tx. save_remote_review takes events, enqueued in same tx via sqlite::persistence::enqueue_events. Deps drop event_publisher: social command, ReviewObjectHandler. Remaining .publish( sites: announce-only: users/delete_account UserDeleted, person/get + get_credits PersonEnrichmentRequested, jobs/enrichment_staleness, http-axum search, ImageStored (sync_poster, poster_sync_handler, image-converter backfill, update_profile; object storage), federation_event_bridge (handler reaction), nats relay (transport). non-atomic by necessity: CompositeSocialAdapter::announce (k_ap-owned write). Everything else rides its write.
This commit is contained in:
@@ -22,7 +22,7 @@ pub use repositories::{
|
||||
/// Row helpers shared with the `sqlite-federation` crate (which cannot live
|
||||
/// here because it depends on `activitypub`/`k-ap`).
|
||||
pub mod persistence {
|
||||
pub use crate::shared::datetime_to_str;
|
||||
pub use crate::shared::{datetime_to_str, enqueue_events};
|
||||
}
|
||||
|
||||
pub fn create_profile_fields_repo(
|
||||
|
||||
@@ -1,130 +1,126 @@
|
||||
use async_trait::async_trait;
|
||||
use chrono::Utc;
|
||||
use domain::{errors::DomainError, value_objects::FollowStatus};
|
||||
use domain::{
|
||||
errors::DomainError,
|
||||
events::DomainEvent,
|
||||
ports::{FollowCommand, FollowWrite},
|
||||
};
|
||||
|
||||
use crate::shared::datetime_to_str;
|
||||
use crate::shared::{datetime_to_str, enqueue_events};
|
||||
|
||||
use super::infra_err;
|
||||
use super::rows::follow_status_to_str;
|
||||
use crate::repositories::social::SqliteSocialRepository;
|
||||
|
||||
#[async_trait]
|
||||
impl domain::ports::FollowCommand for SqliteSocialRepository {
|
||||
async fn add_follow(
|
||||
impl FollowCommand for SqliteSocialRepository {
|
||||
async fn apply(
|
||||
&self,
|
||||
follower_id: uuid::Uuid,
|
||||
target_actor_url: &str,
|
||||
status: FollowStatus,
|
||||
writes: &[FollowWrite],
|
||||
events: &[DomainEvent],
|
||||
) -> Result<(), DomainError> {
|
||||
let uid = follower_id.to_string();
|
||||
let status_str = follow_status_to_str(&status);
|
||||
let now = datetime_to_str(&Utc::now().naive_utc());
|
||||
sqlx::query(
|
||||
"INSERT INTO ap_following (local_user_id, remote_actor_url, follow_activity_id, created_at, status)
|
||||
VALUES (?1, ?2, '', ?3, ?4)
|
||||
ON CONFLICT(local_user_id, remote_actor_url) DO UPDATE SET status = excluded.status",
|
||||
)
|
||||
.bind(&uid)
|
||||
.bind(target_actor_url)
|
||||
.bind(&now)
|
||||
.bind(status_str)
|
||||
.execute(&self.pool)
|
||||
.await
|
||||
.map_err(infra_err)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn update_follow_status(
|
||||
&self,
|
||||
follower_id: uuid::Uuid,
|
||||
target_actor_url: &str,
|
||||
status: FollowStatus,
|
||||
) -> Result<(), DomainError> {
|
||||
let uid = follower_id.to_string();
|
||||
let status_str = follow_status_to_str(&status);
|
||||
sqlx::query(
|
||||
"UPDATE ap_following SET status = ?1 WHERE local_user_id = ?2 AND remote_actor_url = ?3",
|
||||
)
|
||||
.bind(status_str)
|
||||
.bind(&uid)
|
||||
.bind(target_actor_url)
|
||||
.execute(&self.pool)
|
||||
.await
|
||||
.map_err(infra_err)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn remove_follow(
|
||||
&self,
|
||||
follower_id: uuid::Uuid,
|
||||
target_actor_url: &str,
|
||||
) -> Result<(), DomainError> {
|
||||
let uid = follower_id.to_string();
|
||||
sqlx::query("DELETE FROM ap_following WHERE local_user_id = ?1 AND remote_actor_url = ?2")
|
||||
.bind(&uid)
|
||||
.bind(target_actor_url)
|
||||
.execute(&self.pool)
|
||||
.await
|
||||
.map_err(infra_err)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn add_follower(
|
||||
&self,
|
||||
local_user_id: uuid::Uuid,
|
||||
follower_actor_url: &str,
|
||||
status: FollowStatus,
|
||||
) -> Result<(), DomainError> {
|
||||
let uid = local_user_id.to_string();
|
||||
let status_str = follow_status_to_str(&status);
|
||||
let now = datetime_to_str(&Utc::now().naive_utc());
|
||||
sqlx::query(
|
||||
"INSERT INTO ap_followers (local_user_id, remote_actor_url, status, created_at, follow_activity_id)
|
||||
VALUES (?1, ?2, ?3, ?4, '')
|
||||
ON CONFLICT(local_user_id, remote_actor_url) DO UPDATE SET status = excluded.status",
|
||||
)
|
||||
.bind(&uid)
|
||||
.bind(follower_actor_url)
|
||||
.bind(status_str)
|
||||
.bind(&now)
|
||||
.execute(&self.pool)
|
||||
.await
|
||||
.map_err(infra_err)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn update_follower_status(
|
||||
&self,
|
||||
local_user_id: uuid::Uuid,
|
||||
follower_actor_url: &str,
|
||||
status: FollowStatus,
|
||||
) -> Result<(), DomainError> {
|
||||
let uid = local_user_id.to_string();
|
||||
let status_str = follow_status_to_str(&status);
|
||||
sqlx::query(
|
||||
"UPDATE ap_followers SET status = ?1 WHERE local_user_id = ?2 AND remote_actor_url = ?3",
|
||||
)
|
||||
.bind(status_str)
|
||||
.bind(&uid)
|
||||
.bind(follower_actor_url)
|
||||
.execute(&self.pool)
|
||||
.await
|
||||
.map_err(infra_err)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn remove_follower_record(
|
||||
&self,
|
||||
local_user_id: uuid::Uuid,
|
||||
follower_actor_url: &str,
|
||||
) -> Result<(), DomainError> {
|
||||
let uid = local_user_id.to_string();
|
||||
sqlx::query("DELETE FROM ap_followers WHERE local_user_id = ?1 AND remote_actor_url = ?2")
|
||||
.bind(&uid)
|
||||
.bind(follower_actor_url)
|
||||
.execute(&self.pool)
|
||||
.await
|
||||
.map_err(infra_err)?;
|
||||
Ok(())
|
||||
let mut tx = self.pool.begin().await.map_err(infra_err)?;
|
||||
for write in writes {
|
||||
apply_write(&mut tx, write).await?;
|
||||
}
|
||||
enqueue_events(&mut tx, events).await?;
|
||||
tx.commit().await.map_err(infra_err)
|
||||
}
|
||||
}
|
||||
|
||||
async fn apply_write(
|
||||
tx: &mut sqlx::Transaction<'_, sqlx::Sqlite>,
|
||||
write: &FollowWrite,
|
||||
) -> Result<(), DomainError> {
|
||||
let now = datetime_to_str(&Utc::now().naive_utc());
|
||||
match write {
|
||||
FollowWrite::AddFollow {
|
||||
follower_id,
|
||||
target_actor_url,
|
||||
status,
|
||||
} => {
|
||||
sqlx::query(
|
||||
"INSERT INTO ap_following (local_user_id, remote_actor_url, follow_activity_id, created_at, status)
|
||||
VALUES (?1, ?2, '', ?3, ?4)
|
||||
ON CONFLICT(local_user_id, remote_actor_url) DO UPDATE SET status = excluded.status",
|
||||
)
|
||||
.bind(follower_id.to_string())
|
||||
.bind(target_actor_url)
|
||||
.bind(&now)
|
||||
.bind(follow_status_to_str(status))
|
||||
.execute(&mut **tx)
|
||||
.await
|
||||
}
|
||||
FollowWrite::UpdateFollowStatus {
|
||||
follower_id,
|
||||
target_actor_url,
|
||||
status,
|
||||
} => {
|
||||
sqlx::query(
|
||||
"UPDATE ap_following SET status = ?1 WHERE local_user_id = ?2 AND remote_actor_url = ?3",
|
||||
)
|
||||
.bind(follow_status_to_str(status))
|
||||
.bind(follower_id.to_string())
|
||||
.bind(target_actor_url)
|
||||
.execute(&mut **tx)
|
||||
.await
|
||||
}
|
||||
FollowWrite::RemoveFollow {
|
||||
follower_id,
|
||||
target_actor_url,
|
||||
} => {
|
||||
sqlx::query(
|
||||
"DELETE FROM ap_following WHERE local_user_id = ?1 AND remote_actor_url = ?2",
|
||||
)
|
||||
.bind(follower_id.to_string())
|
||||
.bind(target_actor_url)
|
||||
.execute(&mut **tx)
|
||||
.await
|
||||
}
|
||||
FollowWrite::AddFollower {
|
||||
local_user_id,
|
||||
follower_actor_url,
|
||||
status,
|
||||
} => {
|
||||
sqlx::query(
|
||||
"INSERT INTO ap_followers (local_user_id, remote_actor_url, status, created_at, follow_activity_id)
|
||||
VALUES (?1, ?2, ?3, ?4, '')
|
||||
ON CONFLICT(local_user_id, remote_actor_url) DO UPDATE SET status = excluded.status",
|
||||
)
|
||||
.bind(local_user_id.to_string())
|
||||
.bind(follower_actor_url)
|
||||
.bind(follow_status_to_str(status))
|
||||
.bind(&now)
|
||||
.execute(&mut **tx)
|
||||
.await
|
||||
}
|
||||
FollowWrite::UpdateFollowerStatus {
|
||||
local_user_id,
|
||||
follower_actor_url,
|
||||
status,
|
||||
} => {
|
||||
sqlx::query(
|
||||
"UPDATE ap_followers SET status = ?1 WHERE local_user_id = ?2 AND remote_actor_url = ?3",
|
||||
)
|
||||
.bind(follow_status_to_str(status))
|
||||
.bind(local_user_id.to_string())
|
||||
.bind(follower_actor_url)
|
||||
.execute(&mut **tx)
|
||||
.await
|
||||
}
|
||||
FollowWrite::RemoveFollowerRecord {
|
||||
local_user_id,
|
||||
follower_actor_url,
|
||||
} => {
|
||||
sqlx::query(
|
||||
"DELETE FROM ap_followers WHERE local_user_id = ?1 AND remote_actor_url = ?2",
|
||||
)
|
||||
.bind(local_user_id.to_string())
|
||||
.bind(follower_actor_url)
|
||||
.execute(&mut **tx)
|
||||
.await
|
||||
}
|
||||
}
|
||||
.map_err(infra_err)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ use domain::{
|
||||
/// THE outbox write path: inserts one `event_queue` row per event, each with a
|
||||
/// fresh `EventId`, inside the caller's transaction so a change and the events
|
||||
/// announcing it commit or roll back together.
|
||||
pub(crate) async fn enqueue_events(
|
||||
pub async fn enqueue_events(
|
||||
tx: &mut sqlx::Transaction<'_, sqlx::Sqlite>,
|
||||
events: &[domain::events::DomainEvent],
|
||||
) -> Result<(), DomainError> {
|
||||
|
||||
Reference in New Issue
Block a user