flabk/flabk/src/servek/mod.rs

127 lines
2.5 KiB
Rust

use axum::response::{Html, IntoResponse};
use serde::{Deserialize, Serialize};
use tower_cookies::{Cookie, Cookies};
use crate::svc::auth::{AuthError, Claims};
mod html;
pub mod servek;
#[derive(Debug, Clone, Serialize)]
struct Timeline {
pub show_postbox: bool,
pub notes: Vec<Note>,
}
#[derive(Debug, Clone, Serialize)]
struct Note {
pub profile: Box<Profile>,
pub content: Option<String>,
}
impl Note {
fn new(profile: Box<Profile>, content: Option<String>) -> Self {
Self { profile, content }
}
}
#[derive(Debug, Clone, Serialize)]
struct Profile {
pub display_name: String,
pub username: String,
pub avatar_url: String,
}
impl Profile {
fn new(username: String) -> Self {
let display_name = username
.strip_prefix('@')
.unwrap()
.split_once('@')
.unwrap()
.0
.to_string();
Profile {
display_name,
username,
avatar_url: "/favicon.svg".to_string(),
}
}
}
#[derive(Debug, Clone, Serialize)]
struct Notification {
pub message: String,
pub tag_name: String,
}
#[derive(Debug, Clone, Deserialize)]
pub struct LoginRequest {
pub username: String,
pub password: String,
}
#[derive(Debug, Clone, Deserialize)]
pub struct CreateProfileRequest {
pub username: String,
pub password: String,
pub email: String,
}
// pub enum HtmlOrRedirect {
// Html(Html<String>),
// Redirect(()),
// }
#[derive(Debug, Clone, Serialize)]
pub struct Redirect {
pub location: String,
}
#[derive(Debug, Clone, Serialize)]
pub enum NavType {
LoggedIn,
LoggedOut,
}
impl From<Result<Claims, AuthError>> for NavType {
fn from(claims: Result<Claims, AuthError>) -> Self {
if claims.map(|c| c.expired()).unwrap_or(true) {
NavType::LoggedOut
} else {
NavType::LoggedIn
}
}
}
impl From<Claims> for NavType {
fn from(c: Claims) -> Self {
if c.expired() {
NavType::LoggedOut
} else {
NavType::LoggedIn
}
}
}
#[derive(Debug, Clone, Serialize)]
pub struct WithNav<T> {
pub nav_type: NavType,
pub obj: T,
}
impl<T> WithNav<T> {
pub fn new(obj: T, nav_type: NavType) -> WithNav<T> {
WithNav { nav_type, obj }
}
}
impl<T> Into<serde_json::Value> for WithNav<T>
where
T: Serialize,
{
fn into(self) -> serde_json::Value {
serde_json::json!(self)
}
}