Skip to content

Commit

Permalink
Added support for --or argument
Browse files Browse the repository at this point in the history
  • Loading branch information
blakejakopovic committed Dec 4, 2022
1 parent c15f038 commit a9dfb57
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 18 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 0 additions & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use uuid::Uuid;
use clap::{Arg, Command, ArgAction, ArgMatches};
use serde::{Serialize, Deserialize};

Expand Down Expand Up @@ -120,8 +119,6 @@ pub struct RequestBuilder {
until: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
limit: Option<u32>,
#[serde(skip_serializing)]
pub subscription_id: String
}

impl RequestBuilder {
Expand All @@ -136,7 +133,6 @@ impl RequestBuilder {
since: None,
until: None,
limit: None,
subscription_id: "".to_string()
}
}

Expand Down Expand Up @@ -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()
}
Expand Down Expand Up @@ -259,10 +250,5 @@ pub fn request_from_cli(cli_matches: ArgMatches) -> RequestBuilder {
Some(limit) => { request.limit(*limit); }
}

match cli_matches.get_one::<String>("subscription-id") {
None => { request.subscription_id(Uuid::new_v4().to_string()) },
Some(id) => { request.subscription_id(id.to_string()) }
};

request
}
36 changes: 32 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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<String> = env::args().collect();

let request_json = request.to_json();
// Support for --or as an OR filter input separator
let or_list: Vec<Vec<String>> = 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<String> = 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::<String>("subscription-id") {
None => { Uuid::new_v4().to_string() },
Some(id) => { id.to_string() }
};

println!(r#"["REQ", "{}", {}]"#, subscription_id, filter.join(","));
}

0 comments on commit a9dfb57

Please sign in to comment.