From 0353586705f62b35893989dc636216184b758af9 Mon Sep 17 00:00:00 2001 From: glyph Date: Mon, 21 Mar 2022 11:17:30 +0200 Subject: [PATCH] add friends, follows and blocks route handlers and templates --- peach-web/src/router.rs | 12 ++++ peach-web/src/routes/scuttlebutt/blocks.rs | 21 ++++++ peach-web/src/routes/scuttlebutt/follows.rs | 21 ++++++ peach-web/src/routes/scuttlebutt/friends.rs | 21 ++++++ peach-web/src/routes/scuttlebutt/mod.rs | 3 + peach-web/src/templates/mod.rs | 2 + peach-web/src/templates/peers_list.rs | 78 +++++++++++++++++++++ 7 files changed, 158 insertions(+) create mode 100644 peach-web/src/routes/scuttlebutt/blocks.rs create mode 100644 peach-web/src/routes/scuttlebutt/follows.rs create mode 100644 peach-web/src/routes/scuttlebutt/friends.rs create mode 100644 peach-web/src/templates/peers_list.rs diff --git a/peach-web/src/router.rs b/peach-web/src/router.rs index bc35f5a..279225f 100644 --- a/peach-web/src/router.rs +++ b/peach-web/src/router.rs @@ -94,6 +94,18 @@ pub fn mount_peachpub_routes(request: &Request) -> Response { routes::settings::admin::delete::handle_form(request) }, + (GET) (/scuttlebutt/blocks) => { + Response::html(routes::scuttlebutt::blocks::build_template()) + }, + + (GET) (/scuttlebutt/follows) => { + Response::html(routes::scuttlebutt::follows::build_template()) + }, + + (GET) (/scuttlebutt/friends) => { + Response::html(routes::scuttlebutt::friends::build_template()) + }, + (GET) (/scuttlebutt/peers) => { Response::html(routes::scuttlebutt::peers::build_template()) }, diff --git a/peach-web/src/routes/scuttlebutt/blocks.rs b/peach-web/src/routes/scuttlebutt/blocks.rs new file mode 100644 index 0000000..3d23b69 --- /dev/null +++ b/peach-web/src/routes/scuttlebutt/blocks.rs @@ -0,0 +1,21 @@ +use maud::PreEscaped; + +use crate::{templates, utils::sbot}; + +/// Scuttlebutt blocks list template builder. +pub fn build_template() -> PreEscaped { + // retrieve the list of blocked peers + match sbot::get_blocks_list() { + // populate the peers_list template with blocks and render it + Ok(blocks) => templates::peers_list::build_template(blocks, "Blocks"), + Err(e) => { + // render the sbot error template with the error message + let error_template = templates::error::build_template(e.to_string()); + // wrap the nav bars around the error template content + let body = templates::nav::build_template(error_template, "Blocks", Some("/")); + + // render the base template with the provided body + templates::base::build_template(body) + } + } +} diff --git a/peach-web/src/routes/scuttlebutt/follows.rs b/peach-web/src/routes/scuttlebutt/follows.rs new file mode 100644 index 0000000..da0bcd2 --- /dev/null +++ b/peach-web/src/routes/scuttlebutt/follows.rs @@ -0,0 +1,21 @@ +use maud::PreEscaped; + +use crate::{templates, utils::sbot}; + +/// Scuttlebutt follows list template builder. +pub fn build_template() -> PreEscaped { + // retrieve the list of follows + match sbot::get_follows_list() { + // populate the peers_list template with follows + Ok(follows) => templates::peers_list::build_template(follows, "Follows"), + Err(e) => { + // render the sbot error template with the error message + let error_template = templates::error::build_template(e.to_string()); + // wrap the nav bars around the error template content + let body = templates::nav::build_template(error_template, "Follows", Some("/")); + + // render the base template with the provided body + templates::base::build_template(body) + } + } +} diff --git a/peach-web/src/routes/scuttlebutt/friends.rs b/peach-web/src/routes/scuttlebutt/friends.rs new file mode 100644 index 0000000..4f32600 --- /dev/null +++ b/peach-web/src/routes/scuttlebutt/friends.rs @@ -0,0 +1,21 @@ +use maud::PreEscaped; + +use crate::{templates, utils::sbot}; + +/// Scuttlebutt friends list template builder. +pub fn build_template() -> PreEscaped { + // retrieve the list of friends + match sbot::get_friends_list() { + // populate the peers_list template with friends and render it + Ok(friends) => templates::peers_list::build_template(friends, "Friends"), + Err(e) => { + // render the sbot error template with the error message + let error_template = templates::error::build_template(e.to_string()); + // wrap the nav bars around the error template content + let body = templates::nav::build_template(error_template, "Friends", Some("/")); + + // render the base template with the provided body + templates::base::build_template(body) + } + } +} diff --git a/peach-web/src/routes/scuttlebutt/mod.rs b/peach-web/src/routes/scuttlebutt/mod.rs index d9cebd1..018aef5 100644 --- a/peach-web/src/routes/scuttlebutt/mod.rs +++ b/peach-web/src/routes/scuttlebutt/mod.rs @@ -1 +1,4 @@ +pub mod blocks; +pub mod follows; +pub mod friends; pub mod peers; diff --git a/peach-web/src/templates/mod.rs b/peach-web/src/templates/mod.rs index d92a585..8711a46 100644 --- a/peach-web/src/templates/mod.rs +++ b/peach-web/src/templates/mod.rs @@ -1,4 +1,6 @@ pub mod base; +pub mod error; pub mod flash; pub mod inactive; pub mod nav; +pub mod peers_list; diff --git a/peach-web/src/templates/peers_list.rs b/peach-web/src/templates/peers_list.rs new file mode 100644 index 0000000..315385d --- /dev/null +++ b/peach-web/src/templates/peers_list.rs @@ -0,0 +1,78 @@ +use std::collections::HashMap; + +use maud::{html, Markup, PreEscaped}; +use peach_lib::sbot::SbotStatus; + +use crate::templates; + +/// Render an unordered list of peers with one list element for each peer. +fn peers_template(peers: Vec>) -> Markup { + html! { + ul class="center list" { + @for peer in peers { + @let (name, name_alt) = match peer.get("name") { + Some(name) => ( + name.to_owned(), + format!("{}'s profile image", name) + ), + None => ( + // set a fall-back value for name in case the data is unavailable + "name unavailable".to_string(), + "Profile image".to_string() + ) + }; + @let profile_link = format!("/scuttlebutt/profile?public_key={}", peer["id"]); + li { + a class="list-item link" href=(profile_link) { + @if peer.get("blob_path").is_some() && peer["blob_exists"] == "true" { + @let blob_path = format!("/blob/{}", peer["blob_path"]); + img id="peerImage" class="icon list-icon" src=(blob_path) alt=(name_alt); + } @else { + // use a placeholder image if we don't have the blob + img id="peerImage" class="icon icon-active list-icon" src="/icons/user.svg" alt="Placeholder profile image"; + } + p id="peerName" class="font-normal list-text" { (name) }; + @let name_title = format!("{}'s public key", name); + label class="label-small label-ellipsis list-label font-gray" for="peerName" title=(name_title) { + (peer["id"]) + } + } + } + } + } + } +} + +/// Scuttlebutt peers list template builder. +/// +/// A list of peers. Currently used to render lists of friends, follows and +/// blocks. The title of the page is set according to the provided parameter. +pub fn build_template(peers: Vec>, title: &str) -> PreEscaped { + let peer_list_template = match SbotStatus::read() { + // only render the complete peers list if the sbot is active + Ok(status) if status.state == Some("active".to_string()) => { + html! { + div class="card center" { + @if !peers.is_empty() { + // render the peers list template + (peers_template(peers)) + } @else { + p { "No follows found" } + } + } + } + } + _ => { + // the sbot is not active; render a message instead of the menu + templates::inactive::build_template("Social lists and interactions are unavailable.") + } + }; + + // wrap the nav bars around the settings menu template content + // parameters are template, title and back url + let body = + templates::nav::build_template(peer_list_template, title, Some("/scuttlebutt/peers")); + + // render the base template with the provided body + templates::base::build_template(body) +}