add: user account warnings

This commit is contained in:
trisua 2025-04-11 22:12:43 -04:00
parent 535a854a47
commit 5995aaf31c
16 changed files with 459 additions and 1 deletions

View file

@ -66,6 +66,7 @@ pub const MOD_REPORTS: &str = include_str!("./public/html/mod/reports.html");
pub const MOD_FILE_REPORT: &str = include_str!("./public/html/mod/file_report.html");
pub const MOD_IP_BANS: &str = include_str!("./public/html/mod/ip_bans.html");
pub const MOD_PROFILE: &str = include_str!("./public/html/mod/profile.html");
pub const MOD_WARNINGS: &str = include_str!("./public/html/mod/warnings.html");
// langs
pub const LANG_EN_US: &str = include_str!("./langs/en-US.toml");
@ -197,6 +198,7 @@ pub(crate) async fn write_assets(config: &Config) -> PathBufD {
write_template!(html_path->"mod/file_report.html"(crate::assets::MOD_FILE_REPORT) --config=config);
write_template!(html_path->"mod/ip_bans.html"(crate::assets::MOD_IP_BANS) --config=config);
write_template!(html_path->"mod/profile.html"(crate::assets::MOD_PROFILE) --config=config);
write_template!(html_path->"mod/warnings.html"(crate::assets::MOD_WARNINGS) --config=config);
html_path
}

View file

@ -112,3 +112,5 @@ version = "1.0.0"
"mod_panel:label.open_reported_content" = "Open reported content"
"mod_panel:label.manage_profile" = "Manage profile"
"mod_panel:label.permissions_level_builder" = "Permission level builder"
"mod_panel:label.warnings" = "Warnings"
"mod_panel:label.create_warning" = "Create warning"

View file

@ -22,6 +22,14 @@
<span>View settings</span>
</a>
<a
href="/mod_panel/profile/{{ profile.id }}/warnings"
class="button quaternary"
>
{{ icon "shield-alert" }}
<span>View warnings</span>
</a>
<button
class="red quaternary"
onclick="delete_account(event)"

View file

@ -0,0 +1,132 @@
{% extends "root.html" %} {% block head %}
<title>User warnings - {{ config.name }}</title>
{% endblock %} {% block body %} {{ macros::nav(selected="notifications") }}
<main class="flex flex-col gap-2">
<div class="card-nest">
<div class="card small flex items-center justify-between gap-2">
<span class="flex items-center gap-2">
{{ icon "gavel" }}
<span>{{ text "mod_panel:label.create_warning" }}</span>
</span>
<a
href="/mod_panel/profile/{{ profile.id }}"
class="button quaternary small red"
>
{{ icon "x" }}
<span>{{ text "dialog:action.cancel" }}</span>
</a>
</div>
<form
class="card flex flex-col gap-2"
onsubmit="create_warning_from_form(event)"
>
<div class="flex flex-col gap-1">
<label for="content"
>{{ text "communities:label.content" }}</label
>
<textarea
type="text"
name="content"
id="content"
placeholder="content"
required
minlength="2"
maxlength="4096"
></textarea>
</div>
<button class="primary">
{{ text "communities:action.create" }}
</button>
</form>
</div>
<div class="card-nest">
<div class="card small flex items-center justify-between gap-2">
<span class="flex items-center gap-2">
{{ icon "message-circle-warning" }}
<span>{{ text "mod_panel:label.warnings" }}</span>
</span>
</div>
<div class="card flex flex-col gap-4">
{% for item in items %}
<div class="card-nest">
<div class="card small flex items-center justify-between gap-2">
<a
class="flex items-center gap-2 flush"
href="/api/v1/auth/user/find/{{ item.moderator }}"
title="Moderator"
>
<!-- prettier-ignore -->
{{ components::avatar(username=item.moderator, selector_type="id") }}
<span>{{ item.moderator }}</span>
<span class="fade date">{{ item.created }}</span>
</a>
<button
class="small quaternary red"
onclick="remove_warning('{{ item.id }}')"
>
{{ icon "trash" }}
<span>{{ text "general:action.delete" }}</span>
</button>
</div>
<div class="card secondary flex flex-col gap-2">
<span class="no_p_margin"
>{{ item.content|markdown|safe }}</span
>
</div>
</div>
{% endfor %}
<!-- prettier-ignore -->
{{ components::pagination(page=page, items=items|length) }}
</div>
</div>
</main>
<script>
async function create_warning_from_form(e) {
e.preventDefault();
await trigger("atto::debounce", ["warnings::create"]);
fetch("/api/v1/warnings/{{ profile.id }}", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
content: e.target.content.value,
}),
})
.then((res) => res.json())
.then((res) => {
trigger("atto::toast", [
res.ok ? "success" : "error",
res.message,
]);
if (res.ok) {
e.target.reset();
}
});
}
function remove_warning(id) {
fetch(`/api/v1/warnings/${id}`, {
method: "DELETE",
})
.then((res) => res.json())
.then((res) => {
trigger("atto::toast", [
res.ok ? "success" : "error",
res.message,
]);
});
}
</script>
{% endblock %}

View file

@ -27,7 +27,7 @@ pub async fn create_request(
match data.create_ipban(IpBan::new(ip, user.id, req.reason)).await {
Ok(_) => Json(ApiReturn {
ok: true,
message: "IP ban deleted".to_string(),
message: "IP ban created".to_string(),
payload: (),
}),
Err(e) => Json(e.into()),

View file

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

View file

@ -0,0 +1,65 @@
use crate::{
get_user_from_token,
model::{ApiReturn, Error},
routes::api::v1::CreateUserWarning,
State,
};
use axum::{Extension, Json, extract::Path, response::IntoResponse};
use axum_extra::extract::CookieJar;
use tetratto_core::model::{auth::UserWarning, permissions::FinePermission};
/// Create a new user warning.
pub async fn create_request(
jar: CookieJar,
Path(uid): Path<usize>,
Extension(data): Extension<State>,
Json(req): Json<CreateUserWarning>,
) -> 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()),
};
if !user.permissions.check(FinePermission::MANAGE_BANS) {
return Json(Error::NotAllowed.into());
}
match data
.create_user_warning(UserWarning::new(uid, user.id, req.content))
.await
{
Ok(_) => Json(ApiReturn {
ok: true,
message: "User warning created".to_string(),
payload: (),
}),
Err(e) => Json(e.into()),
}
}
/// Delete the given user warning.
pub async fn delete_request(
jar: CookieJar,
Path(id): Path<usize>,
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()),
};
if !user.permissions.check(FinePermission::MANAGE_WARNINGS) {
return Json(Error::NotAllowed.into());
}
match data.delete_user_warning(id, user).await {
Ok(_) => Json(ApiReturn {
ok: true,
message: "User warning deleted".to_string(),
payload: (),
}),
Err(e) => Json(e.into()),
}
}

View file

@ -162,6 +162,12 @@ pub fn routes() -> Router {
"/auth/user/find_by_ip/{ip}",
get(auth::profile::redirect_from_ip),
)
// warnings
.route("/warnings/{id}", post(auth::user_warnings::create_request))
.route(
"/warnings/{id}",
delete(auth::user_warnings::delete_request),
)
// notifications
.route(
"/notifications/my",
@ -326,3 +332,8 @@ pub struct CreateIpBan {
pub struct DisableTotp {
pub totp: String,
}
#[derive(Deserialize)]
pub struct CreateUserWarning {
pub content: String,
}

View file

@ -35,6 +35,10 @@ pub fn routes() -> Router {
"/mod_panel/profile/{id}",
get(mod_panel::manage_profile_request),
)
.route(
"/mod_panel/profile/{id}/warnings",
get(mod_panel::manage_profile_warnings_request),
)
// auth
.route("/auth/register", get(auth::register_request))
.route("/auth/login", get(auth::login_request))

View file

@ -184,3 +184,51 @@ pub async fn manage_profile_request(
// return
Ok(Html(data.1.render("mod/profile.html", &context).unwrap()))
}
/// `/mod_panel/profile/{id}/warnings`
pub async fn manage_profile_warnings_request(
jar: CookieJar,
Extension(data): Extension<State>,
Path(id): Path<usize>,
Query(req): Query<PaginatedQuery>,
) -> impl IntoResponse {
let data = data.read().await;
let user = match get_user_from_token!(jar, data.0) {
Some(ua) => ua,
None => {
return Err(Html(
render_error(Error::NotAllowed, &jar, &data, &None).await,
));
}
};
if !user.permissions.check(FinePermission::MANAGE_USERS) {
return Err(Html(
render_error(Error::NotAllowed, &jar, &data, &None).await,
));
}
let profile = match data.0.get_user_by_id(id).await {
Ok(p) => p,
Err(e) => return Err(Html(render_error(e, &jar, &data, &Some(user)).await)),
};
let list = match data
.0
.get_user_warnings_by_user(profile.id, 12, req.page)
.await
{
Ok(p) => p,
Err(e) => return Err(Html(render_error(e, &jar, &data, &Some(user)).await)),
};
let lang = get_lang!(jar, data.0);
let mut context = initial_context(&data.0.0, lang, &Some(user)).await;
context.insert("profile", &profile);
context.insert("items", &list);
context.insert("page", &req.page);
// return
Ok(Html(data.1.render("mod/warnings.html", &context).unwrap()))
}