Compare commits

...

6 commits

16 changed files with 192 additions and 56 deletions

View file

@ -1,5 +1,5 @@
[workspace]
members = ["apb", "web"]
members = ["apb", "web", "mdhtml"]
[package]
name = "upub"
@ -24,6 +24,8 @@ chrono = { version = "0.4", features = ["serde"] }
uuid = { version = "1.8", features = ["v4"] }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
serde_default = "0.1"
serde-inline-default = "0.2"
mdhtml = { path = "mdhtml", features = ["markdown"] }
jrd = "0.1"
tracing = "0.1"
@ -35,7 +37,7 @@ sea-orm = { version = "0.12", features = ["macros", "sqlx-sqlite", "runtime-toki
reqwest = { version = "0.12", features = ["json"] }
axum = "0.7"
tower-http = { version = "0.5", features = ["cors", "trace"] }
apb = { path = "apb", features = ["unstructured", "orm", "activitypub-fe", "activitypub-counters"] }
apb = { path = "apb", features = ["unstructured", "orm", "activitypub-fe", "activitypub-counters", "litepub"] }
# nodeinfo = "0.0.2" # the version on crates.io doesn't re-export necessary types to build the struct!!!
nodeinfo = { git = "https://codeberg.org/thefederationinfo/nodeinfo-rs", rev = "e865094804" }
# migrations
@ -44,6 +46,7 @@ sea-orm-migration = { version = "0.12", optional = true }
mastodon-async-entities = { version = "1.1.0", optional = true }
time = { version = "0.3", features = ["serde"], optional = true }
async-recursion = "1.1"
toml = "0.8.12"
[features]
default = ["migrations", "cli"]

View file

@ -29,6 +29,7 @@ default = ["activitypub-miscellaneous-terms"]
activitypub-miscellaneous-terms = [] # https://swicg.github.io/miscellany/
activitypub-counters = [] # https://ns.alemi.dev/as/counters/#
activitypub-fe = [] # https://ns.alemi.dev/as/fe/#
litepub = [] # incomplete, https://litepub.social/
# builtin utils
orm = ["dep:sea-orm"]
fetch = ["dep:reqwest"]

View file

@ -11,6 +11,38 @@ use offer::OfferType;
use intransitive::IntransitiveActivityType;
use ignore::IgnoreType;
#[cfg(feature = "litepub")]
crate::strenum! {
pub enum ActivityType {
Activity,
Add,
Announce,
Create,
Delete,
Dislike,
EmojiReact,
Flag,
Follow,
Join,
Leave,
Like,
Listen,
Move,
Read,
Remove,
Undo,
Update,
View;
IntransitiveActivity(IntransitiveActivityType),
Accept(AcceptType),
Ignore(IgnoreType),
Offer(OfferType),
Reject(RejectType)
};
}
#[cfg(not(feature = "litepub"))]
crate::strenum! {
pub enum ActivityType {
Activity,

View file

@ -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,

View file

@ -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?;

View file

@ -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}");

View file

@ -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?),
}
}

View file

@ -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 {

View file

@ -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();

74
src/config.rs Normal file
View file

@ -0,0 +1,74 @@
#[serde_inline_default::serde_inline_default]
#[derive(Debug, Clone, serde::Deserialize, serde::Serialize, serde_default::DefaultFromSerde)]
pub struct Config {
#[serde(default)]
pub instance: InstanceConfig,
#[serde(default)]
pub datasource: DatasourceConfig,
// TODO should i move app keys here?
}
#[serde_inline_default::serde_inline_default]
#[derive(Debug, Clone, serde::Deserialize, serde::Serialize, serde_default::DefaultFromSerde)]
pub struct DatasourceConfig {
#[serde_inline_default("sqlite://./upub.db".into())]
pub connection_string: String,
#[serde_inline_default(4)]
pub max_connections: u32,
#[serde_inline_default(1)]
pub min_connections: u32,
#[serde_inline_default(300u64)]
pub connect_timeout_seconds: u64,
#[serde_inline_default(300u64)]
pub acquire_timeout_seconds: u64,
#[serde_inline_default(1u64)]
pub slow_query_warn_seconds: u64,
#[serde_inline_default(true)]
pub slow_query_warn_enable: bool,
}
#[serde_inline_default::serde_inline_default]
#[derive(Debug, Clone, serde::Deserialize, serde::Serialize, serde_default::DefaultFromSerde)]
pub struct InstanceConfig {
#[serde_inline_default("μpub".into())]
pub name: String,
#[serde_inline_default("micro social network, federated".into())]
pub description: String,
#[serde_inline_default("upub.social".into())]
pub domain: String,
#[serde(default)]
pub contact: Option<String>,
#[serde(default)]
pub frontend: Option<String>,
}
impl Config {
pub fn load(path: Option<std::path::PathBuf>) -> Self {
let Some(cfg_path) = path else { return Config::default() };
match std::fs::read_to_string(cfg_path) {
Ok(x) => match toml::from_str(&x) {
Ok(cfg) => return cfg,
Err(e) => tracing::error!("failed parsing config file: {e}"),
},
Err(e) => tracing::error!("failed reading config file: {e}"),
}
Config::default()
}
}

View file

@ -1,4 +1,4 @@
use axum::{http::StatusCode, response::{IntoResponse, Redirect}};
use axum::{http::StatusCode, response::Redirect};
#[derive(Debug, thiserror::Error)]
pub enum UpubError {
@ -79,12 +79,7 @@ impl axum::response::IntoResponse for UpubError {
UpubError::Redirect(to) => Redirect::to(&to).into_response(),
UpubError::Status(status) => (status, descr).into_response(),
UpubError::Database(_) => (StatusCode::SERVICE_UNAVAILABLE, descr).into_response(),
UpubError::FetchError(e, body) =>
(
e.status().unwrap_or(StatusCode::INTERNAL_SERVER_ERROR),
format!("failed fetching '{}': {descr} -- server responded with {body}", e.url().map(|x| x.to_string()).unwrap_or_default()),
).into_response(),
UpubError::Reqwest(x) =>
UpubError::Reqwest(x) | UpubError::FetchError(x, _) =>
(
x.status().unwrap_or(StatusCode::INTERNAL_SERVER_ERROR),
format!("failed fetching '{}': {descr}", x.url().map(|x| x.to_string()).unwrap_or_default())

View file

@ -3,7 +3,6 @@ mod model;
mod routes;
mod errors;
mod config;
#[cfg(feature = "cli")]
@ -14,6 +13,8 @@ mod migrations;
#[cfg(feature = "migrations")]
use sea_orm_migration::MigratorTrait;
use std::path::PathBuf;
use config::Config;
use clap::{Parser, Subcommand};
use sea_orm::{ConnectOptions, Database};
@ -30,13 +31,17 @@ struct Args {
/// command to run
command: Mode,
#[arg(short = 'd', long = "db", default_value = "sqlite://./upub.db")]
/// database connection uri
database: String,
/// path to config file, leave empty to not use any
#[arg(short, long)]
config: Option<PathBuf>,
#[arg(short = 'D', long, default_value = "http://localhost:3000")]
/// instance base domain, for AP ids
domain: String,
#[arg(long = "db")]
/// database connection uri, overrides config value
database: Option<String>,
#[arg(long)]
/// instance base domain, for AP ids, overrides config value
domain: Option<String>,
#[arg(long, default_value_t=false)]
/// run with debug level tracing
@ -46,7 +51,10 @@ struct Args {
#[derive(Clone, Subcommand)]
enum Mode {
/// run fediverse server
Serve ,
Serve,
/// print current or default configuration
Config,
#[cfg(feature = "migrations")]
/// apply database migrations
@ -71,11 +79,24 @@ async fn main() {
.with_max_level(if args.debug { tracing::Level::DEBUG } else { tracing::Level::INFO })
.init();
let config = Config::load(args.config);
let database = args.database.unwrap_or(config.datasource.connection_string.clone());
let domain = args.domain.unwrap_or(config.instance.domain.clone());
// TODO can i do connectoptions.into() or .connect() and skip these ugly bindings?
let mut opts = ConnectOptions::new(&args.database);
let mut opts = ConnectOptions::new(&database);
opts
.sqlx_logging_level(tracing::log::LevelFilter::Debug);
.sqlx_logging_level(tracing::log::LevelFilter::Debug)
.max_connections(config.datasource.max_connections)
.min_connections(config.datasource.min_connections)
.acquire_timeout(std::time::Duration::from_secs(config.datasource.acquire_timeout_seconds))
.connect_timeout(std::time::Duration::from_secs(config.datasource.connect_timeout_seconds))
.sqlx_slow_statements_logging_settings(
if config.datasource.slow_query_warn_enable { tracing::log::LevelFilter::Warn } else { tracing::log::LevelFilter::Off },
std::time::Duration::from_secs(config.datasource.slow_query_warn_seconds)
);
let db = Database::connect(opts)
.await.expect("error connecting to db");
@ -88,11 +109,13 @@ async fn main() {
#[cfg(feature = "cli")]
Mode::Cli { command } =>
cli::run(command, db, args.domain)
cli::run(command, db, domain, config)
.await.expect("failed running cli task"),
Mode::Config => println!("{}", toml::to_string_pretty(&config).expect("failed serializing config")),
Mode::Serve => {
let ctx = server::Context::new(db, args.domain)
let ctx = server::Context::new(db, domain, config)
.await.expect("failed creating server context");
use routes::activitypub::ActivityPubRouter;

View file

@ -22,8 +22,8 @@ pub async fn view(
serde_json::Value::new_object()
.set_id(Some(&url!(ctx, "")))
.set_actor_type(Some(apb::ActorType::Application))
.set_name(Some("μpub"))
.set_summary(Some("micro social network, federated"))
.set_name(Some(&ctx.cfg().instance.name))
.set_summary(Some(&ctx.cfg().instance.description))
.set_inbox(apb::Node::link(url!(ctx, "/inbox")))
.set_outbox(apb::Node::link(url!(ctx, "/outbox")))
.set_published(Some(ctx.app().created))

View file

@ -73,8 +73,9 @@ pub async fn post(
Err(StatusCode::UNPROCESSABLE_ENTITY.into()) // won't ingest useless stuff
},
// TODO emojireacts are NOT likes, but let's process them like ones for now maybe?
ActivityType::Like | ActivityType::EmojiReact => Ok(ctx.like(server, activity).await?),
ActivityType::Create => Ok(ctx.create(server, activity).await?),
ActivityType::Like => Ok(ctx.like(server, activity).await?),
ActivityType::Follow => Ok(ctx.follow(server, activity).await?),
ActivityType::Announce => Ok(ctx.announce(server, activity).await?),
ActivityType::Accept(_) => Ok(ctx.accept(server, activity).await?),

View file

@ -8,9 +8,9 @@ pub async fn get(
) -> crate::Result<Json<mastodon_async_entities::instance::Instance>> {
Ok(Json(mastodon_async_entities::instance::Instance {
uri: ctx.domain().to_string(),
title: "μpub".to_string(),
description: "micro social network, federated".to_string(),
email: "me@alemi.dev".to_string(),
title: ctx.cfg().instance.name.clone(),
description: ctx.cfg().instance.description.clone(),
email: ctx.cfg().instance.contact.as_deref().unwrap_or_default().to_string(),
version: crate::VERSION.to_string(),
urls: None,
stats: None,

View file

@ -3,7 +3,7 @@ use std::{collections::BTreeSet, sync::Arc};
use openssl::rsa::Rsa;
use sea_orm::{ColumnTrait, DatabaseConnection, EntityTrait, QueryFilter, QuerySelect, SelectColumns, Set};
use crate::{model, server::fetcher::Fetcher};
use crate::{config::Config, model, server::fetcher::Fetcher};
use super::dispatcher::Dispatcher;
@ -12,6 +12,7 @@ use super::dispatcher::Dispatcher;
pub struct Context(Arc<ContextInner>);
struct ContextInner {
db: DatabaseConnection,
config: Config,
domain: String,
protocol: String,
dispatcher: Dispatcher,
@ -30,7 +31,7 @@ macro_rules! url {
impl Context {
// TODO slim constructor down, maybe make a builder?
pub async fn new(db: DatabaseConnection, mut domain: String) -> crate::Result<Self> {
pub async fn new(db: DatabaseConnection, mut domain: String, config: Config) -> crate::Result<Self> {
let protocol = if domain.starts_with("http://")
{ "http://" } else { "https://" }.to_string();
if domain.ends_with('/') {
@ -71,7 +72,7 @@ impl Context {
.await?;
Ok(Context(Arc::new(ContextInner {
db, domain, protocol, app, dispatcher,
db, domain, protocol, app, dispatcher, config,
relays: BTreeSet::from_iter(relays.into_iter()),
})))
}
@ -84,6 +85,10 @@ impl Context {
&self.0.db
}
pub fn cfg(&self) -> &Config {
&self.0.config
}
pub fn domain(&self) -> &str {
&self.0.domain
}