diff --git a/upub/cli/Cargo.toml b/upub/cli/Cargo.toml index ca7df1a..745fa30 100644 --- a/upub/cli/Cargo.toml +++ b/upub/cli/Cargo.toml @@ -23,3 +23,4 @@ clap = { version = "4.5", features = ["derive"] } sea-orm = "1.0" futures = "0.3" mdhtml = { path = "../../utils/mdhtml/" } +reqwest = { version = "0.12", features = ["json"] } diff --git a/upub/cli/src/fetch.rs b/upub/cli/src/fetch.rs index eeb2e3e..b31d7c9 100644 --- a/upub/cli/src/fetch.rs +++ b/upub/cli/src/fetch.rs @@ -1,11 +1,37 @@ use sea_orm::{EntityTrait, TransactionTrait}; -use upub::traits::{fetch::{Fetchable, RequestError}, Addresser, Normalizer}; +use upub::traits::{fetch::{Fetchable, RequestError}, Addresser, Fetcher, Normalizer}; -pub async fn fetch(ctx: upub::Context, uri: String, save: bool) -> Result<(), RequestError> { +pub async fn fetch(ctx: upub::Context, uri: String, save: bool, actor: Option) -> Result<(), RequestError> { use apb::Base; + let mut pkey = ctx.pkey().to_string(); + let mut from = ctx.base().to_string(); + + if let Some(actor) = actor { + let actor_model = upub::model::actor::Entity::find_by_ap_id(&actor) + .one(ctx.db()) + .await? + .ok_or_else(|| sea_orm::DbErr::RecordNotFound(actor.clone()))?; + + match actor_model.private_key { + None => tracing::error!("requested actor lacks a private key, fetching with server key instead"), + Some(x) => { + pkey = x; + from = actor.to_string(); + }, + } + } + let mut node = apb::Node::link(uri.to_string()); - node.fetch(&ctx).await?; + if let apb::Node::Link(uri) = node { + if let Ok(href) = uri.href() { + node = upub::Context::request(reqwest::Method::GET, href, None, &from, &pkey, ctx.domain()) + .await? + .json::() + .await? + .into(); + } + } let obj = node.extract().expect("node still empty after fetch?");