diff options
| author | DanConwayDev <DanConwayDev@protonmail.com> | 2023-10-01 00:00:00 +0100 |
|---|---|---|
| committer | DanConwayDev <DanConwayDev@protonmail.com> | 2023-10-01 00:00:00 +0100 |
| commit | e237328ec611a5891586530c1d3cb26c16c1093b (patch) | |
| tree | 22ac36baa240354d06ae82eb070609fa3e3fcb82 /src/config.rs | |
| parent | 000901c0cbca8464b5a89bcc93c5474f6564bafd (diff) | |
feat(login) fetch user relays and metadata
get user relay list and metadata events from relays when keys are
used and last fetch attempt was more than an hour ago
uses user's write relays if known, otherwise uses fallback relays
to achieve this a method for intergration testing event fetching
from relays was added
Diffstat (limited to 'src/config.rs')
| -rw-r--r-- | src/config.rs | 62 |
1 files changed, 61 insertions, 1 deletions
diff --git a/src/config.rs b/src/config.rs index f410934..2370e34 100644 --- a/src/config.rs +++ b/src/config.rs | |||
| @@ -4,7 +4,7 @@ use anyhow::{anyhow, Context, Result}; | |||
| 4 | use directories::ProjectDirs; | 4 | use directories::ProjectDirs; |
| 5 | #[cfg(test)] | 5 | #[cfg(test)] |
| 6 | use mockall::*; | 6 | use mockall::*; |
| 7 | use nostr::secp256k1::XOnlyPublicKey; | 7 | use nostr::{secp256k1::XOnlyPublicKey, ToBech32}; |
| 8 | use serde::{self, Deserialize, Serialize}; | 8 | use serde::{self, Deserialize, Serialize}; |
| 9 | 9 | ||
| 10 | #[derive(Default)] | 10 | #[derive(Default)] |
| @@ -71,6 +71,66 @@ pub struct MyConfig { | |||
| 71 | pub struct UserRef { | 71 | pub struct UserRef { |
| 72 | pub public_key: XOnlyPublicKey, | 72 | pub public_key: XOnlyPublicKey, |
| 73 | pub encrypted_key: String, | 73 | pub encrypted_key: String, |
| 74 | pub metadata: UserMetadata, | ||
| 75 | pub relays: UserRelays, | ||
| 76 | pub last_checked: u64, | ||
| 77 | } | ||
| 78 | |||
| 79 | impl UserRef { | ||
| 80 | pub fn new(public_key: XOnlyPublicKey, encrypted_key: String) -> Self { | ||
| 81 | Self { | ||
| 82 | public_key, | ||
| 83 | encrypted_key, | ||
| 84 | relays: UserRelays { | ||
| 85 | relays: vec![], | ||
| 86 | created_at: 0, | ||
| 87 | }, | ||
| 88 | metadata: UserMetadata { | ||
| 89 | #[allow(clippy::expect_used)] | ||
| 90 | name: public_key | ||
| 91 | .to_bech32() | ||
| 92 | .expect("public key should always produce bech32"), | ||
| 93 | // name: format!( | ||
| 94 | // "{}", | ||
| 95 | // public_key | ||
| 96 | // .to_bech32() | ||
| 97 | // .expect("public key should always produce bech32"), | ||
| 98 | // ) | ||
| 99 | // .as_str()[..10].to_string(), | ||
| 100 | created_at: 0, | ||
| 101 | }, | ||
| 102 | last_checked: 0, | ||
| 103 | } | ||
| 104 | } | ||
| 105 | } | ||
| 106 | |||
| 107 | #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] | ||
| 108 | pub struct UserMetadata { | ||
| 109 | pub name: String, | ||
| 110 | pub created_at: u64, | ||
| 111 | } | ||
| 112 | |||
| 113 | #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] | ||
| 114 | pub struct UserRelays { | ||
| 115 | pub relays: Vec<UserRelayRef>, | ||
| 116 | pub created_at: u64, | ||
| 117 | } | ||
| 118 | |||
| 119 | impl UserRelays { | ||
| 120 | pub fn write(&self) -> Vec<String> { | ||
| 121 | self.relays | ||
| 122 | .iter() | ||
| 123 | .filter(|r| r.write) | ||
| 124 | .map(|r| r.url.clone()) | ||
| 125 | .collect() | ||
| 126 | } | ||
| 127 | } | ||
| 128 | |||
| 129 | #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] | ||
| 130 | pub struct UserRelayRef { | ||
| 131 | pub url: String, | ||
| 132 | pub read: bool, | ||
| 133 | pub write: bool, | ||
| 74 | } | 134 | } |
| 75 | 135 | ||
| 76 | #[cfg(test)] | 136 | #[cfg(test)] |