feat(cli): allow choosing which actor to use for fetch

This commit is contained in:
əlemi 2024-09-18 22:16:52 +02:00
parent 716a34e637
commit 047ac5a9e5
Signed by: alemi
GPG key ID: A4895B84D311642C
2 changed files with 30 additions and 3 deletions

View file

@ -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"] }

View file

@ -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<String>) -> 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::<serde_json::Value>()
.await?
.into();
}
}
let obj = node.extract().expect("node still empty after fetch?");