clean code with clippy #527
@ -4,6 +4,7 @@ use crate::model::{
|
||||
role::Role,
|
||||
user::{AdminUser, User, UserWithDetails},
|
||||
};
|
||||
use itertools::Itertools;
|
||||
use rocket::{
|
||||
form::Form,
|
||||
get, post,
|
||||
@ -28,22 +29,39 @@ 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 NotificationToSend {
|
||||
pub struct NotificationToSendGroup {
|
||||
pub(crate) role_id: i32,
|
||||
pub(crate) category: String,
|
||||
pub(crate) message: String,
|
||||
}
|
||||
|
||||
#[post("/notification", data = "<data>")]
|
||||
async fn send(
|
||||
#[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(
|
||||
db: &State<SqlitePool>,
|
||||
data: Form<NotificationToSend>,
|
||||
data: Form<NotificationToSendGroup>,
|
||||
admin: AdminUser,
|
||||
) -> Flash<Redirect> {
|
||||
let d = data.into_inner();
|
||||
@ -67,6 +85,32 @@ async fn send(
|
||||
)
|
||||
}
|
||||
|
||||
pub fn routes() -> Vec<Route> {
|
||||
routes![index, send]
|
||||
#[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]
|
||||
}
|
||||
|
@ -3,18 +3,28 @@
|
||||
{% extends "base" %}
|
||||
{% block content %}
|
||||
<div class="max-w-screen-lg w-full dark:text-white">
|
||||
<h1 class="h1">Nachricht</h1>
|
||||
<h1 class="h1">Nachricht senden</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">Nachricht senden</h2>
|
||||
<form action="/admin/notification" method="post" class="grid gap-3 p-3">
|
||||
<h2 class="h2">Gruppe</h2>
|
||||
<form action="/admin/notification/group" 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 %}
|
||||
|
Loading…
Reference in New Issue
Block a user