add stub ActivityStreams inbox endpoint & worker

This commit is contained in:
anna 2022-12-27 02:16:29 +01:00
parent 6e81a7198b
commit 5b985b0059
Signed by: fef
GPG key ID: EC22E476DC2D3D84
5 changed files with 63 additions and 1 deletions

View file

@ -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"),
}
}
}

35
src/job/inbox.rs Normal file
View file

@ -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)
}
}

View file

@ -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);

20
src/route/inbox.rs Normal file
View file

@ -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<HttpResponse> {
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);
}

View file

@ -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));
}