diff --git a/src/activitypub/user.rs b/src/activitypub/user.rs index 9c0d9a3b..b0d083fb 100644 --- a/src/activitypub/user.rs +++ b/src/activitypub/user.rs @@ -3,7 +3,7 @@ use std::sync::Arc; use axum::{extract::{Path, Query, State}, http::StatusCode, Json}; use sea_orm::{sea_query::Expr, ColumnTrait, Condition, DatabaseConnection, EntityTrait, IntoActiveModel, Order, PaginatorTrait, QueryFilter, QueryOrder, QuerySelect, SelectColumns}; -use crate::{activitystream::{object::{activity::{Activity, ActivityMut, ActivityType}, collection::{page::CollectionPageMut, CollectionMut, CollectionType}, ObjectType}, Base, BaseMut, BaseType, Node}, model::{self, activity, object, user}, server::Context, url}; +use crate::{activitystream::{object::{activity::{Activity, ActivityMut, ActivityType}, collection::{page::CollectionPageMut, CollectionMut, CollectionType}, ObjectType}, Base, BaseMut, BaseType, Node}, errors::LoggableError, model::{self, activity, object, user}, server::Context, url}; use super::{jsonld::LD, JsonLD}; @@ -168,12 +168,36 @@ pub async fn inbox( Path(_id): Path, Json(object): Json ) -> Result, StatusCode> { - tracing::info!("received object on inbox: {}", serde_json::to_string_pretty(&object).unwrap()); match object.base_type() { None => { Err(StatusCode::BAD_REQUEST) }, - Some(BaseType::Link(_x)) => Err(StatusCode::UNPROCESSABLE_ENTITY), // we could but not yet - Some(BaseType::Object(ObjectType::Activity(ActivityType::Activity))) => Err(StatusCode::UNPROCESSABLE_ENTITY), // won't ingest useless stuff - Some(BaseType::Object(ObjectType::Activity(ActivityType::Follow))) => { Ok(JsonLD(serde_json::Value::Null)) }, + Some(BaseType::Link(_x)) => { + tracing::warn!("skipping remote activity: {}", serde_json::to_string_pretty(&object).unwrap()); + Err(StatusCode::UNPROCESSABLE_ENTITY) // we could but not yet + }, + Some(BaseType::Object(ObjectType::Activity(ActivityType::Activity))) => { + tracing::warn!("skipping unprocessable base activity: {}", serde_json::to_string_pretty(&object).unwrap()); + Err(StatusCode::UNPROCESSABLE_ENTITY) // won't ingest useless stuff + }, + Some(BaseType::Object(ObjectType::Activity(ActivityType::Delete))) => { + // TODO verify the signature before just deleting lmao + let oid = object.object().id().ok_or(StatusCode::BAD_REQUEST)?.to_string(); + // TODO maybe we should keep the tombstone? + model::user::Entity::delete_by_id(&oid).exec(ctx.db()).await.info_failed("failed deleting from users"); + model::activity::Entity::delete_by_id(&oid).exec(ctx.db()).await.info_failed("failed deleting from activities"); + model::object::Entity::delete_by_id(&oid).exec(ctx.db()).await.info_failed("failed deleting from objects"); + Ok(JsonLD(serde_json::Value::Null)) + }, + Some(BaseType::Object(ObjectType::Activity(ActivityType::Follow))) => { + let Ok(activity_entity) = activity::Model::new(&object) else { + tracing::warn!("could not serialize activity: {}", serde_json::to_string_pretty(&object).unwrap()); + return Err(StatusCode::UNPROCESSABLE_ENTITY); + }; + tracing::info!("{} wants to follow {}", activity_entity.actor, activity_entity.object.as_deref().unwrap_or("")); + activity::Entity::insert(activity_entity.into_active_model()) + .exec(ctx.db()) + .await.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?; + Ok(JsonLD(serde_json::Value::Null)) + }, Some(BaseType::Object(ObjectType::Activity(ActivityType::Like))) => { let aid = object.actor().id().ok_or(StatusCode::BAD_REQUEST)?.to_string(); let oid = object.object().id().ok_or(StatusCode::BAD_REQUEST)?.to_string(); @@ -200,22 +224,29 @@ pub async fn inbox( tracing::error!("unexpected error incrementing object {oid} like counter: {e}"); Err(StatusCode::INTERNAL_SERVER_ERROR) }, - Ok(_) => Ok(JsonLD(serde_json::Value::Null)), + Ok(_) => { + tracing::info!("{} liked {}", aid, oid); + Ok(JsonLD(serde_json::Value::Null)) + } } }, } }, Some(BaseType::Object(ObjectType::Activity(ActivityType::Create))) => { let Ok(activity_entity) = activity::Model::new(&object) else { + tracing::warn!("could not serialize activity: {}", serde_json::to_string_pretty(&object).unwrap()); return Err(StatusCode::UNPROCESSABLE_ENTITY); }; let Node::Object(obj) = object.object() else { // TODO we could process non-embedded activities or arrays but im lazy rn + tracing::error!("refusing to process activity without embedded object: {}", serde_json::to_string_pretty(&object).unwrap()); return Err(StatusCode::UNPROCESSABLE_ENTITY); }; let Ok(obj_entity) = object::Model::new(&*obj) else { + tracing::warn!("coult not serialize object: {}", serde_json::to_string_pretty(&object).unwrap()); return Err(StatusCode::UNPROCESSABLE_ENTITY); }; + tracing::info!("processing Create activity by {} for {}", activity_entity.actor, activity_entity.object.as_deref().unwrap_or("")); object::Entity::insert(obj_entity.into_active_model()) .exec(ctx.db()) .await.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?; @@ -224,7 +255,13 @@ pub async fn inbox( .await.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?; Ok(JsonLD(serde_json::Value::Null)) // TODO hmmmmmmmmmmm not the best value to return.... }, - Some(BaseType::Object(ObjectType::Activity(_x))) => { Err(StatusCode::NOT_IMPLEMENTED) }, - Some(_x) => { Err(StatusCode::UNPROCESSABLE_ENTITY) } + Some(BaseType::Object(ObjectType::Activity(_x))) => { + tracing::info!("received unimplemented activity on inbox: {}", serde_json::to_string_pretty(&object).unwrap()); + Err(StatusCode::NOT_IMPLEMENTED) + }, + Some(_x) => { + tracing::warn!("ignoring non-activity object in inbox: {}", serde_json::to_string_pretty(&object).unwrap()); + Err(StatusCode::UNPROCESSABLE_ENTITY) + } } }