From af2f09ea4cbb97d3ee91e30bb58e85508989d63a Mon Sep 17 00:00:00 2001 From: clarkzjw Date: Wed, 26 Jul 2023 12:37:38 -0700 Subject: add example from https://github.com/LemmyNet/activitypub-federation-rust --- fedi/live_federation/http.rs | 69 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 fedi/live_federation/http.rs (limited to 'fedi/live_federation/http.rs') diff --git a/fedi/live_federation/http.rs b/fedi/live_federation/http.rs new file mode 100644 index 0000000..d626396 --- /dev/null +++ b/fedi/live_federation/http.rs @@ -0,0 +1,69 @@ +use crate::{ + database::DatabaseHandle, + error::Error, + objects::person::{DbUser, Person, PersonAcceptedActivities}, +}; +use activitypub_federation::{ + axum::{ + inbox::{receive_activity, ActivityData}, + json::FederationJson, + }, + config::Data, + fetch::webfinger::{build_webfinger_response, extract_webfinger_name, Webfinger}, + protocol::context::WithContext, + traits::Object, +}; +use axum::{ + extract::{Path, Query}, + response::{IntoResponse, Response}, + Json, +}; +use axum_macros::debug_handler; +use http::StatusCode; +use serde::Deserialize; + +impl IntoResponse for Error { + fn into_response(self) -> Response { + (StatusCode::INTERNAL_SERVER_ERROR, format!("{}", self.0)).into_response() + } +} + +#[debug_handler] +pub async fn http_get_user( + Path(name): Path, + data: Data, +) -> Result>, Error> { + let db_user = data.read_user(&name)?; + let json_user = db_user.into_json(&data).await?; + Ok(FederationJson(WithContext::new_default(json_user))) +} + +#[debug_handler] +pub async fn http_post_user_inbox( + data: Data, + activity_data: ActivityData, +) -> impl IntoResponse { + receive_activity::, DbUser, DatabaseHandle>( + activity_data, + &data, + ) + .await +} + +#[derive(Deserialize)] +pub struct WebfingerQuery { + resource: String, +} + +#[debug_handler] +pub async fn webfinger( + Query(query): Query, + data: Data, +) -> Result, Error> { + let name = extract_webfinger_name(&query.resource, &data)?; + let db_user = data.read_user(&name)?; + Ok(Json(build_webfinger_response( + query.resource, + db_user.ap_id.into_inner(), + ))) +} -- cgit v1.2.3