@ -17,7 +17,10 @@ use serde::Deserialize;
|
||||
use sqlx::SqlitePool;
|
||||
use tera::Context;
|
||||
|
||||
use crate::model::user::{User, UserWithRoles};
|
||||
use crate::model::{
|
||||
notification::Notification,
|
||||
user::{User, UserWithRoles},
|
||||
};
|
||||
|
||||
pub(crate) mod admin;
|
||||
mod auth;
|
||||
@ -27,6 +30,7 @@ mod cox;
|
||||
mod ergo;
|
||||
mod log;
|
||||
mod misc;
|
||||
mod notification;
|
||||
mod planned;
|
||||
mod stat;
|
||||
|
||||
@ -43,6 +47,7 @@ async fn index(db: &State<SqlitePool>, user: User, flash: Option<FlashMessage<'_
|
||||
context.insert("flash", &msg.into_inner());
|
||||
}
|
||||
|
||||
context.insert("notifications", &Notification::for_user(db, &user).await);
|
||||
context.insert("loggedin_user", &UserWithRoles::from_user(user, db).await);
|
||||
Template::render("index", context.into_json())
|
||||
}
|
||||
@ -86,6 +91,7 @@ pub fn config(rocket: Rocket<Build>) -> Rocket<Build> {
|
||||
.mount("/log", log::routes())
|
||||
.mount("/planned", planned::routes())
|
||||
.mount("/ergo", ergo::routes())
|
||||
.mount("/notification", notification::routes())
|
||||
.mount("/stat", stat::routes())
|
||||
.mount("/boatdamage", boatdamage::routes())
|
||||
.mount("/cox", cox::routes())
|
||||
|
32
src/tera/notification.rs
Normal file
32
src/tera/notification.rs
Normal file
@ -0,0 +1,32 @@
|
||||
use rocket::{
|
||||
get,
|
||||
response::{Flash, Redirect},
|
||||
routes, Route, State,
|
||||
};
|
||||
use sqlx::SqlitePool;
|
||||
|
||||
use crate::model::{notification::Notification, user::User};
|
||||
|
||||
#[get("/<notification_id>/read")]
|
||||
async fn mark_read(db: &State<SqlitePool>, user: User, notification_id: i64) -> Flash<Redirect> {
|
||||
let Some(notification) = Notification::find_by_id(db, notification_id).await else {
|
||||
return Flash::error(
|
||||
Redirect::to("/"),
|
||||
format!("Nachricht mit ID {notification_id} nicht gefunden."),
|
||||
);
|
||||
};
|
||||
|
||||
if notification.user_id == user.id {
|
||||
notification.mark_read(db).await;
|
||||
Flash::success(Redirect::to("/"), "Nachricht als gelesen markiert")
|
||||
} else {
|
||||
Flash::success(
|
||||
Redirect::to("/"),
|
||||
"Du kannst fremde Nachrichten nicht als gelesen markieren.",
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn routes() -> Vec<Route> {
|
||||
routes![mark_read]
|
||||
}
|
Reference in New Issue
Block a user