diff options
Diffstat (limited to 'src/lib/repo_ref.rs')
| -rw-r--r-- | src/lib/repo_ref.rs | 76 |
1 files changed, 55 insertions, 21 deletions
diff --git a/src/lib/repo_ref.rs b/src/lib/repo_ref.rs index 2ef4b8c..d566b43 100644 --- a/src/lib/repo_ref.rs +++ b/src/lib/repo_ref.rs | |||
| @@ -20,7 +20,7 @@ use crate::{ | |||
| 20 | git::{nostr_url::NostrUrlDecoded, Repo, RepoActions}, | 20 | git::{nostr_url::NostrUrlDecoded, Repo, RepoActions}, |
| 21 | }; | 21 | }; |
| 22 | 22 | ||
| 23 | #[derive(Default, Clone)] | 23 | #[derive(Clone)] |
| 24 | pub struct RepoRef { | 24 | pub struct RepoRef { |
| 25 | pub name: String, | 25 | pub name: String, |
| 26 | pub description: String, | 26 | pub description: String, |
| @@ -30,18 +30,30 @@ pub struct RepoRef { | |||
| 30 | pub web: Vec<String>, | 30 | pub web: Vec<String>, |
| 31 | pub relays: Vec<RelayUrl>, | 31 | pub relays: Vec<RelayUrl>, |
| 32 | pub maintainers: Vec<PublicKey>, | 32 | pub maintainers: Vec<PublicKey>, |
| 33 | pub trusted_maintainer: PublicKey, | ||
| 33 | pub events: HashMap<Coordinate, nostr::Event>, | 34 | pub events: HashMap<Coordinate, nostr::Event>, |
| 34 | // code languages and hashtags | ||
| 35 | } | 35 | } |
| 36 | 36 | ||
| 37 | impl TryFrom<nostr::Event> for RepoRef { | 37 | impl TryFrom<(nostr::Event, Option<PublicKey>)> for RepoRef { |
| 38 | type Error = anyhow::Error; | 38 | type Error = anyhow::Error; |
| 39 | 39 | ||
| 40 | fn try_from(event: nostr::Event) -> Result<Self> { | 40 | fn try_from((event, trusted_maintainer): (nostr::Event, Option<PublicKey>)) -> Result<Self> { |
| 41 | if !event.kind.eq(&Kind::GitRepoAnnouncement) { | 41 | if !event.kind.eq(&Kind::GitRepoAnnouncement) { |
| 42 | bail!("incorrect kind"); | 42 | bail!("incorrect kind"); |
| 43 | } | 43 | } |
| 44 | let mut r = Self::default(); | 44 | |
| 45 | let mut r = Self { | ||
| 46 | name: String::new(), | ||
| 47 | description: String::new(), | ||
| 48 | identifier: String::new(), | ||
| 49 | root_commit: String::new(), | ||
| 50 | git_server: Vec::new(), | ||
| 51 | web: Vec::new(), | ||
| 52 | relays: Vec::new(), | ||
| 53 | maintainers: Vec::new(), | ||
| 54 | trusted_maintainer: trusted_maintainer.unwrap_or(event.pubkey), | ||
| 55 | events: HashMap::new(), | ||
| 56 | }; | ||
| 45 | 57 | ||
| 46 | for tag in event.tags.iter() { | 58 | for tag in event.tags.iter() { |
| 47 | match tag.as_slice() { | 59 | match tag.as_slice() { |
| @@ -183,11 +195,7 @@ impl RepoRef { | |||
| 183 | pub fn coordinate_with_hint(&self) -> Coordinate { | 195 | pub fn coordinate_with_hint(&self) -> Coordinate { |
| 184 | Coordinate { | 196 | Coordinate { |
| 185 | kind: Kind::GitRepoAnnouncement, | 197 | kind: Kind::GitRepoAnnouncement, |
| 186 | public_key: *self | 198 | public_key: self.trusted_maintainer, |
| 187 | .maintainers | ||
| 188 | .first() | ||
| 189 | .context("no maintainers in repo ref") | ||
| 190 | .unwrap(), | ||
| 191 | identifier: self.identifier.clone(), | 199 | identifier: self.identifier.clone(), |
| 192 | relays: if let Some(relay) = self.relays.first() { | 200 | relays: if let Some(relay) = self.relays.first() { |
| 193 | vec![relay.clone()] | 201 | vec![relay.clone()] |
| @@ -204,6 +212,18 @@ impl RepoRef { | |||
| 204 | .map(|c| (c.clone(), self.events.get(c).map(|e| e.created_at))) | 212 | .map(|c| (c.clone(), self.events.get(c).map(|e| e.created_at))) |
| 205 | .collect::<Vec<(Coordinate, Option<Timestamp>)>>() | 213 | .collect::<Vec<(Coordinate, Option<Timestamp>)>>() |
| 206 | } | 214 | } |
| 215 | |||
| 216 | pub fn to_nostr_git_url(&self) -> String { | ||
| 217 | format!( | ||
| 218 | "{}", | ||
| 219 | NostrUrlDecoded { | ||
| 220 | original_string: String::new(), | ||
| 221 | coordinates: HashSet::from_iter(vec![self.coordinate_with_hint()]), | ||
| 222 | protocol: None, | ||
| 223 | user: None, | ||
| 224 | } | ||
| 225 | ) | ||
| 226 | } | ||
| 207 | } | 227 | } |
| 208 | 228 | ||
| 209 | pub async fn get_repo_coordinates( | 229 | pub async fn get_repo_coordinates( |
| @@ -469,6 +489,7 @@ mod tests { | |||
| 469 | RelayUrl::parse("ws://relay1.io").unwrap(), | 489 | RelayUrl::parse("ws://relay1.io").unwrap(), |
| 470 | RelayUrl::parse("ws://relay2.io").unwrap(), | 490 | RelayUrl::parse("ws://relay2.io").unwrap(), |
| 471 | ], | 491 | ], |
| 492 | trusted_maintainer: TEST_KEY_1_KEYS.public_key(), | ||
| 472 | maintainers: vec![TEST_KEY_1_KEYS.public_key(), TEST_KEY_2_KEYS.public_key()], | 493 | maintainers: vec![TEST_KEY_1_KEYS.public_key(), TEST_KEY_2_KEYS.public_key()], |
| 473 | events: HashMap::new(), | 494 | events: HashMap::new(), |
| 474 | } | 495 | } |
| @@ -482,20 +503,27 @@ mod tests { | |||
| 482 | #[tokio::test] | 503 | #[tokio::test] |
| 483 | async fn identifier() { | 504 | async fn identifier() { |
| 484 | assert_eq!( | 505 | assert_eq!( |
| 485 | RepoRef::try_from(create().await).unwrap().identifier, | 506 | RepoRef::try_from((create().await, None)) |
| 507 | .unwrap() | ||
| 508 | .identifier, | ||
| 486 | "123412341", | 509 | "123412341", |
| 487 | ) | 510 | ) |
| 488 | } | 511 | } |
| 489 | 512 | ||
| 490 | #[tokio::test] | 513 | #[tokio::test] |
| 491 | async fn name() { | 514 | async fn name() { |
| 492 | assert_eq!(RepoRef::try_from(create().await).unwrap().name, "test name",) | 515 | assert_eq!( |
| 516 | RepoRef::try_from((create().await, None)).unwrap().name, | ||
| 517 | "test name", | ||
| 518 | ) | ||
| 493 | } | 519 | } |
| 494 | 520 | ||
| 495 | #[tokio::test] | 521 | #[tokio::test] |
| 496 | async fn description() { | 522 | async fn description() { |
| 497 | assert_eq!( | 523 | assert_eq!( |
| 498 | RepoRef::try_from(create().await).unwrap().description, | 524 | RepoRef::try_from((create().await, None)) |
| 525 | .unwrap() | ||
| 526 | .description, | ||
| 499 | "test description", | 527 | "test description", |
| 500 | ) | 528 | ) |
| 501 | } | 529 | } |
| @@ -503,7 +531,9 @@ mod tests { | |||
| 503 | #[tokio::test] | 531 | #[tokio::test] |
| 504 | async fn root_commit_is_r_tag() { | 532 | async fn root_commit_is_r_tag() { |
| 505 | assert_eq!( | 533 | assert_eq!( |
| 506 | RepoRef::try_from(create().await).unwrap().root_commit, | 534 | RepoRef::try_from((create().await, None)) |
| 535 | .unwrap() | ||
| 536 | .root_commit, | ||
| 507 | "5e664e5a7845cd1373c79f580ca4fe29ab5b34d2", | 537 | "5e664e5a7845cd1373c79f580ca4fe29ab5b34d2", |
| 508 | ) | 538 | ) |
| 509 | } | 539 | } |
| @@ -526,7 +556,7 @@ mod tests { | |||
| 526 | async fn less_than_40_characters() { | 556 | async fn less_than_40_characters() { |
| 527 | let s = "5e664e5a7845cd1373"; | 557 | let s = "5e664e5a7845cd1373"; |
| 528 | assert_eq!( | 558 | assert_eq!( |
| 529 | RepoRef::try_from(create_with_incorrect_first_commit_ref(s).await) | 559 | RepoRef::try_from((create_with_incorrect_first_commit_ref(s).await, None)) |
| 530 | .unwrap() | 560 | .unwrap() |
| 531 | .root_commit, | 561 | .root_commit, |
| 532 | "", | 562 | "", |
| @@ -537,7 +567,7 @@ mod tests { | |||
| 537 | async fn more_than_40_characters() { | 567 | async fn more_than_40_characters() { |
| 538 | let s = "5e664e5a7845cd1373c79f580ca4fe29ab5b34d2111111111"; | 568 | let s = "5e664e5a7845cd1373c79f580ca4fe29ab5b34d2111111111"; |
| 539 | assert_eq!( | 569 | assert_eq!( |
| 540 | RepoRef::try_from(create_with_incorrect_first_commit_ref(s).await) | 570 | RepoRef::try_from((create_with_incorrect_first_commit_ref(s).await, None)) |
| 541 | .unwrap() | 571 | .unwrap() |
| 542 | .root_commit, | 572 | .root_commit, |
| 543 | "", | 573 | "", |
| @@ -548,7 +578,7 @@ mod tests { | |||
| 548 | async fn not_hex_characters() { | 578 | async fn not_hex_characters() { |
| 549 | let s = "xxx64e5a7845cd1373c79f580ca4fe29ab5b34d2"; | 579 | let s = "xxx64e5a7845cd1373c79f580ca4fe29ab5b34d2"; |
| 550 | assert_eq!( | 580 | assert_eq!( |
| 551 | RepoRef::try_from(create_with_incorrect_first_commit_ref(s).await) | 581 | RepoRef::try_from((create_with_incorrect_first_commit_ref(s).await, None)) |
| 552 | .unwrap() | 582 | .unwrap() |
| 553 | .root_commit, | 583 | .root_commit, |
| 554 | "", | 584 | "", |
| @@ -559,7 +589,9 @@ mod tests { | |||
| 559 | #[tokio::test] | 589 | #[tokio::test] |
| 560 | async fn git_server() { | 590 | async fn git_server() { |
| 561 | assert_eq!( | 591 | assert_eq!( |
| 562 | RepoRef::try_from(create().await).unwrap().git_server, | 592 | RepoRef::try_from((create().await, None)) |
| 593 | .unwrap() | ||
| 594 | .git_server, | ||
| 563 | vec!["https://localhost:1000"], | 595 | vec!["https://localhost:1000"], |
| 564 | ) | 596 | ) |
| 565 | } | 597 | } |
| @@ -567,7 +599,7 @@ mod tests { | |||
| 567 | #[tokio::test] | 599 | #[tokio::test] |
| 568 | async fn web() { | 600 | async fn web() { |
| 569 | assert_eq!( | 601 | assert_eq!( |
| 570 | RepoRef::try_from(create().await).unwrap().web, | 602 | RepoRef::try_from((create().await, None)).unwrap().web, |
| 571 | vec![ | 603 | vec![ |
| 572 | "https://exampleproject.xyz".to_string(), | 604 | "https://exampleproject.xyz".to_string(), |
| 573 | "https://gitworkshop.dev/123".to_string() | 605 | "https://gitworkshop.dev/123".to_string() |
| @@ -578,7 +610,7 @@ mod tests { | |||
| 578 | #[tokio::test] | 610 | #[tokio::test] |
| 579 | async fn relays() { | 611 | async fn relays() { |
| 580 | assert_eq!( | 612 | assert_eq!( |
| 581 | RepoRef::try_from(create().await).unwrap().relays, | 613 | RepoRef::try_from((create().await, None)).unwrap().relays, |
| 582 | vec![ | 614 | vec![ |
| 583 | RelayUrl::parse("ws://relay1.io").unwrap(), | 615 | RelayUrl::parse("ws://relay1.io").unwrap(), |
| 584 | RelayUrl::parse("ws://relay2.io").unwrap(), | 616 | RelayUrl::parse("ws://relay2.io").unwrap(), |
| @@ -589,7 +621,9 @@ mod tests { | |||
| 589 | #[tokio::test] | 621 | #[tokio::test] |
| 590 | async fn maintainers() { | 622 | async fn maintainers() { |
| 591 | assert_eq!( | 623 | assert_eq!( |
| 592 | RepoRef::try_from(create().await).unwrap().maintainers, | 624 | RepoRef::try_from((create().await, None)) |
| 625 | .unwrap() | ||
| 626 | .maintainers, | ||
| 593 | vec![TEST_KEY_1_KEYS.public_key(), TEST_KEY_2_KEYS.public_key()], | 627 | vec![TEST_KEY_1_KEYS.public_key(), TEST_KEY_2_KEYS.public_key()], |
| 594 | ) | 628 | ) |
| 595 | } | 629 | } |