diff options
author | rtkay123 <dev@kanjala.com> | 2025-07-15 08:56:52 +0200 |
---|---|---|
committer | rtkay123 <dev@kanjala.com> | 2025-07-15 08:56:52 +0200 |
commit | 096630708d27bca324cc83f1a830d4b9bbbb7917 (patch) | |
tree | 3a2a2a6ea25502cf32b9179c2d1a4a12852de305 /src/entity/user.rs | |
parent | a69c24e561c8ae16dc730f7713f8d8da0bd25e0e (diff) | |
download | sellershut-096630708d27bca324cc83f1a830d4b9bbbb7917.tar.bz2 sellershut-096630708d27bca324cc83f1a830d4b9bbbb7917.zip |
feat: read identifiers from config
Diffstat (limited to 'src/entity/user.rs')
-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"); |