add: chat notifications

use sql_chanes/notifications_tag.sql; ignore first statement if you never used a preview commit
This commit is contained in:
trisua 2025-05-03 17:51:36 -04:00
parent a009ef9e34
commit 59cfec4819
22 changed files with 267 additions and 136 deletions

View file

@ -3,7 +3,6 @@ pub mod images;
pub mod ipbans;
pub mod profile;
pub mod social;
pub mod subscriptions;
pub mod user_warnings;
use super::{LoginProps, RegisterProps};

View file

@ -1,58 +0,0 @@
use axum::{extract::Path, response::IntoResponse, Extension, Json};
use axum_extra::extract::CookieJar;
use tetratto_core::model::{ApiReturn, Error};
use crate::{get_user_from_token, State};
pub async fn update_last_message_request(
jar: CookieJar,
Extension(data): Extension<State>,
Json((channel_id, message_id)): Json<(usize, usize)>,
) -> impl IntoResponse {
let data = &(data.read().await).0;
let mut user = match get_user_from_token!(jar, data) {
Some(ua) => ua,
None => return Json(Error::NotAllowed.into()),
};
user.subscriptions.insert(channel_id, message_id);
if let Err(e) = data
.update_user_subscriptions(user.id, user.subscriptions)
.await
{
return Json(e.into());
}
Json(ApiReturn {
ok: true,
message: "Subscription connection".to_string(),
payload: (),
})
}
pub async fn delete_request(
jar: CookieJar,
Extension(data): Extension<State>,
Path(channel): Path<usize>,
) -> impl IntoResponse {
let data = &(data.read().await).0;
let mut user = match get_user_from_token!(jar, data) {
Some(ua) => ua,
None => return Json(Error::NotAllowed.into()),
};
user.subscriptions.remove(&channel);
if let Err(e) = data
.update_user_subscriptions(user.id, user.subscriptions)
.await
{
return Json(e.into());
}
Json(ApiReturn {
ok: true,
message: "Subscription removed".to_string(),
payload: (),
})
}

View file

@ -210,6 +210,10 @@ pub fn routes() -> Router {
"/notifications/my",
delete(notifications::delete_all_request),
)
.route(
"/notifications/tag/{*tag}",
delete(notifications::delete_all_by_tag_request),
)
.route("/notifications/{id}", delete(notifications::delete_request))
.route(
"/notifications/{id}/read_status",
@ -303,15 +307,6 @@ pub fn routes() -> Router {
)
.route("/messages", post(channels::messages::create_request))
.route("/messages/{id}", delete(channels::messages::delete_request))
// subscriptions
.route(
"/auth/user/me/subscriptions/{channel_id}/{message_id}",
post(auth::subscriptions::update_last_message_request),
)
.route(
"/auth/user/me/subscriptions/{channel_id}",
delete(auth::subscriptions::delete_request),
)
}
#[derive(Deserialize)]

View file

@ -45,6 +45,27 @@ pub async fn delete_all_request(
}
}
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) {
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>,