forked from alemi/upub
feat: normalized pagination
This commit is contained in:
parent
139e973277
commit
fe30ad59d7
3 changed files with 53 additions and 50 deletions
|
@ -3,7 +3,30 @@ use sea_orm::{ColumnTrait, Condition, EntityTrait, PaginatorTrait, QueryFilter,
|
||||||
|
|
||||||
use crate::{activitypub::{jsonld::LD, JsonLD, Pagination}, activitystream::{object::collection::{page::CollectionPageMut, CollectionMut, CollectionType}, BaseMut, Node}, model, server::Context, url};
|
use crate::{activitypub::{jsonld::LD, JsonLD, Pagination}, activitystream::{object::collection::{page::CollectionPageMut, CollectionMut, CollectionType}, BaseMut, Node}, model, server::Context, url};
|
||||||
|
|
||||||
pub async fn follow___<const OUTGOING: bool>(
|
use model::relation::Column::{Following, Follower};
|
||||||
|
|
||||||
|
pub async fn get<const OUTGOING: bool>(
|
||||||
|
State(ctx): State<Context>,
|
||||||
|
Path(id): Path<String>,
|
||||||
|
) -> Result<JsonLD<serde_json::Value>, StatusCode> {
|
||||||
|
let follow___ = if OUTGOING { "following" } else { "followers" };
|
||||||
|
let count = model::relation::Entity::find()
|
||||||
|
.filter(Condition::all().add(if OUTGOING { Follower } else { Following }.eq(id.clone())))
|
||||||
|
.count(ctx.db()).await.unwrap_or_else(|e| {
|
||||||
|
tracing::error!("failed counting {follow___} for {id}: {e}");
|
||||||
|
0
|
||||||
|
});
|
||||||
|
Ok(JsonLD(
|
||||||
|
serde_json::Value::new_object()
|
||||||
|
.set_id(Some(&format!("{}/users/{id}/{follow___}", ctx.base())))
|
||||||
|
.set_collection_type(Some(CollectionType::OrderedCollection))
|
||||||
|
.set_total_items(Some(count))
|
||||||
|
.set_first(Node::link(format!("{}/users/{id}/{follow___}/page", ctx.base())))
|
||||||
|
.ld_context()
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn page<const OUTGOING: bool>(
|
||||||
State(ctx): State<Context>,
|
State(ctx): State<Context>,
|
||||||
Path(id): Path<String>,
|
Path(id): Path<String>,
|
||||||
Query(page): Query<Pagination>,
|
Query(page): Query<Pagination>,
|
||||||
|
@ -11,9 +34,6 @@ pub async fn follow___<const OUTGOING: bool>(
|
||||||
let follow___ = if OUTGOING { "following" } else { "followers" };
|
let follow___ = if OUTGOING { "following" } else { "followers" };
|
||||||
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);
|
||||||
if let Some(true) = page.page {
|
|
||||||
|
|
||||||
use model::relation::Column::{Following, Follower};
|
|
||||||
match model::relation::Entity::find()
|
match model::relation::Entity::find()
|
||||||
.filter(Condition::all().add(if OUTGOING { Follower } else { Following }.eq(id.clone())))
|
.filter(Condition::all().add(if OUTGOING { Follower } else { Following }.eq(id.clone())))
|
||||||
.select_column(if OUTGOING { Following } else { Follower })
|
.select_column(if OUTGOING { Following } else { Follower })
|
||||||
|
@ -30,29 +50,10 @@ pub async fn follow___<const OUTGOING: bool>(
|
||||||
serde_json::Value::new_object()
|
serde_json::Value::new_object()
|
||||||
.set_collection_type(Some(CollectionType::OrderedCollectionPage))
|
.set_collection_type(Some(CollectionType::OrderedCollectionPage))
|
||||||
.set_part_of(Node::link(url!(ctx, "/users/{id}/{follow___}")))
|
.set_part_of(Node::link(url!(ctx, "/users/{id}/{follow___}")))
|
||||||
.set_next(Node::link(url!(ctx, "/users/{id}/{follow___}?page=true&offset={}", offset+limit)))
|
.set_next(Node::link(url!(ctx, "/users/{id}/{follow___}/page?offset={}", offset+limit)))
|
||||||
.set_ordered_items(Node::array(following.into_iter().map(|x| x.following).collect()))
|
.set_ordered_items(Node::array(following.into_iter().map(|x| x.following).collect()))
|
||||||
.ld_context()
|
.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 {follow___} for {id}: {e}");
|
|
||||||
0
|
|
||||||
});
|
|
||||||
Ok(JsonLD(
|
|
||||||
serde_json::Value::new_object()
|
|
||||||
.set_id(Some(&format!("{}/users/{id}/{follow___}", ctx.base())))
|
|
||||||
.set_collection_type(Some(CollectionType::OrderedCollection))
|
|
||||||
.set_total_items(Some(count))
|
|
||||||
.set_first(Node::link(format!("{}/users/{id}/{follow___}?page=true", ctx.base())))
|
|
||||||
.ld_context()
|
|
||||||
))
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,8 +2,7 @@ pub mod inbox;
|
||||||
|
|
||||||
pub mod outbox;
|
pub mod outbox;
|
||||||
|
|
||||||
mod following;
|
pub mod following;
|
||||||
pub use following::follow___;
|
|
||||||
|
|
||||||
use axum::{extract::{Path, State}, http::StatusCode};
|
use axum::{extract::{Path, State}, http::StatusCode};
|
||||||
use sea_orm::EntityTrait;
|
use sea_orm::EntityTrait;
|
||||||
|
|
|
@ -21,13 +21,16 @@ pub async fn serve(db: DatabaseConnection, domain: String) {
|
||||||
.route("/nodeinfo/:version", get(ap::well_known::nodeinfo))
|
.route("/nodeinfo/:version", get(ap::well_known::nodeinfo))
|
||||||
// actor routes
|
// actor routes
|
||||||
.route("/users/:id", get(ap::user::view))
|
.route("/users/:id", get(ap::user::view))
|
||||||
.route("/users/:id/inbox", get(ap::user::inbox::get))
|
|
||||||
.route("/users/:id/inbox", post(ap::user::inbox::post))
|
.route("/users/:id/inbox", post(ap::user::inbox::post))
|
||||||
.route("/users/:id/outbox", get(ap::user::outbox::get))
|
.route("/users/:id/inbox", get(ap::user::inbox::get))
|
||||||
|
.route("/users/:id/inbox/page", get(ap::user::inbox::page))
|
||||||
.route("/users/:id/outbox", post(ap::user::outbox::post))
|
.route("/users/:id/outbox", post(ap::user::outbox::post))
|
||||||
|
.route("/users/:id/outbox", get(ap::user::outbox::get))
|
||||||
.route("/users/:id/outbox/page", get(ap::user::outbox::page))
|
.route("/users/:id/outbox/page", get(ap::user::outbox::page))
|
||||||
.route("/users/:id/followers", get(ap::user::follow___::<false>))
|
.route("/users/:id/followers", get(ap::user::following::get::<false>))
|
||||||
.route("/users/:id/following", get(ap::user::follow___::<true>))
|
.route("/users/:id/followers/page", get(ap::user::following::page::<false>))
|
||||||
|
.route("/users/:id/following", get(ap::user::following::get::<true>))
|
||||||
|
.route("/users/:id/following/page", get(ap::user::following::page::<true>))
|
||||||
// specific object routes
|
// specific object routes
|
||||||
.route("/activities/:id", get(ap::activity::view))
|
.route("/activities/:id", get(ap::activity::view))
|
||||||
.route("/objects/:id", get(ap::object::view))
|
.route("/objects/:id", get(ap::object::view))
|
||||||
|
|
Loading…
Reference in a new issue