2024-04-30 16:48:28 +02:00
|
|
|
use sea_orm::EntityTrait;
|
|
|
|
|
|
|
|
|
2024-05-11 22:47:43 +02:00
|
|
|
pub async fn fix(ctx: crate::server::Context, likes: bool, shares: bool, replies: bool) -> crate::Result<()> {
|
2024-04-30 16:48:28 +02:00
|
|
|
use futures::TryStreamExt;
|
2024-05-11 22:47:43 +02:00
|
|
|
let db = ctx.db();
|
2024-04-30 16:48:28 +02:00
|
|
|
|
|
|
|
if likes {
|
|
|
|
tracing::info!("fixing likes...");
|
|
|
|
let mut store = std::collections::HashMap::new();
|
|
|
|
{
|
2024-05-11 22:47:43 +02:00
|
|
|
let mut stream = crate::model::like::Entity::find().stream(db).await?;
|
2024-04-30 16:48:28 +02:00
|
|
|
while let Some(like) = stream.try_next().await? {
|
|
|
|
store.insert(like.likes.clone(), store.get(&like.likes).unwrap_or(&0) + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (k, v) in store {
|
|
|
|
let m = crate::model::object::ActiveModel {
|
|
|
|
id: sea_orm::Set(k.clone()),
|
|
|
|
likes: sea_orm::Set(v),
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
if let Err(e) = crate::model::object::Entity::update(m)
|
2024-05-11 22:47:43 +02:00
|
|
|
.exec(db)
|
2024-04-30 16:48:28 +02:00
|
|
|
.await
|
|
|
|
{
|
|
|
|
tracing::warn!("record not updated ({k}): {e}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if shares {
|
|
|
|
tracing::info!("fixing shares...");
|
|
|
|
let mut store = std::collections::HashMap::new();
|
|
|
|
{
|
2024-05-11 22:47:43 +02:00
|
|
|
let mut stream = crate::model::share::Entity::find().stream(db).await?;
|
2024-04-30 16:48:28 +02:00
|
|
|
while let Some(share) = stream.try_next().await? {
|
|
|
|
store.insert(share.shares.clone(), store.get(&share.shares).unwrap_or(&0) + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (k, v) in store {
|
|
|
|
let m = crate::model::object::ActiveModel {
|
|
|
|
id: sea_orm::Set(k.clone()),
|
|
|
|
shares: sea_orm::Set(v),
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
if let Err(e) = crate::model::object::Entity::update(m)
|
2024-05-11 22:47:43 +02:00
|
|
|
.exec(db)
|
2024-04-30 16:48:28 +02:00
|
|
|
.await
|
|
|
|
{
|
|
|
|
tracing::warn!("record not updated ({k}): {e}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if replies {
|
|
|
|
tracing::info!("fixing replies...");
|
|
|
|
let mut store = std::collections::HashMap::new();
|
|
|
|
{
|
2024-05-11 22:47:43 +02:00
|
|
|
let mut stream = crate::model::object::Entity::find().stream(db).await?;
|
2024-04-30 16:48:28 +02:00
|
|
|
while let Some(object) = stream.try_next().await? {
|
|
|
|
if let Some(reply) = object.in_reply_to {
|
|
|
|
let before = store.get(&reply).unwrap_or(&0);
|
|
|
|
store.insert(reply, before + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (k, v) in store {
|
|
|
|
let m = crate::model::object::ActiveModel {
|
|
|
|
id: sea_orm::Set(k.clone()),
|
|
|
|
comments: sea_orm::Set(v),
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
if let Err(e) = crate::model::object::Entity::update(m)
|
2024-05-11 22:47:43 +02:00
|
|
|
.exec(db)
|
2024-04-30 16:48:28 +02:00
|
|
|
.await
|
|
|
|
{
|
|
|
|
tracing::warn!("record not updated ({k}): {e}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tracing::info!("done running fix tasks");
|
|
|
|
Ok(())
|
|
|
|
}
|