Skip to content

Commit

Permalink
nostrs: Add text_note create command
Browse files Browse the repository at this point in the history
  • Loading branch information
bouzuya committed Feb 24, 2023
1 parent 71f5448 commit eda19cd
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 12 deletions.
4 changes: 2 additions & 2 deletions nostrs/src/client.rs
Expand Up @@ -2,15 +2,15 @@ use std::env;

use nostr_sdk::{
prelude::{FromSkStr, Keys},
Client, RelayOptions,
Client, Options, RelayOptions,
};

use crate::config;

pub async fn new_client() -> anyhow::Result<Client> {
let my_keys = Keys::from_sk_str(env::var("SECRET_KEY")?.as_str())?;

let client = Client::new(&my_keys);
let client = Client::new_with_opts(&my_keys, Options::default().wait_for_send(true));
let config = config::load()?;
for (url, options) in config.relays.iter() {
client
Expand Down
11 changes: 9 additions & 2 deletions nostrs/src/handler/text_note.rs
@@ -1,10 +1,17 @@
use std::time::Duration;
use std::{collections::HashSet, time::Duration};

use nostr_sdk::prelude::{Kind, SubscriptionFilter};

use crate::client::new_client;

pub async fn handle() -> anyhow::Result<()> {
pub async fn create(content: String) -> anyhow::Result<()> {
let client = new_client().await?;
let event_id = client.publish_text_note(content, &[]).await?;
println!("{event_id:?}");
Ok(())
}

pub async fn list() -> anyhow::Result<()> {
let client = new_client().await?;
let filter = SubscriptionFilter::new()
.kind(Kind::TextNote)
Expand Down
28 changes: 20 additions & 8 deletions nostrs/src/main.rs
@@ -1,27 +1,39 @@
pub mod client;
mod client;
mod config;
mod handler;

#[derive(clap::Parser)]
#[command(version, about, long_about = None)]
struct Command {
#[command(subcommand)]
subcommand: Subcommand,
resource: Resource,
}

#[derive(clap::Subcommand)]
enum Subcommand {
enum Resource {
Metadata,
TextNote,
TextNote {
#[command(subcommand)]
command: TextNoteCommand,
},
Timeline,
}

#[derive(clap::Subcommand)]
enum TextNoteCommand {
Create { content: String },
List,
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
let command = <Command as clap::Parser>::parse();
match command.subcommand {
Subcommand::Metadata => handler::metadata::handle().await,
Subcommand::TextNote => handler::text_note::handle().await,
Subcommand::Timeline => handler::timeline::handle().await,
match command.resource {
Resource::Metadata => handler::metadata::handle().await,
Resource::TextNote { command } => match command {
TextNoteCommand::Create { content } => handler::text_note::create(content).await,
TextNoteCommand::List => handler::text_note::list().await,
},
Resource::Timeline => handler::timeline::handle().await,
}
}

0 comments on commit eda19cd

Please sign in to comment.