Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| bc857b2c08 | |||
| 7901b29f7c | |||
|
|
a604e1bd40 | ||
|
|
f5374ec861 | ||
|
|
cc30582a1c | ||
|
|
f8dc20c026 | ||
|
|
630cffe33f |
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "k-ap"
|
name = "k-ap"
|
||||||
version = "0.1.0"
|
version = "0.1.4"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
description = "Generic ActivityPub protocol layer"
|
description = "Generic ActivityPub protocol layer"
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
|
|||||||
99
README.md
Normal file
99
README.md
Normal file
@@ -0,0 +1,99 @@
|
|||||||
|
# k-ap
|
||||||
|
|
||||||
|
Generic ActivityPub protocol layer for Rust services. Extracted from the `thoughts` and `movies-diary` projects.
|
||||||
|
|
||||||
|
Wraps [`activitypub_federation`](https://crates.io/crates/activitypub_federation) and provides the plumbing that every AP-enabled service needs: actor management, inbox/outbox routing, follower tracking, WebFinger, NodeInfo, and HTTP signature handling.
|
||||||
|
|
||||||
|
Not domain-specific — no opinions about what your content type looks like.
|
||||||
|
|
||||||
|
## Add as dependency
|
||||||
|
|
||||||
|
```toml
|
||||||
|
[dependencies]
|
||||||
|
k-ap = { git = "https://git.gabrielkaszewski.dev/GKaszewski/k-ap.git", tag = "v0.1.0" }
|
||||||
|
```
|
||||||
|
|
||||||
|
## What you implement
|
||||||
|
|
||||||
|
Three traits wire your data layer into `k-ap`:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
// Your database layer for follows, keypairs, remote actors, blocks
|
||||||
|
impl FederationRepository for MyFederationRepo { ... }
|
||||||
|
|
||||||
|
// Your user lookup (id, username, bio, avatar)
|
||||||
|
impl ApUserRepository for MyUserRepo { ... }
|
||||||
|
|
||||||
|
// Dispatch incoming AP objects to the right handler
|
||||||
|
impl ApObjectHandler for MyObjectHandler { ... }
|
||||||
|
```
|
||||||
|
|
||||||
|
## Wire up the service
|
||||||
|
|
||||||
|
```rust
|
||||||
|
use k_ap::{ActivityPubService, FederationRepository, ApUserRepository, ApObjectHandler};
|
||||||
|
|
||||||
|
let service = ActivityPubService::builder(
|
||||||
|
Arc::new(my_federation_repo),
|
||||||
|
Arc::new(my_user_repo),
|
||||||
|
Arc::new(my_object_handler),
|
||||||
|
"https://example.com",
|
||||||
|
)
|
||||||
|
.allow_registration(true)
|
||||||
|
.software_name("my-app")
|
||||||
|
.build()
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
// Mount the AP routes onto your axum router
|
||||||
|
let router = Router::new().merge(service.router());
|
||||||
|
```
|
||||||
|
|
||||||
|
## What the service handles for you
|
||||||
|
|
||||||
|
- **Actor** — `GET /users/:id` serves the AP Person object with public key
|
||||||
|
- **Inbox** — `POST /users/:id/inbox` + `POST /inbox` (shared), verifies HTTP signatures, dispatches to your `ApObjectHandler`
|
||||||
|
- **Outbox** — `GET /users/:id/outbox` with OrderedCollection pagination
|
||||||
|
- **Followers / Following** — `GET /users/:id/followers` and `/following`
|
||||||
|
- **WebFinger** — `GET /.well-known/webfinger`
|
||||||
|
- **NodeInfo** — `GET /.well-known/nodeinfo` + `GET /nodeinfo/2.1`
|
||||||
|
|
||||||
|
## Broadcast from your domain layer
|
||||||
|
|
||||||
|
```rust
|
||||||
|
// Fan out a new note to all accepted followers
|
||||||
|
service.broadcast_create_note(user_id, ¬e_json).await?;
|
||||||
|
service.broadcast_update_note(user_id, ¬e_json).await?;
|
||||||
|
|
||||||
|
// Announce / Undo Announce
|
||||||
|
service.broadcast_announce_to_followers(user_id, object_ap_id).await?;
|
||||||
|
service.broadcast_undo_announce_to_followers(user_id, object_ap_id, object_url).await?;
|
||||||
|
|
||||||
|
// Like / Unlike to a remote inbox
|
||||||
|
service.broadcast_like_to_inbox(user_id, object_ap_id, inbox_url).await?;
|
||||||
|
service.broadcast_undo_like_to_inbox(user_id, object_ap_id, inbox_url).await?;
|
||||||
|
|
||||||
|
// Follow / Unfollow / Accept / Reject
|
||||||
|
service.follow(local_user_id, remote_actor_url, handle).await?;
|
||||||
|
service.unfollow(local_user_id, remote_actor_url).await?;
|
||||||
|
service.accept_follower(local_user_id, remote_actor_url).await?;
|
||||||
|
service.reject_follower(local_user_id, remote_actor_url).await?;
|
||||||
|
```
|
||||||
|
|
||||||
|
## Project-specific ports
|
||||||
|
|
||||||
|
`k-ap` does not define port traits tied to your domain (e.g. `OutboundFederationPort`, `ActivityPubRepository<Thought>`). Those belong in your adapter layer and are wired up there. See `crates/adapters/activitypub/src/port.rs` in `thoughts` for a reference implementation.
|
||||||
|
|
||||||
|
## Key public types
|
||||||
|
|
||||||
|
| Type | Description |
|
||||||
|
|------|-------------|
|
||||||
|
| `ActivityPubService` | Central service — build once, share via `Arc` |
|
||||||
|
| `FederationData` | Request-scoped data passed through the federation layer |
|
||||||
|
| `FederationRepository` | Trait: follows, keypairs, remote actors, blocks |
|
||||||
|
| `ApUserRepository` | Trait: user lookup by id / username |
|
||||||
|
| `ApObjectHandler` | Trait: dispatch incoming AP objects |
|
||||||
|
| `RemoteActor` | A federated actor record |
|
||||||
|
| `Follower` / `FollowerStatus` | Follower with pending/accepted/rejected state |
|
||||||
|
| `ApUser` | AP-serializable local user |
|
||||||
|
| `ApFederationConfig` | Wraps the `activitypub_federation` config |
|
||||||
|
| `Error` | AP-layer error type |
|
||||||
@@ -19,6 +19,7 @@ use crate::user::ApProfileField;
|
|||||||
pub struct DbActor {
|
pub struct DbActor {
|
||||||
pub user_id: uuid::Uuid,
|
pub user_id: uuid::Uuid,
|
||||||
pub username: String,
|
pub username: String,
|
||||||
|
pub display_name: Option<String>,
|
||||||
pub public_key_pem: String,
|
pub public_key_pem: String,
|
||||||
pub private_key_pem: Option<String>,
|
pub private_key_pem: Option<String>,
|
||||||
pub inbox_url: Url,
|
pub inbox_url: Url,
|
||||||
@@ -152,6 +153,7 @@ pub async fn get_local_actor(
|
|||||||
Ok(DbActor {
|
Ok(DbActor {
|
||||||
user_id,
|
user_id,
|
||||||
username: user.username,
|
username: user.username,
|
||||||
|
display_name: None,
|
||||||
public_key_pem: public_key,
|
public_key_pem: public_key,
|
||||||
private_key_pem: Some(private_key),
|
private_key_pem: Some(private_key),
|
||||||
inbox_url,
|
inbox_url,
|
||||||
@@ -219,6 +221,7 @@ impl Object for DbActor {
|
|||||||
Ok(Some(DbActor {
|
Ok(Some(DbActor {
|
||||||
user_id,
|
user_id,
|
||||||
username: user.username,
|
username: user.username,
|
||||||
|
display_name: None,
|
||||||
public_key_pem: public_key,
|
public_key_pem: public_key,
|
||||||
private_key_pem: private_key,
|
private_key_pem: private_key,
|
||||||
inbox_url,
|
inbox_url,
|
||||||
@@ -228,12 +231,12 @@ impl Object for DbActor {
|
|||||||
following_url,
|
following_url,
|
||||||
ap_id,
|
ap_id,
|
||||||
last_refreshed_at: Utc::now(),
|
last_refreshed_at: Utc::now(),
|
||||||
bio: None,
|
bio: user.bio,
|
||||||
avatar_url: None,
|
avatar_url: user.avatar_url,
|
||||||
banner_url: None,
|
banner_url: user.banner_url,
|
||||||
also_known_as: None,
|
also_known_as: user.also_known_as,
|
||||||
profile_url: None,
|
profile_url: user.profile_url,
|
||||||
attachment: vec![],
|
attachment: user.attachment,
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -327,6 +330,7 @@ impl Object for DbActor {
|
|||||||
Ok(DbActor {
|
Ok(DbActor {
|
||||||
user_id,
|
user_id,
|
||||||
username: json.preferred_username.clone(),
|
username: json.preferred_username.clone(),
|
||||||
|
display_name: json.name.clone(),
|
||||||
public_key_pem: json.public_key.public_key_pem,
|
public_key_pem: json.public_key.public_key_pem,
|
||||||
private_key_pem: None,
|
private_key_pem: None,
|
||||||
inbox_url,
|
inbox_url,
|
||||||
|
|||||||
@@ -25,4 +25,4 @@ pub use repository::{
|
|||||||
BlockedDomain, FederationRepository, Follower, FollowerStatus, FollowingStatus, RemoteActor,
|
BlockedDomain, FederationRepository, Follower, FollowerStatus, FollowingStatus, RemoteActor,
|
||||||
};
|
};
|
||||||
pub use service::ActivityPubService;
|
pub use service::ActivityPubService;
|
||||||
pub use user::{ApProfileField, ApUser, ApUserRepository};
|
pub use user::{ApProfileField, ApUser, ApUserRepository, LookedUpActor};
|
||||||
|
|||||||
@@ -330,6 +330,33 @@ impl ActivityPubService {
|
|||||||
Ok(serde_json::to_string(&WithContext::new_default(person))?)
|
Ok(serde_json::to_string(&WithContext::new_default(person))?)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Resolve a `@user@domain` handle to actor data using a signed HTTP request.
|
||||||
|
/// Unlike a plain unauthenticated fetch, this works with instances (e.g. Threads)
|
||||||
|
/// that require HTTP signatures before returning full actor JSON.
|
||||||
|
pub async fn lookup_actor_by_handle(
|
||||||
|
&self,
|
||||||
|
handle: &str,
|
||||||
|
) -> anyhow::Result<crate::user::LookedUpActor> {
|
||||||
|
let data = self.federation_config.to_request_data();
|
||||||
|
let actor = Self::webfinger_https(handle, &data).await?;
|
||||||
|
let domain = actor.ap_id.host_str().unwrap_or("").to_string();
|
||||||
|
let handle = format!("{}@{}", actor.username, domain);
|
||||||
|
Ok(crate::user::LookedUpActor {
|
||||||
|
handle,
|
||||||
|
display_name: actor.display_name,
|
||||||
|
bio: actor.bio,
|
||||||
|
avatar_url: actor.avatar_url,
|
||||||
|
banner_url: actor.banner_url,
|
||||||
|
ap_url: actor.ap_id,
|
||||||
|
outbox_url: actor.outbox_url,
|
||||||
|
followers_url: actor.followers_url,
|
||||||
|
following_url: actor.following_url,
|
||||||
|
also_known_as: actor.also_known_as,
|
||||||
|
profile_url: actor.profile_url,
|
||||||
|
attachment: actor.attachment,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns the ActivityPub router compatible with any outer state `S`.
|
/// Returns the ActivityPub router compatible with any outer state `S`.
|
||||||
/// Handlers only use `Data<FederationData>` injected by the middleware layer,
|
/// Handlers only use `Data<FederationData>` injected by the middleware layer,
|
||||||
/// so the router is independent of the application state type.
|
/// so the router is independent of the application state type.
|
||||||
|
|||||||
18
src/user.rs
18
src/user.rs
@@ -7,6 +7,24 @@ pub struct ApProfileField {
|
|||||||
pub value: String,
|
pub value: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Resolved actor data returned by [`crate::service::ActivityPubService::lookup_actor_by_handle`].
|
||||||
|
/// Fetched via a signed HTTP request so strict instances (e.g. Threads) return full data.
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct LookedUpActor {
|
||||||
|
pub handle: String,
|
||||||
|
pub display_name: Option<String>,
|
||||||
|
pub bio: Option<String>,
|
||||||
|
pub avatar_url: Option<Url>,
|
||||||
|
pub banner_url: Option<Url>,
|
||||||
|
pub ap_url: Url,
|
||||||
|
pub outbox_url: Url,
|
||||||
|
pub followers_url: Url,
|
||||||
|
pub following_url: Url,
|
||||||
|
pub also_known_as: Option<String>,
|
||||||
|
pub profile_url: Option<Url>,
|
||||||
|
pub attachment: Vec<ApProfileField>,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct ApUser {
|
pub struct ApUser {
|
||||||
pub id: uuid::Uuid,
|
pub id: uuid::Uuid,
|
||||||
|
|||||||
Reference in New Issue
Block a user