From fc572f1c69d2ecaea4cd4275e4ea0c19d3b3d60d Mon Sep 17 00:00:00 2001 From: alemi Date: Sat, 11 May 2024 22:47:43 +0200 Subject: [PATCH] fix: update cli tasks to receive built context --- src/cli/faker.rs | 7 +++++-- src/cli/fetch.rs | 5 +---- src/cli/fix.rs | 15 ++++++++------- src/cli/mod.rs | 18 +++++++++++------- src/cli/relay.rs | 4 +--- src/cli/update.rs | 5 ++--- 6 files changed, 28 insertions(+), 26 deletions(-) diff --git a/src/cli/faker.rs b/src/cli/faker.rs index ee0a5918..4d9da60b 100644 --- a/src/cli/faker.rs +++ b/src/cli/faker.rs @@ -2,14 +2,17 @@ use crate::model::{addressing, config, credential, activity, object, user, Audie use openssl::rsa::Rsa; use sea_orm::IntoActiveModel; -pub async fn faker(db: &sea_orm::DatabaseConnection, domain: String, count: u64) -> Result<(), sea_orm::DbErr> { +pub async fn faker(ctx: crate::server::Context, count: u64) -> Result<(), sea_orm::DbErr> { use sea_orm::{EntityTrait, Set}; + let domain = ctx.domain(); + let db = ctx.db(); + let key = Rsa::generate(2048).unwrap(); let test_user = user::Model { id: format!("{domain}/users/test"), name: Some("μpub".into()), - domain: clean_domain(&domain), + domain: clean_domain(domain), preferred_username: "test".to_string(), summary: Some("hello world! i'm manually generated but served dynamically from db! check progress at https://git.alemi.dev/upub.git".to_string()), following: None, diff --git a/src/cli/fetch.rs b/src/cli/fetch.rs index b674995e..0f1c4f73 100644 --- a/src/cli/fetch.rs +++ b/src/cli/fetch.rs @@ -2,12 +2,9 @@ use sea_orm::{EntityTrait, IntoActiveModel}; use crate::server::fetcher::Fetchable; -pub async fn fetch(db: sea_orm::DatabaseConnection, domain: String, uri: String, save: bool) -> crate::Result<()> { +pub async fn fetch(ctx: crate::server::Context, uri: String, save: bool) -> crate::Result<()> { use apb::Base; - let ctx = crate::server::Context::new(db, domain) - .await.expect("failed creating server context"); - let mut node = apb::Node::link(uri.to_string()); node.fetch(&ctx).await?; diff --git a/src/cli/fix.rs b/src/cli/fix.rs index 027d0b7b..9ad85d85 100644 --- a/src/cli/fix.rs +++ b/src/cli/fix.rs @@ -1,14 +1,15 @@ use sea_orm::EntityTrait; -pub async fn fix(db: sea_orm::DatabaseConnection, likes: bool, shares: bool, replies: bool) -> crate::Result<()> { +pub async fn fix(ctx: crate::server::Context, likes: bool, shares: bool, replies: bool) -> crate::Result<()> { use futures::TryStreamExt; + let db = ctx.db(); if likes { tracing::info!("fixing likes..."); let mut store = std::collections::HashMap::new(); { - let mut stream = crate::model::like::Entity::find().stream(&db).await?; + let mut stream = crate::model::like::Entity::find().stream(db).await?; while let Some(like) = stream.try_next().await? { store.insert(like.likes.clone(), store.get(&like.likes).unwrap_or(&0) + 1); } @@ -21,7 +22,7 @@ pub async fn fix(db: sea_orm::DatabaseConnection, likes: bool, shares: bool, rep ..Default::default() }; if let Err(e) = crate::model::object::Entity::update(m) - .exec(&db) + .exec(db) .await { tracing::warn!("record not updated ({k}): {e}"); @@ -33,7 +34,7 @@ pub async fn fix(db: sea_orm::DatabaseConnection, likes: bool, shares: bool, rep tracing::info!("fixing shares..."); let mut store = std::collections::HashMap::new(); { - let mut stream = crate::model::share::Entity::find().stream(&db).await?; + let mut stream = crate::model::share::Entity::find().stream(db).await?; while let Some(share) = stream.try_next().await? { store.insert(share.shares.clone(), store.get(&share.shares).unwrap_or(&0) + 1); } @@ -46,7 +47,7 @@ pub async fn fix(db: sea_orm::DatabaseConnection, likes: bool, shares: bool, rep ..Default::default() }; if let Err(e) = crate::model::object::Entity::update(m) - .exec(&db) + .exec(db) .await { tracing::warn!("record not updated ({k}): {e}"); @@ -58,7 +59,7 @@ pub async fn fix(db: sea_orm::DatabaseConnection, likes: bool, shares: bool, rep tracing::info!("fixing replies..."); let mut store = std::collections::HashMap::new(); { - let mut stream = crate::model::object::Entity::find().stream(&db).await?; + let mut stream = crate::model::object::Entity::find().stream(db).await?; while let Some(object) = stream.try_next().await? { if let Some(reply) = object.in_reply_to { let before = store.get(&reply).unwrap_or(&0); @@ -74,7 +75,7 @@ pub async fn fix(db: sea_orm::DatabaseConnection, likes: bool, shares: bool, rep ..Default::default() }; if let Err(e) = crate::model::object::Entity::update(m) - .exec(&db) + .exec(db) .await { tracing::warn!("record not updated ({k}): {e}"); diff --git a/src/cli/mod.rs b/src/cli/mod.rs index ecc576e7..707ba1fe 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -10,8 +10,8 @@ pub use faker::*; mod relay; pub use relay::*; -mod register; -pub use register::*; +//mod register; +//pub use register::*; mod update; pub use update::*; @@ -71,17 +71,21 @@ pub async fn run( command: CliCommand, db: sea_orm::DatabaseConnection, domain: String, + config: crate::config::Config, ) -> crate::Result<()> { + let ctx = crate::server::Context::new( + db, domain, config, + ).await?; match command { CliCommand::Faker { count } => - Ok(faker(&db, domain, count).await?), + Ok(faker(ctx, count).await?), CliCommand::Fetch { uri, save } => - Ok(fetch(db, domain, uri, save).await?), + Ok(fetch(ctx, uri, save).await?), CliCommand::Relay { actor, accept } => - Ok(relay(db, domain, actor, accept).await?), + Ok(relay(ctx, actor, accept).await?), CliCommand::Fix { likes, shares, replies } => - Ok(fix(db, likes, shares, replies).await?), + Ok(fix(ctx, likes, shares, replies).await?), CliCommand::Update { days } => - Ok(update_users(db, domain, days).await?), + Ok(update_users(ctx, days).await?), } } diff --git a/src/cli/relay.rs b/src/cli/relay.rs index 888853de..10c565a9 100644 --- a/src/cli/relay.rs +++ b/src/cli/relay.rs @@ -1,8 +1,6 @@ use sea_orm::{ColumnTrait, EntityTrait, IntoActiveModel, QueryFilter, QueryOrder}; -pub async fn relay(db: sea_orm::DatabaseConnection, domain: String, actor: String, accept: bool) -> crate::Result<()> { - let ctx = crate::server::Context::new(db, domain).await?; - +pub async fn relay(ctx: crate::server::Context, actor: String, accept: bool) -> crate::Result<()> { let aid = ctx.aid(uuid::Uuid::new_v4().to_string()); let mut activity_model = crate::model::activity::Model { diff --git a/src/cli/update.rs b/src/cli/update.rs index 7c6843c9..d5318ce0 100644 --- a/src/cli/update.rs +++ b/src/cli/update.rs @@ -1,10 +1,9 @@ use futures::TryStreamExt; use sea_orm::{ColumnTrait, EntityTrait, IntoActiveModel, QueryFilter}; -use crate::server::{fetcher::Fetcher, Context}; +use crate::server::fetcher::Fetcher; -pub async fn update_users(db: sea_orm::DatabaseConnection, domain: String, days: i64) -> crate::Result<()> { - let ctx = Context::new(db, domain).await?; +pub async fn update_users(ctx: crate::server::Context, days: i64) -> crate::Result<()> { let mut count = 0; let mut insertions = Vec::new();