WIP: Add abstract get_about_message function #7
86
src/lib.rs
86
src/lib.rs
@ -190,15 +190,21 @@ 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
|
/// 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)
|
let output = Command::new(&self.sbotcli_path)
|
||||||
.arg("bytype")
|
.arg("bytype")
|
||||||
.arg("--limit")
|
.arg("--limit")
|
||||||
@ -208,9 +214,9 @@ impl Sbot {
|
|||||||
.output()?;
|
.output()?;
|
||||||
if output.status.success() {
|
if output.status.success() {
|
||||||
let stdout = String::from_utf8(output.stdout)?;
|
let stdout = String::from_utf8(output.stdout)?;
|
||||||
let name = utils::regex_finder(r#""name": "(.*)""#, &stdout)?;
|
let regex_string = format!(r#""{}": "(.*)""#, key);
|
||||||
|
let value = utils::regex_finder(®ex_string, &stdout)?;
|
||||||
Ok(name)
|
Ok(value)
|
||||||
} else {
|
} else {
|
||||||
let stderr = std::str::from_utf8(&output.stderr)?;
|
let stderr = std::str::from_utf8(&output.stderr)?;
|
||||||
// TODO: create a more generic error variant
|
// TODO: create a more generic error variant
|
||||||
@ -221,32 +227,37 @@ impl Sbot {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return latest description assignment from `about` msgs
|
/// Return latest name 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
|
||||||
|
/// `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
|
/// 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")
|
/// Return latest profile image assignment from `about` msgs, for the public key
|
||||||
.arg("about")
|
/// associated with the local sbot instance.
|
||||||
.output()?;
|
///
|
||||||
if output.status.success() {
|
/// Calls `sbotcli bytype --limit 10 --reverse about`. On success: parses the `stdout` to extract the
|
||||||
let stdout = String::from_utf8(output.stdout)?;
|
/// `image` and returns it. On error: returns the `stderr` output with a description.
|
||||||
let description = utils::regex_finder(r#""description": "(.*)""#, &stdout)?;
|
///
|
||||||
Ok(description)
|
pub fn get_profile_image(&self) -> Result<Option<String>, SbotCliError> {
|
||||||
} else {
|
let self_id = self.whoami()?;
|
||||||
let stderr = std::str::from_utf8(&output.stderr)?;
|
self.get_about_message("image", &self_id)
|
||||||
// TODO: create a more generic error variant
|
|
||||||
Err(SbotCliError::GetAboutMsgs(format!(
|
|
||||||
"Error fetching about messages: {}",
|
|
||||||
stderr
|
|
||||||
)))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -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…
x
Reference in New Issue
Block a user