2024-04-18 04:48:49 +02:00
|
|
|
use axum::extract::{Path, Query, State};
|
2024-04-12 22:21:23 +02:00
|
|
|
use sea_orm::{ColumnTrait, QueryFilter};
|
2024-04-18 04:48:49 +02:00
|
|
|
use crate::{errors::UpubError, model::{self, addressing::EmbeddedActivity}, server::{auth::AuthIdentity, Context}};
|
2024-04-06 16:56:13 +02:00
|
|
|
use apb::{ActivityMut, ObjectMut, BaseMut, Node};
|
2024-03-20 05:44:50 +01:00
|
|
|
|
2024-04-18 04:48:49 +02:00
|
|
|
use super::{jsonld::LD, JsonLD, TryFetch};
|
2024-03-20 05:44:50 +01:00
|
|
|
|
2024-04-12 19:36:00 +02:00
|
|
|
// TODO this is used outside /routes, maybe move in model?
|
2024-03-23 06:32:15 +01:00
|
|
|
pub fn ap_activity(activity: model::activity::Model) -> serde_json::Value {
|
|
|
|
serde_json::Value::new_object()
|
|
|
|
.set_id(Some(&activity.id))
|
|
|
|
.set_activity_type(Some(activity.activity_type))
|
|
|
|
.set_actor(Node::link(activity.actor))
|
|
|
|
.set_object(Node::maybe_link(activity.object))
|
|
|
|
.set_target(Node::maybe_link(activity.target))
|
|
|
|
.set_published(Some(activity.published))
|
|
|
|
.set_to(Node::links(activity.to.0.clone()))
|
2024-04-06 16:56:13 +02:00
|
|
|
.set_bto(Node::Empty)
|
2024-03-23 06:32:15 +01:00
|
|
|
.set_cc(Node::links(activity.cc.0.clone()))
|
2024-04-06 16:56:13 +02:00
|
|
|
.set_bcc(Node::Empty)
|
2024-03-23 06:32:15 +01:00
|
|
|
}
|
2024-03-21 03:07:35 +01:00
|
|
|
|
2024-04-12 22:21:23 +02:00
|
|
|
pub async fn view(
|
|
|
|
State(ctx): State<Context>,
|
|
|
|
Path(id): Path<String>,
|
|
|
|
AuthIdentity(auth): AuthIdentity,
|
2024-04-18 04:48:49 +02:00
|
|
|
Query(query): Query<TryFetch>,
|
|
|
|
) -> crate::Result<JsonLD<serde_json::Value>> {
|
2024-04-15 21:36:31 +02:00
|
|
|
let aid = if id.starts_with('+') {
|
|
|
|
format!("https://{}", id.replacen('+', "", 1).replace('@', "/"))
|
|
|
|
} else {
|
|
|
|
ctx.aid(id.clone())
|
|
|
|
};
|
2024-04-12 22:21:23 +02:00
|
|
|
match model::addressing::Entity::find_activities()
|
2024-04-18 04:48:49 +02:00
|
|
|
.filter(model::activity::Column::Id.eq(&aid))
|
2024-04-12 22:21:23 +02:00
|
|
|
.filter(auth.filter_condition())
|
|
|
|
.into_model::<EmbeddedActivity>()
|
2024-03-21 01:42:29 +01:00
|
|
|
.one(ctx.db())
|
2024-04-18 04:48:49 +02:00
|
|
|
.await?
|
2024-03-21 01:42:29 +01:00
|
|
|
{
|
2024-04-18 04:48:49 +02:00
|
|
|
Some(activity) => Ok(JsonLD(serde_json::Value::from(activity).ld_context())),
|
2024-04-18 04:50:06 +02:00
|
|
|
None => if auth.is_local() && query.fetch && !ctx.is_local(&aid) {
|
2024-04-18 04:48:49 +02:00
|
|
|
Ok(JsonLD(ap_activity(ctx.fetch().activity(&aid).await?).ld_context()))
|
|
|
|
} else {
|
|
|
|
Err(UpubError::not_found())
|
2024-03-20 05:44:50 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|