fix: i checked the logs, content type ld+json...

was it really just this??? ffs
This commit is contained in:
əlemi 2024-03-21 03:07:35 +01:00
parent cacef6e029
commit bc182bdf70
Signed by: alemi
GPG key ID: A4895B84D311642C
5 changed files with 50 additions and 16 deletions

View file

@ -1,15 +1,17 @@
use axum::{extract::{Path, State}, http::StatusCode, Json}; use axum::{extract::{Path, State}, http::StatusCode};
use sea_orm::EntityTrait; use sea_orm::EntityTrait;
use crate::{activitystream::{prelude::*, Node}, model::{activity, object}, server::Context}; use crate::{activitystream::{prelude::*, Node}, model::{activity, object}, server::Context};
use super::JsonLD;
pub async fn view(State(ctx) : State<Context>, Path(id): Path<String>) -> Result<Json<serde_json::Value>, StatusCode> {
pub async fn view(State(ctx) : State<Context>, Path(id): Path<String>) -> Result<JsonLD<serde_json::Value>, StatusCode> {
match activity::Entity::find_by_id(ctx.aid(id)) match activity::Entity::find_by_id(ctx.aid(id))
.find_also_related(object::Entity) .find_also_related(object::Entity)
.one(ctx.db()) .one(ctx.db())
.await .await
{ {
Ok(Some((activity, object))) => Ok(Json(activity.underlying_json_object().set_object(Node::maybe_object(object)))), Ok(Some((activity, object))) => Ok(JsonLD(activity.underlying_json_object().set_object(Node::maybe_object(object)))),
Ok(None) => Err(StatusCode::NOT_FOUND), Ok(None) => Err(StatusCode::NOT_FOUND),
Err(e) => { Err(e) => {
tracing::error!("error querying for activity: {e}"); tracing::error!("error querying for activity: {e}");

26
src/activitypub/jsonld.rs Normal file
View file

@ -0,0 +1,26 @@
// got this from https://github.com/kitsune-soc/kitsune/blob/b023a12b687dd9a274233a5a9950f2de5e192344/kitsune/src/http/responder.rs
// i was trying to do it with middlewares but this is way cleaner
use axum::{
response::{IntoResponse, Response},
Json,
};
use serde::Serialize;
pub struct JsonLD<T>(pub T);
impl<T> IntoResponse for JsonLD<T>
where
T: Serialize,
{
fn into_response(self) -> Response {
(
[(
"Content-Type",
"application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"",
)],
Json(self.0),
)
.into_response()
}
}

View file

@ -1,6 +1,8 @@
pub mod user; pub mod user;
pub mod object; pub mod object;
pub mod activity; pub mod activity;
pub mod jsonld;
pub use jsonld::JsonLD;
use axum::{extract::State, http::StatusCode, Json}; use axum::{extract::State, http::StatusCode, Json};
use sea_orm::{EntityTrait, IntoActiveModel}; use sea_orm::{EntityTrait, IntoActiveModel};
@ -46,7 +48,7 @@ pub async fn view(State(ctx): State<Context>) -> Result<Json<serde_json::Value>,
)) ))
} }
pub async fn inbox(State(ctx) : State<Context>, Json(object): Json<serde_json::Value>) -> Result<Json<serde_json::Value>, StatusCode> { pub async fn inbox(State(ctx) : State<Context>, Json(object): Json<serde_json::Value>) -> Result<JsonLD<serde_json::Value>, StatusCode> {
match object.base_type() { match object.base_type() {
None => { Err(StatusCode::BAD_REQUEST) }, None => { Err(StatusCode::BAD_REQUEST) },
Some(BaseType::Link(_x)) => Err(StatusCode::UNPROCESSABLE_ENTITY), // we could but not yet Some(BaseType::Link(_x)) => Err(StatusCode::UNPROCESSABLE_ENTITY), // we could but not yet
@ -70,7 +72,7 @@ pub async fn inbox(State(ctx) : State<Context>, Json(object): Json<serde_json::V
model::activity::Entity::insert(activity_entity.into_active_model()) model::activity::Entity::insert(activity_entity.into_active_model())
.exec(ctx.db()) .exec(ctx.db())
.await.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?; .await.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
Ok(Json(serde_json::Value::Null)) // TODO hmmmmmmmmmmm not the best value to return.... 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(BaseType::Object(ObjectType::Activity(_x))) => { Err(StatusCode::NOT_IMPLEMENTED) },
Some(_x) => { Err(StatusCode::UNPROCESSABLE_ENTITY) } Some(_x) => { Err(StatusCode::UNPROCESSABLE_ENTITY) }

View file

@ -1,12 +1,14 @@
use axum::{extract::{Path, State}, http::StatusCode, Json}; use axum::{extract::{Path, State}, http::StatusCode};
use sea_orm::EntityTrait; use sea_orm::EntityTrait;
use crate::{activitystream::Base, model::object, server::Context}; use crate::{activitystream::Base, model::object, server::Context};
use super::JsonLD;
pub async fn view(State(ctx) : State<Context>, Path(id): Path<String>) -> Result<Json<serde_json::Value>, StatusCode> {
pub async fn view(State(ctx) : State<Context>, Path(id): Path<String>) -> Result<JsonLD<serde_json::Value>, StatusCode> {
match object::Entity::find_by_id(ctx.oid(id)).one(ctx.db()).await { match object::Entity::find_by_id(ctx.oid(id)).one(ctx.db()).await {
Ok(Some(object)) => Ok(Json(object.underlying_json_object())), Ok(Some(object)) => Ok(JsonLD(object.underlying_json_object())),
Ok(None) => Err(StatusCode::NOT_FOUND), Ok(None) => Err(StatusCode::NOT_FOUND),
Err(e) => { Err(e) => {
tracing::error!("error querying for object: {e}"); tracing::error!("error querying for object: {e}");

View file

@ -6,13 +6,15 @@ use sea_orm::{ColumnTrait, Condition, DatabaseConnection, EntityTrait, IntoActiv
use crate::{activitystream::{self, object::{activity::ActivityType, collection::CollectionType, ObjectType}, BaseType, Node}, model::{self, activity, object, user}, server::Context, url}; use crate::{activitystream::{self, object::{activity::ActivityType, collection::CollectionType, ObjectType}, BaseType, Node}, model::{self, activity, object, user}, server::Context, url};
pub async fn list(State(_db) : State<Arc<DatabaseConnection>>) -> Result<Json<serde_json::Value>, StatusCode> { use super::JsonLD;
pub async fn list(State(_db) : State<Arc<DatabaseConnection>>) -> Result<JsonLD<serde_json::Value>, StatusCode> {
todo!() todo!()
} }
pub async fn view(State(ctx) : State<Context>, Path(id): Path<String>) -> Result<Json<serde_json::Value>, StatusCode> { pub async fn view(State(ctx) : State<Context>, Path(id): Path<String>) -> Result<JsonLD<serde_json::Value>, StatusCode> {
match user::Entity::find_by_id(ctx.uid(id)).one(ctx.db()).await { match user::Entity::find_by_id(ctx.uid(id)).one(ctx.db()).await {
Ok(Some(user)) => Ok(Json(user.underlying_json_object())), Ok(Some(user)) => Ok(JsonLD(user.underlying_json_object())),
Ok(None) => Err(StatusCode::NOT_FOUND), Ok(None) => Err(StatusCode::NOT_FOUND),
Err(e) => { Err(e) => {
tracing::error!("error querying for user: {e}"); tracing::error!("error querying for user: {e}");
@ -25,7 +27,7 @@ pub async fn outbox(
State(ctx): State<Context>, State(ctx): State<Context>,
Path(id): Path<String>, Path(id): Path<String>,
Query(page): Query<super::Page>, Query(page): Query<super::Page>,
) -> Result<Json<serde_json::Value>, StatusCode> { ) -> Result<JsonLD<serde_json::Value>, StatusCode> {
if let Some(true) = page.page { if let Some(true) = page.page {
// find requested recent post, to filter based on its date (use now() as fallback) // find requested recent post, to filter based on its date (use now() as fallback)
@ -56,7 +58,7 @@ pub async fn outbox(
.into_iter() .into_iter()
.map(|(a, o)| a.underlying_json_object().set_object(Node::maybe_object(o))) .map(|(a, o)| a.underlying_json_object().set_object(Node::maybe_object(o)))
.collect(); .collect();
Ok(Json( Ok(JsonLD(
activitystream::object() activitystream::object()
// TODO set id, calculate uri from given args // TODO set id, calculate uri from given args
.set_collection_type(Some(CollectionType::OrderedCollectionPage)) .set_collection_type(Some(CollectionType::OrderedCollectionPage))
@ -68,7 +70,7 @@ pub async fn outbox(
} }
} else { } else {
Ok(Json( Ok(JsonLD(
crate::activitystream::object() crate::activitystream::object()
.set_id(Some(&url!(ctx, "/users/{id}/outbox"))) .set_id(Some(&url!(ctx, "/users/{id}/outbox")))
.set_collection_type(Some(CollectionType::OrderedCollection)) .set_collection_type(Some(CollectionType::OrderedCollection))
@ -81,7 +83,7 @@ pub async fn inbox(
State(ctx): State<Context>, State(ctx): State<Context>,
Path(_id): Path<String>, Path(_id): Path<String>,
Json(object): Json<serde_json::Value> Json(object): Json<serde_json::Value>
) -> Result<Json<serde_json::Value>, StatusCode> { ) -> Result<JsonLD<serde_json::Value>, StatusCode> {
match object.base_type() { match object.base_type() {
None => { Err(StatusCode::BAD_REQUEST) }, None => { Err(StatusCode::BAD_REQUEST) },
Some(BaseType::Link(_x)) => Err(StatusCode::UNPROCESSABLE_ENTITY), // we could but not yet Some(BaseType::Link(_x)) => Err(StatusCode::UNPROCESSABLE_ENTITY), // we could but not yet
@ -105,7 +107,7 @@ pub async fn inbox(
activity::Entity::insert(activity_entity.into_active_model()) activity::Entity::insert(activity_entity.into_active_model())
.exec(ctx.db()) .exec(ctx.db())
.await.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?; .await.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
Ok(Json(serde_json::Value::Null)) // TODO hmmmmmmmmmmm not the best value to return.... 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(BaseType::Object(ObjectType::Activity(_x))) => { Err(StatusCode::NOT_IMPLEMENTED) },
Some(_x) => { Err(StatusCode::UNPROCESSABLE_ENTITY) } Some(_x) => { Err(StatusCode::UNPROCESSABLE_ENTITY) }