Abstract get_about_message function

This commit is contained in:
notplants 2021-11-15 15:14:40 +01:00
parent c300ffd130
commit bbb3a41662
1 changed files with 58 additions and 28 deletions

View File

@ -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<Option<String>, 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<Option<String>, 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(&regex_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<Option<String>, 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<Option<String>, 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<Option<String>, 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);
}
}