diff --git a/peach-web/src/context/sbot.rs b/peach-web/src/context/sbot.rs index c745501..9d319a6 100644 --- a/peach-web/src/context/sbot.rs +++ b/peach-web/src/context/sbot.rs @@ -1,8 +1,8 @@ use std::collections::HashMap; -use golgi::{api::friends::RelationshipQuery, blobs, Sbot}; +use golgi::{api::friends::RelationshipQuery, blobs, messages::SsbMessageValue, Sbot}; use peach_lib::sbot::{SbotConfig, SbotStatus}; -use rocket::serde::Serialize; +use rocket::{futures::TryStreamExt, serde::Serialize}; use crate::{error::PeachWebError, utils}; @@ -26,6 +26,74 @@ pub async fn init_sbot_with_config( // CONTEXT STRUCTS AND BUILDERS +#[derive(Debug, Serialize)] +pub struct StatusContext { + 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, + // latest sequence number for the local log + pub latest_seq: Option, +} + +impl StatusContext { + pub fn default() -> Self { + StatusContext { + back: Some("/".to_string()), + flash_name: None, + flash_msg: None, + title: Some("Scuttlebutt Status".to_string()), + theme: None, + sbot_config: None, + sbot_status: None, + latest_seq: None, + } + } + + pub async fn build() -> 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?; + + // retrieve the local id + let id = sbot_client.whoami().await?; + + let history_stream = sbot_client.create_history_stream(id).await?; + let mut msgs: Vec = history_stream.try_collect().await?; + + // reverse the list of messages so we can easily reference the latest one + msgs.reverse(); + + // assign the sequence number of the latest msg + context.latest_seq = Some(msgs[0].sequence); + + context.sbot_config = sbot_config; + } 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, status 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) + } +} + // peers who are blocked by the local account #[derive(Debug, Serialize)] pub struct BlocksContext { diff --git a/peach-web/src/routes/status/scuttlebutt.rs b/peach-web/src/routes/status/scuttlebutt.rs index 99b97b9..2e9ff06 100644 --- a/peach-web/src/routes/status/scuttlebutt.rs +++ b/peach-web/src/routes/status/scuttlebutt.rs @@ -1,40 +1,37 @@ -use peach_lib::sbot::{SbotConfig, SbotStatus}; use rocket::{get, State}; -use rocket_dyn_templates::{tera::Context, Template}; +use rocket_dyn_templates::Template; use crate::routes::authentication::Authenticated; -use crate::utils; -use crate::RocketConfig; +use crate::{context::sbot::StatusContext, RocketConfig}; // HELPERS AND ROUTES FOR /status/scuttlebutt #[get("/scuttlebutt")] -pub fn scuttlebutt_status(_auth: Authenticated, config: &State) -> Template { - // retrieve current ui theme - let theme = utils::get_theme(); +pub async fn scuttlebutt_status(_auth: Authenticated, config: &State) -> Template { + let context = StatusContext::build().await; - // retrieve go-sbot systemd process status - let sbot_status = SbotStatus::read().ok(); - - // retrieve go-sbot configuration parameters - let sbot_config = SbotConfig::read().ok(); - - let mut context = Context::new(); - context.insert("theme", &theme); - context.insert("sbot_status", &sbot_status); - context.insert("sbot_config", &sbot_config); - context.insert("flash_name", &None::<()>); - context.insert("flash_msg", &None::<()>); - context.insert("title", &Some("Scuttlebutt Status")); - - // define back arrow url based on mode - if config.standalone_mode { + let back = if config.standalone_mode { // return to home page - context.insert("back", &Some("/")); + Some("/".to_string()) } else { // return to status menu - context.insert("back", &Some("/status")); - } + Some("/status".to_string()) + }; - Template::render("status/scuttlebutt", &context.into_json()) + match context { + Ok(mut context) => { + // define back arrow url based on mode + context.back = back; + + Template::render("status/scuttlebutt", &context) + } + Err(e) => { + let mut context = StatusContext::default(); + + // define back arrow url based on mode + context.back = back; + + Template::render("status/scuttlebutt", &context) + } + } } diff --git a/peach-web/templates/status/scuttlebutt.html.tera b/peach-web/templates/status/scuttlebutt.html.tera index 787f8a5..998efad 100644 --- a/peach-web/templates/status/scuttlebutt.html.tera +++ b/peach-web/templates/status/scuttlebutt.html.tera @@ -52,33 +52,13 @@
-
-
- - -
-
- - -
-
- - -
-
- - +
+
+ +
-