feat: nostr domain hosting
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2025-04-03 12:56:20 +01:00
parent a4850b4e06
commit c432f603ec
32 changed files with 724 additions and 70 deletions

View File

@ -6,6 +6,7 @@ edition = "2021"
[features]
default = ["mysql"]
mysql = ["sqlx/mysql"]
nostr-domain = []
[dependencies]
anyhow.workspace = true

View File

@ -0,0 +1,25 @@
-- Add migration script here
create table nostr_domain
(
id integer unsigned not null auto_increment primary key,
owner_id integer unsigned not null,
name varchar(200) not null,
enabled bit(1) not null default 0,
created timestamp not null default current_timestamp,
relays varchar(1024),
unique key ix_domain_unique (name),
constraint fk_nostr_domain_user foreign key (owner_id) references users (id)
);
create table nostr_domain_handle
(
id integer unsigned not null auto_increment primary key,
domain_id integer unsigned not null,
handle varchar(100) not null,
created timestamp not null default current_timestamp,
pubkey binary(32) not null,
relays varchar(1024),
unique key ix_domain_handle_unique (domain_id, handle),
constraint fk_nostr_domain_handle_domain foreign key (domain_id) references nostr_domain (id) on delete cascade
)

View File

@ -10,7 +10,7 @@ pub use mysql::*;
pub use async_trait::async_trait;
#[async_trait]
pub trait LNVpsDb: Sync + Send {
pub trait LNVpsDb: LNVPSNostrDb + Send + Sync {
/// Migrate database
async fn migrate(&self) -> Result<()>;
@ -173,3 +173,40 @@ pub trait LNVpsDb: Sync + Send {
/// Get access policy
async fn get_access_policy(&self, access_policy_id: u64) -> Result<AccessPolicy>;
}
#[cfg(feature = "nostr-domain")]
#[async_trait]
pub trait LNVPSNostrDb: Sync + Send {
/// Get single handle for a domain
async fn get_handle(&self, handle_id: u64) -> Result<NostrDomainHandle>;
/// Get single handle for a domain
async fn get_handle_by_name(&self, domain_id: u64, handle: &str) -> Result<NostrDomainHandle>;
/// Insert a new handle
async fn insert_handle(&self, handle: &NostrDomainHandle) -> Result<u64>;
/// Update an existing domain handle
async fn update_handle(&self, handle: &NostrDomainHandle) -> Result<()>;
/// Delete handle entry
async fn delete_handle(&self, handle_id: u64) -> Result<()>;
/// List handles
async fn list_handles(&self, domain_id: u64) -> Result<Vec<NostrDomainHandle>>;
/// Get domain object by id
async fn get_domain(&self, id: u64) -> Result<NostrDomain>;
/// Get domain object by name
async fn get_domain_by_name(&self, name: &str) -> Result<NostrDomain>;
/// List domains owned by a user
async fn list_domains(&self, owner_id: u64) -> Result<Vec<NostrDomain>>;
/// Insert a new domain
async fn insert_domain(&self, domain: &NostrDomain) -> Result<u64>;
/// Delete a domain
async fn delete_domain(&self, domain_id: u64) -> Result<()>;
}

View File

@ -489,3 +489,24 @@ impl FromStr for PaymentMethod {
}
}
}
#[derive(FromRow, Clone, Debug, Default)]
pub struct NostrDomain {
pub id: u64,
pub owner_id: u64,
pub name: String,
pub created: DateTime<Utc>,
pub enabled: bool,
pub relays: Option<String>,
pub handles: i64,
}
#[derive(FromRow, Clone, Debug, Default)]
pub struct NostrDomainHandle {
pub id: u64,
pub domain_id: u64,
pub handle: String,
pub created: DateTime<Utc>,
pub pubkey: Vec<u8>,
pub relays: Option<String>,
}

View File

@ -1,7 +1,7 @@
use crate::{
AccessPolicy, IpRange, LNVpsDb, Router, User, UserSshKey, Vm, VmCostPlan, VmCustomPricing,
VmCustomPricingDisk, VmCustomTemplate, VmHost, VmHostDisk, VmHostRegion, VmIpAssignment,
VmOsImage, VmPayment, VmTemplate,
AccessPolicy, IpRange, LNVPSNostrDb, LNVpsDb, NostrDomain, NostrDomainHandle, Router, User,
UserSshKey, Vm, VmCostPlan, VmCustomPricing, VmCustomPricingDisk, VmCustomTemplate, VmHost,
VmHostDisk, VmHostRegion, VmIpAssignment, VmOsImage, VmPayment, VmTemplate,
};
use anyhow::{bail, Error, Result};
use async_trait::async_trait;
@ -556,3 +556,115 @@ impl LNVpsDb for LNVpsDbMysql {
.map_err(Error::new)
}
}
#[cfg(feature = "nostr-domain")]
#[async_trait]
impl LNVPSNostrDb for LNVpsDbMysql {
async fn get_handle(&self, handle_id: u64) -> Result<NostrDomainHandle> {
sqlx::query_as("select * from nostr_domain_handle where id=?")
.bind(handle_id)
.fetch_one(&self.db)
.await
.map_err(Error::new)
}
async fn get_handle_by_name(&self, domain_id: u64, handle: &str) -> Result<NostrDomainHandle> {
sqlx::query_as("select * from nostr_domain_handle where domain_id=? and handle=?")
.bind(domain_id)
.bind(handle)
.fetch_one(&self.db)
.await
.map_err(Error::new)
}
async fn insert_handle(&self, handle: &NostrDomainHandle) -> Result<u64> {
Ok(
sqlx::query(
"insert into nostr_domain_handle(domain_id,handle,pubkey,relays) values(?,?,?,?) returning id",
)
.bind(handle.domain_id)
.bind(&handle.handle)
.bind(&handle.pubkey)
.bind(&handle.relays)
.fetch_one(&self.db)
.await
.map_err(Error::new)?
.try_get(0)?,
)
}
async fn update_handle(&self, handle: &NostrDomainHandle) -> Result<()> {
sqlx::query("update nostr_domain_handle set handle=?,pubkey=?,relays=? where id=?")
.bind(&handle.handle)
.bind(&handle.pubkey)
.bind(&handle.relays)
.bind(handle.id)
.execute(&self.db)
.await?;
Ok(())
}
async fn delete_handle(&self, handle_id: u64) -> Result<()> {
sqlx::query("delete from nostr_domain_handle where id=?")
.bind(handle_id)
.execute(&self.db)
.await?;
Ok(())
}
async fn list_handles(&self, domain_id: u64) -> Result<Vec<NostrDomainHandle>> {
sqlx::query_as("select * from nostr_domain_handle where domain_id=?")
.bind(domain_id)
.fetch_all(&self.db)
.await
.map_err(Error::new)
}
async fn get_domain(&self, id: u64) -> Result<NostrDomain> {
sqlx::query_as("select *,(select count(1) from nostr_domain_handle where domain_id=nostr_domain.id) handles from nostr_domain where id=?")
.bind(id)
.fetch_one(&self.db)
.await
.map_err(Error::new)
}
async fn get_domain_by_name(&self, name: &str) -> Result<NostrDomain> {
sqlx::query_as("select *,(select count(1) from nostr_domain_handle where domain_id=nostr_domain.id) handles from nostr_domain where name=?")
.bind(name)
.fetch_one(&self.db)
.await
.map_err(Error::new)
}
async fn list_domains(&self, owner_id: u64) -> Result<Vec<NostrDomain>> {
sqlx::query_as("select *,(select count(1) from nostr_domain_handle where domain_id=nostr_domain.id) handles from nostr_domain where owner_id=?")
.bind(owner_id)
.fetch_all(&self.db)
.await
.map_err(Error::new)
}
async fn insert_domain(&self, domain: &NostrDomain) -> Result<u64> {
Ok(
sqlx::query(
"insert into nostr_domain(owner_id,name,relays) values(?,?,?) returning id",
)
.bind(domain.owner_id)
.bind(&domain.name)
.bind(&domain.relays)
.fetch_one(&self.db)
.await
.map_err(Error::new)?
.try_get(0)?,
)
}
async fn delete_domain(&self, domain_id: u64) -> Result<()> {
sqlx::query("update nostr_domain set deleted = current_timestamp where id = ?")
.bind(domain_id)
.fetch_one(&self.db)
.await
.map_err(Error::new)?;
Ok(())
}
}