152 lines
4.1 KiB
Rust
152 lines
4.1 KiB
Rust
|
use crate::{
|
||
|
get_user_from_token,
|
||
|
routes::api::v1::{UpdateAppHomepage, UpdateAppQuotaStatus, UpdateAppRedirect, UpdateAppTitle},
|
||
|
State,
|
||
|
};
|
||
|
use axum::{Extension, Json, extract::Path, response::IntoResponse};
|
||
|
use axum_extra::extract::CookieJar;
|
||
|
use tetratto_core::model::{apps::ThirdPartyApp, permissions::FinePermission, ApiReturn, Error};
|
||
|
use super::CreateApp;
|
||
|
|
||
|
pub async fn create_request(
|
||
|
jar: CookieJar,
|
||
|
Extension(data): Extension<State>,
|
||
|
Json(req): Json<CreateApp>,
|
||
|
) -> impl IntoResponse {
|
||
|
let data = &(data.read().await).0;
|
||
|
let user = match get_user_from_token!(jar, data) {
|
||
|
Some(ua) => ua,
|
||
|
None => return Json(Error::NotAllowed.into()),
|
||
|
};
|
||
|
|
||
|
match data
|
||
|
.create_app(ThirdPartyApp::new(
|
||
|
req.title,
|
||
|
user.id,
|
||
|
req.homepage,
|
||
|
req.redirect,
|
||
|
))
|
||
|
.await
|
||
|
{
|
||
|
Ok(s) => Json(ApiReturn {
|
||
|
ok: true,
|
||
|
message: "App created".to_string(),
|
||
|
payload: s.id.to_string(),
|
||
|
}),
|
||
|
Err(e) => Json(e.into()),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
pub async fn update_title_request(
|
||
|
jar: CookieJar,
|
||
|
Extension(data): Extension<State>,
|
||
|
Path(id): Path<usize>,
|
||
|
Json(req): Json<UpdateAppTitle>,
|
||
|
) -> impl IntoResponse {
|
||
|
let data = &(data.read().await).0;
|
||
|
let user = match get_user_from_token!(jar, data) {
|
||
|
Some(ua) => ua,
|
||
|
None => return Json(Error::NotAllowed.into()),
|
||
|
};
|
||
|
|
||
|
match data.update_app_title(id, &user, &req.title).await {
|
||
|
Ok(_) => Json(ApiReturn {
|
||
|
ok: true,
|
||
|
message: "App updated".to_string(),
|
||
|
payload: (),
|
||
|
}),
|
||
|
Err(e) => Json(e.into()),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
pub async fn update_homepage_request(
|
||
|
jar: CookieJar,
|
||
|
Extension(data): Extension<State>,
|
||
|
Path(id): Path<usize>,
|
||
|
Json(req): Json<UpdateAppHomepage>,
|
||
|
) -> impl IntoResponse {
|
||
|
let data = &(data.read().await).0;
|
||
|
let user = match get_user_from_token!(jar, data) {
|
||
|
Some(ua) => ua,
|
||
|
None => return Json(Error::NotAllowed.into()),
|
||
|
};
|
||
|
|
||
|
match data.update_app_homepage(id, &user, &req.homepage).await {
|
||
|
Ok(_) => Json(ApiReturn {
|
||
|
ok: true,
|
||
|
message: "App updated".to_string(),
|
||
|
payload: (),
|
||
|
}),
|
||
|
Err(e) => Json(e.into()),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
pub async fn update_redirect_request(
|
||
|
jar: CookieJar,
|
||
|
Extension(data): Extension<State>,
|
||
|
Path(id): Path<usize>,
|
||
|
Json(req): Json<UpdateAppRedirect>,
|
||
|
) -> impl IntoResponse {
|
||
|
let data = &(data.read().await).0;
|
||
|
let user = match get_user_from_token!(jar, data) {
|
||
|
Some(ua) => ua,
|
||
|
None => return Json(Error::NotAllowed.into()),
|
||
|
};
|
||
|
|
||
|
match data.update_app_redirect(id, &user, &req.redirect).await {
|
||
|
Ok(_) => Json(ApiReturn {
|
||
|
ok: true,
|
||
|
message: "App updated".to_string(),
|
||
|
payload: (),
|
||
|
}),
|
||
|
Err(e) => Json(e.into()),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
pub async fn update_quota_status_request(
|
||
|
jar: CookieJar,
|
||
|
Extension(data): Extension<State>,
|
||
|
Path(id): Path<usize>,
|
||
|
Json(req): Json<UpdateAppQuotaStatus>,
|
||
|
) -> impl IntoResponse {
|
||
|
let data = &(data.read().await).0;
|
||
|
let user = match get_user_from_token!(jar, data) {
|
||
|
Some(ua) => ua,
|
||
|
None => return Json(Error::NotAllowed.into()),
|
||
|
};
|
||
|
|
||
|
if !user.permissions.check(FinePermission::MANAGE_APPS) {
|
||
|
return Json(Error::NotAllowed.into());
|
||
|
}
|
||
|
|
||
|
match data.update_app_quota_status(id, req.quota_status).await {
|
||
|
Ok(_) => Json(ApiReturn {
|
||
|
ok: true,
|
||
|
message: "App updated".to_string(),
|
||
|
payload: (),
|
||
|
}),
|
||
|
Err(e) => Json(e.into()),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
pub async fn delete_request(
|
||
|
jar: CookieJar,
|
||
|
Extension(data): Extension<State>,
|
||
|
Path(id): Path<usize>,
|
||
|
) -> impl IntoResponse {
|
||
|
let data = &(data.read().await).0;
|
||
|
let user = match get_user_from_token!(jar, data) {
|
||
|
Some(ua) => ua,
|
||
|
None => return Json(Error::NotAllowed.into()),
|
||
|
};
|
||
|
|
||
|
match data.delete_app(id, &user).await {
|
||
|
Ok(_) => Json(ApiReturn {
|
||
|
ok: true,
|
||
|
message: "App deleted".to_string(),
|
||
|
payload: (),
|
||
|
}),
|
||
|
Err(e) => Json(e.into()),
|
||
|
}
|
||
|
}
|