diff options
| author | DanConwayDev <DanConwayDev@protonmail.com> | 2024-07-19 22:28:15 +0100 |
|---|---|---|
| committer | DanConwayDev <DanConwayDev@protonmail.com> | 2024-07-19 22:28:15 +0100 |
| commit | 118d3d86efe155ee6525aba60711e100636a6646 (patch) | |
| tree | 28ec40ed687cd564c8b0dba65333b30fd22af549 /src/repo_ref.rs | |
| parent | 29a093993ce7d0210ac39ceb1a25acc9350492e7 (diff) | |
feat: intergrate `fetch` into `init`
as part of a project to use fetch and the stored cache everywhere
Diffstat (limited to 'src/repo_ref.rs')
| -rw-r--r-- | src/repo_ref.rs | 103 |
1 files changed, 13 insertions, 90 deletions
diff --git a/src/repo_ref.rs b/src/repo_ref.rs index 5d754f2..a1ef11a 100644 --- a/src/repo_ref.rs +++ b/src/repo_ref.rs | |||
| @@ -6,17 +6,12 @@ use std::{ | |||
| 6 | }; | 6 | }; |
| 7 | 7 | ||
| 8 | use anyhow::{bail, Context, Result}; | 8 | use anyhow::{bail, Context, Result}; |
| 9 | use nostr::{ | 9 | use nostr::{nips::nip01::Coordinate, FromBech32, PublicKey, Tag, TagStandard, ToBech32}; |
| 10 | nips::{nip01::Coordinate, nip19::Nip19}, | ||
| 11 | FromBech32, PublicKey, Tag, TagStandard, ToBech32, | ||
| 12 | }; | ||
| 13 | use nostr_sdk::{Kind, NostrSigner, Timestamp}; | 10 | use nostr_sdk::{Kind, NostrSigner, Timestamp}; |
| 14 | use serde::{Deserialize, Serialize}; | 11 | use serde::{Deserialize, Serialize}; |
| 15 | 12 | ||
| 16 | #[cfg(not(test))] | 13 | #[cfg(not(test))] |
| 17 | use crate::client::Client; | 14 | use crate::client::Client; |
| 18 | #[cfg(test)] | ||
| 19 | use crate::client::MockConnect; | ||
| 20 | use crate::{ | 15 | use crate::{ |
| 21 | cli_interactor::{Interactor, InteractorPrompt, PromptInputParms}, | 16 | cli_interactor::{Interactor, InteractorPrompt, PromptInputParms}, |
| 22 | client::{get_event_from_global_cache, get_events_from_cache, sign_event, Connect}, | 17 | client::{get_event_from_global_cache, get_events_from_cache, sign_event, Connect}, |
| @@ -224,6 +219,15 @@ pub async fn get_repo_coordinates( | |||
| 224 | #[cfg(test)] client: &crate::client::MockConnect, | 219 | #[cfg(test)] client: &crate::client::MockConnect, |
| 225 | #[cfg(not(test))] client: &Client, | 220 | #[cfg(not(test))] client: &Client, |
| 226 | ) -> Result<HashSet<Coordinate>> { | 221 | ) -> Result<HashSet<Coordinate>> { |
| 222 | try_and_get_repo_coordinates(git_repo, client, true).await | ||
| 223 | } | ||
| 224 | |||
| 225 | pub async fn try_and_get_repo_coordinates( | ||
| 226 | git_repo: &Repo, | ||
| 227 | #[cfg(test)] client: &crate::client::MockConnect, | ||
| 228 | #[cfg(not(test))] client: &Client, | ||
| 229 | prompt_user: bool, | ||
| 230 | ) -> Result<HashSet<Coordinate>> { | ||
| 227 | let mut repo_coordinates = HashSet::new(); | 231 | let mut repo_coordinates = HashSet::new(); |
| 228 | 232 | ||
| 229 | if let Some(repo_override) = git_repo.get_git_config_item("nostr.repo", Some(false))? { | 233 | if let Some(repo_override) = git_repo.get_git_config_item("nostr.repo", Some(false))? { |
| @@ -319,6 +323,9 @@ pub async fn get_repo_coordinates( | |||
| 319 | } | 323 | } |
| 320 | 324 | ||
| 321 | if repo_coordinates.is_empty() { | 325 | if repo_coordinates.is_empty() { |
| 326 | if !prompt_user { | ||
| 327 | bail!("couldn't find repo coordinates in git config nostr.repo or in maintainers.yaml"); | ||
| 328 | } | ||
| 322 | // TODO: present list of events filter by root_commit | 329 | // TODO: present list of events filter by root_commit |
| 323 | // TODO: fallback to search based on identifier | 330 | // TODO: fallback to search based on identifier |
| 324 | let c = ask_for_naddr()?; | 331 | let c = ask_for_naddr()?; |
| @@ -343,90 +350,6 @@ fn ask_for_naddr() -> Result<Coordinate> { | |||
| 343 | }) | 350 | }) |
| 344 | } | 351 | } |
| 345 | 352 | ||
| 346 | pub async fn fetch( | ||
| 347 | git_repo: &Repo, | ||
| 348 | root_commit: String, | ||
| 349 | #[cfg(test)] client: &MockConnect, | ||
| 350 | #[cfg(not(test))] client: &Client, | ||
| 351 | // TODO: more rubust way of finding repo events | ||
| 352 | fallback_relays: Vec<String>, | ||
| 353 | prompt_for_nevent_if_cant_event: bool, | ||
| 354 | ) -> Result<RepoRef> { | ||
| 355 | let repo_config = get_repo_config_from_yaml(git_repo); | ||
| 356 | |||
| 357 | // TODO: check events only from maintainers. get relay list of maintainters. | ||
| 358 | // check those relays. | ||
| 359 | |||
| 360 | let mut repo_event_filter = nostr::Filter::default() | ||
| 361 | .kind(nostr::Kind::Custom(REPO_REF_KIND)) | ||
| 362 | .reference(root_commit); | ||
| 363 | |||
| 364 | let mut relays = fallback_relays; | ||
| 365 | if let Ok(repo_config) = repo_config { | ||
| 366 | repo_event_filter = | ||
| 367 | repo_event_filter.authors(extract_pks(repo_config.maintainers.clone())?); | ||
| 368 | relays = repo_config.relays.clone(); | ||
| 369 | } | ||
| 370 | |||
| 371 | let event = loop { | ||
| 372 | let events: Vec<nostr::Event> = client | ||
| 373 | .get_events(relays.clone(), vec![repo_event_filter.clone()]) | ||
| 374 | .await?; | ||
| 375 | |||
| 376 | // TODO: if maintainers.yaml isn't present, as the user to select from the | ||
| 377 | // pubkeys they want to use. could use WoT as an indicator as well as the repo | ||
| 378 | // and user name. | ||
| 379 | |||
| 380 | // TODO: if maintainers.yaml isn't present, save the selected repo pubkey | ||
| 381 | // somewhere within .git folder for future use and seek to get that next time | ||
| 382 | if let Some(event) = events | ||
| 383 | .iter() | ||
| 384 | .filter(|e| e.kind.as_u16() == REPO_REF_KIND) | ||
| 385 | .max_by_key(|e| e.created_at) | ||
| 386 | { | ||
| 387 | break event.clone(); | ||
| 388 | } | ||
| 389 | if !prompt_for_nevent_if_cant_event { | ||
| 390 | bail!("cannot find repo event"); | ||
| 391 | } | ||
| 392 | println!("cannot find repo event"); | ||
| 393 | loop { | ||
| 394 | let bech32 = Interactor::default() | ||
| 395 | .input(PromptInputParms::default().with_prompt("repository naddr or nevent"))?; | ||
| 396 | if let Ok(nip19) = Nip19::from_bech32(bech32) { | ||
| 397 | repo_event_filter = | ||
| 398 | nostr::Filter::default().kind(nostr::Kind::Custom(REPO_REF_KIND)); | ||
| 399 | match nip19 { | ||
| 400 | Nip19::Coordinate(c) => { | ||
| 401 | repo_event_filter = repo_event_filter | ||
| 402 | .identifier(c.identifier) | ||
| 403 | .author(c.public_key); | ||
| 404 | for r in c.relays { | ||
| 405 | relays.push(r); | ||
| 406 | } | ||
| 407 | } | ||
| 408 | Nip19::Event(n) => { | ||
| 409 | if let Some(author) = n.author { | ||
| 410 | repo_event_filter = repo_event_filter.id(n.event_id).author(author); | ||
| 411 | } | ||
| 412 | for r in n.relays { | ||
| 413 | relays.push(r); | ||
| 414 | } | ||
| 415 | } | ||
| 416 | Nip19::EventId(id) => repo_event_filter = repo_event_filter.id(id), | ||
| 417 | _ => (), | ||
| 418 | } | ||
| 419 | } else { | ||
| 420 | println!("not a valid nevent or naddr"); | ||
| 421 | continue; | ||
| 422 | } | ||
| 423 | break; | ||
| 424 | } | ||
| 425 | }; | ||
| 426 | |||
| 427 | RepoRef::try_from(event.clone()).context("cannot parse event as repo reference") | ||
| 428 | } | ||
| 429 | |||
| 430 | #[derive(Serialize, Deserialize, Default, Clone, Debug, PartialEq, Eq)] | 353 | #[derive(Serialize, Deserialize, Default, Clone, Debug, PartialEq, Eq)] |
| 431 | pub struct RepoConfigYaml { | 354 | pub struct RepoConfigYaml { |
| 432 | pub identifier: Option<String>, | 355 | pub identifier: Option<String>, |