From bbb3a4166201035e4b344bdb833fdd3d5609e7d6 Mon Sep 17 00:00:00 2001 From: notplants Date: Mon, 15 Nov 2021 15:14:40 +0100 Subject: [PATCH] Abstract get_about_message function --- src/lib.rs | 86 ++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 58 insertions(+), 28 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 297f3fa..045a075 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -190,15 +190,21 @@ impl Sbot { } } + /* GET ABOUT MESSAGES */ - /// Return latest name assignment from `about` msgs (the name in this case is for the public key - /// associated with the local sbot instance). + /// Get the value of an about message with the given key. /// /// Calls `sbotcli bytype --limit 10 --reverse about`. On success: parses the `stdout` to extract the - /// `name` and returns it. On error: returns the `stderr` output with a description. + /// `key` and returns it. On error: returns the `stderr` output with a description. /// - pub fn get_name(&self) -> Result, SbotCliError> { + /// # Arguments + /// + /// * `key` - A string slice representing the name of they that is being retrieved + /// * `id` - A string slice representing the id (profile reference / public key) + /// of the user to get the about message from + /// + pub fn get_about_message(&self, key: &str, _id: &str) -> Result, SbotCliError> { let output = Command::new(&self.sbotcli_path) .arg("bytype") .arg("--limit") @@ -208,9 +214,9 @@ impl Sbot { .output()?; if output.status.success() { let stdout = String::from_utf8(output.stdout)?; - let name = utils::regex_finder(r#""name": "(.*)""#, &stdout)?; - - Ok(name) + let regex_string = format!(r#""{}": "(.*)""#, key); + let value = utils::regex_finder(®ex_string, &stdout)?; + Ok(value) } else { let stderr = std::str::from_utf8(&output.stderr)?; // TODO: create a more generic error variant @@ -221,32 +227,37 @@ impl Sbot { } } - /// Return latest description assignment from `about` msgs - /// (the description associated with the public key of the local sbot instance). + /// Return latest name assignment from `about` msgs, for the public key + /// associated with the local sbot instance. + /// + /// Calls `sbotcli bytype --limit 10 --reverse about`. On success: parses the `stdout` to extract the + /// `name` and returns it. On error: returns the `stderr` output with a description. + /// + pub fn get_name(&self) -> Result, SbotCliError> { + let self_id = self.whoami()?; + self.get_about_message("name", &self_id) + } + + /// Return latest description assignment from `about` msgs, for the public key + /// associated with the local sbot instance. /// /// Calls `sbotcli bytype --limit 10 --reverse about`. On success: parses the `stdout` to extract the /// `description` and returns it. On error: returns the `stderr` output with a description. /// pub fn get_description(&self) -> Result, SbotCliError> { - let output = Command::new(&self.sbotcli_path) - .arg("bytype") - .arg("--limit") - .arg("10") - .arg("--reverse") - .arg("about") - .output()?; - if output.status.success() { - let stdout = String::from_utf8(output.stdout)?; - let description = utils::regex_finder(r#""description": "(.*)""#, &stdout)?; - Ok(description) - } else { - let stderr = std::str::from_utf8(&output.stderr)?; - // TODO: create a more generic error variant - Err(SbotCliError::GetAboutMsgs(format!( - "Error fetching about messages: {}", - stderr - ))) - } + let self_id = self.whoami()?; + self.get_about_message("description", &self_id) + } + + /// Return latest profile image assignment from `about` msgs, for the public key + /// associated with the local sbot instance. + /// + /// Calls `sbotcli bytype --limit 10 --reverse about`. On success: parses the `stdout` to extract the + /// `image` and returns it. On error: returns the `stderr` output with a description. + /// + pub fn get_profile_image(&self) -> Result, SbotCliError> { + let self_id = self.whoami()?; + self.get_about_message("image", &self_id) } @@ -402,6 +413,7 @@ impl Sbot { } + /// Publish an about message with a name for one's own profile, /// using whoami to find your own id. /// @@ -539,4 +551,22 @@ mod tests { let result = 2 + 2; assert_eq!(result, 4); } + + use crate::Sbot; + + #[test] + fn new_test() { + let sbot = Sbot::init(Some("/Users/notplants/go/bin/sbotcli"), None).unwrap(); + let self_id = sbot.whoami().unwrap(); + let profile_image = "/Users/notplants/computer/docs/avi/plants.png"; + let result = sbot.add_blob(profile_image); +// sbot.publish_image(profile_image); +// let result = sbot.get_about_message("image", &self_id); +// println!("get_profile_image"); + match result { + Ok(res) => println!("success: {:?}", res), + Err(err) => println!("error: {:?}", err) + } + assert_eq!(2, 2); + } }