fix(cli): better update task for users

This commit is contained in:
əlemi 2024-07-17 22:24:36 +02:00
parent 64cb963282
commit 01984c9e98
Signed by: alemi
GPG key ID: A4895B84D311642C

View file

@ -4,37 +4,29 @@ use upub::traits::Fetcher;
pub async fn update_users(ctx: upub::Context, days: i64) -> Result<(), sea_orm::DbErr> { pub async fn update_users(ctx: upub::Context, days: i64) -> Result<(), sea_orm::DbErr> {
let mut count = 0; let mut count = 0;
let mut insertions = Vec::new(); let mut stream = upub::model::actor::Entity::find()
.filter(upub::model::actor::Column::Updated.lt(chrono::Utc::now() - chrono::Duration::days(days)))
{ .stream(ctx.db())
let mut stream = upub::model::actor::Entity::find() .await?;
.filter(upub::model::actor::Column::Updated.lt(chrono::Utc::now() - chrono::Duration::days(days)))
.stream(ctx.db())
.await?;
while let Some(user) = stream.try_next().await? { while let Some(user) = stream.try_next().await? {
if ctx.is_local(&user.id) { continue } if ctx.is_local(&user.id) { continue }
match ctx.pull(&user.id).await.map(|x| x.actor()) { match ctx.pull(&user.id).await.map(|x| x.actor()) {
Err(e) => tracing::warn!("could not update user {}: {e}", user.id), Err(e) => tracing::warn!("could not update user {}: {e}", user.id),
Ok(Err(e)) => tracing::warn!("could not update user {}: {e}", user.id), Ok(Err(e)) => tracing::warn!("could not update user {}: {e}", user.id),
Ok(Ok(doc)) => match upub::AP::actor_q(&doc, Some(user.internal)) { Ok(Ok(doc)) => match upub::AP::actor_q(&doc, Some(user.internal)) {
Ok(mut u) => { Ok(mut u) => {
u.updated = Set(chrono::Utc::now()); tracing::info!("updating user {}", user.id);
insertions.push((user.id, u)); u.updated = Set(chrono::Utc::now());
count += 1; u.update(ctx.db()).await?;
}, count += 1;
Err(e) => tracing::warn!("failed deserializing user '{}': {e}", user.id),
}, },
} Err(e) => tracing::warn!("failed deserializing user '{}': {e}", user.id),
},
} }
} }
for (uid, user_model) in insertions {
tracing::info!("updating user {}", uid);
user_model.update(ctx.db()).await?;
}
tracing::info!("updated {count} users"); tracing::info!("updated {count} users");
Ok(()) Ok(())