add: notifications for likes

TODO: notifications ui
This commit is contained in:
trisua 2025-03-29 23:51:13 -04:00
parent 6413ed09fb
commit 9dc75d7095
9 changed files with 292 additions and 27 deletions

View file

@ -1,5 +1,6 @@
pub mod auth;
pub mod communities;
pub mod notifications;
pub mod reactions;
use axum::{
@ -120,6 +121,16 @@ pub fn routes() -> Router {
"/auth/profile/find/{id}",
get(auth::profile::redirect_from_id),
)
// notifications
.route(
"/notifications/my",
delete(notifications::delete_all_request),
)
.route("/notifications/{id}", delete(notifications::delete_request))
.route(
"/notifications/{id}/read",
delete(notifications::update_read_status_request),
)
}
#[derive(Deserialize)]
@ -182,3 +193,8 @@ pub struct CreateReaction {
pub struct UpdateUserIsVerified {
pub is_verified: bool,
}
#[derive(Deserialize)]
pub struct UpdateNotificationRead {
pub read: bool,
}

View file

@ -0,0 +1,70 @@
use axum::{Extension, Json, extract::Path, response::IntoResponse};
use axum_extra::extract::CookieJar;
use tetratto_core::model::{ApiReturn, Error};
use crate::{State, get_user_from_token};
use super::UpdateNotificationRead;
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_notification(id, &user).await {
Ok(_) => Json(ApiReturn {
ok: true,
message: "Notification deleted".to_string(),
payload: (),
}),
Err(e) => return 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) {
Some(ua) => ua,
None => return Json(Error::NotAllowed.into()),
};
match data.delete_all_notifications(&user).await {
Ok(_) => Json(ApiReturn {
ok: true,
message: "Notifications deleted".to_string(),
payload: (),
}),
Err(e) => return 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) {
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) => return Json(e.into()),
}
}

View file

@ -61,12 +61,10 @@ pub async fn create_request(
// create reaction
match data
.create_reaction(Reaction::new(
user.id,
asset_id,
req.asset_type,
req.is_like,
))
.create_reaction(
Reaction::new(user.id, asset_id, req.asset_type, req.is_like),
&user,
)
.await
{
Ok(_) => Json(ApiReturn {