diff --git a/src/cli/faker.rs b/src/cli/faker.rs index ee0a591..4d9da60 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 b674995..0f1c4f7 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 027d0b7..9ad85d8 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 ecc576e..707ba1f 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 888853d..10c565a 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 7c6843c..d5318ce 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();