feat: NIP17 dms
This commit is contained in:
@ -12,6 +12,8 @@ use lnvps::status::VmStateCache;
|
||||
use lnvps::worker::{WorkJob, Worker};
|
||||
use lnvps_db::{LNVpsDb, LNVpsDbMysql};
|
||||
use log::error;
|
||||
use nostr::Keys;
|
||||
use nostr_sdk::Client;
|
||||
use std::net::{IpAddr, SocketAddr};
|
||||
use std::time::Duration;
|
||||
|
||||
@ -50,6 +52,16 @@ async fn main() -> Result<(), Error> {
|
||||
db.execute(setup_script).await?;
|
||||
}
|
||||
|
||||
let nostr_client = if let Some(ref c) = settings.nostr {
|
||||
let cx = Client::builder().signer(Keys::parse(&c.nsec)?).build();
|
||||
for r in &c.relays {
|
||||
cx.add_relay(r.clone()).await?;
|
||||
}
|
||||
cx.connect().await;
|
||||
Some(cx)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let router = settings.router.as_ref().map(|r| r.get_router());
|
||||
let status = VmStateCache::new();
|
||||
let worker_provisioner =
|
||||
@ -58,7 +70,13 @@ async fn main() -> Result<(), Error> {
|
||||
.get_provisioner(db.clone(), router, lnd.clone(), exchange.clone());
|
||||
worker_provisioner.init().await?;
|
||||
|
||||
let mut worker = Worker::new(db.clone(), worker_provisioner, &settings, status.clone());
|
||||
let mut worker = Worker::new(
|
||||
db.clone(),
|
||||
worker_provisioner,
|
||||
&settings,
|
||||
status.clone(),
|
||||
nostr_client.clone(),
|
||||
);
|
||||
let sender = worker.sender();
|
||||
|
||||
// send a startup notification
|
||||
|
@ -24,6 +24,9 @@ pub struct Settings {
|
||||
|
||||
/// Network router config
|
||||
pub router: Option<RouterConfig>,
|
||||
|
||||
/// Nostr config for sending DM's
|
||||
pub nostr: Option<NostrConfig>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||
@ -33,6 +36,12 @@ pub struct LndConfig {
|
||||
pub macaroon: PathBuf,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||
pub struct NostrConfig {
|
||||
pub relays: Vec<String>,
|
||||
pub nsec: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||
pub enum RouterConfig {
|
||||
Mikrotik {
|
||||
|
@ -8,10 +8,11 @@ use chrono::{Days, Utc};
|
||||
use lettre::message::{MessageBuilder, MultiPart};
|
||||
use lettre::transport::smtp::authentication::Credentials;
|
||||
use lettre::AsyncTransport;
|
||||
use lettre::{AsyncSmtpTransport, Tokio1Executor, Transport};
|
||||
use lettre::{AsyncSmtpTransport, Tokio1Executor};
|
||||
use lnvps_db::LNVpsDb;
|
||||
use log::{debug, error, info};
|
||||
use rocket::futures::SinkExt;
|
||||
use nostr::{EventBuilder, PublicKey};
|
||||
use nostr_sdk::Client;
|
||||
use std::ops::Add;
|
||||
use tokio::sync::mpsc::{unbounded_channel, UnboundedReceiver, UnboundedSender};
|
||||
|
||||
@ -38,6 +39,7 @@ pub struct Worker {
|
||||
vm_state_cache: VmStateCache,
|
||||
tx: UnboundedSender<WorkJob>,
|
||||
rx: UnboundedReceiver<WorkJob>,
|
||||
client: Option<Client>,
|
||||
last_check_vms: u64,
|
||||
}
|
||||
|
||||
@ -61,6 +63,7 @@ impl Worker {
|
||||
provisioner: P,
|
||||
settings: impl Into<WorkerSettings>,
|
||||
vm_state_cache: VmStateCache,
|
||||
client: Option<Client>,
|
||||
) -> Self {
|
||||
let (tx, rx) = unbounded_channel();
|
||||
Self {
|
||||
@ -70,6 +73,7 @@ impl Worker {
|
||||
settings: settings.into(),
|
||||
tx,
|
||||
rx,
|
||||
client,
|
||||
last_check_vms: Utc::now().timestamp() as u64,
|
||||
}
|
||||
}
|
||||
@ -243,10 +247,20 @@ impl Worker {
|
||||
}
|
||||
}
|
||||
if user.contact_nip4 {
|
||||
// send DM
|
||||
// send dm
|
||||
}
|
||||
if user.contact_nip17 {
|
||||
// send dm
|
||||
if let Some(c) = self.client.as_ref() {
|
||||
let sig = c.signer().await?;
|
||||
let ev = EventBuilder::private_msg(
|
||||
&sig,
|
||||
PublicKey::from_slice(&user.pubkey)?,
|
||||
message,
|
||||
None,
|
||||
)
|
||||
.await?;
|
||||
c.send_event(ev).await?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
Reference in New Issue
Block a user