diff --git a/README.md b/README.md index 2883f98..751bfea 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,12 @@ $ nostreq --authors pubkey1,pubkey2,pubkey3 ["REQ", "2e3479cd-e04d-4e4d-a370-a7c77cf7998e", {"authors":["pubkey1","pubkey2","pubkey3"]}] ``` +Generate a relay request with multiple filters (OR) +```shell +$ nostreq --authors pubkey1 --limit 1 --or --etags pubkey1 +["REQ", "dede0573-ffb4-45e7-a2e7-346178426762", {"authors":["pubkey1"],"limit":1},{"#e":["pubkey1"]}] +``` + Generate a relay request a custom subscription id ```shell $ nostreq --subscription-id myspecialsub diff --git a/src/lib.rs b/src/lib.rs index 7ec04a8..c7d6185 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,3 @@ -use uuid::Uuid; use clap::{Arg, Command, ArgAction, ArgMatches}; use serde::{Serialize, Deserialize}; @@ -120,8 +119,6 @@ pub struct RequestBuilder { until: Option, #[serde(skip_serializing_if = "Option::is_none")] limit: Option, - #[serde(skip_serializing)] - pub subscription_id: String } impl RequestBuilder { @@ -136,7 +133,6 @@ impl RequestBuilder { since: None, until: None, limit: None, - subscription_id: "".to_string() } } @@ -200,11 +196,6 @@ impl RequestBuilder { self } - pub fn subscription_id(&mut self, subid: String) -> &mut Self { - self.subscription_id = subid; - self - } - pub fn to_json(&mut self) -> String { serde_json::to_string(self).unwrap() } @@ -259,10 +250,5 @@ pub fn request_from_cli(cli_matches: ArgMatches) -> RequestBuilder { Some(limit) => { request.limit(*limit); } } - match cli_matches.get_one::("subscription-id") { - None => { request.subscription_id(Uuid::new_v4().to_string()) }, - Some(id) => { request.subscription_id(id.to_string()) } - }; - request } diff --git a/src/main.rs b/src/main.rs index b7c1b33..b9a4b3b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,11 +1,39 @@ use nostreq::*; +use std::env; +use uuid::Uuid; fn main() { - let cli_matches = cli().get_matches(); - let mut request = request_from_cli(cli_matches); + let args: Vec = env::args().collect(); - let request_json = request.to_json(); + // Support for --or as an OR filter input separator + let or_list: Vec> = args[1..].into_iter().fold(Vec::new(), |mut acc, x| { + if x == "--or" || acc.is_empty() { + acc.push(vec![String::from(args[0].to_string())]); + } + if x != "--or" { + acc.last_mut().unwrap().push((&x).to_string()); + } + acc + }); - println!(r#"["REQ", "{}", {}]"#, request.subscription_id, request_json); + // Process each OR filter + let mut filter: Vec = vec![]; + for or in &or_list { + + let cli_matches = cli().get_matches_from(or); + + let mut request = request_from_cli(cli_matches); + + let request_json = request.to_json(); + filter.push(request_json); + } + + let cli_matches = cli().get_matches_from(&or_list[0]); + let subscription_id = match cli_matches.get_one::("subscription-id") { + None => { Uuid::new_v4().to_string() }, + Some(id) => { id.to_string() } + }; + + println!(r#"["REQ", "{}", {}]"#, subscription_id, filter.join(",")); }