Skip to content

Commit

Permalink
fix: problems with rust-nostr 0.37 upgrade
Browse files Browse the repository at this point in the history
but will it pass tests?
  • Loading branch information
DanConwayDev committed Nov 27, 2024
1 parent 360a4e8 commit 244c2c2
Show file tree
Hide file tree
Showing 13 changed files with 405 additions and 63 deletions.
327 changes: 315 additions & 12 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ futures = "0.3.28"
git2 = "0.19.0"
indicatif = "0.17.7"
keyring = "2.0.5"
nostr = "0.37.0"
nostr = { version = "0.37.0", features = ["nip05", "nip49"] }
nostr-connect = "0.37.0"
nostr-database = "0.37.0"
nostr-lmdb = "0.37.0"
Expand Down
6 changes: 3 additions & 3 deletions src/bin/git_remote_nostr/push.rs
Original file line number Diff line number Diff line change
Expand Up @@ -970,14 +970,14 @@ async fn create_merge_status(
),
Tag::from_standardized(nostr::TagStandard::Event {
event_id: proposal.id,
relay_url: repo_ref.relays.first().map(nostr::UncheckedUrl::new),
relay_url: repo_ref.relays.first().cloned(),
marker: Some(Marker::Root),
public_key: None,
uppercase: false,
}),
Tag::from_standardized(nostr::TagStandard::Event {
event_id: merged_patch,
relay_url: repo_ref.relays.first().map(nostr::UncheckedUrl::new),
relay_url: repo_ref.relays.first().cloned(),
marker: Some(Marker::Mention),
public_key: None,
uppercase: false,
Expand All @@ -986,7 +986,7 @@ async fn create_merge_status(
if let Some(revision) = revision {
vec![Tag::from_standardized(nostr::TagStandard::Event {
event_id: revision.id,
relay_url: repo_ref.relays.first().map(nostr::UncheckedUrl::new),
relay_url: repo_ref.relays.first().cloned(),
marker: Some(Marker::Root),
public_key: None,
uppercase: false,
Expand Down
63 changes: 44 additions & 19 deletions src/bin/ngit/sub_commands/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::collections::HashMap;
use anyhow::{Context, Result};
use ngit::cli_interactor::PromptConfirmParms;
use nostr::{nips::nip01::Coordinate, FromBech32, PublicKey, ToBech32};
use nostr_sdk::Kind;
use nostr_sdk::{Kind, RelayUrl};

use crate::{
cli::{extract_signer_cli_arguments, Cli},
Expand Down Expand Up @@ -324,24 +324,45 @@ pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> {

// TODO: check if relays are free to post to so contributors can submit patches
// TODO: recommend some reliable free ones
let relays: Vec<String> = if args.relays.is_empty() {
Interactor::default()
.input(
PromptInputParms::default()
.with_prompt("relays")
.with_default(if let Ok(config) = &repo_config_result {
config.relays.clone().join(" ")
} else if let Some(repo_ref) = &repo_ref {
repo_ref.relays.clone().join(" ")
} else {
user_ref.relays.write().join(" ")
}),
)?
.split(' ')
.map(std::string::ToString::to_string)
.collect()
} else {
args.relays.clone()
let relays: Vec<RelayUrl> = {
let mut default = if let Ok(config) = &repo_config_result {
config.relays.clone().join(" ")
} else if let Some(repo_ref) = &repo_ref {
repo_ref
.relays
.iter()
.map(std::string::ToString::to_string)
.collect::<Vec<String>>()
.join(" ")
} else {
user_ref.relays.write().join(" ")
};
'outer: loop {
let relays: Vec<String> = if args.relays.is_empty() {
Interactor::default()
.input(
PromptInputParms::default()
.with_prompt("relays")
.with_default(default),
)?
.split(' ')
.map(std::string::ToString::to_string)
.collect()
} else {
args.relays.clone()
};
let mut relay_urls = vec![];
for r in &relays {
if let Ok(r) = RelayUrl::parse(r) {
relay_urls.push(r);
} else {
eprintln!("{r} is not a valid relay url");
default = relays.join(" ");
continue 'outer;
}
}
break relay_urls;
}
};

println!(
Expand Down Expand Up @@ -416,6 +437,10 @@ pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> {
false,
)?;

let relays = relays
.iter()
.map(std::string::ToString::to_string)
.collect::<Vec<String>>();
// if yaml file doesnt exist or needs updating
if match &repo_config_result {
Ok(config) => {
Expand Down
3 changes: 2 additions & 1 deletion src/bin/ngit/sub_commands/send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ pub async fn launch(cli_args: &Cli, args: &SubCommandArgs, no_fetch: bool) -> Re
if root_proposal_id.is_none() {
if let Some(event) = events.first() {
let event_bech32 = if let Some(relay) = repo_ref.relays.first() {
Nip19Event::new(event.id, vec![relay]).to_bech32()?
Nip19Event::new(event.id, vec![relay.to_string()]).to_bech32()?
} else {
event.id.to_bech32()?
};
Expand Down Expand Up @@ -366,6 +366,7 @@ async fn get_root_proposal_id_and_mentions_from_in_reply_to(
relay_url: _,
marker: _,
public_key: _,
uppercase: false,
}) => {
let events = get_events_from_local_cache(
git_repo_path,
Expand Down
21 changes: 13 additions & 8 deletions src/lib/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ impl Connect for Client {
processed_relays.extend(relays.clone());

if let Ok(repo_ref) = get_repo_ref_from_cache(git_repo_path, repo_coordinates).await {
request.repo_relays = repo_ref.relays.iter().map(|r| r.clone()).collect();
request.repo_relays = repo_ref.relays.iter().cloned().collect();
}

request.user_relays_for_profiles = {
Expand Down Expand Up @@ -1638,7 +1638,7 @@ pub async fn send_events(
git_repo_path: Option<&Path>,
events: Vec<nostr::Event>,
my_write_relays: Vec<String>,
repo_read_relays: Vec<String>,
repo_read_relays: Vec<RelayUrl>,
animate: bool,
silent: bool,
) -> Result<()> {
Expand All @@ -1651,7 +1651,12 @@ pub async fn send_events(
},
]
.concat();
let mut relays: Vec<&String> = vec![];
let mut relays: Vec<&str> = vec![];

let repo_read_relays = repo_read_relays
.iter()
.map(|r| r.to_string())
.collect::<Vec<String>>();

let all = &[
repo_read_relays.clone(),
Expand Down Expand Up @@ -1714,7 +1719,7 @@ pub async fn send_events(

#[allow(clippy::borrow_deref_ref)]
join_all(relays.iter().map(|&relay| async {
let relay_clean = remove_trailing_slash(&*relay);
let relay_clean = remove_trailing_slash(relay);
let details = format!(
"{}{}{} {}",
if my_write_relays
Expand All @@ -1727,7 +1732,7 @@ pub async fn send_events(
},
if repo_read_relays
.iter()
.any(|r| relay_clean.eq(&remove_trailing_slash(r)))
.any(|r| relay_clean.eq(&remove_trailing_slash(&r.to_string())))
{
" [repo-relay]"
} else {
Expand Down Expand Up @@ -1755,7 +1760,7 @@ pub async fn send_events(
let mut failed = false;
for event in &events {
match client
.send_event_to(git_repo_path, relay.as_str(), event.clone())
.send_event_to(git_repo_path, relay, event.clone())
.await
{
Ok(_) => pb.inc(1),
Expand Down Expand Up @@ -1785,8 +1790,8 @@ pub async fn send_events(
Ok(())
}

fn remove_trailing_slash(s: &String) -> String {
match s.as_str().strip_suffix('/') {
fn remove_trailing_slash(s: &str) -> String {
match s.strip_suffix('/') {
Some(s) => s,
None => s,
}
Expand Down
13 changes: 7 additions & 6 deletions src/lib/git/nostr_url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -852,7 +852,7 @@ mod tests {
.unwrap(),
kind: nostr_sdk::Kind::GitRepoAnnouncement,
relays: if relays {
vec!["wss://nos.lol/".to_string()]
vec![RelayUrl::parse("wss://nos.lol").unwrap()]
} else {
vec![]
},
Expand All @@ -873,7 +873,8 @@ mod tests {
)
.unwrap(),
kind: nostr_sdk::Kind::GitRepoAnnouncement,
relays: vec!["wss://nos.lol".to_string()], // wont add the slash
relays: vec![RelayUrl::parse("wss://nos.lol").unwrap()], /* wont add the
* slash */
}]),
protocol: None,
user: None,
Expand Down Expand Up @@ -957,8 +958,8 @@ mod tests {
.unwrap(),
kind: nostr_sdk::Kind::GitRepoAnnouncement,
relays: vec![
"wss://nos.lol/".to_string(),
"wss://relay.damus.io/".to_string(),
RelayUrl::parse("wss://nos.lol/").unwrap(),
RelayUrl::parse("wss://relay.damus.io/").unwrap(),
],
}]),
protocol: None,
Expand Down Expand Up @@ -1054,8 +1055,8 @@ mod tests {
.unwrap(),
kind: nostr_sdk::Kind::GitRepoAnnouncement,
relays: vec![
"wss://nos.lol/".to_string(),
"wss://relay.damus.io/".to_string(),
RelayUrl::parse("wss://nos.lol/").unwrap(),
RelayUrl::parse("wss://relay.damus.io/").unwrap(),
],
}]),
protocol: None,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/git_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ pub async fn generate_patch_event(
let commit_parent = git_repo
.get_commit_parent(commit)
.context("failed to get parent commit")?;
let relay_hint = repo_ref.relays.first().map(|r| r.clone());
let relay_hint = repo_ref.relays.first().cloned();

sign_event(
EventBuilder::new(
Expand Down
6 changes: 3 additions & 3 deletions src/lib/login/fresh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ pub fn generate_nostr_connect_app(
client
.get_fallback_signer_relays()
.iter()
.flat_map(|s| RelayUrl::parse(s))
.flat_map(RelayUrl::parse)
.collect::<Vec<RelayUrl>>()
} else {
vec![]
Expand Down Expand Up @@ -438,8 +438,8 @@ pub async fn listen_for_remote_signer(
let bunker_url = NostrConnectURI::Bunker {
// TODO the remote signer pubkey may not be the user pubkey
remote_signer_public_key: public_key,
relays: nostr_connect_url.relays(),
secret: nostr_connect_url.secret(),
relays: nostr_connect_url.relays().to_vec(),
secret: nostr_connect_url.secret().map(String::from),
};
Ok((signer, public_key, bunker_url))
} else {
Expand Down
10 changes: 8 additions & 2 deletions src/lib/repo_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,10 @@ mod tests {
"https://exampleproject.xyz".to_string(),
"https://gitworkshop.dev/123".to_string(),
],
relays: vec!["ws://relay1.io".to_string(), "ws://relay2.io".to_string()],
relays: vec![
RelayUrl::parse("ws://relay1.io").unwrap(),
RelayUrl::parse("ws://relay2.io").unwrap(),
],
maintainers: vec![TEST_KEY_1_KEYS.public_key(), TEST_KEY_2_KEYS.public_key()],
events: HashMap::new(),
}
Expand Down Expand Up @@ -593,7 +596,10 @@ mod tests {
async fn relays() {
assert_eq!(
RepoRef::try_from(create().await).unwrap().relays,
vec!["ws://relay1.io".to_string(), "ws://relay2.io".to_string()],
vec![
RelayUrl::parse("ws://relay1.io").unwrap(),
RelayUrl::parse("ws://relay2.io").unwrap(),
],
)
}

Expand Down
6 changes: 3 additions & 3 deletions test_utils/src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::{
use anyhow::{Context, Result};
use git2::{Branch, Oid, RepositoryInitOptions, Signature, Time};
use nostr::nips::nip01::Coordinate;
use nostr_sdk::{Kind, ToBech32};
use nostr_sdk::{Kind, RelayUrl, ToBech32};

use crate::generate_repo_ref_event;

Expand All @@ -28,8 +28,8 @@ impl Default for GitTestRepo {
public_key: repo_event.pubkey,
identifier: repo_event.tags.identifier().unwrap().to_string(),
relays: vec![
"ws://localhost:8055".to_string(),
"ws://localhost:8056".to_string(),
RelayUrl::parse("ws://localhost:8055").unwrap(),
RelayUrl::parse("ws://localhost:8056").unwrap(),
],
};

Expand Down
6 changes: 3 additions & 3 deletions tests/git_remote_nostr/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use anyhow::{Context, Result};
use futures::join;
use git2::Oid;
use nostr::nips::nip01::Coordinate;
use nostr_sdk::{secp256k1::rand, Event, JsonUtil, Kind, ToBech32};
use nostr_sdk::{secp256k1::rand, Event, JsonUtil, Kind, RelayUrl, ToBech32};
use relay::Relay;
use serial_test::serial;
use test_utils::{git::GitTestRepo, *};
Expand All @@ -23,8 +23,8 @@ fn get_nostr_remote_url() -> Result<String> {
public_key: repo_event.pubkey,
identifier: repo_event.tags.identifier().unwrap().to_string(),
relays: vec![
"ws://localhost:8055".to_string(),
"ws://localhost:8056".to_string(),
RelayUrl::parse("ws://localhost:8055").unwrap(),
RelayUrl::parse("ws://localhost:8056").unwrap(),
],
}
.to_bech32()?;
Expand Down
3 changes: 2 additions & 1 deletion tests/ngit_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ mod cannot_find_repo_event {
use super::*;
mod cli_prompts {
use nostr::{nips::nip01::Coordinate, ToBech32};
use nostr_sdk::RelayUrl;

use super::*;
async fn run_async_repo_event_ref_needed(invalid_input: bool, naddr: bool) -> Result<()> {
Expand Down Expand Up @@ -91,7 +92,7 @@ mod cannot_find_repo_event {
kind: nostr::Kind::GitRepoAnnouncement,
public_key: TEST_KEY_1_KEYS.public_key(),
identifier: repo_event.tags.identifier().unwrap().to_string(),
relays: vec!["ws://localhost:8056".to_string()],
relays: vec![RelayUrl::parse("ws://localhost:8056").unwrap()],
}
.to_bech32()?,
)?;
Expand Down

0 comments on commit 244c2c2

Please sign in to comment.