diff options
author | clarkzjw <[email protected]> | 2023-07-26 12:37:38 -0700 |
---|---|---|
committer | clarkzjw <[email protected]> | 2023-07-26 12:37:38 -0700 |
commit | af2f09ea4cbb97d3ee91e30bb58e85508989d63a (patch) | |
tree | 671ead9c450a0abf71efc00ba1f2966ae6e60e02 /fedi/live_federation/http.rs | |
parent | f847de64eb8f724fa512801b43a26522afff61ae (diff) | |
download | photo-af2f09ea4cbb97d3ee91e30bb58e85508989d63a.tar.gz |
add example from https://github.com/LemmyNet/activitypub-federation-rust
Diffstat (limited to 'fedi/live_federation/http.rs')
-rw-r--r-- | fedi/live_federation/http.rs | 69 |
1 files changed, 69 insertions, 0 deletions
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 @@ | |||
1 | use crate::{ | ||
2 | database::DatabaseHandle, | ||
3 | error::Error, | ||
4 | objects::person::{DbUser, Person, PersonAcceptedActivities}, | ||
5 | }; | ||
6 | use activitypub_federation::{ | ||
7 | axum::{ | ||
8 | inbox::{receive_activity, ActivityData}, | ||
9 | json::FederationJson, | ||
10 | }, | ||
11 | config::Data, | ||
12 | fetch::webfinger::{build_webfinger_response, extract_webfinger_name, Webfinger}, | ||
13 | protocol::context::WithContext, | ||
14 | traits::Object, | ||
15 | }; | ||
16 | use axum::{ | ||
17 | extract::{Path, Query}, | ||
18 | response::{IntoResponse, Response}, | ||
19 | Json, | ||
20 | }; | ||
21 | use axum_macros::debug_handler; | ||
22 | use http::StatusCode; | ||
23 | use serde::Deserialize; | ||
24 | |||
25 | impl IntoResponse for Error { | ||
26 | fn into_response(self) -> Response { | ||
27 | (StatusCode::INTERNAL_SERVER_ERROR, format!("{}", self.0)).into_response() | ||
28 | } | ||
29 | } | ||
30 | |||
31 | #[debug_handler] | ||
32 | pub async fn http_get_user( | ||
33 | Path(name): Path<String>, | ||
34 | data: Data<DatabaseHandle>, | ||
35 | ) -> Result<FederationJson<WithContext<Person>>, Error> { | ||
36 | let db_user = data.read_user(&name)?; | ||
37 | let json_user = db_user.into_json(&data).await?; | ||
38 | Ok(FederationJson(WithContext::new_default(json_user))) | ||
39 | } | ||
40 | |||
41 | #[debug_handler] | ||
42 | pub async fn http_post_user_inbox( | ||
43 | data: Data<DatabaseHandle>, | ||
44 | activity_data: ActivityData, | ||
45 | ) -> impl IntoResponse { | ||
46 | receive_activity::<WithContext<PersonAcceptedActivities>, DbUser, DatabaseHandle>( | ||
47 | activity_data, | ||
48 | &data, | ||
49 | ) | ||
50 | .await | ||
51 | } | ||
52 | |||
53 | #[derive(Deserialize)] | ||
54 | pub struct WebfingerQuery { | ||
55 | resource: String, | ||
56 | } | ||
57 | |||
58 | #[debug_handler] | ||
59 | pub async fn webfinger( | ||
60 | Query(query): Query<WebfingerQuery>, | ||
61 | data: Data<DatabaseHandle>, | ||
62 | ) -> Result<Json<Webfinger>, Error> { | ||
63 | let name = extract_webfinger_name(&query.resource, &data)?; | ||
64 | let db_user = data.read_user(&name)?; | ||
65 | Ok(Json(build_webfinger_response( | ||
66 | query.resource, | ||
67 | db_user.ap_id.into_inner(), | ||
68 | ))) | ||
69 | } | ||