forked from alemi/upub
fix: improvements for debugging
This commit is contained in:
parent
b464aa0e98
commit
36aec4f447
1 changed files with 55 additions and 33 deletions
|
@ -1,7 +1,7 @@
|
||||||
use axum::{extract::{Path, Query, State}, http::StatusCode, response::IntoResponse, Json};
|
use axum::{extract::{Path, Query, State}, http::StatusCode, response::IntoResponse, Json};
|
||||||
use sea_orm::{ColumnTrait, Condition, EntityTrait, IntoActiveModel, Order, QueryFilter, QueryOrder, QuerySelect, SelectColumns, Set};
|
use sea_orm::{ColumnTrait, Condition, EntityTrait, IntoActiveModel, Order, QueryFilter, QueryOrder, QuerySelect, SelectColumns, Set};
|
||||||
|
|
||||||
use crate::{activitypub::{jsonld::LD, JsonLD, Pagination, PUBLIC_TARGET}, activitystream::{object::{activity::{Activity, ActivityMut, ActivityType}, collection::{page::CollectionPageMut, CollectionMut, CollectionType}, Addressed}, Base, BaseMut, BaseType, Node, ObjectType}, auth::{AuthIdentity, Identity}, model::{self, activity, object}, server::Context, url};
|
use crate::{activitypub::{jsonld::LD, JsonLD, Pagination, PUBLIC_TARGET}, activitystream::{object::{activity::{Activity, ActivityMut, ActivityType}, collection::{page::CollectionPageMut, CollectionMut, CollectionType}, Addressed}, Base, BaseMut, BaseType, Node, ObjectType}, auth::{AuthIdentity, Identity}, model::{self, activity, object, FieldError}, server::Context, url};
|
||||||
|
|
||||||
pub async fn get(
|
pub async fn get(
|
||||||
State(ctx): State<Context>,
|
State(ctx): State<Context>,
|
||||||
|
@ -16,6 +16,41 @@ pub async fn get(
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, thiserror::Error)]
|
||||||
|
pub enum UpubError {
|
||||||
|
#[error("database error: {0}")]
|
||||||
|
Database(#[from] sea_orm::DbErr),
|
||||||
|
|
||||||
|
#[error("api returned {0}")]
|
||||||
|
Status(StatusCode),
|
||||||
|
|
||||||
|
#[error("missing field: {0}")]
|
||||||
|
Field(#[from] FieldError),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<StatusCode> for UpubError {
|
||||||
|
fn from(value: StatusCode) -> Self {
|
||||||
|
UpubError::Status(value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl IntoResponse for UpubError {
|
||||||
|
fn into_response(self) -> axum::response::Response {
|
||||||
|
(StatusCode::INTERNAL_SERVER_ERROR, self.to_string()).into_response()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct CreationResult(pub String);
|
||||||
|
impl IntoResponse for CreationResult {
|
||||||
|
fn into_response(self) -> axum::response::Response {
|
||||||
|
(
|
||||||
|
StatusCode::CREATED,
|
||||||
|
[("Location", self.0.as_str())]
|
||||||
|
)
|
||||||
|
.into_response()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn page(
|
pub async fn page(
|
||||||
State(ctx): State<Context>,
|
State(ctx): State<Context>,
|
||||||
Path(id): Path<String>,
|
Path(id): Path<String>,
|
||||||
|
@ -72,19 +107,19 @@ pub async fn post(
|
||||||
Path(id): Path<String>,
|
Path(id): Path<String>,
|
||||||
AuthIdentity(auth): AuthIdentity,
|
AuthIdentity(auth): AuthIdentity,
|
||||||
Json(activity): Json<serde_json::Value>,
|
Json(activity): Json<serde_json::Value>,
|
||||||
) -> Result<impl IntoResponse, StatusCode> {
|
) -> Result<CreationResult, UpubError> {
|
||||||
match auth {
|
match auth {
|
||||||
Identity::Anonymous => Err(StatusCode::UNAUTHORIZED),
|
Identity::Anonymous => Err(StatusCode::UNAUTHORIZED.into()),
|
||||||
Identity::Remote(_) => Err(StatusCode::NOT_IMPLEMENTED),
|
Identity::Remote(_) => Err(StatusCode::NOT_IMPLEMENTED.into()),
|
||||||
Identity::Local(uid) => if ctx.uid(id.clone()) == uid {
|
Identity::Local(uid) => if ctx.uid(id.clone()) == uid {
|
||||||
match activity.base_type() {
|
match activity.base_type() {
|
||||||
None => Err(StatusCode::BAD_REQUEST),
|
None => Err(StatusCode::BAD_REQUEST.into()),
|
||||||
Some(BaseType::Link(_)) => Err(StatusCode::UNPROCESSABLE_ENTITY),
|
Some(BaseType::Link(_)) => Err(StatusCode::UNPROCESSABLE_ENTITY.into()),
|
||||||
// Some(BaseType::Object(ObjectType::Note)) => {
|
// Some(BaseType::Object(ObjectType::Note)) => {
|
||||||
// },
|
// },
|
||||||
Some(BaseType::Object(ObjectType::Activity(ActivityType::Create))) => {
|
Some(BaseType::Object(ObjectType::Activity(ActivityType::Create))) => {
|
||||||
let Some(object) = activity.object().get().map(|x| x.underlying_json_object()) else {
|
let Some(object) = activity.object().get().map(|x| x.underlying_json_object()) else {
|
||||||
return Err(StatusCode::BAD_REQUEST);
|
return Err(StatusCode::BAD_REQUEST.into());
|
||||||
};
|
};
|
||||||
let oid = uuid::Uuid::new_v4().to_string();
|
let oid = uuid::Uuid::new_v4().to_string();
|
||||||
let aid = uuid::Uuid::new_v4().to_string();
|
let aid = uuid::Uuid::new_v4().to_string();
|
||||||
|
@ -103,14 +138,10 @@ pub async fn post(
|
||||||
activity_model.object = Some(oid.clone());
|
activity_model.object = Some(oid.clone());
|
||||||
|
|
||||||
model::object::Entity::insert(object_model.into_active_model())
|
model::object::Entity::insert(object_model.into_active_model())
|
||||||
.exec(ctx.db())
|
.exec(ctx.db()).await?;
|
||||||
.await
|
|
||||||
.map_err(|_e| StatusCode::INTERNAL_SERVER_ERROR)?;
|
|
||||||
|
|
||||||
model::activity::Entity::insert(activity_model.into_active_model())
|
model::activity::Entity::insert(activity_model.into_active_model())
|
||||||
.exec(ctx.db())
|
.exec(ctx.db()).await?;
|
||||||
.await
|
|
||||||
.map_err(|_e| StatusCode::INTERNAL_SERVER_ERROR)?;
|
|
||||||
|
|
||||||
let mut addressed = activity.addressed();
|
let mut addressed = activity.addressed();
|
||||||
let followers = url!(ctx, "/users/{id}/followers"); // TODO maybe can be done better?
|
let followers = url!(ctx, "/users/{id}/followers"); // TODO maybe can be done better?
|
||||||
|
@ -121,8 +152,7 @@ pub async fn post(
|
||||||
.select_column(model::relation::Column::Follower)
|
.select_column(model::relation::Column::Follower)
|
||||||
.into_tuple::<String>()
|
.into_tuple::<String>()
|
||||||
.all(ctx.db())
|
.all(ctx.db())
|
||||||
.await
|
.await?
|
||||||
.map_err(|_e| StatusCode::INTERNAL_SERVER_ERROR)?
|
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.for_each(|x| addressed.push(x));
|
.for_each(|x| addressed.push(x));
|
||||||
}
|
}
|
||||||
|
@ -139,9 +169,7 @@ pub async fn post(
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
model::addressing::Entity::insert_many(addressings)
|
model::addressing::Entity::insert_many(addressings)
|
||||||
.exec(ctx.db())
|
.exec(ctx.db()).await?;
|
||||||
.await
|
|
||||||
.map_err(|_e| StatusCode::INTERNAL_SERVER_ERROR)?;
|
|
||||||
|
|
||||||
let deliveries : Vec<model::delivery::ActiveModel> = addressed
|
let deliveries : Vec<model::delivery::ActiveModel> = addressed
|
||||||
.iter()
|
.iter()
|
||||||
|
@ -162,10 +190,9 @@ pub async fn post(
|
||||||
|
|
||||||
model::delivery::Entity::insert_many(deliveries)
|
model::delivery::Entity::insert_many(deliveries)
|
||||||
.exec(ctx.db())
|
.exec(ctx.db())
|
||||||
.await
|
.await?;
|
||||||
.map_err(|_e| StatusCode::INTERNAL_SERVER_ERROR)?;
|
|
||||||
|
|
||||||
Ok(StatusCode::CREATED)
|
Ok(CreationResult(aid))
|
||||||
},
|
},
|
||||||
Some(BaseType::Object(ObjectType::Activity(ActivityType::Like))) => {
|
Some(BaseType::Object(ObjectType::Activity(ActivityType::Like))) => {
|
||||||
let aid = uuid::Uuid::new_v4().to_string();
|
let aid = uuid::Uuid::new_v4().to_string();
|
||||||
|
@ -175,9 +202,7 @@ pub async fn post(
|
||||||
activity_model.actor = uid.clone();
|
activity_model.actor = uid.clone();
|
||||||
|
|
||||||
model::activity::Entity::insert(activity_model.into_active_model())
|
model::activity::Entity::insert(activity_model.into_active_model())
|
||||||
.exec(ctx.db())
|
.exec(ctx.db()).await?;
|
||||||
.await
|
|
||||||
.map_err(|_e| StatusCode::INTERNAL_SERVER_ERROR)?;
|
|
||||||
|
|
||||||
let mut addressed = activity.addressed();
|
let mut addressed = activity.addressed();
|
||||||
let followers = url!(ctx, "/users/{id}/followers"); // TODO maybe can be done better?
|
let followers = url!(ctx, "/users/{id}/followers"); // TODO maybe can be done better?
|
||||||
|
@ -188,8 +213,7 @@ pub async fn post(
|
||||||
.select_column(model::relation::Column::Follower)
|
.select_column(model::relation::Column::Follower)
|
||||||
.into_tuple::<String>()
|
.into_tuple::<String>()
|
||||||
.all(ctx.db())
|
.all(ctx.db())
|
||||||
.await
|
.await?
|
||||||
.map_err(|_e| StatusCode::INTERNAL_SERVER_ERROR)?
|
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.for_each(|x| addressed.push(x));
|
.for_each(|x| addressed.push(x));
|
||||||
}
|
}
|
||||||
|
@ -207,8 +231,7 @@ pub async fn post(
|
||||||
|
|
||||||
model::addressing::Entity::insert_many(addressings)
|
model::addressing::Entity::insert_many(addressings)
|
||||||
.exec(ctx.db())
|
.exec(ctx.db())
|
||||||
.await
|
.await?;
|
||||||
.map_err(|_e| StatusCode::INTERNAL_SERVER_ERROR)?;
|
|
||||||
|
|
||||||
let deliveries : Vec<model::delivery::ActiveModel> = addressed
|
let deliveries : Vec<model::delivery::ActiveModel> = addressed
|
||||||
.iter()
|
.iter()
|
||||||
|
@ -229,10 +252,9 @@ pub async fn post(
|
||||||
|
|
||||||
model::delivery::Entity::insert_many(deliveries)
|
model::delivery::Entity::insert_many(deliveries)
|
||||||
.exec(ctx.db())
|
.exec(ctx.db())
|
||||||
.await
|
.await?;
|
||||||
.map_err(|_e| StatusCode::INTERNAL_SERVER_ERROR)?;
|
|
||||||
|
|
||||||
Ok(StatusCode::CREATED)
|
Ok(CreationResult(aid))
|
||||||
},
|
},
|
||||||
// Some(BaseType::Object(ObjectType::Activity(ActivityType::Follow))) => {
|
// Some(BaseType::Object(ObjectType::Activity(ActivityType::Follow))) => {
|
||||||
// },
|
// },
|
||||||
|
@ -242,10 +264,10 @@ pub async fn post(
|
||||||
// },
|
// },
|
||||||
// Some(BaseType::Object(ObjectType::Activity(ActivityType::Reject(RejectType::Reject)))) => {
|
// Some(BaseType::Object(ObjectType::Activity(ActivityType::Reject(RejectType::Reject)))) => {
|
||||||
// },
|
// },
|
||||||
Some(_) => Err(StatusCode::NOT_IMPLEMENTED),
|
Some(_) => Err(StatusCode::NOT_IMPLEMENTED.into()),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Err(StatusCode::FORBIDDEN)
|
Err(StatusCode::FORBIDDEN.into())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue