Add support for querying NIP-05 on /.well-known/nostr.json?name=username

This commit is contained in:
Tony Klink 2024-01-30 11:43:03 -06:00
parent e1306608ef
commit 377da44eed
Signed by: klink
GPG key ID: 85175567C4D19231
12 changed files with 278 additions and 101 deletions

View file

@ -36,7 +36,7 @@ pub struct Nip05 {
#[derive(Serialize, Deserialize, Debug, Validate, Clone)]
pub struct UserQuery {
#[validate(length(min = 1))]
pub user: String,
pub name: String,
}
#[derive(Serialize, Deserialize, Debug, Clone, Validate)]

View file

@ -45,14 +45,9 @@ pub async fn get_account(
}
pub async fn get_user(user_query: UserQuery, context: Context) -> Result<impl Reply, Rejection> {
let name = user_query.user;
let mut subscriber = context.pubsub.subscribe(channels::MSG_NIP05).await;
let user = User {
name: Some(name),
pubkey: None,
};
let command = Command::DbReqGetUser(user);
let command = Command::DbReqGetUser(user_query.name);
context
.pubsub
.publish(
@ -65,18 +60,12 @@ pub async fn get_user(user_query: UserQuery, context: Context) -> Result<impl Re
.await;
if let Ok(message) = subscriber.recv().await {
let mut response = json!({"names": {}, "relays": {}});
match message.content {
Command::DbResUser(user) => {
response = json!({
"names": {
user.username: user.pubkey
},
"relays": {}
});
Command::DbResUser(profile) => {
let response = serde_json::to_value(profile).unwrap();
Ok(warp::reply::json(&response))
}
Command::ServiceError(e) => Ok(warp::reply::json(&response)),
Command::ServiceError(e) => Err(warp::reject::custom(e)),
_ => Err(warp::reject::custom(Error::internal_with_message(
"Unhandeled message type",
))),

View file

@ -1,4 +1,4 @@
mod accounts;
// mod accounts;
pub mod dto;
mod filter;
mod handler;

View file

@ -1,6 +1,6 @@
use crate::noose::user::User;
use super::accounts::create_account;
// use super::accounts::create_account;
use super::dto::{Account, UserQuery};
use super::filter::{validate_body_filter, validate_query_filter};
use super::handler::{get_account, get_user};
@ -9,41 +9,49 @@ use crate::utils::structs::Context;
use warp::{Filter, Rejection, Reply};
pub fn routes(context: Context) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
let cors = warp::cors().allow_any_origin();
let index = warp::path::end().map(|| warp::reply::html("<h1>SNEED!</h1>"));
index
.or(nip05_get(context.clone()))
.or(account_create(context.clone()))
// .or(account_create(context.clone()))
.with(&cors)
}
fn well_known() -> impl Filter<Extract = (), Error = Rejection> + Clone {
warp::get().and(warp::path(".well-known"))
}
fn nostr_well_known() -> impl Filter<Extract = (), Error = Rejection> + Clone {
well_known().and(warp::path("nostr.json"))
}
pub fn nip05_get(context: Context) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
warp::get()
.and(warp::path(".well-known"))
.and(warp::path("nostr.json"))
nostr_well_known()
.and(validate_query_filter::<UserQuery>())
.and(with_context(context))
.and(with_context(context.clone()))
.and_then(get_user)
}
pub fn account_create(
context: Context,
) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
warp::path("account")
.and(warp::post())
.and(validate_body_filter::<User>())
.and(with_context(context))
.and_then(create_account)
}
// pub fn account_create(
// context: Context,
// ) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
// warp::path("account")
// .and(warp::post())
// .and(validate_body_filter::<User>())
// .and(with_context(context))
// .and_then(create_account)
// }
pub fn account_get(
context: Context,
) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
warp::path("account")
.and(warp::get())
.and(validate_body_filter::<Account>())
.and(with_context(context))
.and_then(get_account)
}
// pub fn account_get(
// context: Context,
// ) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
// warp::path("account")
// .and(warp::get())
// .and(validate_body_filter::<Account>())
// .and(with_context(context))
// .and_then(get_account)
// }
// pub fn account_update(
// context: Context,