From 096630708d27bca324cc83f1a830d4b9bbbb7917 Mon Sep 17 00:00:00 2001 From: rtkay123 Date: Tue, 15 Jul 2025 08:56:52 +0200 Subject: feat: read identifiers from config --- src/entity/user.rs | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) (limited to 'src/entity/user.rs') diff --git a/src/entity/user.rs b/src/entity/user.rs index da22f00..aa1207d 100644 --- a/src/entity/user.rs +++ b/src/entity/user.rs @@ -8,7 +8,7 @@ use activitypub_federation::{ }; use async_trait::async_trait; use serde::{Deserialize, Serialize}; -use stack_up::Services; +use stack_up::{Environment, Services}; use tracing::trace; use url::Url; use uuid::Uuid; @@ -56,10 +56,39 @@ impl TryFrom for User { } impl User { - pub async fn new(username: &str, services: &Services) -> Result { + pub async fn new( + username: &str, + hostname: &str, + services: &Services, + environment: Environment, + ) -> Result { + trace!(username = ?username, "checking for system user"); + + let user = sqlx::query_as!( + DbUser, + "select * from account where username = $1 and local = $2", + username, + true + ) + .fetch_optional(&services.postgres) + .await?; + + if let Some(user) = user { + trace!(username = ?username, "system user exists"); + return Self::try_from(user); + } else { + trace!(username = ?username, "system user does not exist. creating"); + } + trace!("creating keypair for new user"); let keys = generate_actor_keypair()?; - let stub = &format!("http://localhost/users/{username}"); + let stub = &format!( + "{}://{hostname}/users/{username}", + match environment { + Environment::Development => "http", + Environment::Production => "https", + } + ); let id = Uuid::now_v7(); trace!(id = ?id, "creating a new user"); -- cgit v1.2.3