From 2596140ba29dc959643ae02fb2db4de980ee12e9 Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Fri, 23 May 2025 07:53:32 +0100 Subject: fix: remove blossom url trailing slash when creating announcment with `ngit init` --- src/bin/ngit/sub_commands/init.rs | 12 ++++++------ src/lib/mod.rs | 21 +++++++++++++++++++++ src/lib/repo_ref.rs | 5 ++++- 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/src/bin/ngit/sub_commands/init.rs b/src/bin/ngit/sub_commands/init.rs index 70a2cd9..5c090ae 100644 --- a/src/bin/ngit/sub_commands/init.rs +++ b/src/bin/ngit/sub_commands/init.rs @@ -10,6 +10,7 @@ use anyhow::{Context, Result, bail}; use console::Style; use dialoguer::theme::{ColorfulTheme, Theme}; use ngit::{ + UrlWithoutSlash, cli_interactor::{PromptChoiceParms, PromptConfirmParms, PromptMultiChoiceParms}, client::{Params, send_events}, git::nostr_url::{CloneUrl, NostrUrlDecoded}, @@ -231,7 +232,7 @@ pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> { repo_ref .blossoms .iter() - .map(std::string::ToString::to_string) + .map(UrlWithoutSlash::to_string_without_trailing_slash) .collect::>() // } else if user_ref.blossoms.read().is_empty() { // client.get_fallback_relays().clone() @@ -290,7 +291,7 @@ pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> { } } if args.blossoms.is_empty() { - let blossom = format_ngit_blossom_url_as_relay_url(ngit_relay)?; + let blossom = format_ngit_relay_url_as_blossom_url(ngit_relay)?; if !blossoms_defaults.contains(&blossom) { blossoms_defaults.push(blossom); } @@ -483,8 +484,7 @@ pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> { blossoms_defaults, selections, |s| { - Url::parse(s) - .map(|_| s.to_string()) + format_ngit_relay_url_as_blossom_url(s) .context(format!("Invalid blossom URL format: {s}")) }, )?; @@ -974,10 +974,10 @@ fn format_ngit_relay_url_as_relay_url(url: &str) -> Result { Ok(format!("wss://{ngit_relay_url}")) } -fn format_ngit_blossom_url_as_relay_url(url: &str) -> Result { +fn format_ngit_relay_url_as_blossom_url(url: &str) -> Result { let ngit_relay_url = normalize_ngit_relay_url(url)?; if ngit_relay_url.contains("http://") { - return Ok(ngit_relay_url.to_string()); + return Ok(ngit_relay_url); } Ok(format!("https://{ngit_relay_url}")) } diff --git a/src/lib/mod.rs b/src/lib/mod.rs index 2072a80..7c7bd6a 100644 --- a/src/lib/mod.rs +++ b/src/lib/mod.rs @@ -8,9 +8,30 @@ pub mod repo_state; use anyhow::{Result, anyhow}; use directories::ProjectDirs; +use nostr_sdk::Url; pub fn get_dirs() -> Result { ProjectDirs::from("", "", "ngit").ok_or(anyhow!( "should find operating system home directories with rust-directories crate" )) } + +pub trait UrlWithoutSlash { + fn as_str_without_trailing_slash(&self) -> &str; + fn to_string_without_trailing_slash(&self) -> String; +} + +impl UrlWithoutSlash for Url { + fn as_str_without_trailing_slash(&self) -> &str { + let url_str = self.as_str(); + if let Some(without) = url_str.strip_suffix('/') { + without + } else { + url_str + } + } + + fn to_string_without_trailing_slash(&self) -> String { + self.as_str_without_trailing_slash().to_string() + } +} diff --git a/src/lib/repo_ref.rs b/src/lib/repo_ref.rs index 1a0fe85..df1427a 100644 --- a/src/lib/repo_ref.rs +++ b/src/lib/repo_ref.rs @@ -18,6 +18,7 @@ use serde::{Deserialize, Serialize}; #[cfg(not(test))] use crate::client::Client; use crate::{ + UrlWithoutSlash, cli_interactor::{ Interactor, InteractorPrompt, PromptChoiceParms, PromptConfirmParms, PromptInputParms, }, @@ -204,7 +205,9 @@ impl RepoRef { } else { vec![Tag::custom( nostr::TagKind::Custom(std::borrow::Cow::Borrowed("blossoms")), - self.blossoms.iter().map(|r| r.to_string()), + self.blossoms + .iter() + .map(|r| r.to_string_without_trailing_slash()), )] }, // code languages and hashtags -- cgit v1.2.3