upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/src/lib/repo_ref.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/repo_ref.rs')
-rw-r--r--src/lib/repo_ref.rs29
1 files changed, 18 insertions, 11 deletions
diff --git a/src/lib/repo_ref.rs b/src/lib/repo_ref.rs
index 05234e2..8319c78 100644
--- a/src/lib/repo_ref.rs
+++ b/src/lib/repo_ref.rs
@@ -9,7 +9,7 @@ use std::{
9use anyhow::{bail, Context, Result}; 9use anyhow::{bail, Context, Result};
10use console::Style; 10use console::Style;
11use nostr::{nips::nip01::Coordinate, FromBech32, PublicKey, Tag, TagStandard, ToBech32}; 11use nostr::{nips::nip01::Coordinate, FromBech32, PublicKey, Tag, TagStandard, ToBech32};
12use nostr_sdk::{Kind, NostrSigner, Timestamp}; 12use nostr_sdk::{Kind, NostrSigner, RelayUrl, Timestamp};
13use serde::{Deserialize, Serialize}; 13use serde::{Deserialize, Serialize};
14 14
15#[cfg(not(test))] 15#[cfg(not(test))]
@@ -28,7 +28,7 @@ pub struct RepoRef {
28 pub root_commit: String, 28 pub root_commit: String,
29 pub git_server: Vec<String>, 29 pub git_server: Vec<String>,
30 pub web: Vec<String>, 30 pub web: Vec<String>,
31 pub relays: Vec<String>, 31 pub relays: Vec<RelayUrl>,
32 pub maintainers: Vec<PublicKey>, 32 pub maintainers: Vec<PublicKey>,
33 pub events: HashMap<Coordinate, nostr::Event>, 33 pub events: HashMap<Coordinate, nostr::Event>,
34 // code languages and hashtags 34 // code languages and hashtags
@@ -78,8 +78,11 @@ impl TryFrom<nostr::Event> for RepoRef {
78 } 78 }
79 79
80 if let Some(t) = event.tags.iter().find(|t| t.as_slice()[0].eq("relays")) { 80 if let Some(t) = event.tags.iter().find(|t| t.as_slice()[0].eq("relays")) {
81 r.relays = t.clone().to_vec(); 81 for relay in t.clone().to_vec() {
82 r.relays.remove(0); 82 if let Ok(relay) = RelayUrl::parse(relay) {
83 r.relays.push(relay);
84 }
85 }
83 } 86 }
84 87
85 if let Some(t) = event 88 if let Some(t) = event
@@ -119,9 +122,7 @@ impl TryFrom<nostr::Event> for RepoRef {
119impl RepoRef { 122impl RepoRef {
120 pub async fn to_event(&self, signer: &Arc<dyn NostrSigner>) -> Result<nostr::Event> { 123 pub async fn to_event(&self, signer: &Arc<dyn NostrSigner>) -> Result<nostr::Event> {
121 sign_event( 124 sign_event(
122 nostr_sdk::EventBuilder::new( 125 nostr_sdk::EventBuilder::new(nostr::event::Kind::GitRepoAnnouncement, "").tags(
123 nostr::event::Kind::GitRepoAnnouncement,
124 "",
125 [ 126 [
126 vec![ 127 vec![
127 Tag::identifier(if self.identifier.to_string().is_empty() { 128 Tag::identifier(if self.identifier.to_string().is_empty() {
@@ -158,7 +159,7 @@ impl RepoRef {
158 ), 159 ),
159 Tag::custom( 160 Tag::custom(
160 nostr::TagKind::Custom(std::borrow::Cow::Borrowed("relays")), 161 nostr::TagKind::Custom(std::borrow::Cow::Borrowed("relays")),
161 self.relays.clone(), 162 self.relays.iter().map(|r| r.to_string()),
162 ), 163 ),
163 Tag::custom( 164 Tag::custom(
164 nostr::TagKind::Custom(std::borrow::Cow::Borrowed("maintainers")), 165 nostr::TagKind::Custom(std::borrow::Cow::Borrowed("maintainers")),
@@ -206,7 +207,7 @@ impl RepoRef {
206 .unwrap(), 207 .unwrap(),
207 identifier: self.identifier.clone(), 208 identifier: self.identifier.clone(),
208 relays: if let Some(relay) = self.relays.first() { 209 relays: if let Some(relay) = self.relays.first() {
209 vec![relay.to_string()] 210 vec![relay.clone()]
210 } else { 211 } else {
211 vec![] 212 vec![]
212 }, 213 },
@@ -481,7 +482,10 @@ mod tests {
481 "https://exampleproject.xyz".to_string(), 482 "https://exampleproject.xyz".to_string(),
482 "https://gitworkshop.dev/123".to_string(), 483 "https://gitworkshop.dev/123".to_string(),
483 ], 484 ],
484 relays: vec!["ws://relay1.io".to_string(), "ws://relay2.io".to_string()], 485 relays: vec![
486 RelayUrl::parse("ws://relay1.io").unwrap(),
487 RelayUrl::parse("ws://relay2.io").unwrap(),
488 ],
485 maintainers: vec![TEST_KEY_1_KEYS.public_key(), TEST_KEY_2_KEYS.public_key()], 489 maintainers: vec![TEST_KEY_1_KEYS.public_key(), TEST_KEY_2_KEYS.public_key()],
486 events: HashMap::new(), 490 events: HashMap::new(),
487 } 491 }
@@ -592,7 +596,10 @@ mod tests {
592 async fn relays() { 596 async fn relays() {
593 assert_eq!( 597 assert_eq!(
594 RepoRef::try_from(create().await).unwrap().relays, 598 RepoRef::try_from(create().await).unwrap().relays,
595 vec!["ws://relay1.io".to_string(), "ws://relay2.io".to_string()], 599 vec![
600 RelayUrl::parse("ws://relay1.io").unwrap(),
601 RelayUrl::parse("ws://relay2.io").unwrap(),
602 ],
596 ) 603 )
597 } 604 }
598 605