1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
|
use std::{collections::HashSet, path::Path, sync::Arc};
use anyhow::{Context, Result, bail};
use nostr::{PublicKey, Url, event::Tag, signer::NostrSigner};
use nostr_sdk::{Alphabet, JsonUtil, Kind, SingleLetterTag, Timestamp, ToBech32};
use serde::{self, Deserialize, Serialize};
#[cfg(not(test))]
use crate::client::Client;
#[cfg(test)]
use crate::client::MockConnect;
use crate::{
client::{Connect, get_event_from_global_cache, sign_event},
git_events::KIND_USER_GRASP_LIST,
};
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
pub struct UserRef {
pub public_key: PublicKey,
pub metadata: UserMetadata,
pub relays: UserRelays,
pub grasp_list: UserGraspList,
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
pub struct UserMetadata {
pub name: String,
pub created_at: Timestamp,
pub nip05: Option<String>,
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
pub struct UserRelays {
pub relays: Vec<UserRelayRef>,
pub created_at: Timestamp,
}
impl UserRelays {
pub fn write(&self) -> Vec<String> {
self.relays
.iter()
.filter(|r| r.write)
.map(|r| r.url.clone())
.collect()
}
pub fn read(&self) -> Vec<String> {
self.relays
.iter()
.filter(|r| r.read)
.map(|r| r.url.clone())
.collect()
}
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
pub struct UserGraspList {
pub urls: Vec<Url>,
pub created_at: Timestamp,
}
impl UserGraspList {
pub async fn to_event(&mut self, signer: &Arc<dyn NostrSigner>) -> Result<nostr::Event> {
let event = sign_event(
nostr_sdk::EventBuilder::new(KIND_USER_GRASP_LIST, "").tags(
self.urls
.iter()
.map(|url| {
Tag::custom(
nostr::TagKind::Custom(std::borrow::Cow::Borrowed("g")),
vec![url.to_string()],
)
})
.collect::<Vec<_>>(),
),
signer,
"user grasp list".to_string(),
)
.await?;
self.created_at = event.created_at;
Ok(event)
}
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
pub struct UserRelayRef {
pub url: String,
pub read: bool,
pub write: bool,
}
pub async fn get_user_details(
public_key: &PublicKey,
#[cfg(test)] client: Option<&MockConnect>,
#[cfg(not(test))] client: Option<&Client>,
git_repo_path: Option<&Path>,
cache_only: bool,
fetch_profile_updates: bool,
) -> Result<UserRef> {
if let Ok(user_ref) = get_user_ref_from_cache(git_repo_path, public_key).await {
if fetch_profile_updates {
if let Some(client) = client {
let term = console::Term::stderr();
term.write_line("searching for profile updates...")?;
let (reports, progress_reporter) = client
.fetch_all(git_repo_path, None, &HashSet::from_iter(vec![*public_key]))
.await?;
if !reports.iter().any(|r| r.is_err()) {
progress_reporter.clear()?;
term.clear_last_lines(1)?;
}
return get_user_ref_from_cache(git_repo_path, public_key).await;
}
}
Ok(user_ref)
} else {
// No cached profile found. Fall back to fetching from default relays
// (bootstrapping).
let empty = UserRef {
public_key: public_key.to_owned(),
metadata: extract_user_metadata(public_key, &[])?,
relays: extract_user_relays(public_key, &[]),
grasp_list: extract_user_grasp_list(public_key, &[]),
};
if cache_only {
Ok(empty)
} else if let Some(client) = client {
let term = console::Term::stderr();
term.write_line("searching for profile...")?;
let (_, progress_reporter) = client
.fetch_all(git_repo_path, None, &HashSet::from_iter(vec![*public_key]))
.await?;
if let Ok(user_ref) = get_user_ref_from_cache(git_repo_path, public_key).await {
progress_reporter.clear()?;
// if std::env::var("NGITTEST").is_err() {term.clear_last_lines(1)?;}
Ok(user_ref)
} else {
Ok(empty)
}
} else {
Ok(empty)
}
}
}
pub async fn get_user_ref_from_cache(
git_repo_path: Option<&Path>,
public_key: &PublicKey,
) -> Result<UserRef> {
let filters = vec![
nostr::Filter::default()
.author(*public_key)
.kind(Kind::Metadata),
nostr::Filter::default()
.author(*public_key)
.kind(Kind::RelayList),
nostr::Filter::default()
.author(*public_key)
.kind(KIND_USER_GRASP_LIST),
];
let events = get_event_from_global_cache(git_repo_path, filters.clone()).await?;
if events.is_empty() {
bail!("no metadata and profile list in cache for selected public key");
}
Ok(UserRef {
public_key: public_key.to_owned(),
metadata: extract_user_metadata(public_key, &events)?,
relays: extract_user_relays(public_key, &events),
grasp_list: extract_user_grasp_list(public_key, &events),
})
}
pub fn extract_user_metadata(
public_key: &nostr::PublicKey,
events: &[nostr::Event],
) -> Result<UserMetadata> {
let event = events
.iter()
.filter(|e| e.kind.eq(&nostr::Kind::Metadata) && e.pubkey.eq(public_key))
.max_by_key(|e| e.created_at);
let metadata: Option<nostr::Metadata> = if let Some(event) = event {
Some(
nostr::Metadata::from_json(event.content.clone())
.context("metadata cannot be found in kind 0 event content")?,
)
} else {
None
};
Ok(UserMetadata {
name: if let Some(metadata) = metadata.clone() {
if let Some(n) = metadata.name {
n
} else if let Some(n) = metadata.custom.get("displayName") {
// strip quote marks that custom.get() adds
let binding = n.to_string();
let mut chars = binding.chars();
chars.next();
chars.next_back();
chars.as_str().to_string()
} else if let Some(n) = metadata.display_name {
n
} else {
public_key.to_bech32()?
}
} else {
public_key.to_bech32()?
},
nip05: if let Some(metadata) = metadata {
metadata.nip05
} else {
None
},
created_at: if let Some(event) = event {
event.created_at
} else {
Timestamp::from(0)
},
})
}
pub fn extract_user_relays(public_key: &nostr::PublicKey, events: &[nostr::Event]) -> UserRelays {
let event = events
.iter()
.filter(|e| e.kind.eq(&nostr::Kind::RelayList) && e.pubkey.eq(public_key))
.max_by_key(|e| e.created_at);
UserRelays {
relays: if let Some(event) = event {
event
.tags
.iter()
.filter(|t| {
t.as_slice().len() > 1
&& t.kind()
.eq(&nostr::TagKind::SingleLetter(SingleLetterTag::lowercase(
Alphabet::R,
)))
})
.map(|t| UserRelayRef {
url: t.as_slice()[1].clone(),
read: t.as_slice().len() == 2 || t.as_slice()[2].eq("read"),
write: t.as_slice().len() == 2 || t.as_slice()[2].eq("write"),
})
.collect()
} else {
vec![]
},
created_at: if let Some(event) = event {
event.created_at
} else {
Timestamp::from(0)
},
}
}
pub fn extract_user_grasp_list(
public_key: &nostr::PublicKey,
events: &[nostr::Event],
) -> UserGraspList {
let event = events
.iter()
.filter(|e| e.kind.eq(&KIND_USER_GRASP_LIST) && e.pubkey.eq(public_key))
.max_by_key(|e| e.created_at);
UserGraspList {
urls: if let Some(event) = event {
event
.tags
.iter()
.filter_map(|t| {
if t.as_slice().len() > 1 && t.as_slice()[0] == "g" {
Url::parse(&t.as_slice()[1]).ok()
} else {
None
}
})
.collect()
} else {
vec![]
},
created_at: if let Some(event) = event {
event.created_at
} else {
Timestamp::from(0)
},
}
}
|