2024-07-03 14:19:58 +02:00
|
|
|
// SPDX-FileCopyrightText: 2024 Simon Bruder <simon@sbruder.de>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
|
2024-07-13 13:41:23 +02:00
|
|
|
mod auth;
|
2024-07-03 14:19:58 +02:00
|
|
|
mod item;
|
2024-07-07 13:48:31 +02:00
|
|
|
mod item_class;
|
2024-07-11 01:12:34 +02:00
|
|
|
mod templates;
|
2024-07-03 14:19:58 +02:00
|
|
|
|
2024-07-13 13:41:23 +02:00
|
|
|
use actix_identity::Identity;
|
2024-07-12 17:23:17 +02:00
|
|
|
use actix_web::{error, get, web, Responder};
|
2024-07-11 01:12:34 +02:00
|
|
|
use maud::html;
|
2024-07-12 17:23:17 +02:00
|
|
|
use serde::Deserialize;
|
|
|
|
use sqlx::PgPool;
|
|
|
|
use uuid::Uuid;
|
|
|
|
|
|
|
|
use crate::manage;
|
|
|
|
use crate::models::EntityType;
|
2024-07-03 14:19:58 +02:00
|
|
|
|
|
|
|
pub fn config(cfg: &mut web::ServiceConfig) {
|
2024-07-07 13:48:31 +02:00
|
|
|
cfg.service(index)
|
2024-07-12 17:23:17 +02:00
|
|
|
.service(jump)
|
2024-07-13 13:41:23 +02:00
|
|
|
.configure(auth::config)
|
2024-07-07 13:48:31 +02:00
|
|
|
.configure(item::config)
|
|
|
|
.configure(item_class::config);
|
2024-07-03 14:19:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/")]
|
2024-07-13 13:41:23 +02:00
|
|
|
async fn index(user: Identity) -> impl Responder {
|
|
|
|
templates::base(
|
|
|
|
templates::TemplateConfig {
|
|
|
|
user: Some(user),
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
html! {},
|
|
|
|
)
|
2024-07-03 14:19:58 +02:00
|
|
|
}
|
2024-07-12 17:23:17 +02:00
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct JumpData {
|
|
|
|
id: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/jump")]
|
|
|
|
async fn jump(
|
|
|
|
pool: web::Data<PgPool>,
|
|
|
|
data: web::Query<JumpData>,
|
2024-07-13 13:41:23 +02:00
|
|
|
_user: Identity, // this endpoint leaks information about the existence of items
|
2024-07-12 17:23:17 +02:00
|
|
|
) -> Result<impl Responder, error::Error> {
|
|
|
|
let mut id = data.id.clone();
|
|
|
|
|
|
|
|
let entity_type = if let Ok(id) = Uuid::parse_str(&id) {
|
|
|
|
manage::query_entity_type(&pool, id)
|
|
|
|
.await
|
|
|
|
.map_err(error::ErrorInternalServerError)?
|
|
|
|
} else if let Ok(short_id) = id.parse::<i32>() {
|
|
|
|
if let Ok(item) = manage::item::get_by_short_id(&pool, short_id)
|
|
|
|
.await
|
|
|
|
.map_err(error::ErrorInternalServerError)
|
|
|
|
{
|
|
|
|
id = item.id.to_string();
|
|
|
|
Some(EntityType::Item)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(prefix) = entity_type.map(|entity_type| match entity_type {
|
|
|
|
EntityType::Item => "item",
|
|
|
|
EntityType::ItemClass => "item-class",
|
|
|
|
}) {
|
|
|
|
Ok(web::Redirect::to(format!("/{prefix}/{id}")).see_other())
|
|
|
|
} else {
|
|
|
|
Ok(web::Redirect::to(format!("/items/add?name={id}")).see_other())
|
|
|
|
}
|
|
|
|
}
|