upub_cli/
export.rs

1use futures::TryStreamExt;
2use sea_orm::{ColumnTrait, QueryFilter, QueryOrder};
3use upub::selector::{RichFillable, RichObject};
4
5
6pub async fn export(
7	ctx: upub::Context,
8	actor: String,
9	file: std::path::PathBuf,
10	pretty: bool,
11) -> Result<(), Box<dyn std::error::Error>> {
12	let uid = ctx.uid(&actor);
13	let internal = upub::model::actor::Entity::ap_to_internal(&uid, ctx.db())
14		.await?
15		.ok_or(sea_orm::DbErr::RecordNotFound(format!("could not find user {actor}")))?;
16
17	let mut objects = Vec::new();
18	
19	let mut stream = upub::Query::objects(upub::query_feed_opts!(Some(internal), true))
20		.filter(upub::model::object::Column::AttributedTo.eq(uid))
21		.order_by_asc(upub::model::addressing::Column::Published)
22		.order_by_asc(upub::model::activity::Column::Internal)
23		.into_model::<RichObject>()
24		.stream(ctx.db())
25		.await?;
26
27	while let Some(obj) = stream.try_next().await? {
28		objects.push(ctx.ap(obj.load_batched_models(ctx.db()).await?));
29	}
30
31	let writer = std::fs::File::create(file)?;
32
33	if pretty {
34		serde_json::to_writer_pretty(writer, &objects)?;
35	} else {
36		serde_json::to_writer(writer, &objects)?;
37	}
38
39	Ok(())
40}