diff options
Diffstat (limited to 'src/entity')
-rw-r--r-- | src/entity/user.rs | 35 |
1 files changed, 32 insertions, 3 deletions
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<DbUser> for User { } impl User { - pub async fn new(username: &str, services: &Services) -> Result<Self, AppError> { + pub async fn new( + username: &str, + hostname: &str, + services: &Services, + environment: Environment, + ) -> Result<Self, AppError> { + 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"); |