update sbot status context and add latest seq num

This commit is contained in:
2022-03-02 15:24:05 +02:00
parent 9a07ab3ac0
commit b1724f6eb4
3 changed files with 98 additions and 53 deletions

View File

@ -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<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>,
// latest sequence number for the local log
pub latest_seq: Option<u64>,
}
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<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?;
// retrieve the local id
let id = sbot_client.whoami().await?;
let history_stream = sbot_client.create_history_stream(id).await?;
let mut msgs: Vec<SsbMessageValue> = 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 {

View File

@ -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<RocketConfig>) -> Template {
// retrieve current ui theme
let theme = utils::get_theme();
pub async fn scuttlebutt_status(_auth: Authenticated, config: &State<RocketConfig>) -> 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)
}
}
}

View File

@ -52,33 +52,13 @@
</div>
<hr style="color: var(--light-gray);">
<div id="middleSection" style="margin-top: 1rem;">
<div id="ssbSocialCounts" class="center" style="display: flex; justify-content: space-between; width: 90%;">
<div style="display: flex; align-items: last baseline;">
<label class="card-text" style="margin-right: 2px;">21</label>
<label class="label-small font-gray">Friends</label>
</div>
<div style="display: flex; align-items: last baseline;">
<label class="card-text" style="margin-right: 2px;">5</label>
<label class="label-small font-gray">Follows</label>
</div>
<div style="display: flex; align-items: last baseline;">
<label class="card-text" style="margin-right: 2px;">38</label>
<label class="label-small font-gray">Followers</label>
</div>
<div style="display: flex; align-items: last baseline;">
<label class="card-text" style="margin-right: 2px;">2</label>
<label class="label-small font-gray">Blocks</label>
<div id="sbotInfo" class="center" style="display: flex; justify-content: space-between; width: 90%;">
<div class="center" style="display: flex; align-items: last baseline;">
<label class="card-text" style="margin-right: 5px;">{{ latest_seq }}</label>
<label class="label-small font-gray">MESSAGES IN LOCAL DATABASE</label>
</div>
</div>
</div>
<!--
not implemented yet:
<p class="card-text">Latest sequence number: </p>
<p class="card-text">Network key: </p>
<p>Blobstore size: </p>
<p>Last time you visited this page, latest sequence was x ... now it's y</p>
<p>Number of follows / followers / friends / blocks</p>
-->
<hr style="color: var(--light-gray);">
<!-- THREE-ACROSS STACK -->
<div class="three-grid card-container" style="margin-top: 1rem;">