refactor: consolidate DbActor construction into a single factory #13

Open
opened 2026-05-30 01:32:35 +00:00 by GKaszewski · 0 comments
Owner

Problem

DbActor has 4 different construction paths:

  1. get_local_actor() — via Data<FederationData> context
  2. build_local_actor() — same logic but without FederationData (for service construction)
  3. read_from_id()Object trait deserialization path
  4. from_json() — remote actor ingest, does 3 things: cache upsert, UUID generation, struct building

URL construction is split between ActorUrls::build() and manual format!() calls in into_json(). from_json() is 76 lines performing 3 distinct tasks. private_key_pem is only populated in some paths, creating inconsistent state.

Proposal

Extract actor construction into a builder/factory:

DbActor::local(user_id, base_url, user_repo, actor_repo).await?
DbActor::from_remote(json, data).await?  // handles upsert internally

Separate from_json()'s three responsibilities:

  1. RemoteActor::from_ap_json(json) — pure conversion
  2. actor_repo.upsert_remote_actor(actor) — cache (called by caller)
  3. DbActor::from_remote_actor(actor) — struct building

Files

  • src/actors.rs (primary — get_local_actor, build_local_actor, from_json, read_from_id)
  • src/service/mod.rs (calls build_local_actor)

Trade-offs

  • Centralizes URL construction and keypair handling
  • Makes from_json side effects explicit instead of hidden
  • Might conflict with activitypub_federation's Object trait expectations
## Problem `DbActor` has 4 different construction paths: 1. `get_local_actor()` — via `Data<FederationData>` context 2. `build_local_actor()` — same logic but without `FederationData` (for service construction) 3. `read_from_id()` — `Object` trait deserialization path 4. `from_json()` — remote actor ingest, does 3 things: cache upsert, UUID generation, struct building URL construction is split between `ActorUrls::build()` and manual `format!()` calls in `into_json()`. `from_json()` is 76 lines performing 3 distinct tasks. `private_key_pem` is only populated in some paths, creating inconsistent state. ## Proposal Extract actor construction into a builder/factory: ```rust DbActor::local(user_id, base_url, user_repo, actor_repo).await? DbActor::from_remote(json, data).await? // handles upsert internally ``` Separate `from_json()`'s three responsibilities: 1. `RemoteActor::from_ap_json(json)` — pure conversion 2. `actor_repo.upsert_remote_actor(actor)` — cache (called by caller) 3. `DbActor::from_remote_actor(actor)` — struct building ## Files - `src/actors.rs` (primary — `get_local_actor`, `build_local_actor`, `from_json`, `read_from_id`) - `src/service/mod.rs` (calls `build_local_actor`) ## Trade-offs - Centralizes URL construction and keypair handling - Makes `from_json` side effects explicit instead of hidden - Might conflict with `activitypub_federation`'s `Object` trait expectations
Sign in to join this conversation.
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: GKaszewski/k-ap#13