feat: config sending dms on pay to relay signup

This commit is contained in:
thesimplekid 2023-07-02 22:56:32 -04:00 committed by Greg Heartsfield
parent 1131c1986e
commit ddc58a2f1c
3 changed files with 37 additions and 12 deletions

View File

@ -202,6 +202,9 @@ reject_future_seconds = 1800
# LNBits api secret # LNBits api secret
#api_secret = "<ln bits api>" #api_secret = "<ln bits api>"
# Nostr direct message on signup
#direct_message=true
# Terms of service # Terms of service
#terms_message = """ #terms_message = """
#This service (and supporting services) are provided "as is", without warranty of any kind, express or implied. #This service (and supporting services) are provided "as is", without warranty of any kind, express or implied.
@ -223,4 +226,6 @@ reject_future_seconds = 1800
# Whether or not new sign ups should be allowed # Whether or not new sign ups should be allowed
#sign_ups = false #sign_ups = false
# optional if `direct_message=false`
#secret_key = "<nostr nsec>" #secret_key = "<nostr nsec>"

View File

@ -94,7 +94,8 @@ pub struct PayToRelay {
pub api_secret: String, pub api_secret: String,
pub terms_message: String, pub terms_message: String,
pub sign_ups: bool, // allow new users to sign up to relay pub sign_ups: bool, // allow new users to sign up to relay
pub secret_key: String, pub direct_message: bool, // Send direct message to user with invoice and terms
pub secret_key: Option<String>,
pub processor: Processor, pub processor: Processor,
} }
@ -243,7 +244,14 @@ impl Settings {
// Should check that url is valid // Should check that url is valid
assert_ne!(settings.pay_to_relay.node_url, ""); assert_ne!(settings.pay_to_relay.node_url, "");
assert_ne!(settings.pay_to_relay.terms_message, ""); assert_ne!(settings.pay_to_relay.terms_message, "");
assert_ne!(settings.pay_to_relay.secret_key, "");
if settings.pay_to_relay.direct_message {
assert_ne!(
settings.pay_to_relay.secret_key,
Some("<nostr nsec>".to_string())
);
assert!(settings.pay_to_relay.secret_key.is_some());
}
} }
Ok(settings) Ok(settings)
@ -306,7 +314,8 @@ impl Default for Settings {
node_url: "".to_string(), node_url: "".to_string(),
api_secret: "".to_string(), api_secret: "".to_string(),
sign_ups: false, sign_ups: false,
secret_key: "".to_string(), direct_message: true,
secret_key: None,
processor: Processor::LNBits, processor: Processor::LNBits,
}, },
verified_users: VerifiedUsers { verified_users: VerifiedUsers {

View File

@ -25,7 +25,7 @@ pub struct Payment {
/// Settings /// Settings
settings: crate::config::Settings, settings: crate::config::Settings,
// Nostr Keys // Nostr Keys
nostr_keys: Keys, nostr_keys: Option<Keys>,
/// Payment Processor /// Payment Processor
processor: Arc<dyn PaymentProcessor>, processor: Arc<dyn PaymentProcessor>,
} }
@ -102,7 +102,11 @@ impl Payment {
info!("Create payment handler"); info!("Create payment handler");
// Create nostr key from sk string // Create nostr key from sk string
let nostr_keys = Keys::from_sk_str(&settings.pay_to_relay.secret_key)?; let nostr_keys = if let Some(secret_key) = &settings.pay_to_relay.secret_key {
Some(Keys::from_sk_str(secret_key)?)
} else {
None
};
// Create processor kind defined in settings // Create processor kind defined in settings
let processor = match &settings.pay_to_relay.processor { let processor = match &settings.pay_to_relay.processor {
@ -193,6 +197,11 @@ impl Payment {
pubkey: &str, pubkey: &str,
invoice_info: &InvoiceInfo, invoice_info: &InvoiceInfo,
) -> Result<()> { ) -> Result<()> {
let nostr_keys = match &self.nostr_keys {
Some(key) => key,
None => return Err(Error::CustomError("Nostr key not defined".to_string())),
};
// Create Nostr key from pk // Create Nostr key from pk
let key = Keys::from_pk_str(pubkey)?; let key = Keys::from_pk_str(pubkey)?;
@ -200,16 +209,16 @@ impl Payment {
// Event DM with terms of service // Event DM with terms of service
let message_event: NostrEvent = EventBuilder::new_encrypted_direct_msg( let message_event: NostrEvent = EventBuilder::new_encrypted_direct_msg(
&self.nostr_keys, nostr_keys,
pubkey, pubkey,
&self.settings.pay_to_relay.terms_message, &self.settings.pay_to_relay.terms_message,
)? )?
.to_event(&self.nostr_keys)?; .to_event(nostr_keys)?;
// Event DM with invoice // Event DM with invoice
let invoice_event: NostrEvent = let invoice_event: NostrEvent =
EventBuilder::new_encrypted_direct_msg(&self.nostr_keys, pubkey, &invoice_info.bolt11)? EventBuilder::new_encrypted_direct_msg(nostr_keys, pubkey, &invoice_info.bolt11)?
.to_event(&self.nostr_keys)?; .to_event(nostr_keys)?;
// Persist DM events to DB // Persist DM events to DB
self.repo.write_event(&message_event.clone().into()).await?; self.repo.write_event(&message_event.clone().into()).await?;
@ -246,8 +255,10 @@ impl Payment {
.create_invoice_record(&key, invoice_info.clone()) .create_invoice_record(&key, invoice_info.clone())
.await?; .await?;
if self.settings.pay_to_relay.direct_message {
// Admission event invoice and terms to pubkey that is joining // Admission event invoice and terms to pubkey that is joining
self.send_admission_message(pubkey, &invoice_info).await?; self.send_admission_message(pubkey, &invoice_info).await?;
}
Ok(invoice_info) Ok(invoice_info)
} }