add: better app data queries
This commit is contained in:
parent
9f61d9ce6a
commit
22aea48cc5
12 changed files with 175 additions and 60 deletions
|
@ -422,12 +422,9 @@ macro_rules! ignore_users_gen {
|
|||
|
||||
#[macro_export]
|
||||
macro_rules! get_app_from_key {
|
||||
($db:ident, $jar:ident) => {
|
||||
if let Some(token) = $jar.get("Atto-Secret-Key") {
|
||||
match $db
|
||||
.get_app_by_api_key(&token.to_string().replace("Atto-Secret-Key=", ""))
|
||||
.await
|
||||
{
|
||||
($db:ident, $headers:ident) => {
|
||||
if let Some(token) = $headers.get("Atto-Secret-Key") {
|
||||
match $db.get_app_by_api_key(token.to_str().unwrap()).await {
|
||||
Ok(x) => Some(x),
|
||||
Err(_) => None,
|
||||
}
|
||||
|
|
|
@ -19,7 +19,7 @@
|
|||
(div
|
||||
("class" "card flex flex-col gap-2")
|
||||
(p ("class" "fade") (text "App data keys are not included in this metric, only stored values count towards your limit."))
|
||||
(text "{% set percentage = (data_limit / app.data_used) * 100 %}")
|
||||
(text "{% set percentage = (app.data_used / data_limit) * 100 %}")
|
||||
(div ("class" "progress_bar") (div ("class" "poll_bar") ("style" "width: {{ percentage }}%")))
|
||||
(div
|
||||
("class" "w-full flex justify-between items-center")
|
||||
|
|
|
@ -47,13 +47,12 @@
|
|||
("class" "flex flex-col gap-1")
|
||||
(label
|
||||
("for" "title")
|
||||
(text "{{ text \"developer:label.redirect\" }}"))
|
||||
(text "{{ text \"developer:label.redirect\" }} (optional)"))
|
||||
(input
|
||||
("type" "url")
|
||||
("name" "redirect")
|
||||
("id" "redirect")
|
||||
("placeholder" "redirect URL")
|
||||
("required" "")
|
||||
("minlength" "2")
|
||||
("maxlength" "32")))
|
||||
(button
|
||||
|
@ -125,7 +124,7 @@
|
|||
body: JSON.stringify({
|
||||
title: e.target.title.value,
|
||||
homepage: e.target.homepage.value,
|
||||
redirect: e.target.redirect.value,
|
||||
redirect: e.target.redirect.value || \"\",
|
||||
}),
|
||||
})
|
||||
.then((res) => res.json())
|
||||
|
|
|
@ -39,6 +39,13 @@
|
|||
(str (text "dialog:action.cancel")))))))
|
||||
(script
|
||||
(text "setTimeout(() => {
|
||||
// {% if app.redirect|length == 0 %}
|
||||
alert(\"App has an invalid redirect. Please contact the owner for help.\");
|
||||
window.close();
|
||||
return;
|
||||
// {% endif %}
|
||||
|
||||
// ...
|
||||
globalThis.authorize = async (event) => {
|
||||
if (
|
||||
!(await trigger(\"atto::confirm\", [
|
||||
|
|
|
@ -3,20 +3,19 @@ use crate::{
|
|||
routes::api::v1::{InsertAppData, QueryAppData, UpdateAppDataValue},
|
||||
State,
|
||||
};
|
||||
use axum::{Extension, Json, extract::Path, response::IntoResponse};
|
||||
use axum_extra::extract::CookieJar;
|
||||
use axum::{extract::Path, http::HeaderMap, response::IntoResponse, Extension, Json};
|
||||
use tetratto_core::model::{
|
||||
apps::{AppData, AppDataQuery},
|
||||
apps::{AppData, AppDataQuery, AppDataQueryResult},
|
||||
ApiReturn, Error,
|
||||
};
|
||||
|
||||
pub async fn query_request(
|
||||
jar: CookieJar,
|
||||
headers: HeaderMap,
|
||||
Extension(data): Extension<State>,
|
||||
Json(req): Json<QueryAppData>,
|
||||
) -> impl IntoResponse {
|
||||
let data = &(data.read().await).0;
|
||||
let app = match get_app_from_key!(data, jar) {
|
||||
let app = match get_app_from_key!(data, headers) {
|
||||
Some(x) => x,
|
||||
None => return Json(Error::NotAllowed.into()),
|
||||
};
|
||||
|
@ -39,12 +38,12 @@ pub async fn query_request(
|
|||
}
|
||||
|
||||
pub async fn create_request(
|
||||
jar: CookieJar,
|
||||
headers: HeaderMap,
|
||||
Extension(data): Extension<State>,
|
||||
Json(req): Json<InsertAppData>,
|
||||
) -> impl IntoResponse {
|
||||
let data = &(data.read().await).0;
|
||||
let app = match get_app_from_key!(data, jar) {
|
||||
let app = match get_app_from_key!(data, headers) {
|
||||
Some(x) => x,
|
||||
None => return Json(Error::NotAllowed.into()),
|
||||
};
|
||||
|
@ -61,7 +60,7 @@ pub async fn create_request(
|
|||
}
|
||||
|
||||
// ...
|
||||
if let Err(e) = data.update_app_data_used(app.id, new_size as i32).await {
|
||||
if let Err(e) = data.add_app_data_used(app.id, req.value.len() as i32).await {
|
||||
return Json(e.into());
|
||||
}
|
||||
|
||||
|
@ -71,7 +70,7 @@ pub async fn create_request(
|
|||
{
|
||||
Ok(s) => Json(ApiReturn {
|
||||
ok: true,
|
||||
message: "App created".to_string(),
|
||||
message: "Data inserted".to_string(),
|
||||
payload: s.id.to_string(),
|
||||
}),
|
||||
Err(e) => Json(e.into()),
|
||||
|
@ -79,13 +78,13 @@ pub async fn create_request(
|
|||
}
|
||||
|
||||
pub async fn update_value_request(
|
||||
jar: CookieJar,
|
||||
headers: HeaderMap,
|
||||
Extension(data): Extension<State>,
|
||||
Path(id): Path<usize>,
|
||||
Json(req): Json<UpdateAppDataValue>,
|
||||
) -> impl IntoResponse {
|
||||
let data = &(data.read().await).0;
|
||||
let app = match get_app_from_key!(data, jar) {
|
||||
let app = match get_app_from_key!(data, headers) {
|
||||
Some(x) => x,
|
||||
None => return Json(Error::NotAllowed.into()),
|
||||
};
|
||||
|
@ -109,7 +108,7 @@ pub async fn update_value_request(
|
|||
}
|
||||
|
||||
// ...
|
||||
if let Err(e) = data.update_app_data_used(app.id, new_size as i32).await {
|
||||
if let Err(e) = data.add_app_data_used(app.id, req.value.len() as i32).await {
|
||||
return Json(e.into());
|
||||
}
|
||||
|
||||
|
@ -124,12 +123,12 @@ pub async fn update_value_request(
|
|||
}
|
||||
|
||||
pub async fn delete_request(
|
||||
jar: CookieJar,
|
||||
headers: HeaderMap,
|
||||
Extension(data): Extension<State>,
|
||||
Path(id): Path<usize>,
|
||||
) -> impl IntoResponse {
|
||||
let data = &(data.read().await).0;
|
||||
let app = match get_app_from_key!(data, jar) {
|
||||
let app = match get_app_from_key!(data, headers) {
|
||||
Some(x) => x,
|
||||
None => return Json(Error::NotAllowed.into()),
|
||||
};
|
||||
|
@ -141,7 +140,7 @@ pub async fn delete_request(
|
|||
|
||||
// ...
|
||||
if let Err(e) = data
|
||||
.update_app_data_used(app.id, (app.data_used - app_data.value.len()) as i32)
|
||||
.add_app_data_used(app.id, -(app_data.value.len() as i32))
|
||||
.await
|
||||
{
|
||||
return Json(e.into());
|
||||
|
@ -156,3 +155,60 @@ pub async fn delete_request(
|
|||
Err(e) => Json(e.into()),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn delete_query_request(
|
||||
headers: HeaderMap,
|
||||
Extension(data): Extension<State>,
|
||||
Json(req): Json<QueryAppData>,
|
||||
) -> impl IntoResponse {
|
||||
let data = &(data.read().await).0;
|
||||
let app = match get_app_from_key!(data, headers) {
|
||||
Some(x) => x,
|
||||
None => return Json(Error::NotAllowed.into()),
|
||||
};
|
||||
|
||||
// ...
|
||||
let rows = match data
|
||||
.query_app_data(AppDataQuery {
|
||||
app: app.id,
|
||||
query: req.query.clone(),
|
||||
mode: req.mode.clone(),
|
||||
})
|
||||
.await
|
||||
{
|
||||
Ok(x) => match x {
|
||||
AppDataQueryResult::One(x) => vec![x],
|
||||
AppDataQueryResult::Many(x) => x,
|
||||
},
|
||||
Err(e) => return Json(e.into()),
|
||||
};
|
||||
|
||||
let mut subtract_amount: usize = 0;
|
||||
for row in &rows {
|
||||
subtract_amount += row.value.len();
|
||||
}
|
||||
drop(rows);
|
||||
|
||||
if let Err(e) = data
|
||||
.add_app_data_used(app.id, -(subtract_amount as i32))
|
||||
.await
|
||||
{
|
||||
return Json(e.into());
|
||||
}
|
||||
|
||||
match data
|
||||
.query_delete_app_data(AppDataQuery {
|
||||
app: app.id,
|
||||
query: req.query,
|
||||
mode: req.mode,
|
||||
})
|
||||
.await
|
||||
{
|
||||
Ok(_) => Json(ApiReturn {
|
||||
ok: true,
|
||||
message: "Data deleted".to_string(),
|
||||
payload: (),
|
||||
}),
|
||||
Err(e) => Json(e.into()),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -433,9 +433,10 @@ pub fn routes() -> Router {
|
|||
.route("/apps/{id}/roll", post(apps::roll_api_key_request))
|
||||
// app data
|
||||
.route("/app_data", post(app_data::create_request))
|
||||
.route("/app_data/query", post(app_data::query_request))
|
||||
.route("/app_data/{id}", delete(app_data::delete_request))
|
||||
.route("/app_data/{id}/value", post(app_data::update_value_request))
|
||||
.route("/app_data/query", post(app_data::query_request))
|
||||
.route("/app_data/query", delete(app_data::delete_query_request))
|
||||
// warnings
|
||||
.route("/warnings/{id}", get(auth::user_warnings::get_request))
|
||||
.route("/warnings/{id}", post(auth::user_warnings::create_request))
|
||||
|
@ -987,6 +988,7 @@ pub struct UpdatePostIsOpen {
|
|||
pub struct CreateApp {
|
||||
pub title: String,
|
||||
pub homepage: String,
|
||||
#[serde(default)]
|
||||
pub redirect: String,
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue