add: store ad clicked state in cookies

This commit is contained in:
trisua 2025-08-12 15:09:08 -04:00
parent 83971b3d20
commit befd9096b1
5 changed files with 119 additions and 65 deletions

View file

@ -6,8 +6,9 @@ use crate::{
};
use axum::{
extract::Path,
response::{IntoResponse, Redirect},
response::{Html, IntoResponse},
Extension, Json,
http::StatusCode,
};
use tetratto_core::model::{
economy::UserAd,
@ -139,8 +140,56 @@ pub async fn click_request(
let data = &(data.read().await).0;
let user = get_user_from_token!(jar, data);
let bad_return = (
StatusCode::FOUND,
[
(
"Set-Cookie".to_string(),
format!(
"Atto-Clicked=true; Path=/api/v1/ads/host/{host}/{id}/click; Max-Age=86400"
),
),
("Location".to_string(), data.0.0.host.clone()),
],
Html(""),
);
if jar.get("Atto-Clicked").is_some() {
// we've already clicked this ad, don't charge
match data.get_ad_by_id(id).await {
Ok(x) => {
return (
StatusCode::FOUND,
[
(
"Set-Cookie".to_string(),
format!(
"Atto-Clicked=true; Path=/api/v1/ads/host/{host}/{id}/click; Max-Age=86400"
),
),
("Location".to_string(), x.target),
],
Html(""),
);
}
Err(_) => return bad_return,
}
}
match data.ad_click(host, id, user).await {
Ok(t) => Redirect::to(&t),
Err(_) => Redirect::to(&data.0.0.host),
Ok(t) => (
StatusCode::FOUND,
[
(
"Set-Cookie".to_string(),
format!(
"Atto-Clicked=true; Path=/api/v1/ads/host/{host}/{id}/click; Max-Age=86400"
),
),
("Location".to_string(), t),
],
Html(""),
),
Err(_) => bad_return,
}
}