2024-04-19 03:32:21 +02:00
|
|
|
pub mod replies;
|
|
|
|
|
2024-04-19 04:42:43 +02:00
|
|
|
use apb::{BaseMut, CollectionMut, ObjectMut};
|
2024-04-19 03:32:21 +02:00
|
|
|
use axum::extract::{Path, Query, State};
|
|
|
|
use sea_orm::{ColumnTrait, QueryFilter};
|
|
|
|
|
|
|
|
use crate::{errors::UpubError, model::{self, addressing::EmbeddedActivity}, server::{auth::AuthIdentity, fetcher::Fetcher, Context}};
|
|
|
|
|
|
|
|
use super::{jsonld::LD, JsonLD, TryFetch};
|
|
|
|
|
|
|
|
pub async fn view(
|
|
|
|
State(ctx): State<Context>,
|
|
|
|
Path(id): Path<String>,
|
|
|
|
AuthIdentity(auth): AuthIdentity,
|
|
|
|
Query(query): Query<TryFetch>,
|
|
|
|
) -> crate::Result<JsonLD<serde_json::Value>> {
|
|
|
|
let oid = if id.starts_with('+') {
|
|
|
|
format!("https://{}", id.replacen('+', "", 1).replace('@', "/"))
|
|
|
|
} else {
|
|
|
|
ctx.oid(id.clone())
|
|
|
|
};
|
2024-04-19 04:42:43 +02:00
|
|
|
|
2024-04-19 04:57:06 +02:00
|
|
|
let result = model::addressing::Entity::find_objects()
|
2024-04-19 03:32:21 +02:00
|
|
|
.filter(model::object::Column::Id.eq(&oid))
|
|
|
|
.filter(auth.filter_condition())
|
|
|
|
.into_model::<EmbeddedActivity>()
|
|
|
|
.one(ctx.db())
|
2024-04-19 04:42:43 +02:00
|
|
|
.await?;
|
|
|
|
|
|
|
|
let object = match result {
|
|
|
|
Some(EmbeddedActivity { activity: _, object: Some(obj) }) => obj,
|
|
|
|
_ => {
|
|
|
|
if auth.is_local() && query.fetch && !ctx.is_local(&oid) {
|
|
|
|
ctx.fetch_object(&oid).await?
|
|
|
|
} else {
|
|
|
|
return Err(UpubError::not_found())
|
|
|
|
}
|
2024-04-19 03:32:21 +02:00
|
|
|
},
|
2024-04-19 04:42:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
let replies =
|
|
|
|
serde_json::Value::new_object()
|
|
|
|
.set_id(Some(&crate::url!(ctx, "/objects/{id}/replies")))
|
|
|
|
.set_collection_type(Some(apb::CollectionType::OrderedCollection))
|
|
|
|
.set_first(apb::Node::link(crate::url!(ctx, "/objects/{id}/replies/page")))
|
|
|
|
.set_total_items(Some(object.comments as u64));
|
|
|
|
|
|
|
|
|
|
|
|
Ok(JsonLD(
|
|
|
|
object.ap()
|
|
|
|
.set_replies(apb::Node::object(replies))
|
|
|
|
.ld_context()
|
|
|
|
))
|
2024-04-19 03:32:21 +02:00
|
|
|
}
|