2024-04-19 03:29:28 +02:00
|
|
|
use axum::extract::{Path, Query, State};
|
2024-04-22 02:52:18 +02:00
|
|
|
use sea_orm::{ColumnTrait, PaginatorTrait, QueryFilter, QuerySelect};
|
2024-04-19 03:29:28 +02:00
|
|
|
|
2024-04-21 17:11:50 +02:00
|
|
|
use crate::{model::{self, addressing::WrappedObject}, routes::activitypub::{jsonld::LD, JsonLD, Pagination}, server::{auth::AuthIdentity, Context}, url};
|
2024-04-19 03:29:28 +02:00
|
|
|
|
|
|
|
pub async fn get(
|
|
|
|
State(ctx): State<Context>,
|
|
|
|
Path(id): Path<String>,
|
|
|
|
AuthIdentity(auth): AuthIdentity,
|
|
|
|
) -> crate::Result<JsonLD<serde_json::Value>> {
|
|
|
|
let context = if id.starts_with('+') {
|
|
|
|
format!("https://{}", id.replacen('+', "", 1).replace('@', "/"))
|
|
|
|
} else {
|
|
|
|
url!(ctx, "/context/{id}")
|
|
|
|
};
|
|
|
|
|
|
|
|
let count = model::addressing::Entity::find_objects()
|
|
|
|
.filter(auth.filter_condition())
|
|
|
|
.filter(model::object::Column::Context.eq(context))
|
|
|
|
.count(ctx.db())
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
Ok(JsonLD(ctx.ap_collection(&url!(ctx, "/context/{id}"), Some(count)).ld_context()))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn page(
|
|
|
|
State(ctx): State<Context>,
|
|
|
|
Path(id): Path<String>,
|
|
|
|
Query(page): Query<Pagination>,
|
|
|
|
AuthIdentity(auth): AuthIdentity,
|
|
|
|
) -> crate::Result<JsonLD<serde_json::Value>> {
|
|
|
|
let limit = page.batch.unwrap_or(20).min(50);
|
|
|
|
let offset = page.offset.unwrap_or(0);
|
|
|
|
|
|
|
|
let context = if id.starts_with('+') {
|
2024-04-22 01:01:42 +02:00
|
|
|
id.replacen('+', "https://", 1).replace('@', "/")
|
2024-04-22 00:01:09 +02:00
|
|
|
} else if id.starts_with("tag:") {
|
|
|
|
id.clone()
|
2024-04-19 03:29:28 +02:00
|
|
|
} else {
|
2024-04-22 00:01:09 +02:00
|
|
|
url!(ctx, "/context/{id}") // TODO need a better way to figure out which ones are our contexts
|
2024-04-19 03:29:28 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
let items = model::addressing::Entity::find_objects()
|
|
|
|
.filter(auth.filter_condition())
|
|
|
|
.filter(model::object::Column::Context.eq(context))
|
|
|
|
.limit(limit)
|
|
|
|
.offset(offset)
|
2024-04-21 17:11:50 +02:00
|
|
|
.into_model::<WrappedObject>()
|
2024-04-19 03:29:28 +02:00
|
|
|
.all(ctx.db())
|
|
|
|
.await?;
|
|
|
|
|
2024-04-22 00:16:40 +02:00
|
|
|
let mut out = Vec::new();
|
|
|
|
for item in items {
|
|
|
|
out.push(item.ap_filled(ctx.db()).await?);
|
|
|
|
}
|
|
|
|
|
2024-04-19 03:29:28 +02:00
|
|
|
Ok(JsonLD(
|
|
|
|
ctx.ap_collection_page(
|
2024-04-19 06:57:01 +02:00
|
|
|
&url!(ctx, "/context/{id}/page"),
|
2024-04-22 00:16:40 +02:00
|
|
|
offset, limit, out,
|
2024-04-19 03:29:28 +02:00
|
|
|
).ld_context()
|
|
|
|
))
|
|
|
|
}
|