You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
1.4 KiB
Rust

use actix_web::http::header::{ACCEPT, CONTENT_TYPE};
use actix_web::{post, web, HttpRequest, HttpResponse};
use mime::Mime;
use std::str::FromStr;
use crate::core::*;
use crate::job::inbox::InboxWorker;
use crate::state::AppState;
#[post("")]
async fn post_inbox(body: String, request: HttpRequest, state: AppState) -> Result<HttpResponse> {
const CONTENT_TYPES: &[&str] = &[
"application/activity+json",
"application/ld+json",
"application/json",
];
let content_type = request
.headers()
.get(CONTENT_TYPE)
.ok_or(Error::BadRequest)?
.to_str()?;
let content_type = Mime::from_str(content_type).map_err(|_| Error::BadRequest)?;
let is_apub = if content_type.type_() == "application" {
match content_type.subtype().as_str() {
"activity" => content_type.suffix().map(|s| s.as_str()) == Some("json"),
"ld" => content_type.suffix().map(|s| s.as_str()) == Some("json"),
"json" => true,
_ => false,
}
} else {
false
};
if !is_apub {
return Ok(HttpResponse::UnsupportedMediaType()
.append_header((ACCEPT, "application/activity+json, application/ld+json"))
.finish());
}
state.sched.schedule(InboxWorker::new(body, content_type));
Ok(HttpResponse::Accepted().finish())
}
pub fn configure(cfg: &mut web::ServiceConfig) {
cfg.service(post_inbox);
}