From c8b3ecc2783eca9046cdc9ad8010301df70ffd02 Mon Sep 17 00:00:00 2001 From: notplants Date: Mon, 15 Nov 2021 12:23:48 +0100 Subject: [PATCH 1/3] Add publish_own_name and publish_own_description --- src/lib.rs | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 77 insertions(+), 5 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 0eef45f..9057285 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -190,7 +190,7 @@ impl Sbot { } } - /* GET NAME */ + /* 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). @@ -221,6 +221,35 @@ impl Sbot { } } + /// Return latest description assignment from `about` msgs + /// (the description associated with the public key of 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 + ))) + } + } + + /* INVITES */ /// Accept an invite code (trigger a mutual follow with the peer who generated the invite). @@ -347,7 +376,7 @@ impl Sbot { /// # Arguments /// /// * `name` - A string slice representing a profile name (bio) - /// * `id` - A string slice representing an id (profile reference / public key) + /// * `id` - A string slice representing a ssb_id (profile reference / public key) of who the name is for /// pub fn publish_name(&self, id: &str, name: &str) -> Result { let output = Command::new(&self.sbotcli_path) @@ -371,6 +400,41 @@ impl Sbot { } } + + /// Publish an about message with a name for one's own profile, + /// using whoami to find your own id. + /// + /// Calls `sbotcli publish about --name [name] [self_id]`. + /// passing the id of the currently running sbot as self_id. + /// On success: trims the trailing whitespace from `stdout` and returns the message reference. + /// On error: returns the `stderr` output with a description. + /// + /// # Arguments + /// + /// * `name` - A string slice of the new name you would like to self-identify with + /// + pub fn publish_own_name(&self, name: &str) -> Result { + let self_id = &self.whoami()?; + self.publish_name(name, self_id) + } + + /// Publish an about message with a description for one's own profile, + /// using whoami to find your own id. + /// + /// Calls `sbotcli publish about --description [description] [self_id]`. + /// passing the id of the currently running sbot as self_id. + /// On success: trims the trailing whitespace from `stdout` and returns the message reference. + /// On error: returns the `stderr` output with a description. + /// + /// # Arguments + /// + /// * `description` - A string slice of the description you would like to use + /// + pub fn publish_own_description(&self, description: &str) -> Result { + let self_id = &self.whoami()?; + self.publish_description(description, self_id) + } + /// Publish a post (public message). /// /// Calls `sbotcli publish post [text]". On success: trims the trailing whitespace from `stdout` and returns the message reference. On error: returns the `stderr` output with a description. @@ -439,7 +503,7 @@ impl Sbot { /// Calls `sbotcli call whoami`. On success: parses the `stdout` to extract the ID and returns it. /// On error: returns the `stderr` output with a description. /// - pub fn whoami(&self) -> Result, SbotCliError> { + pub fn whoami(&self) -> Result { let output = Command::new(&self.sbotcli_path) .arg("call") .arg("whoami") @@ -447,8 +511,16 @@ impl Sbot { if output.status.success() { let stdout = String::from_utf8(output.stdout)?; let id = utils::regex_finder(r#""id": "(.*)"\n"#, &stdout)?; - - Ok(id) + match id { + // if the regex matches, then return the result + Some(id) => { + Ok(id) + }, + // if the regex does not match, then return an error + None => { + Err(SbotCliError::WhoAmI("Error calling whoami: regex not matched".to_string())) + } + } } else { let stderr = std::str::from_utf8(&output.stderr)?; Err(SbotCliError::WhoAmI(format!( From 58372716757fececa49f19b7eded39716aca5adb Mon Sep 17 00:00:00 2001 From: notplants Date: Mon, 15 Nov 2021 12:29:33 +0100 Subject: [PATCH 2/3] Change ssb_id to id to keep style uniformity --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 9057285..89fccde 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -376,7 +376,7 @@ impl Sbot { /// # Arguments /// /// * `name` - A string slice representing a profile name (bio) - /// * `id` - A string slice representing a ssb_id (profile reference / public key) of who the name is for + /// * `id` - A string slice representing a id (profile reference / public key) of who the name is for /// pub fn publish_name(&self, id: &str, name: &str) -> Result { let output = Command::new(&self.sbotcli_path) From c300ffd130e3e6df0942c1e93402bacbb320e02c Mon Sep 17 00:00:00 2001 From: notplants Date: Mon, 15 Nov 2021 14:15:06 +0100 Subject: [PATCH 3/3] Response to code review --- src/lib.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 89fccde..297f3fa 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -376,7 +376,8 @@ impl Sbot { /// # Arguments /// /// * `name` - A string slice representing a profile name (bio) - /// * `id` - A string slice representing a id (profile reference / public key) of who the name is for + /// * `id` - A string slice representing the id (profile reference / public key) + /// of the profile being named /// pub fn publish_name(&self, id: &str, name: &str) -> Result { let output = Command::new(&self.sbotcli_path) @@ -518,7 +519,7 @@ impl Sbot { }, // if the regex does not match, then return an error None => { - Err(SbotCliError::WhoAmI("Error calling whoami: regex not matched".to_string())) + Err(SbotCliError::WhoAmI("Error calling whoami: failed to capture the id value using regex".to_string())) } } } else {