set, retrieve and reset back_url cookies

This commit is contained in:
glyph 2022-06-21 12:13:36 +01:00
parent a6f52ce384
commit 5bd8a68ddf
4 changed files with 41 additions and 9 deletions

View File

@ -1,6 +1,10 @@
use rouille::{router, Request, Response}; 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() // TODO: add mount_peachcloud_routes()
// https://github.com/tomaka/rouille/issues/232#issuecomment-919225104 // https://github.com/tomaka/rouille/issues/232#issuecomment-919225104
@ -22,6 +26,8 @@ pub fn mount_peachpub_routes(
router!(request, router!(request,
(GET) (/) => { (GET) (/) => {
Response::html(routes::home::build_template()) Response::html(routes::home::build_template())
// reset the back_url cookie each time we visit the homepage
.reset_cookie("back_url")
}, },
(GET) (/auth/change) => { (GET) (/auth/change) => {
@ -49,6 +55,9 @@ pub fn mount_peachpub_routes(
(GET) (/scuttlebutt/blocks) => { (GET) (/scuttlebutt/blocks) => {
Response::html(routes::scuttlebutt::blocks::build_template()) 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) => { (POST) (/scuttlebutt/follow) => {
@ -57,10 +66,16 @@ pub fn mount_peachpub_routes(
(GET) (/scuttlebutt/follows) => { (GET) (/scuttlebutt/follows) => {
Response::html(routes::scuttlebutt::follows::build_template()) 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) => { (GET) (/scuttlebutt/friends) => {
Response::html(routes::scuttlebutt::friends::build_template()) 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) => { (GET) (/scuttlebutt/invites) => {
@ -117,6 +132,9 @@ pub fn mount_peachpub_routes(
(POST) (/scuttlebutt/search) => { (POST) (/scuttlebutt/search) => {
routes::scuttlebutt::search::handle_form(request) 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) => { (POST) (/scuttlebutt/unblock) => {
@ -187,7 +205,7 @@ pub fn mount_peachpub_routes(
}, },
(GET) (/status/scuttlebutt) => { (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 // 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::{ use crate::{
templates, templates,
utils::{flash::FlashRequest, sbot, sbot::Profile, theme}, utils::{cookie::CookieRequest, flash::FlashRequest, sbot, sbot::Profile, theme},
}; };
// ROUTE: /scuttlebutt/profile // 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."), _ => 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 // query the current theme so we can pass it into the base template builder
let theme = theme::get_theme(); 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 // wrap the nav bars around the settings menu template content
// parameters are template, title and back url // parameters are template, title and back url
let body = let body = templates::nav::build_template(
templates::nav::build_template(menu_template, "Scuttlebutt Settings", Some("/settings")); menu_template,
"Scuttlebutt Settings",
Some("/settings/scuttlebutt"),
);
// query the current theme so we can pass it into the base template builder // query the current theme so we can pass it into the base template builder
let theme = theme::get_theme(); let theme = theme::get_theme();

View File

@ -4,7 +4,7 @@ use rouille::Request;
use crate::{ use crate::{
templates, templates,
utils::{flash::FlashRequest, theme}, utils::{cookie::CookieRequest, flash::FlashRequest, theme},
}; };
/// Read the status of the go-sbot service and render buttons accordingly. /// 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 // wrap the nav bars around the settings menu template content
// parameters are template, title and back url // parameters are template, title and back url
let body = let body = templates::nav::build_template(menu_template, "Scuttlebutt Settings", back_url);
templates::nav::build_template(menu_template, "Scuttlebutt Settings", Some("/settings"));
// query the current theme so we can pass it into the base template builder // query the current theme so we can pass it into the base template builder
let theme = theme::get_theme(); let theme = theme::get_theme();