diff options
| author | DanConwayDev <DanConwayDev@protonmail.com> | 2023-11-01 00:00:00 +0000 |
|---|---|---|
| committer | DanConwayDev <DanConwayDev@protonmail.com> | 2023-11-01 00:00:00 +0000 |
| commit | 57321aa9136293b24757a6695a5c92087af539ab (patch) | |
| tree | 2867d02430e346b13f7abcbb1022113d4fac31fa /src/repo_ref.rs | |
| parent | 82f1ba360b60c8339c7caea0dec7903ee5e764cd (diff) | |
feat(prs-create) send to repo relays
fetch repository reference events to identify repository relays
send pr events to repository relays alongside user relays
Diffstat (limited to 'src/repo_ref.rs')
| -rw-r--r-- | src/repo_ref.rs | 105 |
1 files changed, 103 insertions, 2 deletions
diff --git a/src/repo_ref.rs b/src/repo_ref.rs index 3b0b1b4..a92b5b3 100644 --- a/src/repo_ref.rs +++ b/src/repo_ref.rs | |||
| @@ -1,6 +1,12 @@ | |||
| 1 | use anyhow::{Context, Result}; | 1 | use anyhow::{bail, Context, Result}; |
| 2 | use nostr::Tag; | 2 | use nostr::Tag; |
| 3 | 3 | ||
| 4 | #[cfg(not(test))] | ||
| 5 | use crate::client::Client; | ||
| 6 | use crate::client::Connect; | ||
| 7 | #[cfg(test)] | ||
| 8 | use crate::client::MockConnect; | ||
| 9 | |||
| 4 | #[derive(Default)] | 10 | #[derive(Default)] |
| 5 | pub struct RepoRef { | 11 | pub struct RepoRef { |
| 6 | pub name: String, | 12 | pub name: String, |
| @@ -12,10 +18,43 @@ pub struct RepoRef { | |||
| 12 | // code languages and hashtags | 18 | // code languages and hashtags |
| 13 | } | 19 | } |
| 14 | 20 | ||
| 21 | impl TryFrom<nostr::Event> for RepoRef { | ||
| 22 | type Error = anyhow::Error; | ||
| 23 | |||
| 24 | fn try_from(event: nostr::Event) -> Result<Self> { | ||
| 25 | if !event.kind.as_u64().eq(&REPO_REF_KIND) { | ||
| 26 | bail!("incorrect kind"); | ||
| 27 | } | ||
| 28 | let mut r = Self::default(); | ||
| 29 | |||
| 30 | if let Some(t) = event.tags.iter().find(|t| t.as_vec()[0].eq("name")) { | ||
| 31 | r.name = t.as_vec()[1].clone(); | ||
| 32 | } | ||
| 33 | |||
| 34 | if let Some(t) = event.tags.iter().find(|t| t.as_vec()[0].eq("description")) { | ||
| 35 | r.description = t.as_vec()[1].clone(); | ||
| 36 | } | ||
| 37 | |||
| 38 | if let Some(t) = event.tags.iter().find(|t| t.as_vec()[0].eq("d")) { | ||
| 39 | r.root_commit = t.as_vec()[1].clone(); | ||
| 40 | } | ||
| 41 | |||
| 42 | r.relays = event | ||
| 43 | .tags | ||
| 44 | .iter() | ||
| 45 | .filter(|t| t.as_vec()[0].eq("relay")) | ||
| 46 | .map(|t| t.as_vec()[1].clone()) | ||
| 47 | .collect(); | ||
| 48 | |||
| 49 | Ok(r) | ||
| 50 | } | ||
| 51 | } | ||
| 52 | static REPO_REF_KIND: u64 = 300_317; | ||
| 53 | |||
| 15 | impl RepoRef { | 54 | impl RepoRef { |
| 16 | pub fn to_event(&self, keys: &nostr::Keys) -> Result<nostr::Event> { | 55 | pub fn to_event(&self, keys: &nostr::Keys) -> Result<nostr::Event> { |
| 17 | nostr_sdk::EventBuilder::new( | 56 | nostr_sdk::EventBuilder::new( |
| 18 | nostr::event::Kind::Custom(30017), | 57 | nostr::event::Kind::Custom(REPO_REF_KIND), |
| 19 | "", | 58 | "", |
| 20 | &[ | 59 | &[ |
| 21 | vec![ | 60 | vec![ |
| @@ -36,6 +75,36 @@ impl RepoRef { | |||
| 36 | } | 75 | } |
| 37 | } | 76 | } |
| 38 | 77 | ||
| 78 | pub async fn fetch( | ||
| 79 | root_commit: String, | ||
| 80 | #[cfg(test)] client: &MockConnect, | ||
| 81 | #[cfg(not(test))] client: &Client, | ||
| 82 | // TODO: more rubust way of finding repo events | ||
| 83 | relays: Vec<String>, | ||
| 84 | ) -> Result<RepoRef> { | ||
| 85 | // TODO: fetch relay information from file | ||
| 86 | |||
| 87 | let events: Vec<nostr::Event> = client | ||
| 88 | .get_events( | ||
| 89 | relays, | ||
| 90 | vec![ | ||
| 91 | nostr::Filter::default() | ||
| 92 | .kind(nostr::Kind::Custom(REPO_REF_KIND)) | ||
| 93 | .identifier(root_commit), | ||
| 94 | ], | ||
| 95 | ) | ||
| 96 | .await?; | ||
| 97 | |||
| 98 | RepoRef::try_from( | ||
| 99 | events | ||
| 100 | .iter() | ||
| 101 | .filter(|e| e.kind.as_u64() == REPO_REF_KIND) | ||
| 102 | .max_by_key(|e| e.created_at) | ||
| 103 | .context("cannot find repository reference event")? | ||
| 104 | .clone(), | ||
| 105 | ) | ||
| 106 | } | ||
| 107 | |||
| 39 | #[cfg(test)] | 108 | #[cfg(test)] |
| 40 | mod tests { | 109 | mod tests { |
| 41 | use test_utils::*; | 110 | use test_utils::*; |
| @@ -52,6 +121,38 @@ mod tests { | |||
| 52 | .to_event(&TEST_KEY_1_KEYS) | 121 | .to_event(&TEST_KEY_1_KEYS) |
| 53 | .unwrap() | 122 | .unwrap() |
| 54 | } | 123 | } |
| 124 | mod try_from { | ||
| 125 | use super::*; | ||
| 126 | |||
| 127 | #[test] | ||
| 128 | fn name() { | ||
| 129 | assert_eq!(RepoRef::try_from(create()).unwrap().name, "test name",) | ||
| 130 | } | ||
| 131 | |||
| 132 | #[test] | ||
| 133 | fn description() { | ||
| 134 | assert_eq!( | ||
| 135 | RepoRef::try_from(create()).unwrap().description, | ||
| 136 | "test description", | ||
| 137 | ) | ||
| 138 | } | ||
| 139 | |||
| 140 | #[test] | ||
| 141 | fn root_commit() { | ||
| 142 | assert_eq!( | ||
| 143 | RepoRef::try_from(create()).unwrap().root_commit, | ||
| 144 | "23471389461", | ||
| 145 | ) | ||
| 146 | } | ||
| 147 | |||
| 148 | #[test] | ||
| 149 | fn relays() { | ||
| 150 | assert_eq!( | ||
| 151 | RepoRef::try_from(create()).unwrap().relays, | ||
| 152 | vec!["ws://relay1.io".to_string(), "ws://relay2.io".to_string()], | ||
| 153 | ) | ||
| 154 | } | ||
| 155 | } | ||
| 55 | 156 | ||
| 56 | mod to_event { | 157 | mod to_event { |
| 57 | use super::*; | 158 | use super::*; |