fix: update cli tasks to receive built context

This commit is contained in:
əlemi 2024-05-11 22:47:43 +02:00
parent 15746c699f
commit fc572f1c69
Signed by: alemi
GPG key ID: A4895B84D311642C
6 changed files with 28 additions and 26 deletions

View file

@ -2,14 +2,17 @@ use crate::model::{addressing, config, credential, activity, object, user, Audie
use openssl::rsa::Rsa; use openssl::rsa::Rsa;
use sea_orm::IntoActiveModel; 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}; use sea_orm::{EntityTrait, Set};
let domain = ctx.domain();
let db = ctx.db();
let key = Rsa::generate(2048).unwrap(); let key = Rsa::generate(2048).unwrap();
let test_user = user::Model { let test_user = user::Model {
id: format!("{domain}/users/test"), id: format!("{domain}/users/test"),
name: Some("μpub".into()), name: Some("μpub".into()),
domain: clean_domain(&domain), domain: clean_domain(domain),
preferred_username: "test".to_string(), 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()), 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, following: None,

View file

@ -2,12 +2,9 @@ use sea_orm::{EntityTrait, IntoActiveModel};
use crate::server::fetcher::Fetchable; 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; 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()); let mut node = apb::Node::link(uri.to_string());
node.fetch(&ctx).await?; node.fetch(&ctx).await?;

View file

@ -1,14 +1,15 @@
use sea_orm::EntityTrait; 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; use futures::TryStreamExt;
let db = ctx.db();
if likes { if likes {
tracing::info!("fixing likes..."); tracing::info!("fixing likes...");
let mut store = std::collections::HashMap::new(); 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? { while let Some(like) = stream.try_next().await? {
store.insert(like.likes.clone(), store.get(&like.likes).unwrap_or(&0) + 1); 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() ..Default::default()
}; };
if let Err(e) = crate::model::object::Entity::update(m) if let Err(e) = crate::model::object::Entity::update(m)
.exec(&db) .exec(db)
.await .await
{ {
tracing::warn!("record not updated ({k}): {e}"); 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..."); tracing::info!("fixing shares...");
let mut store = std::collections::HashMap::new(); 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? { while let Some(share) = stream.try_next().await? {
store.insert(share.shares.clone(), store.get(&share.shares).unwrap_or(&0) + 1); 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() ..Default::default()
}; };
if let Err(e) = crate::model::object::Entity::update(m) if let Err(e) = crate::model::object::Entity::update(m)
.exec(&db) .exec(db)
.await .await
{ {
tracing::warn!("record not updated ({k}): {e}"); 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..."); tracing::info!("fixing replies...");
let mut store = std::collections::HashMap::new(); 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? { while let Some(object) = stream.try_next().await? {
if let Some(reply) = object.in_reply_to { if let Some(reply) = object.in_reply_to {
let before = store.get(&reply).unwrap_or(&0); 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() ..Default::default()
}; };
if let Err(e) = crate::model::object::Entity::update(m) if let Err(e) = crate::model::object::Entity::update(m)
.exec(&db) .exec(db)
.await .await
{ {
tracing::warn!("record not updated ({k}): {e}"); tracing::warn!("record not updated ({k}): {e}");

View file

@ -10,8 +10,8 @@ pub use faker::*;
mod relay; mod relay;
pub use relay::*; pub use relay::*;
mod register; //mod register;
pub use register::*; //pub use register::*;
mod update; mod update;
pub use update::*; pub use update::*;
@ -71,17 +71,21 @@ pub async fn run(
command: CliCommand, command: CliCommand,
db: sea_orm::DatabaseConnection, db: sea_orm::DatabaseConnection,
domain: String, domain: String,
config: crate::config::Config,
) -> crate::Result<()> { ) -> crate::Result<()> {
let ctx = crate::server::Context::new(
db, domain, config,
).await?;
match command { match command {
CliCommand::Faker { count } => CliCommand::Faker { count } =>
Ok(faker(&db, domain, count).await?), Ok(faker(ctx, count).await?),
CliCommand::Fetch { uri, save } => CliCommand::Fetch { uri, save } =>
Ok(fetch(db, domain, uri, save).await?), Ok(fetch(ctx, uri, save).await?),
CliCommand::Relay { actor, accept } => CliCommand::Relay { actor, accept } =>
Ok(relay(db, domain, actor, accept).await?), Ok(relay(ctx, actor, accept).await?),
CliCommand::Fix { likes, shares, replies } => CliCommand::Fix { likes, shares, replies } =>
Ok(fix(db, likes, shares, replies).await?), Ok(fix(ctx, likes, shares, replies).await?),
CliCommand::Update { days } => CliCommand::Update { days } =>
Ok(update_users(db, domain, days).await?), Ok(update_users(ctx, days).await?),
} }
} }

View file

@ -1,8 +1,6 @@
use sea_orm::{ColumnTrait, EntityTrait, IntoActiveModel, QueryFilter, QueryOrder}; use sea_orm::{ColumnTrait, EntityTrait, IntoActiveModel, QueryFilter, QueryOrder};
pub async fn relay(db: sea_orm::DatabaseConnection, domain: String, actor: String, accept: bool) -> crate::Result<()> { pub async fn relay(ctx: crate::server::Context, actor: String, accept: bool) -> crate::Result<()> {
let ctx = crate::server::Context::new(db, domain).await?;
let aid = ctx.aid(uuid::Uuid::new_v4().to_string()); let aid = ctx.aid(uuid::Uuid::new_v4().to_string());
let mut activity_model = crate::model::activity::Model { let mut activity_model = crate::model::activity::Model {

View file

@ -1,10 +1,9 @@
use futures::TryStreamExt; use futures::TryStreamExt;
use sea_orm::{ColumnTrait, EntityTrait, IntoActiveModel, QueryFilter}; 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<()> { pub async fn update_users(ctx: crate::server::Context, days: i64) -> crate::Result<()> {
let ctx = Context::new(db, domain).await?;
let mut count = 0; let mut count = 0;
let mut insertions = Vec::new(); let mut insertions = Vec::new();