summaryrefslogtreecommitdiffstats
path: root/crates/sellershut/src/entity/user.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/sellershut/src/entity/user.rs')
-rw-r--r--crates/sellershut/src/entity/user.rs91
1 files changed, 29 insertions, 62 deletions
diff --git a/crates/sellershut/src/entity/user.rs b/crates/sellershut/src/entity/user.rs
index 6fb12ae..5c9ac4f 100644
--- a/crates/sellershut/src/entity/user.rs
+++ b/crates/sellershut/src/entity/user.rs
@@ -7,27 +7,20 @@ use activitypub_federation::{
activity_sending::SendActivityTask,
config::Data,
fetch::object_id::ObjectId,
- http_signatures::generate_actor_keypair,
kinds::actor::{ApplicationType, GroupType, OrganizationType, PersonType, ServiceType},
protocol::{context::WithContext, public_key::PublicKey},
traits::{Activity, Actor, Object},
};
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
-use stack_up::Environment;
use time::OffsetDateTime;
use tracing::trace;
use url::Url;
-use uuid::Uuid;
-use crate::{
- error::AppError,
- state::{AppHandle, Services},
-};
+use crate::{error::AppError, state::AppHandle};
#[derive(PartialEq, Clone, Debug)]
pub struct User {
- pub id: String,
pub username: String,
pub ap_id: ObjectId<User>,
pub private_key: Option<String>,
@@ -67,6 +60,22 @@ pub enum UserType {
Service(ServiceType),
}
+impl From<sellershut_core::users::UserType> for UserType {
+ fn from(value: sellershut_core::users::UserType) -> Self {
+ match value {
+ sellershut_core::users::UserType::Person => Self::Person(PersonType::default()),
+ sellershut_core::users::UserType::Application => {
+ Self::Application(ApplicationType::default())
+ }
+ sellershut_core::users::UserType::Group => Self::Group(GroupType::default()),
+ sellershut_core::users::UserType::Organization => {
+ Self::Organization(OrganizationType::default())
+ }
+ sellershut_core::users::UserType::Service => Self::Service(ServiceType::default()),
+ }
+ }
+}
+
impl Display for UserType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
@@ -101,7 +110,6 @@ impl TryFrom<DbUser> for User {
type Error = AppError;
fn try_from(value: DbUser) -> Result<Self, Self::Error> {
Ok(Self {
- id: value.id,
username: value.username,
ap_id: Url::parse(&value.ap_id)?.into(),
private_key: value.private_key,
@@ -119,58 +127,18 @@ impl TryFrom<DbUser> for User {
}
impl User {
- 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!(
- "{}://{hostname}/users/{username}",
- match environment {
- Environment::Development => "http",
- Environment::Production => "https",
- }
- );
- let id = Uuid::now_v7();
-
- let kind = UserType::Service(ServiceType::Service);
-
- trace!(id = ?id, "creating a new user");
- let user = sqlx::query_as!(
- DbUser,
- "insert into account (id, username, ap_id, private_key, public_key, inbox, outbox, local, user_type) values ($1, $2, $3, $4, $5, $6, $7, $8, $9) returning *",
- id,
- username,
- stub,
- keys.private_key,
- keys.public_key,
- &format!("{stub}/inbox"),
- &format!("{stub}/outbox"),
- true,
- kind.to_string(),
- ).fetch_one(&services.postgres).await?;
- Self::try_from(user)
+ pub fn new(pk: &str, user: sellershut_core::users::User) -> Result<Self, AppError> {
+ Ok(Self {
+ username: user.username.clone(),
+ ap_id: Url::parse(&user.id)?.into(),
+ private_key: Some(pk.to_owned()),
+ description: user.description.clone(),
+ user_type: user.user_type().into(),
+ public_key: user.public_key,
+ inbox: Url::parse(&user.inbox)?,
+ outbox: Some(Url::parse(&user.outbox)?),
+ avatar_url: None,
+ })
}
pub(crate) async fn send<A>(
@@ -317,7 +285,6 @@ impl Object for User {
#[doc = " create and update, so an `upsert` operation should be used."]
async fn from_json(json: Self::Kind, data: &Data<Self::DataType>) -> Result<Self, Self::Error> {
Ok(Self {
- id: todo!(),
username: todo!(),
ap_id: todo!(),
private_key: todo!(),