Compare commits

..

No commits in common. "1285c3bc28189acc8c173f2c58bc82903cb6138f" and "830aa58e7b6f236649b1fa2c7fda52393b8de8b3" have entirely different histories.

2 changed files with 8 additions and 62 deletions

View File

@ -4,7 +4,6 @@ use crate::model::{
role::Role,
user::{AdminUser, User, UserWithDetails},
};
use itertools::Itertools;
use rocket::{
form::Form,
get, post,
@ -29,39 +28,22 @@ async fn index(
"loggedin_user",
&UserWithDetails::from_user(user.user, db).await,
);
let users: Vec<User> = User::all(db)
.await
.into_iter()
.filter(|u| u.last_access.is_some()) // Not useful to send notifications to people who are
// not logging in
.sorted_by_key(|u| u.name.clone())
.collect();
context.insert("roles", &Role::all(db).await);
context.insert("users", &users);
Template::render("admin/notification", context.into_json())
}
#[derive(FromForm, Debug)]
pub struct NotificationToSendGroup {
pub struct NotificationToSend {
pub(crate) role_id: i32,
pub(crate) category: String,
pub(crate) message: String,
}
#[derive(FromForm, Debug)]
pub struct NotificationToSendUser {
pub(crate) user_id: i32,
pub(crate) category: String,
pub(crate) message: String,
}
#[post("/notification/group", data = "<data>")]
async fn send_group(
#[post("/notification", data = "<data>")]
async fn send(
db: &State<SqlitePool>,
data: Form<NotificationToSendGroup>,
data: Form<NotificationToSend>,
admin: AdminUser,
) -> Flash<Redirect> {
let d = data.into_inner();
@ -85,32 +67,6 @@ async fn send_group(
)
}
#[post("/notification/user", data = "<data>")]
async fn send_user(
db: &State<SqlitePool>,
data: Form<NotificationToSendUser>,
admin: AdminUser,
) -> Flash<Redirect> {
let d = data.into_inner();
Log::create(
db,
format!("{admin:?} trying to send this notification: {d:?}"),
)
.await;
let Some(user) = User::find_by_id(db, d.user_id).await else {
return Flash::error(Redirect::to("/admin/notification"), "User gibt's ned");
};
Notification::create(db, &user, &d.message, &d.category, None).await;
Log::create(db, "Notification successfully sent".into()).await;
Flash::success(
Redirect::to("/admin/notification"),
"Nachricht ausgeschickt",
)
}
pub fn routes() -> Vec<Route> {
routes![index, send_user, send_group]
routes![index, send]
}

View File

@ -3,28 +3,18 @@
{% extends "base" %}
{% block content %}
<div class="max-w-screen-lg w-full dark:text-white">
<h1 class="h1">Nachricht senden</h1>
<h1 class="h1">Nachricht</h1>
<div class="grid ">
<div class="bg-white dark:bg-primary-900 text-black dark:text-white rounded-md block shadow mt-5"
role="alert">
<h2 class="h2">Gruppe</h2>
<form action="/admin/notification/group" method="post" class="grid gap-3 p-3">
<h2 class="h2">Nachricht senden</h2>
<form action="/admin/notification" method="post" class="grid gap-3 p-3">
{{ macros::select(label="Gruppe", data=roles, name="role_id") }}
{{ macros::input(label="Überschrift", name="category", type="text", required=true) }}
{{ macros::input(label="Nachricht", name="message", type="text", required=true) }}
<input type="submit" class="btn btn-primary" value="Abschicken" />
</form>
</div>
<div class="bg-white dark:bg-primary-900 text-black dark:text-white rounded-md block shadow mt-5"
role="alert">
<h2 class="h2">Person</h2>
<form action="/admin/notification/user" method="post" class="grid gap-3 p-3">
{{ macros::select(label="Person", data=users, name="user_id") }}
{{ macros::input(label="Überschrift", name="category", type="text", required=true) }}
{{ macros::input(label="Nachricht", name="message", type="text", required=true) }}
<input type="submit" class="btn btn-primary" value="Abschicken" />
</form>
</div>
</div>
</div>
{% endblock content %}