diff options
Diffstat (limited to 'src/lib/client.rs')
| -rw-r--r-- | src/lib/client.rs | 140 |
1 files changed, 61 insertions, 79 deletions
diff --git a/src/lib/client.rs b/src/lib/client.rs index 32f5bd7..59ec583 100644 --- a/src/lib/client.rs +++ b/src/lib/client.rs | |||
| @@ -19,7 +19,7 @@ use std::{ | |||
| 19 | time::Duration, | 19 | time::Duration, |
| 20 | }; | 20 | }; |
| 21 | 21 | ||
| 22 | use anyhow::{bail, Context, Result}; | 22 | use anyhow::{Context, Result, bail}; |
| 23 | use async_trait::async_trait; | 23 | use async_trait::async_trait; |
| 24 | use console::Style; | 24 | use console::Style; |
| 25 | use futures::{ | 25 | use futures::{ |
| @@ -29,12 +29,12 @@ use futures::{ | |||
| 29 | use indicatif::{MultiProgress, ProgressBar, ProgressDrawTarget, ProgressState, ProgressStyle}; | 29 | use indicatif::{MultiProgress, ProgressBar, ProgressDrawTarget, ProgressState, ProgressStyle}; |
| 30 | #[cfg(test)] | 30 | #[cfg(test)] |
| 31 | use mockall::*; | 31 | use mockall::*; |
| 32 | use nostr::{nips::nip01::Coordinate, signer::SignerBackend, Event}; | 32 | use nostr::{Event, nips::nip01::Coordinate, signer::SignerBackend}; |
| 33 | use nostr_database::NostrEventsDatabase; | 33 | use nostr_database::NostrEventsDatabase; |
| 34 | use nostr_lmdb::NostrLMDB; | 34 | use nostr_lmdb::NostrLMDB; |
| 35 | use nostr_sdk::{ | 35 | use nostr_sdk::{ |
| 36 | prelude::RelayLimits, EventBuilder, EventId, Kind, NostrSigner, Options, PublicKey, RelayUrl, | 36 | EventBuilder, EventId, Kind, NostrSigner, Options, PublicKey, RelayUrl, SingleLetterTag, |
| 37 | SingleLetterTag, Timestamp, | 37 | Timestamp, prelude::RelayLimits, |
| 38 | }; | 38 | }; |
| 39 | 39 | ||
| 40 | use crate::{ | 40 | use crate::{ |
| @@ -894,18 +894,16 @@ pub async fn get_state_from_cache( | |||
| 894 | ) -> Result<RepoState> { | 894 | ) -> Result<RepoState> { |
| 895 | if let Some(git_repo_path) = git_repo_path { | 895 | if let Some(git_repo_path) = git_repo_path { |
| 896 | RepoState::try_from( | 896 | RepoState::try_from( |
| 897 | get_events_from_local_cache( | 897 | get_events_from_local_cache(git_repo_path, vec![get_filter_state_events( |
| 898 | git_repo_path, | 898 | &repo_ref.coordinates(), |
| 899 | vec![get_filter_state_events(&repo_ref.coordinates())], | 899 | )]) |
| 900 | ) | ||
| 901 | .await?, | 900 | .await?, |
| 902 | ) | 901 | ) |
| 903 | } else { | 902 | } else { |
| 904 | RepoState::try_from( | 903 | RepoState::try_from( |
| 905 | get_event_from_global_cache( | 904 | get_event_from_global_cache(git_repo_path, vec![get_filter_state_events( |
| 906 | git_repo_path, | 905 | &repo_ref.coordinates(), |
| 907 | vec![get_filter_state_events(&repo_ref.coordinates())], | 906 | )]) |
| 908 | ) | ||
| 909 | .await?, | 907 | .await?, |
| 910 | ) | 908 | ) |
| 911 | } | 909 | } |
| @@ -975,20 +973,17 @@ async fn create_relays_request( | |||
| 975 | } | 973 | } |
| 976 | 974 | ||
| 977 | if let Some(git_repo_path) = git_repo_path { | 975 | if let Some(git_repo_path) = git_repo_path { |
| 978 | for event in &get_events_from_local_cache( | 976 | for event in &get_events_from_local_cache(git_repo_path, vec![ |
| 979 | git_repo_path, | 977 | nostr::Filter::default() |
| 980 | vec![ | 978 | .kinds(vec![Kind::GitPatch]) |
| 981 | nostr::Filter::default() | 979 | .custom_tag( |
| 982 | .kinds(vec![Kind::GitPatch]) | 980 | SingleLetterTag::lowercase(nostr_sdk::Alphabet::A), |
| 983 | .custom_tag( | 981 | repo_coordinates_without_relays |
| 984 | SingleLetterTag::lowercase(nostr_sdk::Alphabet::A), | 982 | .iter() |
| 985 | repo_coordinates_without_relays | 983 | .map(std::string::ToString::to_string) |
| 986 | .iter() | 984 | .collect::<Vec<String>>(), |
| 987 | .map(std::string::ToString::to_string) | 985 | ), |
| 988 | .collect::<Vec<String>>(), | 986 | ]) |
| 989 | ), | ||
| 990 | ], | ||
| 991 | ) | ||
| 992 | .await? | 987 | .await? |
| 993 | { | 988 | { |
| 994 | if event_is_patch_set_root(event) || event_is_revision_root(event) { | 989 | if event_is_patch_set_root(event) || event_is_revision_root(event) { |
| @@ -998,11 +993,11 @@ async fn create_relays_request( | |||
| 998 | } | 993 | } |
| 999 | } | 994 | } |
| 1000 | 995 | ||
| 1001 | let profile_events = get_event_from_global_cache( | 996 | let profile_events = |
| 1002 | git_repo_path, | 997 | get_event_from_global_cache(git_repo_path, vec![get_filter_contributor_profiles( |
| 1003 | vec![get_filter_contributor_profiles(contributors.clone())], | 998 | contributors.clone(), |
| 1004 | ) | 999 | )]) |
| 1005 | .await?; | 1000 | .await?; |
| 1006 | for c in &contributors { | 1001 | for c in &contributors { |
| 1007 | if let Some(event) = profile_events | 1002 | if let Some(event) = profile_events |
| 1008 | .iter() | 1003 | .iter() |
| @@ -1564,20 +1559,17 @@ pub async fn get_proposals_and_revisions_from_cache( | |||
| 1564 | git_repo_path: &Path, | 1559 | git_repo_path: &Path, |
| 1565 | repo_coordinates: HashSet<Coordinate>, | 1560 | repo_coordinates: HashSet<Coordinate>, |
| 1566 | ) -> Result<Vec<nostr::Event>> { | 1561 | ) -> Result<Vec<nostr::Event>> { |
| 1567 | let mut proposals = get_events_from_local_cache( | 1562 | let mut proposals = get_events_from_local_cache(git_repo_path, vec![ |
| 1568 | git_repo_path, | 1563 | nostr::Filter::default() |
| 1569 | vec![ | 1564 | .kind(nostr::Kind::GitPatch) |
| 1570 | nostr::Filter::default() | 1565 | .custom_tag( |
| 1571 | .kind(nostr::Kind::GitPatch) | 1566 | nostr::SingleLetterTag::lowercase(nostr_sdk::Alphabet::A), |
| 1572 | .custom_tag( | 1567 | repo_coordinates |
| 1573 | nostr::SingleLetterTag::lowercase(nostr_sdk::Alphabet::A), | 1568 | .iter() |
| 1574 | repo_coordinates | 1569 | .map(std::string::ToString::to_string) |
| 1575 | .iter() | 1570 | .collect::<Vec<String>>(), |
| 1576 | .map(std::string::ToString::to_string) | 1571 | ), |
| 1577 | .collect::<Vec<String>>(), | 1572 | ]) |
| 1578 | ), | ||
| 1579 | ], | ||
| 1580 | ) | ||
| 1581 | .await? | 1573 | .await? |
| 1582 | .iter() | 1574 | .iter() |
| 1583 | .filter(|e| event_is_patch_set_root(e)) | 1575 | .filter(|e| event_is_patch_set_root(e)) |
| @@ -1593,29 +1585,23 @@ pub async fn get_all_proposal_patch_events_from_cache( | |||
| 1593 | repo_ref: &RepoRef, | 1585 | repo_ref: &RepoRef, |
| 1594 | proposal_id: &nostr::EventId, | 1586 | proposal_id: &nostr::EventId, |
| 1595 | ) -> Result<Vec<nostr::Event>> { | 1587 | ) -> Result<Vec<nostr::Event>> { |
| 1596 | let mut commit_events = get_events_from_local_cache( | 1588 | let mut commit_events = get_events_from_local_cache(git_repo_path, vec![ |
| 1597 | git_repo_path, | 1589 | nostr::Filter::default() |
| 1598 | vec![ | 1590 | .kind(nostr::Kind::GitPatch) |
| 1599 | nostr::Filter::default() | 1591 | .event(*proposal_id), |
| 1600 | .kind(nostr::Kind::GitPatch) | 1592 | nostr::Filter::default() |
| 1601 | .event(*proposal_id), | 1593 | .kind(nostr::Kind::GitPatch) |
| 1602 | nostr::Filter::default() | 1594 | .id(*proposal_id), |
| 1603 | .kind(nostr::Kind::GitPatch) | 1595 | ]) |
| 1604 | .id(*proposal_id), | ||
| 1605 | ], | ||
| 1606 | ) | ||
| 1607 | .await?; | 1596 | .await?; |
| 1608 | 1597 | ||
| 1609 | let permissioned_users: HashSet<PublicKey> = [ | 1598 | let permissioned_users: HashSet<PublicKey> = [repo_ref.maintainers.clone(), vec![ |
| 1610 | repo_ref.maintainers.clone(), | 1599 | commit_events |
| 1611 | vec![ | 1600 | .iter() |
| 1612 | commit_events | 1601 | .find(|e| e.id.eq(proposal_id)) |
| 1613 | .iter() | 1602 | .context("proposal not in cache")? |
| 1614 | .find(|e| e.id.eq(proposal_id)) | 1603 | .pubkey, |
| 1615 | .context("proposal not in cache")? | 1604 | ]] |
| 1616 | .pubkey, | ||
| 1617 | ], | ||
| 1618 | ] | ||
| 1619 | .concat() | 1605 | .concat() |
| 1620 | .iter() | 1606 | .iter() |
| 1621 | .copied() | 1607 | .copied() |
| @@ -1629,15 +1615,12 @@ pub async fn get_all_proposal_patch_events_from_cache( | |||
| 1629 | .collect(); | 1615 | .collect(); |
| 1630 | 1616 | ||
| 1631 | if !revision_roots.is_empty() { | 1617 | if !revision_roots.is_empty() { |
| 1632 | for event in get_events_from_local_cache( | 1618 | for event in get_events_from_local_cache(git_repo_path, vec![ |
| 1633 | git_repo_path, | 1619 | nostr::Filter::default() |
| 1634 | vec![ | 1620 | .kind(nostr::Kind::GitPatch) |
| 1635 | nostr::Filter::default() | 1621 | .events(revision_roots) |
| 1636 | .kind(nostr::Kind::GitPatch) | 1622 | .authors(permissioned_users.clone()), |
| 1637 | .events(revision_roots) | 1623 | ]) |
| 1638 | .authors(permissioned_users.clone()), | ||
| 1639 | ], | ||
| 1640 | ) | ||
| 1641 | .await? | 1624 | .await? |
| 1642 | { | 1625 | { |
| 1643 | commit_events.push(event); | 1626 | commit_events.push(event); |
| @@ -1652,10 +1635,9 @@ pub async fn get_all_proposal_patch_events_from_cache( | |||
| 1652 | } | 1635 | } |
| 1653 | 1636 | ||
| 1654 | pub async fn get_event_from_cache_by_id(git_repo: &Repo, event_id: &EventId) -> Result<Event> { | 1637 | pub async fn get_event_from_cache_by_id(git_repo: &Repo, event_id: &EventId) -> Result<Event> { |
| 1655 | Ok(get_events_from_local_cache( | 1638 | Ok(get_events_from_local_cache(git_repo.get_path()?, vec![ |
| 1656 | git_repo.get_path()?, | 1639 | nostr::Filter::default().id(*event_id), |
| 1657 | vec![nostr::Filter::default().id(*event_id)], | 1640 | ]) |
| 1658 | ) | ||
| 1659 | .await? | 1641 | .await? |
| 1660 | .first() | 1642 | .first() |
| 1661 | .context("failed to find event in cache")? | 1643 | .context("failed to find event in cache")? |