Introduce names
RPC methods
#53
110
src/api/about.rs
110
src/api/about.rs
@ -5,10 +5,12 @@
|
|||||||
//! - [`Sbot::get_about_info`]
|
//! - [`Sbot::get_about_info`]
|
||||||
//! - [`Sbot::get_about_message_stream`]
|
//! - [`Sbot::get_about_message_stream`]
|
||||||
//! - [`Sbot::get_description`]
|
//! - [`Sbot::get_description`]
|
||||||
|
//! - [`Sbot::get_image`]
|
||||||
//! - [`Sbot::get_latest_about_message`]
|
//! - [`Sbot::get_latest_about_message`]
|
||||||
//! - [`Sbot::get_name`]
|
//! - [`Sbot::get_name`]
|
||||||
//! - [`Sbot::get_name_and_image`]
|
//! - [`Sbot::get_name_and_image`]
|
||||||
//! - [`Sbot::get_profile_info`]
|
//! - [`Sbot::get_profile_info`]
|
||||||
|
//! - [`Sbot::get_signifier`]
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
@ -19,6 +21,7 @@ use crate::{
|
|||||||
error::GolgiError,
|
error::GolgiError,
|
||||||
messages::{SsbMessageContentType, SsbMessageValue},
|
messages::{SsbMessageContentType, SsbMessageValue},
|
||||||
sbot::Sbot,
|
sbot::Sbot,
|
||||||
|
utils,
|
||||||
};
|
};
|
||||||
|
|
||||||
impl Sbot {
|
impl Sbot {
|
||||||
@ -82,6 +85,40 @@ impl Sbot {
|
|||||||
Ok(about_message_stream)
|
Ok(about_message_stream)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Get the latest `image` value for a peer. The value is a blob reference.
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// use golgi::{Sbot, GolgiError, sbot::Keystore};
|
||||||
|
///
|
||||||
|
/// async fn image_info() -> Result<(), GolgiError> {
|
||||||
|
/// let mut sbot_client = Sbot::init(Keystore::Patchwork, None, None).await?;
|
||||||
|
///
|
||||||
|
/// let ssb_id = "@zqshk7o2Rpd/OaZ/MxH6xXONgonP1jH+edK9+GZb/NY=.ed25519";
|
||||||
|
///
|
||||||
|
/// let image = sbot_client.get_image(ssb_id).await?;
|
||||||
|
///
|
||||||
|
/// println!("peer {} is identified by {}", ssb_id, image);
|
||||||
|
///
|
||||||
|
/// Ok(())
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
|
pub async fn get_image(&mut self, ssb_id: &str) -> Result<String, GolgiError> {
|
||||||
|
let mut sbot_connection = self.get_sbot_connection().await?;
|
||||||
|
let req_id = sbot_connection
|
||||||
|
.client
|
||||||
|
.names_get_image_for_req_send(ssb_id)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
utils::get_async(
|
||||||
|
&mut sbot_connection.rpc_reader,
|
||||||
|
req_id,
|
||||||
|
utils::string_res_parse,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
|
||||||
/// Get the value of the latest `about` type message, containing the given
|
/// Get the value of the latest `about` type message, containing the given
|
||||||
/// `key`, for a peer.
|
/// `key`, for a peer.
|
||||||
///
|
///
|
||||||
@ -186,8 +223,16 @@ impl Sbot {
|
|||||||
&mut self,
|
&mut self,
|
||||||
ssb_id: &str,
|
ssb_id: &str,
|
||||||
) -> Result<HashMap<String, String>, GolgiError> {
|
) -> Result<HashMap<String, String>, GolgiError> {
|
||||||
let keys_to_search_for = vec!["name", "description", "image"];
|
let key_to_search_for = vec!["description"];
|
||||||
self.get_about_info(ssb_id, keys_to_search_for).await
|
let description = self.get_about_info(ssb_id, key_to_search_for).await?;
|
||||||
|
|
||||||
|
let mut profile_info = self.get_name_and_image(ssb_id).await?;
|
||||||
|
// extend the `profile_info` HashMap by adding the key-value from the
|
||||||
|
// `description` HashMap; `profile_info` now contains all three
|
||||||
|
// key-value pairs
|
||||||
|
profile_info.extend(description.into_iter());
|
||||||
|
|
||||||
|
Ok(profile_info)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the latest `name` and `image` values for a peer. This method can
|
/// Get the latest `name` and `image` values for a peer. This method can
|
||||||
@ -232,8 +277,15 @@ impl Sbot {
|
|||||||
&mut self,
|
&mut self,
|
||||||
ssb_id: &str,
|
ssb_id: &str,
|
||||||
) -> Result<HashMap<String, String>, GolgiError> {
|
) -> Result<HashMap<String, String>, GolgiError> {
|
||||||
let keys_to_search_for = vec!["name", "image"];
|
let mut name_and_image: HashMap<String, String> = HashMap::new();
|
||||||
self.get_about_info(ssb_id, keys_to_search_for).await
|
|
||||||
|
let name = self.get_name(ssb_id).await?;
|
||||||
|
let image = self.get_image(ssb_id).await?;
|
||||||
|
|
||||||
|
name_and_image.insert("name".to_string(), name);
|
||||||
|
name_and_image.insert("image".to_string(), image);
|
||||||
|
|
||||||
|
Ok(name_and_image)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the latest values for the provided keys from the `about` type
|
/// Get the latest values for the provided keys from the `about` type
|
||||||
@ -335,7 +387,8 @@ impl Sbot {
|
|||||||
Ok(profile_info)
|
Ok(profile_info)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the latest `name` value for a peer.
|
/// Get the latest `name` value for a peer. The public key of the peer
|
||||||
|
/// will be returned if no `name` value is found.
|
||||||
///
|
///
|
||||||
/// # Example
|
/// # Example
|
||||||
///
|
///
|
||||||
@ -347,17 +400,15 @@ impl Sbot {
|
|||||||
///
|
///
|
||||||
/// let ssb_id = "@zqshk7o2Rpd/OaZ/MxH6xXONgonP1jH+edK9+GZb/NY=.ed25519";
|
/// let ssb_id = "@zqshk7o2Rpd/OaZ/MxH6xXONgonP1jH+edK9+GZb/NY=.ed25519";
|
||||||
///
|
///
|
||||||
/// if let Some(name) = sbot_client.get_name(ssb_id).await? {
|
/// let name = sbot_client.get_name(ssb_id).await?;
|
||||||
/// println!("peer {} is named {}", ssb_id, name)
|
///
|
||||||
/// } else {
|
/// println!("peer {} is named {}", ssb_id, name);
|
||||||
/// eprintln!("no name found for peer {}", ssb_id)
|
|
||||||
/// }
|
|
||||||
///
|
///
|
||||||
/// Ok(())
|
/// Ok(())
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
pub async fn get_name(&mut self, ssb_id: &str) -> Result<Option<String>, GolgiError> {
|
pub async fn get_name(&mut self, ssb_id: &str) -> Result<String, GolgiError> {
|
||||||
self.get_latest_about_message(ssb_id, "name").await
|
self.get_signifier(ssb_id).await
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the latest `description` value for a peer.
|
/// Get the latest `description` value for a peer.
|
||||||
@ -384,4 +435,39 @@ impl Sbot {
|
|||||||
pub async fn get_description(&mut self, ssb_id: &str) -> Result<Option<String>, GolgiError> {
|
pub async fn get_description(&mut self, ssb_id: &str) -> Result<Option<String>, GolgiError> {
|
||||||
self.get_latest_about_message(ssb_id, "description").await
|
self.get_latest_about_message(ssb_id, "description").await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Get the latest `name` value (signifier) for a peer. The public key of
|
||||||
|
/// the peer will be returned if no `name` value is found.
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// use golgi::{Sbot, GolgiError, sbot::Keystore};
|
||||||
|
///
|
||||||
|
/// async fn name_info() -> Result<(), GolgiError> {
|
||||||
|
/// let mut sbot_client = Sbot::init(Keystore::Patchwork, None, None).await?;
|
||||||
|
///
|
||||||
|
/// let ssb_id = "@zqshk7o2Rpd/OaZ/MxH6xXONgonP1jH+edK9+GZb/NY=.ed25519";
|
||||||
|
///
|
||||||
|
/// let name = sbot_client.get_signifier(ssb_id).await?;
|
||||||
|
///
|
||||||
|
/// println!("peer {} is named {}", ssb_id, name);
|
||||||
|
///
|
||||||
|
/// Ok(())
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
|
pub async fn get_signifier(&mut self, ssb_id: &str) -> Result<String, GolgiError> {
|
||||||
|
let mut sbot_connection = self.get_sbot_connection().await?;
|
||||||
|
let req_id = sbot_connection
|
||||||
|
.client
|
||||||
|
.names_get_signifier_req_send(ssb_id)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
utils::get_async(
|
||||||
|
&mut sbot_connection.rpc_reader,
|
||||||
|
req_id,
|
||||||
|
utils::string_res_parse,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user