upub-cli 0.3.2

cli maintenance tasks for upub
Documentation
use sea_orm::{ColumnTrait, EntityTrait, IntoActiveModel, QueryFilter};
use upub::traits::{fetch::RequestError, Fetcher};

pub async fn thread(ctx: upub::Context) -> Result<(), RequestError> {
	use futures::TryStreamExt;
	let db = ctx.db();

	tracing::info!("fixing contexts...");
	let mut stream = upub::model::object::Entity::find()
		.filter(upub::model::object::Column::Context.is_null())
		.stream(db)
		.await?;

	while let Some(mut object) = stream.try_next().await? {
		match object.in_reply_to {
			None => object.context = Some(object.id.clone()),
			Some(ref in_reply_to) => {
				match ctx.fetch_object(in_reply_to, ctx.db()).await {
					Ok(reply) => {
						if let Some(context) = reply.context {
							object.context = Some(context);
						} else {
							continue;
						}
					},
					Err(e) => {
						tracing::error!("could not fetch in_reply_to: {e} -- {e:?}");
						continue;
					},
				}
			},
		}
		tracing::info!("updating context of {}", object.id);
		upub::model::object::Entity::update(object.into_active_model())
			.exec(ctx.db())
			.await?;
	}

	tracing::info!("done fixing contexts");
	Ok(())
}