Merge pull request 'Improve back button behaviour' (#120) from improved_back_button into main

Reviewed-on: #120
This commit is contained in:
glyph 2022-06-23 11:00:21 +00:00
commit ab0e27c14d
8 changed files with 108 additions and 11 deletions

2
Cargo.lock generated
View File

@ -2331,7 +2331,7 @@ dependencies = [
[[package]]
name = "peach-web"
version = "0.6.15"
version = "0.6.17"
dependencies = [
"async-std",
"base64 0.13.0",

View File

@ -1,6 +1,6 @@
[package]
name = "peach-web"
version = "0.6.16"
version = "0.6.17"
authors = ["Andrew Reid <gnomad@cryptolab.net>", "Max Fowler <max@mfowler.info>"]
edition = "2018"
description = "peach-web is a web application which provides a web interface for monitoring and interacting with the PeachCloud device. This allows administration of the single-board computer (ie. Raspberry Pi) running PeachCloud, as well as the ssb-server and related plugins."

View File

@ -1,6 +1,10 @@
use rouille::{router, Request, Response};
use crate::{routes, templates, utils::flash::FlashResponse, SessionData};
use crate::{
routes, templates,
utils::{cookie::CookieResponse, flash::FlashResponse},
SessionData,
};
// TODO: add mount_peachcloud_routes()
// https://github.com/tomaka/rouille/issues/232#issuecomment-919225104
@ -22,6 +26,8 @@ pub fn mount_peachpub_routes(
router!(request,
(GET) (/) => {
Response::html(routes::home::build_template())
// reset the back_url cookie each time we visit the homepage
.reset_cookie("back_url")
},
(GET) (/auth/change) => {
@ -49,6 +55,9 @@ pub fn mount_peachpub_routes(
(GET) (/scuttlebutt/blocks) => {
Response::html(routes::scuttlebutt::blocks::build_template())
// add a back_url cookie to allow the path of the back button
// to be set correctly on the /scuttlebutt/profile page
.add_cookie("back_url=/scuttlebutt/blocks")
},
(POST) (/scuttlebutt/follow) => {
@ -57,10 +66,16 @@ pub fn mount_peachpub_routes(
(GET) (/scuttlebutt/follows) => {
Response::html(routes::scuttlebutt::follows::build_template())
// add a back_url cookie to allow the path of the back button
// to be set correctly on the /scuttlebutt/profile page
.add_cookie("back_url=/scuttlebutt/follows")
},
(GET) (/scuttlebutt/friends) => {
Response::html(routes::scuttlebutt::friends::build_template())
// add a back_url cookie to allow the path of the back button
// to be set correctly on the /scuttlebutt/profile page
.add_cookie("back_url=/scuttlebutt/friends")
},
(GET) (/scuttlebutt/invites) => {
@ -117,6 +132,9 @@ pub fn mount_peachpub_routes(
(POST) (/scuttlebutt/search) => {
routes::scuttlebutt::search::handle_form(request)
// add a back_url cookie to allow the path of the back button
// to be set correctly on the /scuttlebutt/profile page
.add_cookie("back_url=/scuttlebutt/search")
},
(POST) (/scuttlebutt/unblock) => {
@ -187,7 +205,7 @@ pub fn mount_peachpub_routes(
},
(GET) (/status/scuttlebutt) => {
Response::html(routes::status::scuttlebutt::build_template())
Response::html(routes::status::scuttlebutt::build_template()).add_cookie("back_url=/status/scuttlebutt")
},
// render the not_found template and set a 404 status code if none of

View File

@ -4,7 +4,7 @@ use rouille::Request;
use crate::{
templates,
utils::{flash::FlashRequest, sbot, sbot::Profile, theme},
utils::{cookie::CookieRequest, flash::FlashRequest, sbot, sbot::Profile, theme},
};
// ROUTE: /scuttlebutt/profile
@ -174,7 +174,15 @@ pub fn build_template(request: &Request, ssb_id: Option<String>) -> PreEscaped<S
_ => templates::inactive::build_template("Profile is unavailable."),
};
let body = templates::nav::build_template(profile_template, "Profile", Some("/"));
// a request to /scuttlebutt/profile can originate via the Friends,
// Follows or Blocks menu - as well as the Search page and Homepage.
// therefore, we check to see if the `back_url` cookie has been set
// and assign the path of the back button accordingly.
// for example, if the request has come via the Friends menu then the
// `back_url` cookie will be set with a value of "/scuttlebutt/friends".
let back_url = request.retrieve_cookie("back_url").or(Some("/"));
let body = templates::nav::build_template(profile_template, "Profile", back_url);
// query the current theme so we can pass it into the base template builder
let theme = theme::get_theme();

View File

@ -183,8 +183,11 @@ pub fn build_template(request: &Request) -> PreEscaped<String> {
// wrap the nav bars around the settings menu template content
// parameters are template, title and back url
let body =
templates::nav::build_template(menu_template, "Scuttlebutt Settings", Some("/settings"));
let body = templates::nav::build_template(
menu_template,
"Scuttlebutt Settings",
Some("/settings/scuttlebutt"),
);
// query the current theme so we can pass it into the base template builder
let theme = theme::get_theme();

View File

@ -4,7 +4,7 @@ use rouille::Request;
use crate::{
templates,
utils::{flash::FlashRequest, theme},
utils::{cookie::CookieRequest, flash::FlashRequest, theme},
};
/// Read the status of the go-sbot service and render buttons accordingly.
@ -53,10 +53,13 @@ pub fn build_template(request: &Request) -> PreEscaped<String> {
}
};
// retrieve the value of the "back_url" cookie
// if the cookie value is not found then set a hardcoded fallback value
let back_url = request.retrieve_cookie("back_url").or(Some("/settings"));
// wrap the nav bars around the settings menu template content
// parameters are template, title and back url
let body =
templates::nav::build_template(menu_template, "Scuttlebutt Settings", Some("/settings"));
let body = templates::nav::build_template(menu_template, "Scuttlebutt Settings", back_url);
// query the current theme so we can pass it into the base template builder
let theme = theme::get_theme();

View File

@ -0,0 +1,64 @@
use rouille::{input, Request, Response};
// The CookieRequest and CookieResponse traits are currently only used
// to add, retrieve and reset the `back_url` cookie. That cookie is
// used to set the URL of the in-UI back button when visiting a page
// which can be arrived at via several paths.
//
// An example of this is the Scuttlebutt Settings menu (/settings/scuttlebutt),
// which can be accessed via the Settings menu (/settings) or the Scuttlebutt
// Status page (/status/scuttlebutt). We need to be able to set the path of
// the back button to point to the correct page (ie. the one from which we've
// come).
//
// The `back_url` cookie is also used on the Profile page
// (/scuttlebutt/profile).
/// Cookie trait for `Request`.
pub trait CookieRequest {
/// Retrieve a cookie value from a `Request`.
fn retrieve_cookie(&self, cookie_name: &str) -> Option<&str>;
}
impl CookieRequest for Request {
fn retrieve_cookie(&self, cookie_name: &str) -> Option<&str> {
// check for cookie using given name
let cookie_val = input::cookies(self)
.find(|&(n, _)| n == cookie_name)
// return the value of the cookie (key is already known)
.map(|key_val| key_val.1);
cookie_val
}
}
/// Cookie trait for `Response`.
pub trait CookieResponse {
/// Add a cookie containing the given data to a `Response`. Data should be
/// in the form of `cookie_name=cookie_val`.
fn add_cookie(self, cookie_name_val: &str) -> Response;
/// Reset a cookie value for a `Response`.
fn reset_cookie(self, cookie_name: &str) -> Response;
}
impl CookieResponse for Response {
fn add_cookie(self, cookie_name_val: &str) -> Response {
// set the cookie header
// max-age is currently set to 3600 seconds (1 hour)
self.with_additional_header(
"Set-Cookie",
format!("{}; Max-Age=3600; SameSite=Lax; Path=/", cookie_name_val),
)
}
fn reset_cookie(self, cookie_name: &str) -> Response {
// set a blank cookie to clear the cookie from the previous request
self.with_additional_header(
"Set-Cookie",
format!(
"{}=; Max-Age=0; SameSite=Lax; Path=/; Expires=Fri, 21 Aug 1987 12:00:00 UTC",
cookie_name
),
)
}
}

View File

@ -1,3 +1,4 @@
pub mod cookie;
pub mod flash;
pub mod sbot;
pub mod theme;