327 lines
9.3 KiB
Rust
327 lines
9.3 KiB
Rust
use axum::{
|
|
extract::{Query, Path},
|
|
response::{Html, IntoResponse},
|
|
Extension,
|
|
};
|
|
use crate::cookie::CookieJar;
|
|
use tetratto_core::model::{
|
|
economy::{CoinTransferMethod, UserAd, UserAdSize},
|
|
Error,
|
|
};
|
|
use crate::{assets::initial_context, get_lang, get_user_from_token, State};
|
|
use super::{render_error, PaginatedQuery};
|
|
use serde::Deserialize;
|
|
|
|
/// `/wallet`
|
|
pub async fn wallet_request(
|
|
jar: CookieJar,
|
|
Extension(data): Extension<State>,
|
|
Query(props): Query<PaginatedQuery>,
|
|
) -> impl IntoResponse {
|
|
let data = data.read().await;
|
|
let user = match get_user_from_token!(jar, data.0) {
|
|
Some(ua) => ua,
|
|
None => {
|
|
return Err(Html(
|
|
render_error(Error::NotAllowed, &jar, &data, &None).await,
|
|
));
|
|
}
|
|
};
|
|
|
|
let list = match data.0.get_transfers_by_user(user.id, 12, props.page).await {
|
|
Ok(x) => match data.0.fill_transfers(x).await {
|
|
Ok(x) => x,
|
|
Err(e) => return Err(Html(render_error(e, &jar, &data, &Some(user)).await)),
|
|
},
|
|
Err(e) => return Err(Html(render_error(e, &jar, &data, &Some(user)).await)),
|
|
};
|
|
|
|
let lang = get_lang!(jar, data.0);
|
|
let mut context = initial_context(&data.0.0.0, lang, &Some(user)).await;
|
|
|
|
context.insert("list", &list);
|
|
context.insert("page", &props.page);
|
|
|
|
// return
|
|
Ok(Html(
|
|
data.1.render("economy/wallet.html", &context).unwrap(),
|
|
))
|
|
}
|
|
|
|
/// `/products`
|
|
pub async fn products_request(
|
|
jar: CookieJar,
|
|
Extension(data): Extension<State>,
|
|
Query(props): Query<PaginatedQuery>,
|
|
) -> impl IntoResponse {
|
|
let data = data.read().await;
|
|
let user = match get_user_from_token!(jar, data.0) {
|
|
Some(ua) => ua,
|
|
None => {
|
|
return Err(Html(
|
|
render_error(Error::NotAllowed, &jar, &data, &None).await,
|
|
));
|
|
}
|
|
};
|
|
|
|
let list = match data
|
|
.0
|
|
.get_products_by_user(
|
|
user.id,
|
|
12,
|
|
if props.page_set_id == 0 {
|
|
props.page
|
|
} else {
|
|
0
|
|
},
|
|
)
|
|
.await
|
|
{
|
|
Ok(x) => x,
|
|
Err(e) => return Err(Html(render_error(e, &jar, &data, &Some(user)).await)),
|
|
};
|
|
|
|
let ads_list = match data
|
|
.0
|
|
.get_ads_by_user(
|
|
user.id,
|
|
12,
|
|
if props.page_set_id == 1 {
|
|
props.page
|
|
} else {
|
|
0
|
|
},
|
|
)
|
|
.await
|
|
{
|
|
Ok(x) => x,
|
|
Err(e) => return Err(Html(render_error(e, &jar, &data, &Some(user)).await)),
|
|
};
|
|
|
|
let lang = get_lang!(jar, data.0);
|
|
let mut context = initial_context(&data.0.0.0, lang, &Some(user)).await;
|
|
|
|
context.insert("list", &list);
|
|
context.insert("ads_list", &ads_list);
|
|
context.insert("page", &props.page);
|
|
context.insert("page_set_id", &props.page_set_id);
|
|
|
|
// return
|
|
Ok(Html(
|
|
data.1.render("economy/products.html", &context).unwrap(),
|
|
))
|
|
}
|
|
|
|
/// `/product/{id}/edit`
|
|
pub async fn edit_product_request(
|
|
jar: CookieJar,
|
|
Extension(data): Extension<State>,
|
|
Path(id): Path<usize>,
|
|
) -> impl IntoResponse {
|
|
let data = data.read().await;
|
|
let user = match get_user_from_token!(jar, data.0) {
|
|
Some(ua) => ua,
|
|
None => {
|
|
return Err(Html(
|
|
render_error(Error::NotAllowed, &jar, &data, &None).await,
|
|
));
|
|
}
|
|
};
|
|
|
|
let product = match data.0.get_product_by_id(id).await {
|
|
Ok(x) => x,
|
|
Err(e) => return Err(Html(render_error(e, &jar, &data, &Some(user)).await)),
|
|
};
|
|
|
|
if user.id != product.owner {
|
|
return Err(Html(
|
|
render_error(Error::NotAllowed, &jar, &data, &None).await,
|
|
));
|
|
}
|
|
|
|
let lang = get_lang!(jar, data.0);
|
|
let mut context = initial_context(&data.0.0.0, lang, &Some(user)).await;
|
|
context.insert("product", &product);
|
|
|
|
// return
|
|
Ok(Html(data.1.render("economy/edit.html", &context).unwrap()))
|
|
}
|
|
|
|
/// `/product/{id}`
|
|
pub async fn product_request(
|
|
jar: CookieJar,
|
|
Extension(data): Extension<State>,
|
|
Path(id): Path<usize>,
|
|
) -> impl IntoResponse {
|
|
let data = data.read().await;
|
|
let user = match get_user_from_token!(jar, data.0) {
|
|
Some(ua) => ua,
|
|
None => {
|
|
return Err(Html(
|
|
render_error(Error::NotAllowed, &jar, &data, &None).await,
|
|
));
|
|
}
|
|
};
|
|
|
|
let product = match data.0.get_product_by_id(id).await {
|
|
Ok(x) => x,
|
|
Err(e) => return Err(Html(render_error(e, &jar, &data, &Some(user)).await)),
|
|
};
|
|
|
|
let owner = match data.0.get_user_by_id(product.owner).await {
|
|
Ok(x) => x,
|
|
Err(e) => return Err(Html(render_error(e, &jar, &data, &Some(user)).await)),
|
|
};
|
|
|
|
let already_purchased = if product.single_use {
|
|
data.0
|
|
.get_transfer_by_sender_method(user.id, CoinTransferMethod::Purchase(product.id))
|
|
.await
|
|
.is_ok()
|
|
} else {
|
|
false
|
|
};
|
|
|
|
let applied_configurations_mapped: Vec<usize> =
|
|
user.applied_configurations.iter().map(|x| x.1).collect();
|
|
|
|
let lang = get_lang!(jar, data.0);
|
|
let mut context = initial_context(&data.0.0.0, lang, &Some(user)).await;
|
|
|
|
context.insert("product", &product);
|
|
context.insert("owner", &owner);
|
|
context.insert("already_purchased", &already_purchased);
|
|
context.insert(
|
|
"applied_configurations_mapped",
|
|
&applied_configurations_mapped,
|
|
);
|
|
|
|
// return
|
|
Ok(Html(
|
|
data.1.render("economy/product.html", &context).unwrap(),
|
|
))
|
|
}
|
|
|
|
/// `/product/ad/{id}/edit`
|
|
pub async fn edit_ad_request(
|
|
jar: CookieJar,
|
|
Extension(data): Extension<State>,
|
|
Path(id): Path<usize>,
|
|
) -> impl IntoResponse {
|
|
let data = data.read().await;
|
|
let user = match get_user_from_token!(jar, data.0) {
|
|
Some(ua) => ua,
|
|
None => {
|
|
return Err(Html(
|
|
render_error(Error::NotAllowed, &jar, &data, &None).await,
|
|
));
|
|
}
|
|
};
|
|
|
|
let ad = match data.0.get_ad_by_id(id).await {
|
|
Ok(x) => x,
|
|
Err(e) => return Err(Html(render_error(e, &jar, &data, &Some(user)).await)),
|
|
};
|
|
|
|
if user.id != ad.owner {
|
|
return Err(Html(
|
|
render_error(Error::NotAllowed, &jar, &data, &None).await,
|
|
));
|
|
}
|
|
|
|
let lang = get_lang!(jar, data.0);
|
|
let mut context = initial_context(&data.0.0.0, lang, &Some(user)).await;
|
|
context.insert("ad", &ad);
|
|
|
|
// return
|
|
Ok(Html(
|
|
data.1.render("economy/edit_ad.html", &context).unwrap(),
|
|
))
|
|
}
|
|
|
|
#[derive(Deserialize)]
|
|
pub struct RandomAdQuery {
|
|
pub host: usize,
|
|
#[serde(default)]
|
|
pub size: UserAdSize,
|
|
#[serde(default)]
|
|
pub noclick: bool,
|
|
}
|
|
|
|
/// `/adn/random`
|
|
pub async fn random_ad_request(
|
|
Extension(data): Extension<State>,
|
|
Query(props): Query<RandomAdQuery>,
|
|
) -> impl IntoResponse {
|
|
let data = data.read().await;
|
|
let ad = match data.0.random_ad_charged(props.size.clone()).await {
|
|
Ok(x) => x,
|
|
Err(_) => UserAd {
|
|
// polyfill ad
|
|
id: 0,
|
|
created: 0,
|
|
upload_id: 0,
|
|
owner: data.0.0.0.system_user,
|
|
target: data.0.0.0.host.clone(),
|
|
last_charge_time: 0,
|
|
is_running: true,
|
|
size: props.size,
|
|
},
|
|
};
|
|
|
|
let mut context = tera::Context::new();
|
|
context.insert("disable_click", &props.noclick);
|
|
context.insert("config", &data.0.0.0);
|
|
context.insert("host", &props.host);
|
|
context.insert("ad", &ad);
|
|
|
|
// return
|
|
(
|
|
[(
|
|
"content-security-policy",
|
|
"default-src 'self' *.spotify.com musicbrainz.org; img-src * data:; media-src *; font-src *; style-src 'unsafe-inline' 'self' *; script-src 'self' 'unsafe-inline' *; worker-src * blob:; object-src 'self' *; upgrade-insecure-requests; connect-src * localhost; frame-src 'self' blob: *; frame-ancestors *",
|
|
)],
|
|
Html(data.1.render("economy/ad.html", &context).unwrap()),
|
|
)
|
|
}
|
|
|
|
/// `/adn/{id}`
|
|
pub async fn known_ad_request(
|
|
Extension(data): Extension<State>,
|
|
Query(props): Query<RandomAdQuery>,
|
|
Path(id): Path<usize>,
|
|
) -> impl IntoResponse {
|
|
let data = data.read().await;
|
|
let ad = match data.0.get_ad_by_id(id).await {
|
|
Ok(x) => x,
|
|
Err(_) => UserAd {
|
|
// polyfill ad
|
|
id: 0,
|
|
created: 0,
|
|
upload_id: 0,
|
|
owner: data.0.0.0.system_user,
|
|
target: data.0.0.0.host.clone(),
|
|
last_charge_time: 0,
|
|
is_running: true,
|
|
size: props.size,
|
|
},
|
|
};
|
|
|
|
let mut context = tera::Context::new();
|
|
context.insert("disable_click", &props.noclick);
|
|
context.insert("config", &data.0.0.0);
|
|
context.insert("host", &props.host);
|
|
context.insert("ad", &ad);
|
|
|
|
// return
|
|
(
|
|
[
|
|
(
|
|
"content-security-policy",
|
|
"default-src 'self' *.spotify.com musicbrainz.org; img-src * data:; media-src *; font-src *; style-src 'unsafe-inline' 'self' *; script-src 'self' 'unsafe-inline' *; worker-src * blob:; object-src 'self' *; upgrade-insecure-requests; connect-src * localhost; frame-src 'self' blob: *; frame-ancestors *",
|
|
),
|
|
("Cache-Control", "no-cache"),
|
|
],
|
|
Html(data.1.render("economy/ad.html", &context).unwrap()),
|
|
)
|
|
}
|