2024-03-20 05:44:50 +01:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2024-03-20 07:55:24 +01:00
|
|
|
use axum::{extract::{Path, Query, State}, http::StatusCode, Json};
|
2024-03-22 02:50:38 +01:00
|
|
|
use sea_orm::{ColumnTrait, Condition, DatabaseConnection, EntityTrait, IntoActiveModel, Order, PaginatorTrait, QueryFilter, QueryOrder, QuerySelect, SelectColumns};
|
2024-03-20 05:44:50 +01:00
|
|
|
|
2024-03-21 19:15:19 +01:00
|
|
|
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};
|
2024-03-20 05:44:50 +01:00
|
|
|
|
2024-03-21 19:15:19 +01:00
|
|
|
use super::{jsonld::LD, JsonLD};
|
2024-03-21 03:07:35 +01:00
|
|
|
|
|
|
|
pub async fn list(State(_db) : State<Arc<DatabaseConnection>>) -> Result<JsonLD<serde_json::Value>, StatusCode> {
|
2024-03-20 05:44:50 +01:00
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
|
2024-03-21 03:07:35 +01:00
|
|
|
pub async fn view(State(ctx) : State<Context>, Path(id): Path<String>) -> Result<JsonLD<serde_json::Value>, StatusCode> {
|
2024-03-20 09:42:25 +01:00
|
|
|
match user::Entity::find_by_id(ctx.uid(id)).one(ctx.db()).await {
|
2024-03-21 19:15:19 +01:00
|
|
|
Ok(Some(user)) => Ok(JsonLD(user.underlying_json_object().ld_context())),
|
2024-03-20 05:44:50 +01:00
|
|
|
Ok(None) => Err(StatusCode::NOT_FOUND),
|
|
|
|
Err(e) => {
|
|
|
|
tracing::error!("error querying for user: {e}");
|
|
|
|
Err(StatusCode::INTERNAL_SERVER_ERROR)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-22 01:52:02 +01:00
|
|
|
pub async fn followers(
|
|
|
|
State(ctx): State<Context>,
|
|
|
|
Path(id): Path<String>,
|
2024-03-22 02:50:38 +01:00
|
|
|
Query(page): Query<super::Pagination>,
|
2024-03-22 01:52:02 +01:00
|
|
|
) -> Result<JsonLD<serde_json::Value>, StatusCode> {
|
2024-03-22 02:50:38 +01:00
|
|
|
let limit = page.batch.unwrap_or(20).min(50);
|
|
|
|
let offset = page.offset.unwrap_or(0);
|
|
|
|
if let Some(true) = page.page {
|
|
|
|
match model::relation::Entity::find()
|
|
|
|
.filter(Condition::all().add(model::relation::Column::Following.eq(id.clone())))
|
|
|
|
.select_column(model::relation::Column::Follower)
|
|
|
|
.limit(limit) // TODO allow customizing, with boundaries
|
|
|
|
.offset(page.offset.unwrap_or(0))
|
|
|
|
.all(ctx.db()).await
|
|
|
|
{
|
|
|
|
Err(e) => {
|
|
|
|
tracing::error!("error queriying who {id} is following: {e}");
|
|
|
|
Err(StatusCode::INTERNAL_SERVER_ERROR)
|
|
|
|
},
|
|
|
|
Ok(following) => {
|
|
|
|
Ok(JsonLD(
|
|
|
|
serde_json::Value::new_object()
|
|
|
|
.set_collection_type(Some(CollectionType::OrderedCollectionPage))
|
|
|
|
.set_part_of(Node::link(url!(ctx, "/users/{id}/followers")))
|
|
|
|
.set_next(Node::link(url!(ctx, "/users/{id}/followers?page=true&offset={}", offset+limit)))
|
|
|
|
.set_ordered_items(Node::array(following.into_iter().map(|x| x.follower).collect()))
|
|
|
|
.ld_context()
|
|
|
|
))
|
|
|
|
},
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let count = model::relation::Entity::find()
|
|
|
|
.filter(Condition::all().add(model::relation::Column::Following.eq(id.clone())))
|
|
|
|
.count(ctx.db()).await.unwrap_or_else(|e| {
|
|
|
|
tracing::error!("failed counting followers for {id}: {e}");
|
|
|
|
0
|
|
|
|
});
|
|
|
|
Ok(JsonLD(
|
|
|
|
serde_json::Value::new_object()
|
2024-03-22 05:33:39 +01:00
|
|
|
.set_id(Some(&format!("{}/users/{}/followers", ctx.base(), id)))
|
2024-03-22 02:50:38 +01:00
|
|
|
.set_collection_type(Some(CollectionType::OrderedCollection))
|
|
|
|
.set_total_items(Some(count))
|
2024-03-22 05:33:39 +01:00
|
|
|
.set_first(Node::link(format!("{}/users/{}/followers?page=true", ctx.base(), id)))
|
2024-03-22 02:50:38 +01:00
|
|
|
.ld_context()
|
|
|
|
))
|
|
|
|
}
|
2024-03-22 01:52:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn following(
|
|
|
|
State(ctx): State<Context>,
|
|
|
|
Path(id): Path<String>,
|
2024-03-22 02:50:38 +01:00
|
|
|
Query(page): Query<super::Pagination>,
|
2024-03-22 01:52:02 +01:00
|
|
|
) -> Result<JsonLD<serde_json::Value>, StatusCode> {
|
2024-03-22 02:50:38 +01:00
|
|
|
let limit = page.batch.unwrap_or(20).min(50);
|
|
|
|
let offset = page.offset.unwrap_or(0);
|
|
|
|
if let Some(true) = page.page {
|
|
|
|
match model::relation::Entity::find()
|
|
|
|
.filter(Condition::all().add(model::relation::Column::Follower.eq(id.clone())))
|
|
|
|
.select_column(model::relation::Column::Following)
|
|
|
|
.limit(limit) // TODO allow customizing, with boundaries
|
|
|
|
.offset(page.offset.unwrap_or(0))
|
|
|
|
.all(ctx.db()).await
|
|
|
|
{
|
|
|
|
Err(e) => {
|
|
|
|
tracing::error!("error queriying who {id} is following: {e}");
|
|
|
|
Err(StatusCode::INTERNAL_SERVER_ERROR)
|
|
|
|
},
|
|
|
|
Ok(following) => {
|
|
|
|
Ok(JsonLD(
|
|
|
|
serde_json::Value::new_object()
|
|
|
|
.set_collection_type(Some(CollectionType::OrderedCollectionPage))
|
|
|
|
.set_part_of(Node::link(url!(ctx, "/users/{id}/following")))
|
|
|
|
.set_next(Node::link(url!(ctx, "/users/{id}/following?page=true&offset={}", offset+limit)))
|
|
|
|
.set_ordered_items(Node::array(following.into_iter().map(|x| x.following).collect()))
|
|
|
|
.ld_context()
|
|
|
|
))
|
|
|
|
},
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let count = model::relation::Entity::find()
|
|
|
|
.filter(Condition::all().add(model::relation::Column::Follower.eq(id.clone())))
|
|
|
|
.count(ctx.db()).await.unwrap_or_else(|e| {
|
|
|
|
tracing::error!("failed counting following for {id}: {e}");
|
|
|
|
0
|
|
|
|
});
|
|
|
|
Ok(JsonLD(
|
|
|
|
serde_json::Value::new_object()
|
|
|
|
.set_id(Some(&format!("{}/users/{}/following", ctx.base(), id)))
|
|
|
|
.set_collection_type(Some(CollectionType::OrderedCollection))
|
|
|
|
.set_total_items(Some(count))
|
|
|
|
.set_first(Node::link(format!("{}/users/{}/following?page=true", ctx.base(), id)))
|
|
|
|
.ld_context()
|
|
|
|
))
|
|
|
|
}
|
2024-03-22 01:52:02 +01:00
|
|
|
}
|
|
|
|
|
2024-03-20 07:55:24 +01:00
|
|
|
pub async fn outbox(
|
2024-03-20 08:56:35 +01:00
|
|
|
State(ctx): State<Context>,
|
2024-03-20 07:55:24 +01:00
|
|
|
Path(id): Path<String>,
|
2024-03-22 02:50:38 +01:00
|
|
|
Query(page): Query<super::Pagination>,
|
2024-03-21 03:07:35 +01:00
|
|
|
) -> Result<JsonLD<serde_json::Value>, StatusCode> {
|
2024-03-22 02:50:38 +01:00
|
|
|
let limit = page.batch.unwrap_or(20).min(50);
|
|
|
|
let offset = page.offset.unwrap_or(0);
|
2024-03-20 07:55:24 +01:00
|
|
|
if let Some(true) = page.page {
|
|
|
|
match activity::Entity::find()
|
2024-03-21 01:24:55 +01:00
|
|
|
.find_also_related(object::Entity)
|
2024-03-20 07:55:24 +01:00
|
|
|
.order_by(activity::Column::Published, Order::Desc)
|
2024-03-22 02:50:38 +01:00
|
|
|
.limit(limit)
|
|
|
|
.offset(offset)
|
2024-03-20 08:56:35 +01:00
|
|
|
.all(ctx.db()).await
|
2024-03-20 07:55:24 +01:00
|
|
|
{
|
|
|
|
Err(_e) => Err(StatusCode::INTERNAL_SERVER_ERROR),
|
|
|
|
Ok(items) => {
|
2024-03-21 01:11:08 +01:00
|
|
|
let next = ctx.id(items.last().map(|(a, _o)| a.id.as_str()).unwrap_or("").to_string());
|
2024-03-20 07:55:24 +01:00
|
|
|
let items = items
|
|
|
|
.into_iter()
|
2024-03-21 01:11:08 +01:00
|
|
|
.map(|(a, o)| a.underlying_json_object().set_object(Node::maybe_object(o)))
|
2024-03-20 07:55:24 +01:00
|
|
|
.collect();
|
2024-03-21 03:07:35 +01:00
|
|
|
Ok(JsonLD(
|
2024-03-21 19:15:19 +01:00
|
|
|
serde_json::Value::new_object()
|
2024-03-21 00:04:01 +01:00
|
|
|
// TODO set id, calculate uri from given args
|
|
|
|
.set_collection_type(Some(CollectionType::OrderedCollectionPage))
|
|
|
|
.set_part_of(Node::link(url!(ctx, "/users/{id}/outbox")))
|
|
|
|
.set_next(Node::link(url!(ctx, "/users/{id}/outbox?page=true&max_id={next}")))
|
|
|
|
.set_ordered_items(Node::array(items))
|
2024-03-21 19:32:07 +01:00
|
|
|
.ld_context()
|
2024-03-21 00:04:01 +01:00
|
|
|
))
|
2024-03-20 07:55:24 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2024-03-21 03:07:35 +01:00
|
|
|
Ok(JsonLD(
|
2024-03-21 19:15:19 +01:00
|
|
|
serde_json::Value::new_object()
|
2024-03-21 00:04:01 +01:00
|
|
|
.set_id(Some(&url!(ctx, "/users/{id}/outbox")))
|
|
|
|
.set_collection_type(Some(CollectionType::OrderedCollection))
|
|
|
|
.set_first(Node::link(url!(ctx, "/users/{id}/outbox?page=true")))
|
2024-03-21 19:32:07 +01:00
|
|
|
.ld_context()
|
2024-03-21 00:04:01 +01:00
|
|
|
))
|
2024-03-20 05:44:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn inbox(
|
2024-03-20 08:56:35 +01:00
|
|
|
State(ctx): State<Context>,
|
2024-03-20 05:44:50 +01:00
|
|
|
Path(_id): Path<String>,
|
|
|
|
Json(object): Json<serde_json::Value>
|
2024-03-21 03:07:35 +01:00
|
|
|
) -> Result<JsonLD<serde_json::Value>, StatusCode> {
|
2024-03-22 01:41:42 +01:00
|
|
|
tracing::info!("received object on inbox: {}", serde_json::to_string_pretty(&object).unwrap());
|
2024-03-20 05:44:50 +01:00
|
|
|
match object.base_type() {
|
|
|
|
None => { Err(StatusCode::BAD_REQUEST) },
|
|
|
|
Some(BaseType::Link(_x)) => Err(StatusCode::UNPROCESSABLE_ENTITY), // we could but not yet
|
2024-03-22 02:50:38 +01:00
|
|
|
Some(BaseType::Object(ObjectType::Activity(ActivityType::Activity))) => Err(StatusCode::UNPROCESSABLE_ENTITY), // won't ingest useless stuff
|
2024-03-20 05:44:50 +01:00
|
|
|
Some(BaseType::Object(ObjectType::Activity(ActivityType::Follow))) => { todo!() },
|
|
|
|
Some(BaseType::Object(ObjectType::Activity(ActivityType::Like))) => { todo!() },
|
|
|
|
Some(BaseType::Object(ObjectType::Activity(ActivityType::Create))) => {
|
|
|
|
let Ok(activity_entity) = activity::Model::new(&object) else {
|
|
|
|
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
|
|
|
|
return Err(StatusCode::UNPROCESSABLE_ENTITY);
|
|
|
|
};
|
|
|
|
let Ok(obj_entity) = object::Model::new(&*obj) else {
|
|
|
|
return Err(StatusCode::UNPROCESSABLE_ENTITY);
|
|
|
|
};
|
|
|
|
object::Entity::insert(obj_entity.into_active_model())
|
2024-03-20 08:56:35 +01:00
|
|
|
.exec(ctx.db())
|
2024-03-20 05:44:50 +01:00
|
|
|
.await.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
|
|
|
activity::Entity::insert(activity_entity.into_active_model())
|
2024-03-20 08:56:35 +01:00
|
|
|
.exec(ctx.db())
|
2024-03-20 05:44:50 +01:00
|
|
|
.await.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
2024-03-21 03:07:35 +01:00
|
|
|
Ok(JsonLD(serde_json::Value::Null)) // TODO hmmmmmmmmmmm not the best value to return....
|
2024-03-20 05:44:50 +01:00
|
|
|
},
|
|
|
|
Some(BaseType::Object(ObjectType::Activity(_x))) => { Err(StatusCode::NOT_IMPLEMENTED) },
|
|
|
|
Some(_x) => { Err(StatusCode::UNPROCESSABLE_ENTITY) }
|
|
|
|
}
|
|
|
|
}
|