From 5bd8a68ddfef21a5a596a6ce14bb5bf4f9278c26 Mon Sep 17 00:00:00 2001 From: glyph Date: Tue, 21 Jun 2022 12:13:36 +0100 Subject: [PATCH] set, retrieve and reset back_url cookies --- peach-web/src/private_router.rs | 22 +++++++++++++++++-- peach-web/src/routes/scuttlebutt/profile.rs | 12 ++++++++-- .../routes/settings/scuttlebutt/configure.rs | 7 ++++-- .../src/routes/settings/scuttlebutt/menu.rs | 9 +++++--- 4 files changed, 41 insertions(+), 9 deletions(-) diff --git a/peach-web/src/private_router.rs b/peach-web/src/private_router.rs index e48f337..5bc7665 100644 --- a/peach-web/src/private_router.rs +++ b/peach-web/src/private_router.rs @@ -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 diff --git a/peach-web/src/routes/scuttlebutt/profile.rs b/peach-web/src/routes/scuttlebutt/profile.rs index 43875ac..007aeb8 100644 --- a/peach-web/src/routes/scuttlebutt/profile.rs +++ b/peach-web/src/routes/scuttlebutt/profile.rs @@ -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) -> PreEscaped 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(); diff --git a/peach-web/src/routes/settings/scuttlebutt/configure.rs b/peach-web/src/routes/settings/scuttlebutt/configure.rs index dcba7b4..d2a54cf 100644 --- a/peach-web/src/routes/settings/scuttlebutt/configure.rs +++ b/peach-web/src/routes/settings/scuttlebutt/configure.rs @@ -183,8 +183,11 @@ pub fn build_template(request: &Request) -> PreEscaped { // 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(); diff --git a/peach-web/src/routes/settings/scuttlebutt/menu.rs b/peach-web/src/routes/settings/scuttlebutt/menu.rs index 66997fd..4360898 100644 --- a/peach-web/src/routes/settings/scuttlebutt/menu.rs +++ b/peach-web/src/routes/settings/scuttlebutt/menu.rs @@ -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 { } }; + // 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();