sneedstr/src/usernames/accounts.rs
2024-01-12 09:35:31 -06:00

63 lines
1.8 KiB
Rust

use crate::noose::user::{User, UserRow};
use crate::bussy::{channels, Command, Message};
use crate::usernames::util::InvalidParameter;
use crate::utils::error::Error;
use warp::{Rejection, Reply};
use super::Context;
pub async fn create_account(user: User, context: Context) -> Result<impl Reply, Rejection> {
let mut subscriber = context.pubsub.subscribe(channels::MSG_NIP05).await;
let pubkey = match user.pubkey {
Some(pk) => {
use nostr::prelude::FromPkStr;
let keys = nostr::Keys::from_pk_str(&pk).unwrap();
keys.public_key().to_string()
}
None => {
return Err(warp::reject::custom(Error::bad_request(
"Pubkey is required",
)))
}
};
let name = match user.name {
Some(n) => n,
None => return Err(warp::reject::custom(Error::bad_request("Name is required"))),
};
let user_row = UserRow::new(pubkey, name, false);
let command = Command::DbReqInsertUser(user_row);
context
.pubsub
.publish(
channels::MSG_NOOSE,
Message {
source: channels::MSG_NIP05,
content: command,
},
)
.await;
if let Ok(result) = subscriber.recv().await {
match result.content {
Command::DbResOk => Ok(warp::reply::with_status(
"ACCOUNT CREATED",
warp::http::StatusCode::CREATED,
)),
Command::ServiceError(e) => Err(warp::reject::custom(e)),
// _ => Err(warp::reject::custom(InvalidParameter)),
_ => Ok(warp::reply::with_status(
"something else",
warp::http::StatusCode::INTERNAL_SERVER_ERROR,
)),
}
} else {
Err(warp::reject::custom(InvalidParameter))
}
}