2023-03-08 03:31:36 +05:30
|
|
|
use crate::{
|
|
|
|
database::DatabaseHandle,
|
|
|
|
error::Error,
|
|
|
|
objects::person::{DbUser, Person, PersonAcceptedActivities},
|
|
|
|
};
|
|
|
|
use activitypub_federation::{
|
|
|
|
axum::{
|
|
|
|
inbox::{receive_activity, ActivityData},
|
2023-03-17 02:11:29 +05:30
|
|
|
json::FederationJson,
|
2023-03-08 03:31:36 +05:30
|
|
|
},
|
2023-03-16 06:41:48 +05:30
|
|
|
config::Data,
|
2023-03-08 03:31:36 +05:30
|
|
|
fetch::webfinger::{build_webfinger_response, extract_webfinger_name, Webfinger},
|
|
|
|
protocol::context::WithContext,
|
2023-03-17 02:11:29 +05:30
|
|
|
traits::Object,
|
2023-03-08 03:31:36 +05:30
|
|
|
};
|
|
|
|
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<String>,
|
2023-03-16 06:41:48 +05:30
|
|
|
data: Data<DatabaseHandle>,
|
2023-03-17 02:11:29 +05:30
|
|
|
) -> Result<FederationJson<WithContext<Person>>, Error> {
|
2023-03-08 03:31:36 +05:30
|
|
|
let db_user = data.read_user(&name)?;
|
2023-03-17 02:11:29 +05:30
|
|
|
let json_user = db_user.into_json(&data).await?;
|
|
|
|
Ok(FederationJson(WithContext::new_default(json_user)))
|
2023-03-08 03:31:36 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
#[debug_handler]
|
|
|
|
pub async fn http_post_user_inbox(
|
2023-03-16 06:41:48 +05:30
|
|
|
data: Data<DatabaseHandle>,
|
2023-03-08 03:31:36 +05:30
|
|
|
activity_data: ActivityData,
|
|
|
|
) -> impl IntoResponse {
|
|
|
|
receive_activity::<WithContext<PersonAcceptedActivities>, DbUser, DatabaseHandle>(
|
|
|
|
activity_data,
|
|
|
|
&data,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
pub struct WebfingerQuery {
|
|
|
|
resource: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[debug_handler]
|
|
|
|
pub async fn webfinger(
|
|
|
|
Query(query): Query<WebfingerQuery>,
|
2023-03-16 06:41:48 +05:30
|
|
|
data: Data<DatabaseHandle>,
|
2023-03-08 03:31:36 +05:30
|
|
|
) -> Result<Json<Webfinger>, Error> {
|
|
|
|
let name = extract_webfinger_name(&query.resource, &data)?;
|
2023-12-12 03:18:32 +05:30
|
|
|
let db_user = data.read_user(name)?;
|
2023-03-08 03:31:36 +05:30
|
|
|
Ok(Json(build_webfinger_response(
|
|
|
|
query.resource,
|
|
|
|
db_user.ap_id.into_inner(),
|
|
|
|
)))
|
|
|
|
}
|