Abstract get_about_message function
This commit is contained in:
parent
c300ffd130
commit
bbb3a41662
114
src/lib.rs
114
src/lib.rs
|
@ -190,63 +190,74 @@ impl Sbot {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* GET ABOUT MESSAGES */
|
/* GET ABOUT MESSAGES */
|
||||||
|
|
||||||
/// Return latest name assignment from `about` msgs (the name in this case is for the public key
|
/// Get the value of an about message with the given key.
|
||||||
/// associated with the local sbot instance).
|
///
|
||||||
|
/// Calls `sbotcli bytype --limit 10 --reverse about`. On success: parses the `stdout` to extract the
|
||||||
|
/// `key` and returns it. On error: returns the `stderr` output with a description.
|
||||||
|
///
|
||||||
|
/// # 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")
|
||||||
|
.arg("10")
|
||||||
|
.arg("--reverse")
|
||||||
|
.arg("about")
|
||||||
|
.output()?;
|
||||||
|
if output.status.success() {
|
||||||
|
let stdout = String::from_utf8(output.stdout)?;
|
||||||
|
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
|
||||||
|
Err(SbotCliError::GetAboutMsgs(format!(
|
||||||
|
"Error fetching about messages: {}",
|
||||||
|
stderr
|
||||||
|
)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 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
|
/// 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.
|
/// `name` and returns it. On error: returns the `stderr` output with a description.
|
||||||
///
|
///
|
||||||
pub fn get_name(&self) -> Result<Option<String>, SbotCliError> {
|
pub fn get_name(&self) -> Result<Option<String>, SbotCliError> {
|
||||||
let output = Command::new(&self.sbotcli_path)
|
let self_id = self.whoami()?;
|
||||||
.arg("bytype")
|
self.get_about_message("name", &self_id)
|
||||||
.arg("--limit")
|
|
||||||
.arg("10")
|
|
||||||
.arg("--reverse")
|
|
||||||
.arg("about")
|
|
||||||
.output()?;
|
|
||||||
if output.status.success() {
|
|
||||||
let stdout = String::from_utf8(output.stdout)?;
|
|
||||||
let name = utils::regex_finder(r#""name": "(.*)""#, &stdout)?;
|
|
||||||
|
|
||||||
Ok(name)
|
|
||||||
} 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
|
|
||||||
)))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return latest description assignment from `about` msgs
|
/// Return latest description assignment from `about` msgs, for the public key
|
||||||
/// (the description associated with the public key of the local sbot instance).
|
/// associated with the local sbot instance.
|
||||||
///
|
///
|
||||||
/// Calls `sbotcli bytype --limit 10 --reverse about`. On success: parses the `stdout` to extract the
|
/// 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.
|
/// `description` and returns it. On error: returns the `stderr` output with a description.
|
||||||
///
|
///
|
||||||
pub fn get_description(&self) -> Result<Option<String>, SbotCliError> {
|
pub fn get_description(&self) -> Result<Option<String>, SbotCliError> {
|
||||||
let output = Command::new(&self.sbotcli_path)
|
let self_id = self.whoami()?;
|
||||||
.arg("bytype")
|
self.get_about_message("description", &self_id)
|
||||||
.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
|
|
||||||
)))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 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,
|
/// Publish an about message with a name for one's own profile,
|
||||||
/// using whoami to find your own id.
|
/// using whoami to find your own id.
|
||||||
///
|
///
|
||||||
|
@ -539,4 +551,22 @@ mod tests {
|
||||||
let result = 2 + 2;
|
let result = 2 + 2;
|
||||||
assert_eq!(result, 4);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue