add context builders for sbot
This commit is contained in:
parent
f459fe47d1
commit
a174027ff5
|
@ -1,2 +1,3 @@
|
||||||
pub mod dns;
|
pub mod dns;
|
||||||
pub mod network;
|
pub mod network;
|
||||||
|
pub mod sbot;
|
||||||
|
|
|
@ -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<SbotConfig>,
|
||||||
|
) -> Result<Sbot, PeachWebError> {
|
||||||
|
// 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<String>,
|
||||||
|
pub flash_name: Option<String>,
|
||||||
|
pub flash_msg: Option<String>,
|
||||||
|
pub title: Option<String>,
|
||||||
|
pub theme: Option<String>,
|
||||||
|
pub sbot_config: Option<SbotConfig>,
|
||||||
|
pub sbot_status: Option<SbotStatus>,
|
||||||
|
// 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<String>,
|
||||||
|
pub name: Option<String>,
|
||||||
|
pub description: Option<String>,
|
||||||
|
pub image: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
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<String>) -> Result<Self, PeachWebError> {
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue