Skip to content

Commit

Permalink
Refactor handle_notifications method in subscriptions.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
kasugamirai committed Apr 16, 2024
1 parent d4b61a3 commit b61c4a2
Showing 1 changed file with 34 additions and 30 deletions.
64 changes: 34 additions & 30 deletions crates/nostr-sdk/examples/subscriptions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,36 +52,40 @@ async fn main() -> Result<()> {

// Handle subscription notifications with `handle_notifications` method
client
.handle_notifications(|notification| async {
if let RelayPoolNotification::Event {
subscription_id,
event,
..
} = notification
{
// Check subscription ID
if subscription_id == sub_id_1 {
// Handle (ex. update specific UI)
}

// Check kind
if event.kind() == Kind::EncryptedDirectMessage {
if let Ok(msg) =
nip04::decrypt(keys.secret_key()?, event.author_ref(), event.content())
{
println!("DM: {msg}");
} else {
tracing::error!("Impossible to decrypt direct message");
}
} else if event.kind() == Kind::TextNote {
println!("TextNote: {:?}", event);
} else {
println!("{:?}", event);
}
}
Ok(false) // Set to true to exit from the loop
})
.handle_notifications(|notification| handle_event(notification, sub_id_1.clone(), &keys))
.await?;

Ok(())
}

async fn handle_event(
notification: RelayPoolNotification,
sub_id_1: SubscriptionId,
keys: &Keys,
) -> Result<bool, Box<dyn std::error::Error>> {
if let RelayPoolNotification::Event {
subscription_id,
event,
..
} = notification
{
// Check subscription ID
if subscription_id == sub_id_1 {
// Handle (ex. update specific UI)
}

// Check kind
if event.kind() == Kind::EncryptedDirectMessage {
if let Ok(msg) = nip04::decrypt(keys.secret_key()?, event.author_ref(), event.content())
{
println!("DM: {msg}");
} else {
tracing::error!("Impossible to decrypt direct message");
}
} else if event.kind() == Kind::TextNote {
println!("TextNote: {:?}", event);
} else {
println!("{:?}", event);
}
}
Ok(false) // Set to true to exit from the loop
}

0 comments on commit b61c4a2

Please sign in to comment.