Skip to content

Commit

Permalink
Delete profile and events (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
TonyGiorgio authored Jun 9, 2024
1 parent bcf6724 commit 7b11703
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 0 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,18 @@ nostr-tool -r wss://nostr.oxtr.dev -p {PRIVATE_KEY} send-direct-message --receiv
nostr-tool -r wss://nostr.oxtr.dev -p {PRIVATE_KEY} delete-event -e {EVENT_ID} -r "The reason for deleting the event"
```

### Delete a profile

Just events:
```shell
nostr-tool -r wss://nostr.oxtr.dev -p {PRIVATE_KEY} delete-profile --events-only --kinds 1
```

Delete metadata profile:
```shell
nostr-tool -r wss://nostr.oxtr.dev -p {PRIVATE_KEY} delete-profile
```

### React to an event

```shell
Expand Down
8 changes: 8 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ enum Commands {
SendDirectMessage(sub_commands::dm::SendDirectMessageSubCommand),
/// Delete an event
DeleteEvent(sub_commands::delete_event::DeleteEventSubCommand),
/// Delete a profile
DeleteProfile(sub_commands::delete_profile::DeleteProfileSubCommand),
/// React to an event
React(sub_commands::react::ReactionSubCommand),
/// Get all events
Expand Down Expand Up @@ -128,6 +130,12 @@ fn main() -> Result<()> {
args.difficulty_target,
sub_command_args,
),
Commands::DeleteProfile(sub_command_args) => sub_commands::delete_profile::delete(
args.private_key,
args.relays,
args.difficulty_target,
sub_command_args,
),
Commands::React(sub_command_args) => sub_commands::react::react_to_event(
args.private_key,
args.relays,
Expand Down
80 changes: 80 additions & 0 deletions src/sub_commands/delete_profile.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
use std::time::Duration;

use clap::Args;
use nostr_sdk::prelude::*;

use crate::utils::{create_client, handle_keys};

#[derive(Args)]
pub struct DeleteProfileSubCommand {
/// Delete just the events instead of the profile
#[arg(long, default_value = "false")]
events_only: bool,
/// If events only are selected, allows specifying kinds
#[arg(short, long, action = clap::ArgAction::Append)]
kinds: Option<Vec<u64>>,
/// Reason for deleting the events
#[arg(short, long)]
reason: Option<String>,
// Print keys as hex
#[arg(long, default_value = "false")]
hex: bool,
/// Timeout in seconds
#[arg(long)]
timeout: Option<u64>,
}

pub fn delete(
private_key: Option<String>,
relays: Vec<String>,
difficulty_target: u8,
sub_command_args: &DeleteProfileSubCommand,
) -> Result<()> {
if relays.is_empty() {
panic!("No relays specified, at least one relay is required!")
}

let keys = handle_keys(private_key, sub_command_args.hex, true)?;
let client = create_client(&keys, relays, difficulty_target)?;

let timeout = sub_command_args.timeout.map(Duration::from_secs);

if sub_command_args.events_only {
// go through all of the user events
let authors: Vec<String> = vec![client.keys().public_key().to_string()];
println!("checking author events...");

// Convert kind number to Kind struct
let kinds: Vec<Kind> = sub_command_args
.kinds
.clone()
.unwrap_or(Vec::new())
.into_iter()
.map(Kind::from)
.collect();

let events: Vec<Event> =
client.get_events_of(vec![Filter::new().authors(authors).kinds(kinds)], timeout)?;

println!("Retrieved events to delete: {}", events.len());
for event in events {
let event_id = client.delete_event(event.id, sub_command_args.reason.clone())?;
if !sub_command_args.hex {
println!("Deleted event with id: {}", event_id.to_bech32()?);
} else {
println!("Deleted event with id: {}", event_id.to_hex());
}
}
} else {
// Not a perfect delete but multiple clients trigger off of this metadata
let metadata = Metadata::default()
.name("Deleted")
.display_name("Deleted")
.about("Deleted")
.custom_field("deleted", Value::Bool(true));

let event_id = client.set_metadata(metadata)?;
println!("Metadata updated ({})", event_id.to_bech32()?);
}
Ok(())
}
1 change: 1 addition & 0 deletions src/sub_commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub mod create_badge;
pub mod create_public_channel;
pub mod custom_event;
pub mod delete_event;
pub mod delete_profile;
pub mod dm;
pub mod generate_keypair;
pub mod hide_public_channel_message;
Expand Down

0 comments on commit 7b11703

Please sign in to comment.