diff options
| author | DanConwayDev <DanConwayDev@protonmail.com> | 2024-07-18 12:14:09 +0100 |
|---|---|---|
| committer | DanConwayDev <DanConwayDev@protonmail.com> | 2024-07-18 12:31:08 +0100 |
| commit | 6f66fab0b5bab74e63c790d8964a0e6b4982000e (patch) | |
| tree | 4ed4675c78c192dfb13f544201b0cb7b37db0400 /src/login.rs | |
| parent | 10d98590ef9317be5b12a6a4830d7394d479c1d5 (diff) | |
refactor: fetch some profiles from user relays
add the ability to fetch more than just the current user from
their user write relays
Diffstat (limited to 'src/login.rs')
| -rw-r--r-- | src/login.rs | 111 |
1 files changed, 55 insertions, 56 deletions
diff --git a/src/login.rs b/src/login.rs index 6c3acfa..aac0141 100644 --- a/src/login.rs +++ b/src/login.rs | |||
| @@ -6,7 +6,7 @@ use nostr::{ | |||
| 6 | PublicKey, | 6 | PublicKey, |
| 7 | }; | 7 | }; |
| 8 | use nostr_sdk::{ | 8 | use nostr_sdk::{ |
| 9 | Alphabet, FromBech32, JsonUtil, Keys, Kind, NostrSigner, SingleLetterTag, ToBech32, Url, | 9 | Alphabet, FromBech32, JsonUtil, Keys, Kind, NostrSigner, SingleLetterTag, ToBech32, |
| 10 | }; | 10 | }; |
| 11 | use nostr_signer::Nip46Signer; | 11 | use nostr_signer::Nip46Signer; |
| 12 | 12 | ||
| @@ -57,7 +57,8 @@ pub async fn launch( | |||
| 57 | get_config_item(git_repo, "nostr.npub") | 57 | get_config_item(git_repo, "nostr.npub") |
| 58 | .unwrap_or("unknown ncryptsec".to_string()), | 58 | .unwrap_or("unknown ncryptsec".to_string()), |
| 59 | ) { | 59 | ) { |
| 60 | if let Ok(user_ref) = get_user_details(&public_key, client, git_repo).await | 60 | if let Ok(user_ref) = |
| 61 | get_user_details(&public_key, client, git_repo.get_path()?).await | ||
| 61 | { | 62 | { |
| 62 | user_ref.metadata.name | 63 | user_ref.metadata.name |
| 63 | } else { | 64 | } else { |
| @@ -92,7 +93,7 @@ pub async fn launch( | |||
| 92 | .await | 93 | .await |
| 93 | .context("cannot get public key from signer")?, | 94 | .context("cannot get public key from signer")?, |
| 94 | client, | 95 | client, |
| 95 | git_repo, | 96 | git_repo.get_path()?, |
| 96 | ) | 97 | ) |
| 97 | .await?; | 98 | .await?; |
| 98 | print_logged_in_as(&user_ref, client.is_none())?; | 99 | print_logged_in_as(&user_ref, client.is_none())?; |
| @@ -395,7 +396,7 @@ async fn fresh_login( | |||
| 395 | signer.public_key().await? | 396 | signer.public_key().await? |
| 396 | }; | 397 | }; |
| 397 | // lookup profile | 398 | // lookup profile |
| 398 | let user_ref = get_user_details(&public_key, client, git_repo).await?; | 399 | let user_ref = get_user_details(&public_key, client, git_repo.get_path()?).await?; |
| 399 | print_logged_in_as(&user_ref, client.is_none())?; | 400 | print_logged_in_as(&user_ref, client.is_none())?; |
| 400 | Ok((signer, user_ref)) | 401 | Ok((signer, user_ref)) |
| 401 | } | 402 | } |
| @@ -610,77 +611,75 @@ async fn get_user_details( | |||
| 610 | public_key: &PublicKey, | 611 | public_key: &PublicKey, |
| 611 | #[cfg(test)] client: Option<&crate::client::MockConnect>, | 612 | #[cfg(test)] client: Option<&crate::client::MockConnect>, |
| 612 | #[cfg(not(test))] client: Option<&Client>, | 613 | #[cfg(not(test))] client: Option<&Client>, |
| 613 | git_repo: &Repo, | 614 | git_repo_path: &Path, |
| 614 | ) -> Result<UserRef> { | 615 | ) -> Result<UserRef> { |
| 615 | let filters = vec![ | 616 | if let Ok(user_ref) = get_user_ref_from_cache(git_repo_path, public_key).await { |
| 616 | nostr::Filter::default() | 617 | Ok(user_ref) |
| 617 | .author(*public_key) | 618 | } else { |
| 618 | .kind(Kind::Metadata), | 619 | let empty = UserRef { |
| 619 | nostr::Filter::default() | 620 | public_key: public_key.to_owned(), |
| 620 | .author(*public_key) | 621 | metadata: extract_user_metadata(public_key, &[])?, |
| 621 | .kind(Kind::RelayList), | 622 | relays: extract_user_relays(public_key, &[]), |
| 622 | ]; | 623 | }; |
| 623 | |||
| 624 | let mut events = get_event_from_global_cache(git_repo.get_path()?, filters.clone()).await?; | ||
| 625 | 624 | ||
| 626 | if let Some(client) = client { | 625 | if let Some(client) = client { |
| 627 | if events.is_empty() { | ||
| 628 | let term = console::Term::stderr(); | 626 | let term = console::Term::stderr(); |
| 629 | term.write_line("searching for profile...")?; | 627 | term.write_line("searching for profile...")?; |
| 630 | let (_, progress_reporter) = client | 628 | let (_, progress_reporter) = client |
| 631 | .fetch_all( | 629 | .fetch_all( |
| 632 | git_repo.get_path()?, | 630 | git_repo_path, |
| 633 | &HashSet::new(), | 631 | &HashSet::new(), |
| 634 | &HashSet::from_iter(vec![*public_key]), | 632 | &HashSet::from_iter(vec![*public_key]), |
| 635 | ) | 633 | ) |
| 636 | .await?; | 634 | .await?; |
| 637 | events = get_event_from_global_cache(git_repo.get_path()?, filters).await?; | 635 | if let Ok(user_ref) = get_user_ref_from_cache(git_repo_path, public_key).await { |
| 638 | if !events.is_empty() { | ||
| 639 | progress_reporter.clear()?; | 636 | progress_reporter.clear()?; |
| 640 | // term.clear_last_lines(1)?; | 637 | // term.clear_last_lines(1)?; |
| 638 | Ok(user_ref) | ||
| 639 | } else { | ||
| 640 | Ok(empty) | ||
| 641 | } | 641 | } |
| 642 | } else { | ||
| 643 | Ok(empty) | ||
| 642 | } | 644 | } |
| 643 | } | 645 | } |
| 646 | } | ||
| 647 | pub async fn get_logged_in_user(git_repo_path: &Path) -> Result<Option<PublicKey>> { | ||
| 648 | let git_repo = Repo::from_path(&git_repo_path.to_path_buf())?; | ||
| 649 | Ok( | ||
| 650 | if let Some(npub) = git_repo.get_git_config_item("nostr.npub", None)? { | ||
| 651 | if let Ok(pubic_key) = PublicKey::parse(npub) { | ||
| 652 | Some(pubic_key) | ||
| 653 | } else { | ||
| 654 | None | ||
| 655 | } | ||
| 656 | } else { | ||
| 657 | None | ||
| 658 | }, | ||
| 659 | ) | ||
| 660 | } | ||
| 661 | |||
| 662 | pub async fn get_user_ref_from_cache( | ||
| 663 | git_repo_path: &Path, | ||
| 664 | public_key: &PublicKey, | ||
| 665 | ) -> Result<UserRef> { | ||
| 666 | let filters = vec![ | ||
| 667 | nostr::Filter::default() | ||
| 668 | .author(*public_key) | ||
| 669 | .kind(Kind::Metadata), | ||
| 670 | nostr::Filter::default() | ||
| 671 | .author(*public_key) | ||
| 672 | .kind(Kind::RelayList), | ||
| 673 | ]; | ||
| 674 | |||
| 675 | let events = get_event_from_global_cache(git_repo_path, filters.clone()).await?; | ||
| 644 | 676 | ||
| 677 | if events.is_empty() { | ||
| 678 | bail!("no metadata and profile list in cache for selected public key"); | ||
| 679 | } | ||
| 645 | Ok(UserRef { | 680 | Ok(UserRef { |
| 646 | public_key: public_key.to_owned(), | 681 | public_key: public_key.to_owned(), |
| 647 | metadata: extract_user_metadata(public_key, &events)?, | 682 | metadata: extract_user_metadata(public_key, &events)?, |
| 648 | relays: extract_user_relays(public_key, &events), | 683 | relays: extract_user_relays(public_key, &events), |
| 649 | }) | 684 | }) |
| 650 | } | 685 | } |
| 651 | |||
| 652 | pub async fn get_logged_in_user_and_relays_from_cache( | ||
| 653 | git_repo_path: &Path, | ||
| 654 | ) -> Result<(Option<PublicKey>, HashSet<Url>)> { | ||
| 655 | let git_repo = Repo::from_path(&git_repo_path.to_path_buf())?; | ||
| 656 | let current_user = if let Some(npub) = git_repo.get_git_config_item("nostr.npub", None)? { | ||
| 657 | if let Ok(pubic_key) = PublicKey::parse(npub) { | ||
| 658 | Some(pubic_key) | ||
| 659 | } else { | ||
| 660 | None | ||
| 661 | } | ||
| 662 | } else { | ||
| 663 | None | ||
| 664 | }; | ||
| 665 | let relays = if let Some(current_user) = current_user { | ||
| 666 | extract_user_relays( | ||
| 667 | ¤t_user, | ||
| 668 | &get_event_from_global_cache( | ||
| 669 | git_repo.get_path()?, | ||
| 670 | vec![ | ||
| 671 | nostr::Filter::default() | ||
| 672 | .author((*current_user).into()) | ||
| 673 | .kind(Kind::RelayList), | ||
| 674 | ], | ||
| 675 | ) | ||
| 676 | .await?, | ||
| 677 | ) | ||
| 678 | .write() | ||
| 679 | .iter() | ||
| 680 | .filter_map(|r| Url::parse(r).ok()) | ||
| 681 | .collect::<HashSet<Url>>() | ||
| 682 | } else { | ||
| 683 | HashSet::new() | ||
| 684 | }; | ||
| 685 | Ok((current_user, relays)) | ||
| 686 | } | ||