2025-03-25 23:58:27 -04:00
|
|
|
use crate::{
|
|
|
|
State, get_user_from_token,
|
|
|
|
model::{ApiReturn, Error},
|
2025-03-31 11:45:34 -04:00
|
|
|
routes::api::v1::{UpdateUserIsVerified, UpdateUserPassword, UpdateUserUsername},
|
2025-03-25 23:58:27 -04:00
|
|
|
};
|
2025-03-29 00:26:56 -04:00
|
|
|
use axum::{
|
|
|
|
Extension, Json,
|
|
|
|
extract::Path,
|
|
|
|
response::{IntoResponse, Redirect},
|
|
|
|
};
|
2025-03-25 23:58:27 -04:00
|
|
|
use axum_extra::extract::CookieJar;
|
2025-03-26 21:46:21 -04:00
|
|
|
use tetratto_core::model::{
|
|
|
|
auth::{Token, UserSettings},
|
|
|
|
permissions::FinePermission,
|
|
|
|
};
|
2025-03-25 23:58:27 -04:00
|
|
|
|
2025-03-29 00:26:56 -04:00
|
|
|
pub async fn redirect_from_id(
|
|
|
|
Extension(data): Extension<State>,
|
|
|
|
Path(id): Path<String>,
|
|
|
|
) -> impl IntoResponse {
|
2025-03-31 22:35:11 -04:00
|
|
|
match (data.read().await)
|
|
|
|
.0
|
2025-03-29 00:26:56 -04:00
|
|
|
.get_user_by_id(match id.parse::<usize>() {
|
|
|
|
Ok(id) => id,
|
|
|
|
Err(_) => return Redirect::to("/"),
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
{
|
2025-03-31 22:35:11 -04:00
|
|
|
Ok(u) => Redirect::to(&format!("/@{}", u.username)),
|
2025-03-29 00:26:56 -04:00
|
|
|
Err(_) => Redirect::to("/"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-03-25 23:58:27 -04:00
|
|
|
/// Update the settings of the given user.
|
|
|
|
pub async fn update_profile_settings_request(
|
|
|
|
jar: CookieJar,
|
|
|
|
Path(id): Path<usize>,
|
|
|
|
Extension(data): Extension<State>,
|
|
|
|
Json(req): Json<UserSettings>,
|
|
|
|
) -> 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()),
|
|
|
|
};
|
|
|
|
|
2025-03-31 15:39:49 -04:00
|
|
|
if user.id != id && !user.permissions.check(FinePermission::MANAGE_USERS) {
|
|
|
|
return Json(Error::NotAllowed.into());
|
2025-03-25 23:58:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
match data.update_user_settings(id, req).await {
|
|
|
|
Ok(_) => Json(ApiReturn {
|
|
|
|
ok: true,
|
2025-03-26 21:46:21 -04:00
|
|
|
message: "Settings updated".to_string(),
|
|
|
|
payload: (),
|
|
|
|
}),
|
|
|
|
Err(e) => Json(e.into()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-03-31 11:45:34 -04:00
|
|
|
/// Update the password of the given user.
|
|
|
|
pub async fn update_profile_password_request(
|
|
|
|
jar: CookieJar,
|
|
|
|
Path(id): Path<usize>,
|
|
|
|
Extension(data): Extension<State>,
|
|
|
|
Json(req): Json<UpdateUserPassword>,
|
|
|
|
) -> 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()),
|
|
|
|
};
|
|
|
|
|
2025-03-31 15:39:49 -04:00
|
|
|
if user.id != id && !user.permissions.check(FinePermission::MANAGE_USERS) {
|
|
|
|
return Json(Error::NotAllowed.into());
|
2025-03-31 11:45:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
match data
|
|
|
|
.update_user_password(id, req.from, req.to, user, false)
|
|
|
|
.await
|
|
|
|
{
|
|
|
|
Ok(_) => Json(ApiReturn {
|
|
|
|
ok: true,
|
|
|
|
message: "Password updated".to_string(),
|
|
|
|
payload: (),
|
|
|
|
}),
|
|
|
|
Err(e) => Json(e.into()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn update_profile_username_request(
|
|
|
|
jar: CookieJar,
|
|
|
|
Path(id): Path<usize>,
|
|
|
|
Extension(data): Extension<State>,
|
|
|
|
Json(req): Json<UpdateUserUsername>,
|
|
|
|
) -> 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()),
|
|
|
|
};
|
|
|
|
|
2025-03-31 15:39:49 -04:00
|
|
|
if user.id != id && !user.permissions.check(FinePermission::MANAGE_USERS) {
|
|
|
|
return Json(Error::NotAllowed.into());
|
2025-03-31 11:45:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if data.get_user_by_username(&req.to).await.is_ok() {
|
|
|
|
return Json(Error::UsernameInUse.into());
|
|
|
|
}
|
|
|
|
|
|
|
|
match data.update_user_username(id, req.to, user).await {
|
|
|
|
Ok(_) => Json(ApiReturn {
|
|
|
|
ok: true,
|
|
|
|
message: "Username updated".to_string(),
|
|
|
|
payload: (),
|
|
|
|
}),
|
|
|
|
Err(e) => Json(e.into()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-03-26 21:46:21 -04:00
|
|
|
/// Update the tokens of the given user.
|
|
|
|
pub async fn update_profile_tokens_request(
|
|
|
|
jar: CookieJar,
|
|
|
|
Path(id): Path<usize>,
|
|
|
|
Extension(data): Extension<State>,
|
|
|
|
Json(req): Json<Vec<Token>>,
|
|
|
|
) -> 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()),
|
|
|
|
};
|
|
|
|
|
2025-03-31 15:39:49 -04:00
|
|
|
if user.id != id && !user.permissions.check(FinePermission::MANAGE_USERS) {
|
|
|
|
return Json(Error::NotAllowed.into());
|
2025-03-26 21:46:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
match data.update_user_tokens(id, req).await {
|
|
|
|
Ok(_) => Json(ApiReturn {
|
|
|
|
ok: true,
|
|
|
|
message: "Tokens updated".to_string(),
|
|
|
|
payload: (),
|
|
|
|
}),
|
|
|
|
Err(e) => Json(e.into()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Update the verification status of the given user.
|
|
|
|
pub async fn update_profile_is_verified_request(
|
|
|
|
jar: CookieJar,
|
|
|
|
Path(id): Path<usize>,
|
|
|
|
Extension(data): Extension<State>,
|
|
|
|
Json(req): Json<UpdateUserIsVerified>,
|
|
|
|
) -> 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_user_verified_status(id, req.is_verified, user)
|
|
|
|
.await
|
|
|
|
{
|
|
|
|
Ok(_) => Json(ApiReturn {
|
|
|
|
ok: true,
|
|
|
|
message: "Verified status updated".to_string(),
|
2025-03-25 23:58:27 -04:00
|
|
|
payload: (),
|
|
|
|
}),
|
|
|
|
Err(e) => Json(e.into()),
|
|
|
|
}
|
|
|
|
}
|