From b2079a2c91a7dcf2887a48671ee55427ea1e148d Mon Sep 17 00:00:00 2001 From: alemi Date: Wed, 20 Mar 2024 07:55:24 +0100 Subject: [PATCH] feat: simple outbox without objects links dont require me to do relations now, but are likely needed --- src/activitypub/mod.rs | 6 ++++ src/activitypub/user.rs | 68 +++++++++++++++++++++++++++++++++-------- 2 files changed, 62 insertions(+), 12 deletions(-) diff --git a/src/activitypub/mod.rs b/src/activitypub/mod.rs index e9f96303..e51fba01 100644 --- a/src/activitypub/mod.rs +++ b/src/activitypub/mod.rs @@ -17,6 +17,12 @@ pub fn uri_id(entity: &str, id: String) -> String { pub fn id_uri(id: &str) -> &str { id.split('/').last().unwrap_or("") } + +#[derive(Debug, serde::Deserialize)] +// TODO i don't really like how pleroma/mastodon do it actually, maybe change this? +pub struct Page { + pub page: Option, + pub max_id: Option, } pub async fn inbox(State(db) : State>, Json(object): Json) -> Result, StatusCode> { diff --git a/src/activitypub/user.rs b/src/activitypub/user.rs index baea0619..17293640 100644 --- a/src/activitypub/user.rs +++ b/src/activitypub/user.rs @@ -1,16 +1,16 @@ use std::sync::Arc; -use axum::{extract::{Path, State}, http::StatusCode, Json}; -use sea_orm::{ColumnTrait, Condition, DatabaseConnection, EntityTrait, IntoActiveModel, QueryFilter}; +use axum::{extract::{Path, Query, State}, http::StatusCode, Json}; +use sea_orm::{ColumnTrait, Condition, DatabaseConnection, EntityTrait, IntoActiveModel, Order, QueryFilter, QueryOrder, QuerySelect}; -use crate::{activitystream::{object::{activity::{Activity, ActivityType}, ObjectType}, Base, BaseType, Node}, model::{activity, object, user}}; +use crate::{activitystream::{self, object::{activity::{Activity, ActivityType}, collection::{page::CollectionPageMut, CollectionMut, CollectionType}, ObjectType}, Base, BaseMut, BaseType, Node}, model::{self, activity, object, user}}; pub async fn list(State(_db) : State>) -> Result, StatusCode> { todo!() } pub async fn view(State(db) : State>, Path(id): Path) -> Result, StatusCode> { - match user::Entity::find_by_id(super::uri_id(id)).one(&*db).await { + match user::Entity::find_by_id(super::uri_id("users", id)).one(&*db).await { Ok(Some(user)) => Ok(Json(user.underlying_json_object())), Ok(None) => Err(StatusCode::NOT_FOUND), Err(e) => { @@ -20,14 +20,58 @@ pub async fn view(State(db) : State>, Path(id): Path>, Path(id): Path) -> Result, StatusCode> { - let uri = super::uri_id(id); - match activity::Entity::find() - .filter(Condition::all().add(activity::Column::Actor.eq(uri))) - .all(&*db).await - { - Ok(_x) => todo!(), - Err(_e) => todo!(), +pub async fn outbox( + State(db): State>, + Path(id): Path, + Query(page): Query, +) -> Result, StatusCode> { + if let Some(true) = page.page { + + // find requested recent post, to filter based on its date (use now() as fallback) + let before = if let Some(before) = page.max_id { + match model::activity::Entity::find_by_id(super::uri_id("activities", before)) + .one(&*db).await + { + Ok(None) => return Err(StatusCode::NOT_FOUND), + Ok(Some(x)) => x.published, + Err(e) => { + tracing::error!("could not fetch activity from db: {e}"); + chrono::Utc::now() + }, + } + } else { chrono::Utc::now() }; + + match activity::Entity::find() + .filter(Condition::all().add(activity::Column::Published.lt(before))) + .order_by(activity::Column::Published, Order::Desc) + .limit(20) // TODO allow customizing, with boundaries + .all(&*db).await + { + Err(_e) => Err(StatusCode::INTERNAL_SERVER_ERROR), + Ok(items) => { + let next = super::id_uri(&items.last().unwrap().id).to_string(); + let items = items + .into_iter() + .map(|i| i.underlying_json_object()) + .collect(); + let mut obj = activitystream::object(); + obj + // TODO set id, calculate uri from given args + .set_collection_type(Some(CollectionType::OrderedCollectionPage)) + .set_part_of(Node::link(&format!("http://localhost:3000/users/{id}/outbox"))) + .set_next(Node::link(&format!("http://localhost:3000/users/{id}/outbox?page=true&max_id={next}"))) + .set_ordered_items(Node::array(items)); + Ok(Json(obj)) + }, + } + + } else { + let mut obj = crate::activitystream::object(); + obj + .set_id(Some(&format!("http://localhost:3000/users/{id}/outbox"))) + .set_collection_type(Some(CollectionType::OrderedCollection)) + .set_first(Node::link(&format!("http://localhost:3000/users/{id}/outbox?page=true"))); + Ok(Json(obj.underlying_json_object())) } }