diff --git a/src/core/mod.rs b/src/core/mod.rs index e9ee5e0..ab02668 100644 --- a/src/core/mod.rs +++ b/src/core/mod.rs @@ -26,6 +26,7 @@ pub enum Error { BadToken(jsonwebtoken::errors::Error), BadCredentials, BadBearcap, + BadRequest, } pub fn utc_now() -> NaiveDateTime { @@ -48,6 +49,7 @@ impl ResponseError for Error { Error::BadToken(_) => StatusCode::UNAUTHORIZED, Error::BadCredentials => StatusCode::UNAUTHORIZED, Error::BadBearcap => StatusCode::UNPROCESSABLE_ENTITY, + Error::BadRequest => StatusCode::BAD_REQUEST, _ => StatusCode::INTERNAL_SERVER_ERROR, } } @@ -68,6 +70,7 @@ impl fmt::Display for Error { Error::BadToken(jwt_error) => jwt_error.fmt(f), Error::BadCredentials => write!(f, "Invalid user name or password"), Error::BadBearcap => write!(f, "Invalid bearcap URL"), + Error::BadRequest => write!(f, "Bad request"), } } } diff --git a/src/job/inbox.rs b/src/job/inbox.rs new file mode 100644 index 0000000..c7b0ff4 --- /dev/null +++ b/src/job/inbox.rs @@ -0,0 +1,35 @@ +use async_trait::async_trait; +use serde::{Deserialize, Serialize}; +use std::fmt; + +use super::Job; +use crate::core::*; +use crate::state::AppState; + +#[derive(Serialize, Deserialize)] +pub struct InboxWorker { + raw: String, +} + +impl InboxWorker { + pub fn new(raw: String) -> InboxWorker { + InboxWorker { raw } + } +} + +#[async_trait] +impl Job for InboxWorker { + async fn perform(&self, state: &AppState) -> Result<()> { + todo!() + } + + fn name(&self) -> &'static str { + "inbox" + } +} + +impl fmt::Debug for InboxWorker { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "ActivityPub ingress: {}", &self.raw) + } +} diff --git a/src/job/mod.rs b/src/job/mod.rs index 88e5aff..aeec22a 100644 --- a/src/job/mod.rs +++ b/src/job/mod.rs @@ -10,6 +10,8 @@ use tokio::time::sleep; use crate::core::*; use crate::state::{AppState, State}; +pub mod inbox; + const MAX_RETRIES: u32 = 3; const ERROR_DELAY: Duration = Duration::from_secs(5 * 60); diff --git a/src/route/inbox.rs b/src/route/inbox.rs new file mode 100644 index 0000000..3b0c9e4 --- /dev/null +++ b/src/route/inbox.rs @@ -0,0 +1,20 @@ +use crate::ap::processor::InboxWorker; +use actix_web::{post, web, HttpResponse, HttpRequest}; + +use crate::core::*; +use crate::state::AppState; + +#[post("")] +async fn post_inbox(body: String, request: HttpRequest, state: AppState) -> Result { + let content_type = request.headers().get("Content-Type").ok_or(Error::BadRequest)?.to_str()?; + if content_type != "application/activity+json" { + return Ok(HttpResponse::BadRequest().finish()); + } + + state.sched.schedule(InboxWorker::new(body)); + Ok(HttpResponse::Ok().finish()) +} + +pub fn configure(cfg: &mut web::ServiceConfig) { + cfg.service(post_inbox); +} diff --git a/src/route/mod.rs b/src/route/mod.rs index 5866f6b..6f22764 100644 --- a/src/route/mod.rs +++ b/src/route/mod.rs @@ -1,7 +1,9 @@ use actix_web::web; pub mod api; +pub mod inbox; pub fn configure(cfg: &mut web::ServiceConfig) { - cfg.service(web::scope("/api").configure(api::configure)); + cfg.service(web::scope("/api").configure(api::configure)) + .service(web::scope("/inbox").configure(inbox::configure)); }