2023-02-19 17:56:01 +05:30
|
|
|
use crate::{
|
|
|
|
error::Error,
|
|
|
|
instance::DatabaseHandle,
|
|
|
|
objects::person::{DbUser, PersonAcceptedActivities},
|
|
|
|
};
|
|
|
|
use activitypub_federation::{
|
2023-06-12 17:02:54 +05:30
|
|
|
actix_web::{inbox::receive_activity, signing_actor},
|
2023-03-17 02:11:29 +05:30
|
|
|
config::{Data, FederationConfig, FederationMiddleware},
|
2023-03-06 06:47:34 +05:30
|
|
|
fetch::webfinger::{build_webfinger_response, extract_webfinger_name},
|
2023-02-19 17:56:01 +05:30
|
|
|
protocol::context::WithContext,
|
2023-06-12 17:02:54 +05:30
|
|
|
traits::{Actor, Object},
|
2023-03-17 02:11:29 +05:30
|
|
|
FEDERATION_CONTENT_TYPE,
|
2023-02-19 17:56:01 +05:30
|
|
|
};
|
|
|
|
use actix_web::{web, web::Bytes, App, HttpRequest, HttpResponse, HttpServer};
|
|
|
|
use anyhow::anyhow;
|
2023-03-02 04:49:10 +05:30
|
|
|
use serde::Deserialize;
|
2023-03-02 19:48:06 +05:30
|
|
|
use tracing::info;
|
2023-02-19 17:56:01 +05:30
|
|
|
|
2023-03-02 04:49:10 +05:30
|
|
|
pub fn listen(config: &FederationConfig<DatabaseHandle>) -> Result<(), Error> {
|
2023-03-08 03:31:36 +05:30
|
|
|
let hostname = config.domain();
|
2023-03-02 19:48:06 +05:30
|
|
|
info!("Listening with actix-web on {hostname}");
|
2023-03-02 04:49:10 +05:30
|
|
|
let config = config.clone();
|
2023-02-19 17:56:01 +05:30
|
|
|
let server = HttpServer::new(move || {
|
|
|
|
App::new()
|
2023-03-17 02:11:29 +05:30
|
|
|
.wrap(FederationMiddleware::new(config.clone()))
|
2023-06-12 17:02:54 +05:30
|
|
|
.route("/", web::get().to(http_get_system_user))
|
2023-02-19 17:56:01 +05:30
|
|
|
.route("/{user}", web::get().to(http_get_user))
|
|
|
|
.route("/{user}/inbox", web::post().to(http_post_user_inbox))
|
2023-03-02 04:49:10 +05:30
|
|
|
.route("/.well-known/webfinger", web::get().to(webfinger))
|
2023-02-19 17:56:01 +05:30
|
|
|
})
|
|
|
|
.bind(hostname)?
|
|
|
|
.run();
|
|
|
|
actix_rt::spawn(server);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-06-12 17:02:54 +05:30
|
|
|
/// Handles requests to fetch system user json over HTTP
|
|
|
|
pub async fn http_get_system_user(data: Data<DatabaseHandle>) -> Result<HttpResponse, Error> {
|
|
|
|
let json_user = data.system_user.clone().into_json(&data).await?;
|
|
|
|
Ok(HttpResponse::Ok()
|
|
|
|
.content_type(FEDERATION_CONTENT_TYPE)
|
|
|
|
.json(WithContext::new_default(json_user)))
|
|
|
|
}
|
|
|
|
|
2023-02-19 17:56:01 +05:30
|
|
|
/// Handles requests to fetch user json over HTTP
|
|
|
|
pub async fn http_get_user(
|
2023-06-12 17:02:54 +05:30
|
|
|
request: HttpRequest,
|
2023-02-19 17:56:01 +05:30
|
|
|
user_name: web::Path<String>,
|
2023-03-16 06:41:48 +05:30
|
|
|
data: Data<DatabaseHandle>,
|
2023-02-19 17:56:01 +05:30
|
|
|
) -> Result<HttpResponse, Error> {
|
2023-06-12 17:02:54 +05:30
|
|
|
let signed_by = signing_actor::<DbUser>(&request, None, &data).await?;
|
|
|
|
// here, checks can be made on the actor or the domain to which
|
|
|
|
// it belongs, to verify whether it is allowed to access this resource
|
|
|
|
info!(
|
|
|
|
"Fetch user request is signed by system account {}",
|
|
|
|
signed_by.id()
|
|
|
|
);
|
|
|
|
|
2023-02-19 17:56:01 +05:30
|
|
|
let db_user = data.local_user();
|
|
|
|
if user_name.into_inner() == db_user.name {
|
2023-03-17 02:11:29 +05:30
|
|
|
let json_user = db_user.into_json(&data).await?;
|
2023-02-19 17:56:01 +05:30
|
|
|
Ok(HttpResponse::Ok()
|
2023-03-17 02:11:29 +05:30
|
|
|
.content_type(FEDERATION_CONTENT_TYPE)
|
|
|
|
.json(WithContext::new_default(json_user)))
|
2023-02-19 17:56:01 +05:30
|
|
|
} else {
|
|
|
|
Err(anyhow!("Invalid user").into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Handles messages received in user inbox
|
|
|
|
pub async fn http_post_user_inbox(
|
|
|
|
request: HttpRequest,
|
|
|
|
body: Bytes,
|
2023-03-16 06:41:48 +05:30
|
|
|
data: Data<DatabaseHandle>,
|
2023-02-19 17:56:01 +05:30
|
|
|
) -> Result<HttpResponse, Error> {
|
|
|
|
receive_activity::<WithContext<PersonAcceptedActivities>, DbUser, DatabaseHandle>(
|
|
|
|
request, body, &data,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
2023-03-02 04:49:10 +05:30
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
pub struct WebfingerQuery {
|
|
|
|
resource: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn webfinger(
|
|
|
|
query: web::Query<WebfingerQuery>,
|
2023-03-16 06:41:48 +05:30
|
|
|
data: Data<DatabaseHandle>,
|
2023-03-02 04:49:10 +05:30
|
|
|
) -> Result<HttpResponse, Error> {
|
|
|
|
let name = extract_webfinger_name(&query.resource, &data)?;
|
|
|
|
let db_user = data.read_user(&name)?;
|
|
|
|
Ok(HttpResponse::Ok().json(build_webfinger_response(
|
|
|
|
query.resource.clone(),
|
|
|
|
db_user.ap_id.into_inner(),
|
|
|
|
)))
|
|
|
|
}
|