Add get_about_info function
This commit is contained in:
parent
61fdb42027
commit
887d635683
28
src/sbot.rs
28
src/sbot.rs
|
@ -304,23 +304,39 @@ impl Sbot {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get HashMap of profile info for given user
|
/// Get HashMap of profile info for given user
|
||||||
|
pub async fn get_profile_info(&mut self, ssb_id: &str) -> Result<HashMap<String, String>, GolgiError> {
|
||||||
|
let mut keys_to_search_for = vec!["name", "description", "image"];
|
||||||
|
self.get_about_info(ssb_id, keys_to_search_for).await
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get HashMap of name and image for given user
|
||||||
|
/// (this is can be used to display profile images of a list of users)
|
||||||
|
pub async fn get_name_and_image(&mut self, ssb_id: &str) -> Result<HashMap<String, String>, GolgiError> {
|
||||||
|
let mut keys_to_search_for = vec!["name", "image"];
|
||||||
|
self.get_about_info(ssb_id, keys_to_search_for).await
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get HashMap of about keys to values for given user
|
||||||
/// by iteratively searching through a stream of about messages,
|
/// by iteratively searching through a stream of about messages,
|
||||||
/// in order of recency,
|
/// in order of recency,
|
||||||
/// until we find all about messages for all needed info
|
/// until we find all about messages for all needed info
|
||||||
/// or reach the end of the stream.
|
/// or reach the end of the stream.
|
||||||
pub async fn get_profile_info(
|
///
|
||||||
|
/// # Arguments
|
||||||
|
///
|
||||||
|
/// * `ssb_id` - A reference to a string slice which represents the id of the user to get info about.
|
||||||
|
/// * `keys_to_search_for` - A mutable vector of string slice, which represent the about keys
|
||||||
|
/// that will be searched for. As they are found, keys are removed from the vector.
|
||||||
|
pub async fn get_about_info(
|
||||||
&mut self,
|
&mut self,
|
||||||
ssb_id: &str
|
ssb_id: &str,
|
||||||
|
mut keys_to_search_for: Vec<&str>
|
||||||
) -> Result<HashMap<String, String>, GolgiError> {
|
) -> Result<HashMap<String, String>, GolgiError> {
|
||||||
// get about_message_stream
|
// get about_message_stream
|
||||||
let about_message_stream = self.get_about_message_stream(ssb_id).await?;
|
let about_message_stream = self.get_about_message_stream(ssb_id).await?;
|
||||||
// now we have a stream of about messages with most recent at the front of the vector
|
// now we have a stream of about messages with most recent at the front of the vector
|
||||||
pin_mut!(about_message_stream); // needed for iteration
|
pin_mut!(about_message_stream); // needed for iteration
|
||||||
let mut profile_info: HashMap<String, String> = HashMap::new();
|
let mut profile_info: HashMap<String, String> = HashMap::new();
|
||||||
// these are the about message keys we are looking for
|
|
||||||
// as we find values for each of these keys,
|
|
||||||
// we will remove them from this vector (as we are no longer searching for them)
|
|
||||||
let mut keys_to_search_for = vec!["name", "description", "image"];
|
|
||||||
// iterate through the stream while it still has more values and
|
// iterate through the stream while it still has more values and
|
||||||
// we still have keys we are looking for
|
// we still have keys we are looking for
|
||||||
while let Some(res) = about_message_stream.next().await {
|
while let Some(res) = about_message_stream.next().await {
|
||||||
|
|
Loading…
Reference in New Issue