federation refinement

This commit is contained in:
2026-05-09 13:53:45 +02:00
parent 86909ecede
commit 0d3c2c937d
56 changed files with 1513 additions and 544 deletions

View File

@@ -6,6 +6,17 @@ use domain::value_objects::UserId;
use crate::data::FederationData;
use crate::error::Error;
use crate::repository::FollowerStatus;
fn ordered_collection(id: String, total: usize, items: Vec<String>) -> serde_json::Value {
json!({
"@context": "https://www.w3.org/ns/activitystreams",
"type": "OrderedCollection",
"id": id,
"totalItems": total,
"orderedItems": items,
})
}
pub async fn followers_handler(
Path(user_id_str): Path<String>,
@@ -13,25 +24,29 @@ pub async fn followers_handler(
) -> Result<FederationJson<serde_json::Value>, Error> {
let user_id = UserId::from_uuid(
uuid::Uuid::parse_str(&user_id_str)
.map_err(|_| Error(anyhow::anyhow!("invalid user id")))?,
.map_err(|_| Error::bad_request(anyhow::anyhow!("invalid user id")))?,
);
// verify user exists
data.user_repo
.find_by_id(&user_id)
.await
.map_err(|e| Error(e.into()))?
.ok_or_else(|| Error(anyhow::anyhow!("user not found")))?;
.map_err(Error::from)?
.ok_or_else(|| Error::not_found(anyhow::anyhow!("user not found")))?;
let followers = data
.federation_repo
.get_followers(user_id)
.await
.map_err(Error::from)?;
let items: Vec<String> = followers
.into_iter()
.filter(|f| f.status == FollowerStatus::Accepted)
.map(|f| f.actor.url)
.collect();
let id = format!("{}/users/{}/followers", data.base_url, user_id_str);
// TODO(ap): implement pagination
Ok(FederationJson(json!({
"@context": "https://www.w3.org/ns/activitystreams",
"type": "OrderedCollection",
"id": id,
"totalItems": 0,
"orderedItems": []
})))
Ok(FederationJson(ordered_collection(id, items.len(), items)))
}
pub async fn following_handler(
@@ -40,23 +55,26 @@ pub async fn following_handler(
) -> Result<FederationJson<serde_json::Value>, Error> {
let user_id = UserId::from_uuid(
uuid::Uuid::parse_str(&user_id_str)
.map_err(|_| Error(anyhow::anyhow!("invalid user id")))?,
.map_err(|_| Error::bad_request(anyhow::anyhow!("invalid user id")))?,
);
// verify user exists
data.user_repo
.find_by_id(&user_id)
.await
.map_err(|e| Error(e.into()))?
.ok_or_else(|| Error(anyhow::anyhow!("user not found")))?;
.map_err(Error::from)?
.ok_or_else(|| Error::not_found(anyhow::anyhow!("user not found")))?;
let following = data
.federation_repo
.get_following(user_id)
.await
.map_err(Error::from)?;
let items: Vec<String> = following
.into_iter()
.map(|a| a.url)
.collect();
let id = format!("{}/users/{}/following", data.base_url, user_id_str);
// TODO(ap): implement pagination
Ok(FederationJson(json!({
"@context": "https://www.w3.org/ns/activitystreams",
"type": "OrderedCollection",
"id": id,
"totalItems": 0,
"orderedItems": []
})))
Ok(FederationJson(ordered_collection(id, items.len(), items)))
}