-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathprofile_state.rs
79 lines (72 loc) · 2.33 KB
/
profile_state.rs
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
use nostrdb::{NdbProfile, ProfileRecord};
#[derive(Default, Debug)]
pub struct ProfileState {
pub display_name: String,
pub name: String,
pub picture: String,
pub banner: String,
pub about: String,
pub website: String,
pub lud16: String,
pub nip05: String,
}
impl ProfileState {
pub fn from_profile(record: &ProfileRecord<'_>) -> Self {
let display_name = get_item(record, |p| p.display_name());
let username = get_item(record, |p| p.name());
let profile_picture = get_item(record, |p| p.picture());
let cover_image = get_item(record, |p| p.banner());
let about = get_item(record, |p| p.about());
let website = get_item(record, |p| p.website());
let lud16 = get_item(record, |p| p.lud16());
let nip05 = get_item(record, |p| p.nip05());
Self {
display_name,
name: username,
picture: profile_picture,
banner: cover_image,
about,
website,
lud16,
nip05,
}
}
pub fn to_json(&self) -> String {
let mut fields = Vec::new();
if !self.display_name.is_empty() {
fields.push(format!(r#""display_name":"{}""#, self.display_name));
}
if !self.name.is_empty() {
fields.push(format!(r#""name":"{}""#, self.name));
}
if !self.picture.is_empty() {
fields.push(format!(r#""picture":"{}""#, self.picture));
}
if !self.banner.is_empty() {
fields.push(format!(r#""banner":"{}""#, self.banner));
}
if !self.about.is_empty() {
fields.push(format!(r#""about":"{}""#, self.about));
}
if !self.website.is_empty() {
fields.push(format!(r#""website":"{}""#, self.website));
}
if !self.lud16.is_empty() {
fields.push(format!(r#""lud16":"{}""#, self.lud16));
}
if !self.nip05.is_empty() {
fields.push(format!(r#""nip05":"{}""#, self.nip05));
}
format!("{{{}}}", fields.join(","))
}
}
fn get_item<'a>(
record: &ProfileRecord<'a>,
item_retriever: fn(NdbProfile<'a>) -> Option<&'a str>,
) -> String {
record
.record()
.profile()
.and_then(item_retriever)
.map_or_else(String::new, ToString::to_string)
}