nostr_rust

My fork of nostr_rust
git clone git://jb55.com/nostr_rust
Log | Files | Refs | README

nip2.rs (3903B)


      1 use crate::{
      2     events::EventPrepare, nostr_client::Client, req::ReqFilter, utils::get_timestamp, Identity,
      3 };
      4 
      5 // Implementation of the NIP2 protocol
      6 // https://github.com/nostr-protocol/nips/blob/master/02.md
      7 
      8 #[derive(Debug, Clone)]
      9 pub struct ContactListTag {
     10     /// 32-bytes hex key - the public key of the contact
     11     pub key: String,
     12     /// main relay URL
     13     pub main_relay: Option<String>,
     14     /// Petname - surname
     15     pub surname: Option<String>,
     16 }
     17 
     18 impl ContactListTag {
     19     pub fn to_tags(&self) -> Vec<String> {
     20         let mut tags: Vec<String> = vec![String::from("p"), self.key.clone()];
     21 
     22         if let Some(main_relay) = &self.main_relay {
     23             tags.push(main_relay.clone());
     24 
     25             if let Some(surname) = &self.surname {
     26                 tags.push(surname.clone());
     27             }
     28         } else if self.surname.is_some() {
     29             tags.push(String::from(""));
     30 
     31             tags.push(self.surname.clone().unwrap());
     32         }
     33 
     34         tags
     35     }
     36 }
     37 
     38 impl Client {
     39     /// Set the contact list of the identity
     40     ///
     41     /// # Example
     42     /// ```rust
     43     /// use nostr_rust::{nostr_client::Client, Identity, nips::nip2::ContactListTag};
     44     /// use std::str::FromStr;
     45     /// let mut client = Client::new(vec!["wss://nostr-pub.wellorder.net"]).unwrap();
     46     /// let identity = Identity::from_str(env!("SECRET_KEY")).unwrap();
     47     ///
     48     /// // Here we set the contact list of the identity
     49     /// client.set_contact_list(&identity, vec![ContactListTag {
     50     ///   key: "884704bd421721e292edbff42eb77547fe115c6ff9825b08fc366be4cd69e9f6".to_string(),
     51     ///   main_relay: Some("wss://nostr-pub.wellorder.net".to_string()),
     52     ///   surname: Some("Rust Nostr Client".to_string()),
     53     /// }]).unwrap();
     54     /// ```
     55     pub fn set_contact_list(
     56         &mut self,
     57         identity: &Identity,
     58         contact_list: Vec<ContactListTag>,
     59     ) -> Result<(), String> {
     60         let event = EventPrepare {
     61             pub_key: identity.public_key_str.clone(),
     62             created_at: get_timestamp(),
     63             kind: 3,
     64             tags: contact_list
     65                 .iter()
     66                 .map(|contact| contact.to_tags())
     67                 .collect(),
     68             content: String::new(),
     69         }
     70         .to_event(identity);
     71 
     72         self.publish_event(&event)?;
     73         Ok(())
     74     }
     75 
     76     /// Get the contact list of a pub key
     77     ///
     78     /// # Example
     79     /// ```rust
     80     /// use nostr_rust::{nostr_client::Client, Identity, nips::nip2::ContactListTag};
     81     /// use std::str::FromStr;
     82     /// let mut client = Client::new(vec!["wss://nostr-pub.wellorder.net"]).unwrap();
     83     /// let contact_list = client.get_contact_list("884704bd421721e292edbff42eb77547fe115c6ff9825b08fc366be4cd69e9f6").unwrap();
     84     /// ```
     85     pub fn get_contact_list(&mut self, pubkey: &str) -> Result<Vec<ContactListTag>, String> {
     86         let mut contact_list: Vec<ContactListTag> = vec![];
     87 
     88         let events = self.get_events_of(vec![ReqFilter {
     89             ids: None,
     90             authors: Some(vec![pubkey.to_string()]),
     91             kinds: Some(vec![3]),
     92             e: None,
     93             p: None,
     94             since: None,
     95             until: None,
     96             limit: Some(1),
     97         }])?;
     98 
     99         for event in events {
    100             for tag in event.tags {
    101                 if tag[0] == "p" {
    102                     let mut contact = ContactListTag {
    103                         key: tag[1].clone(),
    104                         main_relay: None,
    105                         surname: None,
    106                     };
    107 
    108                     if tag.len() > 2 {
    109                         contact.main_relay = Some(tag[2].clone());
    110 
    111                         if tag.len() > 3 {
    112                             contact.surname = Some(tag[3].clone());
    113                         }
    114                     }
    115 
    116                     contact_list.push(contact);
    117                 }
    118             }
    119         }
    120 
    121         Ok(contact_list)
    122     }
    123 }