notedeck

One damus client to rule them all
git clone git://jb55.com/notedeck
Log | Files | Refs | README | LICENSE

profile_state.rs (2386B)


      1 use nostrdb::{NdbProfile, ProfileRecord};
      2 
      3 #[derive(Default, Debug)]
      4 pub struct ProfileState {
      5     pub display_name: String,
      6     pub name: String,
      7     pub picture: String,
      8     pub banner: String,
      9     pub about: String,
     10     pub website: String,
     11     pub lud16: String,
     12     pub nip05: String,
     13 }
     14 
     15 impl ProfileState {
     16     pub fn from_profile(record: &ProfileRecord<'_>) -> Self {
     17         let display_name = get_item(record, |p| p.display_name());
     18         let username = get_item(record, |p| p.name());
     19         let profile_picture = get_item(record, |p| p.picture());
     20         let cover_image = get_item(record, |p| p.banner());
     21         let about = get_item(record, |p| p.about());
     22         let website = get_item(record, |p| p.website());
     23         let lud16 = get_item(record, |p| p.lud16());
     24         let nip05 = get_item(record, |p| p.nip05());
     25 
     26         Self {
     27             display_name,
     28             name: username,
     29             picture: profile_picture,
     30             banner: cover_image,
     31             about,
     32             website,
     33             lud16,
     34             nip05,
     35         }
     36     }
     37 
     38     pub fn to_json(&self) -> String {
     39         let mut fields = Vec::new();
     40 
     41         if !self.display_name.is_empty() {
     42             fields.push(format!(r#""display_name":"{}""#, self.display_name));
     43         }
     44         if !self.name.is_empty() {
     45             fields.push(format!(r#""name":"{}""#, self.name));
     46         }
     47         if !self.picture.is_empty() {
     48             fields.push(format!(r#""picture":"{}""#, self.picture));
     49         }
     50         if !self.banner.is_empty() {
     51             fields.push(format!(r#""banner":"{}""#, self.banner));
     52         }
     53         if !self.about.is_empty() {
     54             fields.push(format!(r#""about":"{}""#, self.about));
     55         }
     56         if !self.website.is_empty() {
     57             fields.push(format!(r#""website":"{}""#, self.website));
     58         }
     59         if !self.lud16.is_empty() {
     60             fields.push(format!(r#""lud16":"{}""#, self.lud16));
     61         }
     62         if !self.nip05.is_empty() {
     63             fields.push(format!(r#""nip05":"{}""#, self.nip05));
     64         }
     65 
     66         format!("{{{}}}", fields.join(","))
     67     }
     68 }
     69 
     70 fn get_item<'a>(
     71     record: &ProfileRecord<'a>,
     72     item_retriever: fn(NdbProfile<'a>) -> Option<&'a str>,
     73 ) -> String {
     74     record
     75         .record()
     76         .profile()
     77         .and_then(item_retriever)
     78         .map_or_else(String::new, ToString::to_string)
     79 }