1
0
Fork 0
forked from alemi/upub

chore: refactor collections with utils, moved stuff

This commit is contained in:
əlemi 2024-03-28 04:52:17 +01:00
parent 68823559c1
commit e68332bc31
Signed by: alemi
GPG key ID: A4895B84D311642C
8 changed files with 118 additions and 115 deletions

View file

@ -1,48 +1,46 @@
use axum::{extract::{Query, State}, http::StatusCode}; use axum::{extract::{Query, State}, http::StatusCode};
use sea_orm::{ColumnTrait, Condition, EntityTrait, QueryFilter, QueryOrder, QuerySelect}; use sea_orm::{ColumnTrait, Condition, EntityTrait, Order, QueryFilter, QueryOrder, QuerySelect};
use crate::{activitystream::{object::collection::{page::CollectionPageMut, CollectionMut, CollectionType}, BaseMut, Node}, model, server::Context, url}; use crate::{activitystream::Node, auth::{AuthIdentity, Identity}, errors::UpubError, model, server::Context, url};
use super::{activity::ap_activity, jsonld::LD, JsonLD, Pagination, PUBLIC_TARGET}; use super::{activity::ap_activity, jsonld::LD, JsonLD, Pagination, PUBLIC_TARGET};
pub async fn get(State(ctx) : State<Context>, Query(page): Query<Pagination>) -> Result<JsonLD<serde_json::Value>, StatusCode> { pub async fn get(
let limit = page.batch.unwrap_or(20).min(100); State(ctx): State<Context>,
let offset = page.offset.unwrap_or(0); ) -> Result<JsonLD<serde_json::Value>, StatusCode> {
if let Some(true) = page.page { Ok(JsonLD(ctx.ap_collection(&url!(ctx, "/inbox"), None).ld_context()))
match model::addressing::Entity::find()
.filter(Condition::all().add(model::addressing::Column::Actor.eq(PUBLIC_TARGET)))
.order_by(model::addressing::Column::Published, sea_orm::Order::Desc)
.find_also_related(model::activity::Entity) // TODO join also with objects
.limit(limit)
.offset(offset)
.all(ctx.db())
.await
{
Ok(x) => Ok(JsonLD(serde_json::Value::new_object()
.set_id(Some(&url!(ctx, "/inbox")))
.set_collection_type(Some(CollectionType::OrderedCollection))
.set_part_of(Node::link(url!(ctx, "/inbox")))
.set_next(Node::link(url!(ctx, "/inbox?page=true&offset={}", offset+limit)))
.set_ordered_items(Node::array(
x.into_iter()
.filter_map(|(_, a)| Some(ap_activity(a?)))
.collect()
))
.ld_context()
)),
Err(e) => {
tracing::error!("failed paginating global outbox: {e}");
Err(StatusCode::INTERNAL_SERVER_ERROR)
},
}
} else {
Ok(JsonLD(serde_json::Value::new_object()
.set_id(Some(&url!(ctx, "/inbox")))
.set_collection_type(Some(CollectionType::OrderedCollection))
.set_first(Node::link(url!(ctx, "/inbox?page=true")))
.ld_context()
))
}
} }
pub async fn page(
State(ctx): State<Context>,
AuthIdentity(auth): AuthIdentity,
Query(page): Query<Pagination>,
) -> Result<JsonLD<serde_json::Value>, UpubError> {
let limit = page.batch.unwrap_or(20).min(50);
let offset = page.offset.unwrap_or(0);
let mut condition = Condition::any()
.add(model::addressing::Column::Actor.eq(PUBLIC_TARGET));
if let Identity::Local(user) = auth {
condition = condition
.add(model::addressing::Column::Actor.eq(user));
}
let activities = model::addressing::Entity::find()
.filter(condition)
.order_by(model::addressing::Column::Published, Order::Asc)
.find_also_related(model::activity::Entity)
.limit(limit)
.offset(offset)
.all(ctx.db())
.await?;
Ok(JsonLD(
ctx.ap_collection_page(
&url!(ctx, "/inbox/page"),
offset, limit,
activities
.into_iter()
.filter_map(|(_, a)| Some(Node::object(ap_activity(a?))))
.collect::<Vec<Node<serde_json::Value>>>()
).ld_context()
))
}

View file

@ -8,7 +8,7 @@ pub mod well_known;
pub mod jsonld; pub mod jsonld;
pub use jsonld::JsonLD; pub use jsonld::JsonLD;
use axum::{extract::State, http::StatusCode, Json}; use axum::{extract::State, http::StatusCode, response::IntoResponse, Json};
use rand::Rng; use rand::Rng;
use sea_orm::{ColumnTrait, Condition, EntityTrait, QueryFilter}; use sea_orm::{ColumnTrait, Condition, EntityTrait, QueryFilter};
@ -39,11 +39,21 @@ pub fn domain(domain: &str) -> String {
#[derive(Debug, serde::Deserialize)] #[derive(Debug, serde::Deserialize)]
// TODO i don't really like how pleroma/mastodon do it actually, maybe change this? // TODO i don't really like how pleroma/mastodon do it actually, maybe change this?
pub struct Pagination { pub struct Pagination {
pub page: Option<bool>,
pub offset: Option<u64>, pub offset: Option<u64>,
pub batch: Option<u64>, pub batch: Option<u64>,
} }
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 view(State(ctx): State<Context>) -> Result<Json<serde_json::Value>, StatusCode> { pub async fn view(State(ctx): State<Context>) -> Result<Json<serde_json::Value>, StatusCode> {
Ok(Json( Ok(Json(
serde_json::Value::new_object() serde_json::Value::new_object()

View file

@ -17,12 +17,10 @@ pub async fn get<const OUTGOING: bool>(
0 0
}); });
Ok(JsonLD( Ok(JsonLD(
serde_json::Value::new_object() ctx.ap_collection(
.set_id(Some(&format!("{}/users/{id}/{follow___}", ctx.base()))) &url!(ctx, "/users/{id}/{follow___}"),
.set_collection_type(Some(CollectionType::OrderedCollection)) Some(count)
.set_total_items(Some(count)) ).ld_context()
.set_first(Node::link(format!("{}/users/{id}/{follow___}/page", ctx.base())))
.ld_context()
)) ))
} }
@ -47,12 +45,12 @@ pub async fn page<const OUTGOING: bool>(
}, },
Ok(following) => { Ok(following) => {
Ok(JsonLD( Ok(JsonLD(
serde_json::Value::new_object() ctx.ap_collection_page(
.set_collection_type(Some(CollectionType::OrderedCollectionPage)) &url!(ctx, "/users/{id}/{follow___}"),
.set_part_of(Node::link(url!(ctx, "/users/{id}/{follow___}"))) offset,
.set_next(Node::link(url!(ctx, "/users/{id}/{follow___}/page?offset={}", offset+limit))) limit,
.set_ordered_items(Node::array(following.into_iter().map(|x| x.following).collect())) following.into_iter().map(|x| Node::link(x.following)).collect()
.ld_context() ).ld_context()
)) ))
}, },
} }

View file

@ -12,12 +12,7 @@ pub async fn get(
Identity::Anonymous => Err(StatusCode::FORBIDDEN), Identity::Anonymous => Err(StatusCode::FORBIDDEN),
Identity::Remote(_) => Err(StatusCode::FORBIDDEN), Identity::Remote(_) => Err(StatusCode::FORBIDDEN),
Identity::Local(user) => if ctx.uid(id.clone()) == user { Identity::Local(user) => if ctx.uid(id.clone()) == user {
Ok(JsonLD(serde_json::Value::new_object() Ok(JsonLD(ctx.ap_collection(&url!(ctx, "/users/{id}/inbox"), None).ld_context()))
.set_id(Some(&url!(ctx, "/users/{id}/inbox")))
.set_collection_type(Some(CollectionType::OrderedCollection))
.set_first(Node::link(url!(ctx, "/users/{id}/inbox/page")))
.ld_context()
))
} else { } else {
Err(StatusCode::FORBIDDEN) Err(StatusCode::FORBIDDEN)
}, },
@ -29,19 +24,16 @@ pub async fn page(
Path(id): Path<String>, Path(id): Path<String>,
AuthIdentity(auth): AuthIdentity, AuthIdentity(auth): AuthIdentity,
Query(page): Query<Pagination>, Query(page): Query<Pagination>,
) -> Result<JsonLD<serde_json::Value>, StatusCode> { ) -> crate::Result<JsonLD<serde_json::Value>> {
let uid = ctx.uid(id.clone()); let uid = ctx.uid(id.clone());
match auth { match auth {
Identity::Anonymous => Err(StatusCode::FORBIDDEN), Identity::Anonymous => Err(StatusCode::FORBIDDEN.into()),
Identity::Remote(_) => Err(StatusCode::FORBIDDEN), Identity::Remote(_) => Err(StatusCode::FORBIDDEN.into()),
Identity::Local(user) => if uid == user { Identity::Local(user) => if uid == user {
let limit = page.batch.unwrap_or(20).min(50); let limit = page.batch.unwrap_or(20).min(50);
let offset = page.offset.unwrap_or(0); let offset = page.offset.unwrap_or(0);
match model::addressing::Entity::find() match model::addressing::Entity::find()
.filter(Condition::any() .filter(Condition::all().add(model::addressing::Column::Actor.eq(uid)))
.add(model::addressing::Column::Actor.eq(PUBLIC_TARGET))
.add(model::addressing::Column::Actor.eq(uid))
)
.order_by(model::addressing::Column::Published, Order::Asc) .order_by(model::addressing::Column::Published, Order::Asc)
.find_also_related(model::activity::Entity) .find_also_related(model::activity::Entity)
.limit(limit) .limit(limit)
@ -50,27 +42,24 @@ pub async fn page(
.await .await
{ {
Ok(activities) => { Ok(activities) => {
Ok(JsonLD(serde_json::Value::new_object() Ok(JsonLD(
.set_id(Some(&url!(ctx, "/users/{id}/inbox/page?offset={offset}"))) ctx.ap_collection_page(
.set_collection_type(Some(CollectionType::OrderedCollectionPage)) &url!(ctx, "/users/{id}/inbox/page"),
.set_part_of(Node::link(url!(ctx, "/users/{id}/inbox"))) offset, limit,
.set_next(Node::link(url!(ctx, "/users/{id}/inbox/page?offset={}", offset+limit)))
.set_ordered_items(Node::array(
activities activities
.into_iter() .into_iter()
.filter_map(|(_, a)| Some(ap_activity(a?))) .filter_map(|(_, a)| Some(Node::object(ap_activity(a?))))
.collect::<Vec<serde_json::Value>>() .collect::<Vec<Node<serde_json::Value>>>()
)) ).ld_context()
.ld_context()
)) ))
}, },
Err(e) => { Err(e) => {
tracing::error!("failed paginating user inbox for {id}: {e}"); tracing::error!("failed paginating user inbox for {id}: {e}");
Err(StatusCode::INTERNAL_SERVER_ERROR) Err(StatusCode::INTERNAL_SERVER_ERROR.into())
}, },
} }
} else { } else {
Err(StatusCode::FORBIDDEN) Err(StatusCode::FORBIDDEN.into())
}, },
} }
} }

View file

@ -1,32 +1,17 @@
use axum::{extract::{Path, Query, State}, http::StatusCode, response::IntoResponse, Json}; use axum::{extract::{Path, Query, State}, http::StatusCode, Json};
use sea_orm::{EntityTrait, IntoActiveModel, Order, QueryOrder, QuerySelect, Set}; use sea_orm::{EntityTrait, IntoActiveModel, Order, QueryOrder, QuerySelect, Set};
use crate::{activitypub::{jsonld::LD, JsonLD, Pagination}, activitystream::{object::{activity::{accept::AcceptType, Activity, ActivityMut, ActivityType}, collection::{page::CollectionPageMut, CollectionMut, CollectionType}, Addressed, ObjectMut}, Base, BaseMut, BaseType, Node, ObjectType}, auth::{AuthIdentity, Identity}, errors::UpubError, model, server::Context, url}; use crate::{activitypub::{jsonld::LD, CreationResult, JsonLD, Pagination}, activitystream::{object::{activity::{accept::AcceptType, Activity, ActivityMut, ActivityType}, collection::{page::CollectionPageMut, CollectionMut, CollectionType}, Addressed, ObjectMut}, Base, BaseMut, BaseType, Node, ObjectType}, auth::{AuthIdentity, Identity}, errors::UpubError, model, server::Context, url};
pub async fn get( pub async fn get(
State(ctx): State<Context>, State(ctx): State<Context>,
Path(id): Path<String>, Path(id): Path<String>,
) -> Result<JsonLD<serde_json::Value>, StatusCode> { ) -> Result<JsonLD<serde_json::Value>, StatusCode> {
Ok(JsonLD( Ok(JsonLD(
serde_json::Value::new_object() ctx.ap_collection(&url!(ctx, "/users/{id}/outbox"), None).ld_context()
.set_id(Some(&url!(ctx, "/users/{id}/outbox")))
.set_collection_type(Some(CollectionType::OrderedCollection))
.set_first(Node::link(url!(ctx, "/users/{id}/outbox/page")))
.ld_context()
)) ))
} }
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>,
@ -57,26 +42,23 @@ pub async fn page(
Err(_e) => Err(StatusCode::INTERNAL_SERVER_ERROR), Err(_e) => Err(StatusCode::INTERNAL_SERVER_ERROR),
Ok(items) => { Ok(items) => {
Ok(JsonLD( Ok(JsonLD(
serde_json::Value::new_object() ctx.ap_collection_page(
// TODO set id, calculate uri from given args &url!(ctx, "/users/{id}/outbox/page"),
.set_id(Some(&url!(ctx, "/users/{id}/outbox/page?offset={offset}"))) offset, limit,
.set_collection_type(Some(CollectionType::OrderedCollectionPage)) items
.set_part_of(Node::link(url!(ctx, "/users/{id}/outbox"))) .into_iter()
.set_next(Node::link(url!(ctx, "/users/{id}/outbox/page?offset={}", limit+offset))) .map(|(a, o)| {
.set_ordered_items(Node::array( let oid = a.object.clone();
items Node::object(
.into_iter()
.map(|(a, o)| {
let oid = a.object.clone();
super::super::activity::ap_activity(a) super::super::activity::ap_activity(a)
.set_object(match o { .set_object(match o {
Some(o) => Node::object(super::super::object::ap_object(o)), Some(o) => Node::object(super::super::object::ap_object(o)),
None => Node::maybe_link(oid), None => Node::maybe_link(oid),
}) })
}) )
.collect() })
)) .collect()
.ld_context() ).ld_context()
)) ))
}, },
} }

View file

@ -16,6 +16,14 @@ pub enum UpubError {
Reqwest(#[from] reqwest::Error), Reqwest(#[from] reqwest::Error),
} }
impl UpubError {
pub fn code(code: axum::http::StatusCode) -> Self {
UpubError::Status(code)
}
}
pub type UpubResult<T> = Result<T, UpubError>;
impl From<axum::http::StatusCode> for UpubError { impl From<axum::http::StatusCode> for UpubError {
fn from(value: axum::http::StatusCode) -> Self { fn from(value: axum::http::StatusCode) -> Self {
UpubError::Status(value) UpubError::Status(value)

View file

@ -14,6 +14,8 @@ use clap::{Parser, Subcommand};
use sea_orm::{ConnectOptions, Database, EntityTrait, IntoActiveModel}; use sea_orm::{ConnectOptions, Database, EntityTrait, IntoActiveModel};
use sea_orm_migration::MigratorTrait; use sea_orm_migration::MigratorTrait;
pub use errors::UpubResult as Result;
use crate::activitystream::{BaseType, ObjectType}; use crate::activitystream::{BaseType, ObjectType};
pub const VERSION: &str = env!("CARGO_PKG_VERSION"); pub const VERSION: &str = env!("CARGO_PKG_VERSION");

View file

@ -3,7 +3,7 @@ use std::{str::Utf8Error, sync::Arc};
use openssl::rsa::Rsa; use openssl::rsa::Rsa;
use sea_orm::{ColumnTrait, Condition, DatabaseConnection, DbErr, EntityTrait, QueryFilter, QuerySelect, SelectColumns, Set}; use sea_orm::{ColumnTrait, Condition, DatabaseConnection, DbErr, EntityTrait, QueryFilter, QuerySelect, SelectColumns, Set};
use crate::{activitypub::PUBLIC_TARGET, dispatcher::Dispatcher, fetcher::Fetcher, model}; use crate::{activitypub::{jsonld::LD, PUBLIC_TARGET}, activitystream::{object::{activity::ActivityMut, collection::{page::CollectionPageMut, CollectionMut, CollectionType}}, Base, BaseMut, Node, Object}, dispatcher::Dispatcher, fetcher::Fetcher, model};
#[derive(Clone)] #[derive(Clone)]
pub struct Context(Arc<ContextInner>); pub struct Context(Arc<ContextInner>);
@ -206,5 +206,21 @@ impl Context {
Ok(()) Ok(())
} }
}
pub fn ap_collection(&self, id: &str, total_items: Option<u64>) -> serde_json::Value {
serde_json::Value::new_object()
.set_id(Some(id))
.set_collection_type(Some(CollectionType::OrderedCollection))
.set_first(Node::link(format!("{id}/page")))
.set_total_items(total_items)
}
pub fn ap_collection_page(&self, id: &str, offset: u64, limit: u64, items: Vec<Node<serde_json::Value>>) -> serde_json::Value {
serde_json::Value::new_object()
.set_id(Some(&format!("{id}?offset={offset}")))
.set_collection_type(Some(CollectionType::OrderedCollectionPage))
.set_part_of(Node::link(id.replace("/page", "")))
.set_next(Node::link(format!("{id}?offset={}", offset+limit)))
.set_ordered_items(Node::Array(items))
}
}