2024-04-14 16:57:36 +02:00
|
|
|
use apb::{server::Inbox, Activity, ActivityType};
|
2024-04-12 20:01:47 +02:00
|
|
|
use axum::{extract::{Query, State}, http::StatusCode, Json};
|
2024-04-22 02:52:18 +02:00
|
|
|
use sea_orm::{QueryFilter, QuerySelect};
|
2024-03-24 04:05:09 +01:00
|
|
|
|
2024-04-13 01:49:04 +02:00
|
|
|
use crate::{errors::UpubError, model::{self, addressing::EmbeddedActivity}, server::{auth::{AuthIdentity, Identity}, Context}, url};
|
2024-03-24 04:05:09 +01:00
|
|
|
|
2024-04-12 20:01:47 +02:00
|
|
|
use super::{jsonld::LD, JsonLD, Pagination};
|
2024-03-24 04:05:09 +01:00
|
|
|
|
|
|
|
|
2024-03-28 04:52:17 +01:00
|
|
|
pub async fn get(
|
|
|
|
State(ctx): State<Context>,
|
|
|
|
) -> Result<JsonLD<serde_json::Value>, StatusCode> {
|
|
|
|
Ok(JsonLD(ctx.ap_collection(&url!(ctx, "/inbox"), None).ld_context()))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn page(
|
|
|
|
State(ctx): State<Context>,
|
|
|
|
AuthIdentity(auth): AuthIdentity,
|
|
|
|
Query(page): Query<Pagination>,
|
2024-04-12 22:56:29 +02:00
|
|
|
) -> crate::Result<JsonLD<serde_json::Value>> {
|
2024-03-28 04:52:17 +01:00
|
|
|
let limit = page.batch.unwrap_or(20).min(50);
|
2024-03-24 04:58:49 +01:00
|
|
|
let offset = page.offset.unwrap_or(0);
|
2024-04-12 19:36:00 +02:00
|
|
|
let activities = model::addressing::Entity::find_activities()
|
|
|
|
.filter(auth.filter_condition())
|
2024-03-28 04:52:17 +01:00
|
|
|
.limit(limit)
|
|
|
|
.offset(offset)
|
2024-04-12 20:01:47 +02:00
|
|
|
.into_model::<EmbeddedActivity>()
|
2024-03-28 04:52:17 +01:00
|
|
|
.all(ctx.db())
|
|
|
|
.await?;
|
2024-04-21 23:19:26 +02:00
|
|
|
let mut out = Vec::new();
|
|
|
|
for activity in activities {
|
|
|
|
out.push(activity.ap_filled(ctx.db()).await?);
|
|
|
|
}
|
2024-03-28 04:52:17 +01:00
|
|
|
Ok(JsonLD(
|
|
|
|
ctx.ap_collection_page(
|
|
|
|
&url!(ctx, "/inbox/page"),
|
2024-04-21 23:19:26 +02:00
|
|
|
offset, limit, out,
|
2024-03-28 04:52:17 +01:00
|
|
|
).ld_context()
|
|
|
|
))
|
2024-03-24 04:05:09 +01:00
|
|
|
}
|
2024-04-12 20:01:47 +02:00
|
|
|
|
2024-04-18 14:01:55 +02:00
|
|
|
macro_rules! pretty_json {
|
|
|
|
($json:ident) => {
|
|
|
|
serde_json::to_string_pretty(&$json).expect("failed serializing to string serde_json::Value")
|
|
|
|
}
|
|
|
|
}
|
2024-04-13 01:49:04 +02:00
|
|
|
|
|
|
|
|
2024-04-12 20:01:47 +02:00
|
|
|
pub async fn post(
|
|
|
|
State(ctx): State<Context>,
|
2024-04-13 01:49:04 +02:00
|
|
|
AuthIdentity(auth): AuthIdentity,
|
2024-04-12 20:01:47 +02:00
|
|
|
Json(activity): Json<serde_json::Value>
|
2024-04-13 01:49:04 +02:00
|
|
|
) -> crate::Result<()> {
|
2024-04-22 22:52:05 +02:00
|
|
|
let Identity::Remote(server) = auth else {
|
2024-04-19 16:15:05 +02:00
|
|
|
if activity.activity_type() != Some(ActivityType::Delete) { // this is spammy af, ignore them!
|
|
|
|
tracing::warn!("refusing unauthorized activity: {}", pretty_json!(activity));
|
|
|
|
}
|
2024-04-22 22:52:05 +02:00
|
|
|
if matches!(auth, Identity::Anonymous) {
|
|
|
|
return Err(UpubError::unauthorized());
|
|
|
|
} else {
|
|
|
|
return Err(UpubError::forbidden());
|
2024-04-18 14:01:55 +02:00
|
|
|
}
|
2024-04-22 22:52:05 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
let Some(actor) = activity.actor().id() else {
|
|
|
|
return Err(UpubError::bad_request());
|
|
|
|
};
|
|
|
|
|
|
|
|
// TODO add whitelist of relays
|
|
|
|
if !server.ends_with(&Context::server(&actor)) {
|
|
|
|
return Err(UpubError::unauthorized());
|
2024-04-13 01:49:04 +02:00
|
|
|
}
|
2024-04-12 20:01:47 +02:00
|
|
|
|
2024-04-14 16:47:36 +02:00
|
|
|
// TODO we could process Links and bare Objects maybe, but probably out of AP spec?
|
|
|
|
match activity.activity_type().ok_or_else(UpubError::bad_request)? {
|
|
|
|
ActivityType::Activity => {
|
2024-04-18 14:01:55 +02:00
|
|
|
tracing::warn!("skipping unprocessable base activity: {}", pretty_json!(activity));
|
2024-04-12 20:01:47 +02:00
|
|
|
Err(StatusCode::UNPROCESSABLE_ENTITY.into()) // won't ingest useless stuff
|
|
|
|
},
|
|
|
|
|
2024-04-22 22:52:05 +02:00
|
|
|
ActivityType::Create => Ok(ctx.create(server, activity).await?),
|
|
|
|
ActivityType::Like => Ok(ctx.like(server, activity).await?),
|
|
|
|
ActivityType::Follow => Ok(ctx.follow(server, activity).await?),
|
|
|
|
ActivityType::Announce => Ok(ctx.announce(server, activity).await?),
|
|
|
|
ActivityType::Accept(_) => Ok(ctx.accept(server, activity).await?),
|
|
|
|
ActivityType::Reject(_) => Ok(ctx.reject(server, activity).await?),
|
|
|
|
ActivityType::Undo => Ok(ctx.undo(server, activity).await?),
|
|
|
|
ActivityType::Delete => Ok(ctx.delete(server, activity).await?),
|
|
|
|
ActivityType::Update => Ok(ctx.update(server, activity).await?),
|
2024-04-14 16:47:36 +02:00
|
|
|
|
|
|
|
_x => {
|
2024-04-18 14:01:55 +02:00
|
|
|
tracing::info!("received unimplemented activity on inbox: {}", pretty_json!(activity));
|
2024-04-12 20:01:47 +02:00
|
|
|
Err(StatusCode::NOT_IMPLEMENTED.into())
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|