138 lines
4 KiB
Rust
138 lines
4 KiB
Rust
use super::UpdateNotificationRead;
|
|
use crate::{get_user_from_token, routes::pages::PaginatedQuery, State};
|
|
use axum::{
|
|
extract::{Path, Query},
|
|
response::IntoResponse,
|
|
Extension, Json,
|
|
};
|
|
use axum_extra::extract::CookieJar;
|
|
use tetratto_core::model::{oauth, ApiReturn, Error};
|
|
|
|
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, oauth::AppScope::UserManageNotifications) {
|
|
Some(ua) => ua,
|
|
None => return Json(Error::NotAllowed.into()),
|
|
};
|
|
|
|
match data.delete_notification(id, &user).await {
|
|
Ok(_) => Json(ApiReturn {
|
|
ok: true,
|
|
message: "Notification deleted".to_string(),
|
|
payload: (),
|
|
}),
|
|
Err(e) => Json(e.into()),
|
|
}
|
|
}
|
|
|
|
pub async fn delete_all_request(
|
|
jar: CookieJar,
|
|
Extension(data): Extension<State>,
|
|
) -> impl IntoResponse {
|
|
let data = &(data.read().await).0;
|
|
let user = match get_user_from_token!(jar, data, oauth::AppScope::UserManageNotifications) {
|
|
Some(ua) => ua,
|
|
None => return Json(Error::NotAllowed.into()),
|
|
};
|
|
|
|
match data.delete_all_notifications(&user).await {
|
|
Ok(_) => Json(ApiReturn {
|
|
ok: true,
|
|
message: "Notifications cleared".to_string(),
|
|
payload: (),
|
|
}),
|
|
Err(e) => Json(e.into()),
|
|
}
|
|
}
|
|
|
|
pub async fn delete_all_by_tag_request(
|
|
jar: CookieJar,
|
|
Extension(data): Extension<State>,
|
|
Path(tag): Path<String>,
|
|
) -> impl IntoResponse {
|
|
let data = &(data.read().await).0;
|
|
let user = match get_user_from_token!(jar, data, oauth::AppScope::UserManageNotifications) {
|
|
Some(ua) => ua,
|
|
None => return Json(Error::NotAllowed.into()),
|
|
};
|
|
|
|
match data.delete_all_notifications_by_tag(&user, &tag).await {
|
|
Ok(_) => Json(ApiReturn {
|
|
ok: true,
|
|
message: "Notifications cleared".to_string(),
|
|
payload: (),
|
|
}),
|
|
Err(e) => Json(e.into()),
|
|
}
|
|
}
|
|
|
|
pub async fn update_read_status_request(
|
|
jar: CookieJar,
|
|
Extension(data): Extension<State>,
|
|
Path(id): Path<usize>,
|
|
Json(req): Json<UpdateNotificationRead>,
|
|
) -> impl IntoResponse {
|
|
let data = &(data.read().await).0;
|
|
let user = match get_user_from_token!(jar, data, oauth::AppScope::UserManageNotifications) {
|
|
Some(ua) => ua,
|
|
None => return Json(Error::NotAllowed.into()),
|
|
};
|
|
|
|
match data.update_notification_read(id, req.read, &user).await {
|
|
Ok(_) => Json(ApiReturn {
|
|
ok: true,
|
|
message: "Notification updated".to_string(),
|
|
payload: (),
|
|
}),
|
|
Err(e) => Json(e.into()),
|
|
}
|
|
}
|
|
|
|
pub async fn update_all_read_status_request(
|
|
jar: CookieJar,
|
|
Extension(data): Extension<State>,
|
|
Json(req): Json<UpdateNotificationRead>,
|
|
) -> impl IntoResponse {
|
|
let data = &(data.read().await).0;
|
|
let user = match get_user_from_token!(jar, data, oauth::AppScope::UserManageNotifications) {
|
|
Some(ua) => ua,
|
|
None => return Json(Error::NotAllowed.into()),
|
|
};
|
|
|
|
match data.update_all_notifications_read(&user, req.read).await {
|
|
Ok(_) => Json(ApiReturn {
|
|
ok: true,
|
|
message: "Notifications updated".to_string(),
|
|
payload: (),
|
|
}),
|
|
Err(e) => Json(e.into()),
|
|
}
|
|
}
|
|
|
|
pub async fn get_list_request(
|
|
jar: CookieJar,
|
|
Extension(data): Extension<State>,
|
|
Query(props): Query<PaginatedQuery>,
|
|
) -> impl IntoResponse {
|
|
let data = &(data.read().await).0;
|
|
let user = match get_user_from_token!(jar, data, oauth::AppScope::UserReadNotifications) {
|
|
Some(ua) => ua,
|
|
None => return Json(Error::NotAllowed.into()),
|
|
};
|
|
|
|
match data
|
|
.get_notifications_by_owner_paginated(user.id, 12, props.page)
|
|
.await
|
|
{
|
|
Ok(l) => Json(ApiReturn {
|
|
ok: true,
|
|
message: "Success".to_string(),
|
|
payload: Some(l),
|
|
}),
|
|
Err(e) => Json(e.into()),
|
|
}
|
|
}
|