1
0
Fork 0
forked from alemi/upub
upub/src/routes/activitypub/activity.rs

34 lines
1,013 B
Rust
Raw Normal View History

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 aid = if id.starts_with('+') {
format!("https://{}", id.replacen('+', "", 1).replace('@', "/"))
} else {
ctx.aid(id.clone())
};
if auth.is_local() && query.fetch && !ctx.is_local(&aid) {
ctx.fetch_activity(&aid).await?;
}
match model::addressing::Entity::find_activities()
.filter(model::activity::Column::Id.eq(&aid))
.filter(auth.filter_condition())
.into_model::<EmbeddedActivity>()
2024-03-21 01:42:29 +01:00
.one(ctx.db())
.await?
2024-03-21 01:42:29 +01:00
{
Some(activity) => Ok(JsonLD(serde_json::Value::from(activity).ld_context())),
None => Err(UpubError::not_found()),
}
}