peach-workspace/peach-web/src/routes/scuttlebutt.rs

113 lines
3.1 KiB
Rust

//! Routes for ScuttleButt related functionality.
use rocket::{get, request::FlashMessage};
use rocket_dyn_templates::Template;
use serde::Serialize;
use crate::routes::authentication::Authenticated;
// HELPERS AND ROUTES FOR /messages
#[derive(Debug, Serialize)]
pub struct MessageContext {
pub back: Option<String>,
pub flash_name: Option<String>,
pub flash_msg: Option<String>,
pub title: Option<String>,
}
impl MessageContext {
pub fn build() -> MessageContext {
MessageContext {
back: None,
flash_name: None,
flash_msg: None,
title: None,
}
}
}
#[get("/messages")]
pub fn messages(flash: Option<FlashMessage>, auth: Authenticated) -> Template {
let mut context = MessageContext::build();
context.back = Some("/".to_string());
context.title = Some("Private Messages".to_string());
// check to see if there is a flash message to display
if let Some(flash) = flash {
// add flash message contents to the context object
context.flash_name = Some(flash.kind().to_string());
context.flash_msg = Some(flash.message().to_string());
};
Template::render("messages", &context)
}
// HELPERS AND ROUTES FOR /peers
#[derive(Debug, Serialize)]
pub struct PeerContext {
pub back: Option<String>,
pub flash_name: Option<String>,
pub flash_msg: Option<String>,
pub title: Option<String>,
}
impl PeerContext {
pub fn build() -> PeerContext {
PeerContext {
back: None,
flash_name: None,
flash_msg: None,
title: None,
}
}
}
#[get("/peers")]
pub fn peers(flash: Option<FlashMessage>, auth: Authenticated) -> Template {
let mut context = PeerContext::build();
context.back = Some("/".to_string());
context.title = Some("Scuttlebutt Peers".to_string());
// check to see if there is a flash message to display
if let Some(flash) = flash {
// add flash message contents to the context object
context.flash_name = Some(flash.kind().to_string());
context.flash_msg = Some(flash.message().to_string());
};
Template::render("peers", &context)
}
// HELPERS AND ROUTES FOR /profile
#[derive(Debug, Serialize)]
pub struct ProfileContext {
pub back: Option<String>,
pub flash_name: Option<String>,
pub flash_msg: Option<String>,
pub title: Option<String>,
}
impl ProfileContext {
pub fn build() -> ProfileContext {
ProfileContext {
back: None,
flash_name: None,
flash_msg: None,
title: None,
}
}
}
#[get("/profile")]
pub fn profile(flash: Option<FlashMessage>, auth: Authenticated) -> Template {
let mut context = ProfileContext::build();
context.back = Some("/".to_string());
context.title = Some("Profile".to_string());
// check to see if there is a flash message to display
if let Some(flash) = flash {
// add flash message contents to the context object
context.flash_name = Some(flash.kind().to_string());
context.flash_msg = Some(flash.message().to_string());
};
Template::render("profile", &context)
}