2023-12-30 05:08:05 +01:00
|
|
|
pub mod model;
|
2024-03-16 03:29:06 +01:00
|
|
|
pub mod migrations;
|
2024-03-14 05:27:08 +01:00
|
|
|
pub mod activitystream;
|
2024-03-20 05:44:10 +01:00
|
|
|
pub mod activitypub;
|
2024-03-15 19:43:29 +01:00
|
|
|
pub mod server;
|
2023-12-30 05:08:05 +01:00
|
|
|
|
2024-03-16 03:29:06 +01:00
|
|
|
use clap::{Parser, Subcommand};
|
2024-03-19 07:39:12 +01:00
|
|
|
use sea_orm::{ConnectOptions, Database, EntityTrait, IntoActiveModel};
|
2024-03-16 03:29:06 +01:00
|
|
|
use sea_orm_migration::MigratorTrait;
|
|
|
|
|
2024-03-19 07:39:12 +01:00
|
|
|
use crate::activitystream::{BaseType, ObjectType};
|
|
|
|
|
2024-03-16 03:29:06 +01:00
|
|
|
#[derive(Parser)]
|
|
|
|
/// all names were taken
|
|
|
|
struct CliArgs {
|
|
|
|
#[clap(subcommand)]
|
|
|
|
/// command to run
|
|
|
|
command: CliCommand,
|
|
|
|
|
2024-03-16 06:06:31 +01:00
|
|
|
#[arg(short, long, default_value = "sqlite://./upub.db")]
|
2024-03-16 03:29:06 +01:00
|
|
|
/// database connection uri
|
|
|
|
database: String,
|
|
|
|
|
|
|
|
#[arg(long, default_value_t=false)]
|
|
|
|
/// run with debug level tracing
|
|
|
|
debug: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Subcommand)]
|
|
|
|
enum CliCommand {
|
|
|
|
/// run fediverse server
|
|
|
|
Serve ,
|
|
|
|
|
|
|
|
/// apply database migrations
|
|
|
|
Migrate,
|
2024-03-16 05:46:14 +01:00
|
|
|
|
|
|
|
/// generate fake user, note and activity
|
|
|
|
Faker,
|
2024-03-19 07:39:12 +01:00
|
|
|
|
|
|
|
/// fetch a single AP object
|
|
|
|
Fetch {
|
|
|
|
/// object id, or uri, to fetch
|
|
|
|
uri: String,
|
|
|
|
|
|
|
|
#[arg(long, default_value_t = false)]
|
|
|
|
/// store fetched object in local db
|
|
|
|
save: bool,
|
|
|
|
},
|
2024-03-16 03:29:06 +01:00
|
|
|
}
|
2024-02-09 17:07:55 +01:00
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
async fn main() {
|
2024-03-15 19:43:29 +01:00
|
|
|
|
2024-03-16 03:29:06 +01:00
|
|
|
let args = CliArgs::parse();
|
|
|
|
|
|
|
|
tracing_subscriber::fmt()
|
|
|
|
.compact()
|
|
|
|
.with_max_level(if args.debug { tracing::Level::DEBUG } else { tracing::Level::INFO })
|
|
|
|
.init();
|
|
|
|
|
2024-03-16 05:45:58 +01:00
|
|
|
let mut opts = ConnectOptions::new(&args.database);
|
|
|
|
opts
|
|
|
|
.max_connections(1);
|
|
|
|
|
|
|
|
let db = Database::connect(opts)
|
2024-03-16 03:29:06 +01:00
|
|
|
.await.expect("error connecting to db");
|
|
|
|
|
|
|
|
match args.command {
|
|
|
|
CliCommand::Serve => server::serve(db)
|
|
|
|
.await,
|
|
|
|
|
|
|
|
CliCommand::Migrate => migrations::Migrator::up(&db, None)
|
|
|
|
.await.expect("error applying migrations"),
|
2024-03-16 05:46:14 +01:00
|
|
|
|
|
|
|
CliCommand::Faker => model::faker(&db)
|
|
|
|
.await.expect("error creating fake entities"),
|
2024-03-19 07:39:12 +01:00
|
|
|
|
|
|
|
CliCommand::Fetch { uri, save } => fetch(&db, &uri, save)
|
|
|
|
.await.expect("error fetching object"),
|
2024-03-15 19:43:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-03-19 07:39:12 +01:00
|
|
|
|
|
|
|
async fn fetch(db: &sea_orm::DatabaseConnection, uri: &str, save: bool) -> reqwest::Result<()> {
|
|
|
|
use crate::activitystream::{Base, Object};
|
|
|
|
|
|
|
|
let mut node = activitystream::Node::from(uri);
|
|
|
|
tracing::info!("fetching object");
|
|
|
|
node.fetch().await?;
|
|
|
|
tracing::info!("fetched node");
|
|
|
|
|
|
|
|
let obj = node.get().expect("node still empty after fetch?");
|
|
|
|
|
|
|
|
tracing::info!("fetched object:{}, name:{}", obj.id().unwrap_or(""), obj.name().unwrap_or(""));
|
|
|
|
|
|
|
|
if save {
|
|
|
|
match obj.base_type() {
|
|
|
|
Some(BaseType::Object(ObjectType::Actor(_))) => {
|
|
|
|
model::user::Entity::insert(
|
|
|
|
model::user::Model::new(obj).unwrap().into_active_model()
|
|
|
|
).exec(db).await.unwrap();
|
|
|
|
},
|
|
|
|
Some(BaseType::Object(ObjectType::Activity(_))) => {
|
|
|
|
model::activity::Entity::insert(
|
|
|
|
model::activity::Model::new(obj).unwrap().into_active_model()
|
|
|
|
).exec(db).await.unwrap();
|
|
|
|
},
|
|
|
|
Some(BaseType::Object(ObjectType::Note)) => {
|
|
|
|
model::object::Entity::insert(
|
|
|
|
model::object::Model::new(obj).unwrap().into_active_model()
|
|
|
|
).exec(db).await.unwrap();
|
|
|
|
},
|
|
|
|
Some(BaseType::Object(t)) => tracing::warn!("not implemented: {:?}", t),
|
|
|
|
Some(BaseType::Link(_)) => tracing::error!("fetched another link?"),
|
|
|
|
None => tracing::error!("no type on object"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|