2025-07-18 16:20:03 -07:00
|
|
|
use axum::{
|
|
|
|
|
extract::FromRequestParts,
|
|
|
|
|
http::request::Parts,
|
|
|
|
|
response::{IntoResponse as _, Redirect, Response},
|
|
|
|
|
};
|
2025-09-14 16:19:44 -04:00
|
|
|
use interim_models::portal::Portal;
|
|
|
|
|
use uuid::Uuid;
|
2025-07-18 16:20:03 -07:00
|
|
|
|
2025-09-23 13:08:51 -07:00
|
|
|
use crate::{app::App, errors::AppError};
|
2025-07-18 16:20:03 -07:00
|
|
|
|
|
|
|
|
/// Helper type for semantically generating URI paths, e.g. for redirects.
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
|
pub struct Navigator {
|
|
|
|
|
root_path: String,
|
|
|
|
|
sub_path: String,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Navigator {
|
2025-09-14 16:19:44 -04:00
|
|
|
pub(crate) fn workspace_page(&self, workspace_id: Uuid) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
sub_path: format!("/w/{0}/", workspace_id.simple()),
|
|
|
|
|
..self.clone()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) fn portal_page(&self, portal: &Portal) -> Self {
|
2025-07-18 16:20:03 -07:00
|
|
|
Self {
|
|
|
|
|
sub_path: format!(
|
2025-09-14 16:19:44 -04:00
|
|
|
"/w/{0}/r/{1}/p/{2}/",
|
|
|
|
|
portal.workspace_id.simple(),
|
|
|
|
|
portal.class_oid.0,
|
|
|
|
|
portal.id.simple()
|
2025-07-18 16:20:03 -07:00
|
|
|
),
|
|
|
|
|
..self.clone()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-14 16:19:44 -04:00
|
|
|
pub(crate) fn get_root_path(&self) -> String {
|
|
|
|
|
self.root_path.to_owned()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) fn abs_path(&self) -> String {
|
|
|
|
|
format!("{0}{1}", self.root_path, self.sub_path)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) fn redirect_to(&self) -> Response {
|
|
|
|
|
Redirect::to(&self.abs_path()).into_response()
|
2025-07-18 16:20:03 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-14 16:19:44 -04:00
|
|
|
impl FromRequestParts<App> for Navigator {
|
2025-07-18 16:20:03 -07:00
|
|
|
type Rejection = AppError;
|
|
|
|
|
|
2025-09-14 16:19:44 -04:00
|
|
|
async fn from_request_parts(_: &mut Parts, state: &App) -> Result<Self, Self::Rejection> {
|
2025-07-18 16:20:03 -07:00
|
|
|
Ok(Navigator {
|
2025-09-14 16:19:44 -04:00
|
|
|
root_path: state.settings.root_path.clone(),
|
2025-07-18 16:20:03 -07:00
|
|
|
sub_path: "/".to_owned(),
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|