From a174027ff5eaf5299a0f2454367ced123962df6e Mon Sep 17 00:00:00 2001 From: glyph Date: Fri, 11 Feb 2022 10:31:07 +0200 Subject: [PATCH] add context builders for sbot --- peach-web/src/context/mod.rs | 1 + peach-web/src/context/sbot.rs | 113 ++++++++++++++++++++++++++++++++++ 2 files changed, 114 insertions(+) create mode 100644 peach-web/src/context/sbot.rs diff --git a/peach-web/src/context/mod.rs b/peach-web/src/context/mod.rs index d2d350c..691bc21 100644 --- a/peach-web/src/context/mod.rs +++ b/peach-web/src/context/mod.rs @@ -1,2 +1,3 @@ pub mod dns; pub mod network; +pub mod sbot; diff --git a/peach-web/src/context/sbot.rs b/peach-web/src/context/sbot.rs new file mode 100644 index 0000000..ca9b3a3 --- /dev/null +++ b/peach-web/src/context/sbot.rs @@ -0,0 +1,113 @@ +use golgi::Sbot; +use peach_lib::sbot::{SbotConfig, SbotStatus}; +use rocket::serde::Serialize; + +use crate::{error::PeachWebError, utils}; + +// HELPER FUNCTIONS + +pub async fn init_sbot_with_config( + sbot_config: &Option, +) -> Result { + // initialise sbot connection with ip:port and shscap from config file + let sbot_client = match sbot_config { + // TODO: panics if we pass `Some(conf.shscap)` as second arg + Some(conf) => { + let ip_port = conf.lis.clone(); + Sbot::init(Some(ip_port), None).await? + } + None => Sbot::init(None, None).await?, + }; + + Ok(sbot_client) +} + +// CONTEXT STRUCTS AND BUILDERS + +#[derive(Debug, Serialize)] +pub struct ProfileContext { + pub back: Option, + pub flash_name: Option, + pub flash_msg: Option, + pub title: Option, + pub theme: Option, + pub sbot_config: Option, + pub sbot_status: Option, + // is this the local profile or the profile of a peer? + pub is_local_profile: bool, + // an ssb_id which may or may not be the local public key + pub id: Option, + pub name: Option, + pub description: Option, + pub image: Option, +} + +impl ProfileContext { + pub fn default() -> Self { + ProfileContext { + back: Some("/".to_string()), + flash_name: None, + flash_msg: None, + title: Some("Profile".to_string()), + theme: None, + sbot_config: None, + sbot_status: None, + is_local_profile: true, + id: None, + name: None, + description: None, + image: None, + } + } + + pub async fn build(ssb_id: Option) -> Result { + let mut context = Self::default(); + + // retrieve current ui theme + context.theme = Some(utils::get_theme()); + + // retrieve go-sbot systemd process status + let sbot_status = SbotStatus::read()?; + + // we only want to try and interact with the sbot if it's active + if sbot_status.state == Some("active".to_string()) { + // retrieve latest go-sbot configuration parameters + let sbot_config = SbotConfig::read().ok(); + + let mut sbot_client = init_sbot_with_config(&sbot_config).await?; + + // if an ssb_id has been provided to the context builder, we assume that + // the profile info being retrieved is for a peer (ie. not for our local + // profile) + let id = if ssb_id.is_some() { + context.is_local_profile = false; + ssb_id.unwrap() + } else { + // if an ssb_id has not been provided, retrieve the local id using whoami + context.is_local_profile = true; + sbot_client.whoami().await? + }; + + // retrieve the profile info for the given id + let info = sbot_client.get_profile_info(&id).await?; + // set each context field accordingly + for (key, val) in info { + match key.as_str() { + "name" => context.name = Some(val), + "description" => context.description = Some(val), + "image" => context.image = Some(val), + _ => (), + } + } + context.id = Some(id); + } else { + // the sbot is not currently active; return a helpful message + context.flash_name = Some("warning".to_string()); + context.flash_msg = Some("The Sbot is currently inactive. As a result, profile data cannot be retrieved. Visit the Scuttlebutt settings menu to start the Sbot and then try again".to_string()); + } + + context.sbot_status = Some(sbot_status); + + Ok(context) + } +}