1use sea_orm::{ColumnTrait, EntityTrait, IntoActiveModel, QueryFilter};
2use upub::traits::{fetch::RequestError, Fetcher};
3
4pub async fn thread(ctx: upub::Context) -> Result<(), RequestError> {
5 use futures::TryStreamExt;
6 let db = ctx.db();
7
8 tracing::info!("fixing contexts...");
9 let mut stream = upub::model::object::Entity::find()
10 .filter(upub::model::object::Column::Context.is_null())
11 .stream(db)
12 .await?;
13
14 while let Some(mut object) = stream.try_next().await? {
15 match object.in_reply_to {
16 None => object.context = Some(object.id.clone()),
17 Some(ref in_reply_to) => {
18 match ctx.fetch_object(in_reply_to, ctx.db()).await {
19 Ok(reply) => {
20 if let Some(context) = reply.context {
21 object.context = Some(context);
22 } else {
23 continue;
24 }
25 },
26 Err(e) => {
27 tracing::error!("could not fetch in_reply_to: {e} -- {e:?}");
28 continue;
29 },
30 }
31 },
32 }
33 tracing::info!("updating context of {}", object.id);
34 upub::model::object::Entity::update(object.into_active_model())
35 .exec(ctx.db())
36 .await?;
37 }
38
39 tracing::info!("done fixing contexts");
40 Ok(())
41}