From 5d2e653a3b78f36c6eb14eb38e117f501544ace5 Mon Sep 17 00:00:00 2001 From: aitzol Date: Fri, 16 Aug 2024 08:02:28 +0200 Subject: [PATCH] Fix moderation issues --- Cargo.lock | 13 +++++-------- plume-models/src/admin.rs | 19 ++++++++++++++++++- plume-models/src/timeline/mod.rs | 3 ++- rust-toolchain | 2 +- src/routes/instance.rs | 2 +- templates/base.rs.html | 2 ++ templates/instance/admin.rs.html | 12 ++---------- templates/instance/admin_header.rs.html | 22 ++++++++++++++++++++++ templates/instance/admin_mod.rs.html | 11 ++--------- templates/instance/emailblocklist.rs.html | 12 +++--------- templates/instance/list.rs.html | 11 ++--------- templates/instance/users.rs.html | 11 ++--------- 12 files changed, 62 insertions(+), 58 deletions(-) create mode 100644 templates/instance/admin_header.rs.html diff --git a/Cargo.lock b/Cargo.lock index 4bb2ce22..b678c6d6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1894,12 +1894,9 @@ dependencies = [ [[package]] name = "hermit-abi" -version = "0.2.6" +version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ee512640fe35acbfb4bb779db6f0d80704c2cacfa2e39b601ef3e3f47d1ae4c7" -dependencies = [ - "libc", -] +checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024" [[package]] name = "hex" @@ -3116,11 +3113,11 @@ dependencies = [ [[package]] name = "num_cpus" -version = "1.15.0" +version = "1.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0fac9e2da13b5eb447a6ce3d392f23a29d8694bff781bf03a16cd9ac8697593b" +checksum = "4161fcb6d602d4d2081af7c3a45852d875a03dd337a6bfdd6e06407b61342a43" dependencies = [ - "hermit-abi 0.2.6", + "hermit-abi 0.3.9", "libc", ] diff --git a/plume-models/src/admin.rs b/plume-models/src/admin.rs index a4fa0455..37a34584 100644 --- a/plume-models/src/admin.rs +++ b/plume-models/src/admin.rs @@ -5,7 +5,7 @@ use rocket::{ Outcome, }; -/// Wrapper around User to use as a request guard on pages reserved to admins. +/// Wrapper around User to use as a request guard on pages exclusively reserved to admins. pub struct Admin(pub User); impl<'a, 'r> FromRequest<'a, 'r> for Admin { @@ -21,6 +21,23 @@ impl<'a, 'r> FromRequest<'a, 'r> for Admin { } } +/// Same as `Admin` but it forwards to next guard if the user is not an admin. +/// It's useful when there are multiple implementations of routes for admin and moderator. +pub struct InclusiveAdmin(pub User); + +impl<'a, 'r> FromRequest<'a, 'r> for InclusiveAdmin { + type Error = (); + + fn from_request(request: &'a Request<'r>) -> request::Outcome { + let user = request.guard::()?; + if user.is_admin() { + Outcome::Success(InclusiveAdmin(user)) + } else { + Outcome::Forward(()) + } + } +} + /// Same as `Admin` but for moderators. pub struct Moderator(pub User); diff --git a/plume-models/src/timeline/mod.rs b/plume-models/src/timeline/mod.rs index d8cc7e32..2a3daa1e 100644 --- a/plume-models/src/timeline/mod.rs +++ b/plume-models/src/timeline/mod.rs @@ -221,7 +221,8 @@ impl Timeline { pub fn add_to_all_timelines(conn: &Connection, post: &Post, kind: Kind<'_>) -> Result<()> { let timelines = timeline_definition::table - .load::(conn.deref()) + //.load::(conn.deref()) + .load::(conn) .map_err(Error::from)?; for t in timelines { diff --git a/rust-toolchain b/rust-toolchain index c88f9562..47c90f71 100644 --- a/rust-toolchain +++ b/rust-toolchain @@ -1 +1 @@ -nightly-2023-04-14 +nightly-2023-11-10 diff --git a/src/routes/instance.rs b/src/routes/instance.rs index 9de9fee0..e05e069c 100644 --- a/src/routes/instance.rs +++ b/src/routes/instance.rs @@ -51,7 +51,7 @@ pub fn index(conn: DbConn, rockets: PlumeRocket) -> Result { } #[get("/admin")] -pub fn admin(_admin: Admin, conn: DbConn, rockets: PlumeRocket) -> Result { +pub fn admin(_admin: InclusiveAdmin, conn: DbConn, rockets: PlumeRocket) -> Result { let local_inst = Instance::get_local()?; Ok(render!(instance::admin( &(&conn, &rockets).to_context(), diff --git a/templates/base.rs.html b/templates/base.rs.html index 564c1f84..711ed238 100644 --- a/templates/base.rs.html +++ b/templates/base.rs.html @@ -87,6 +87,8 @@ @i18n!(ctx.1, "Privacy policy") @if ctx.2.clone().map(|u| u.is_admin()).unwrap_or(false) { @i18n!(ctx.1, "Administration") + } else if ctx.2.clone().map(|u| u.is_moderator()).unwrap_or(false) { + @i18n!(ctx.1, "Moderation") }
diff --git a/templates/instance/admin.rs.html b/templates/instance/admin.rs.html index 7ff3750d..ad6445a0 100644 --- a/templates/instance/admin.rs.html +++ b/templates/instance/admin.rs.html @@ -1,6 +1,6 @@ @use plume_models::instance::Instance; @use validator::ValidationErrors; -@use crate::templates::base; +@use crate::templates::{base, instance::admin_header}; @use crate::template_utils::*; @use crate::routes::instance::InstanceSettingsForm; @use crate::routes::*; @@ -8,15 +8,7 @@ @(ctx: BaseContext, instance: Instance, form: InstanceSettingsForm, errors: ValidationErrors) @:base(ctx, i18n!(ctx.1, "Administration of {0}"; instance.name.clone()), {}, {}, { -

@i18n!(ctx.1, "Administration")

- - @tabs(&[ - (&uri!(instance::admin).to_string(), i18n!(ctx.1, "Configuration"), true), - (&uri!(instance::admin_instances: page = _).to_string(), i18n!(ctx.1, "Instances"), false), - (&uri!(instance::admin_users: page = _).to_string(), i18n!(ctx.1, "Users"), false), - (&uri!(instance::admin_email_blocklist: page=_).to_string(), i18n!(ctx.1, "Email blocklist"), false) - ]) - + @:admin_header(ctx, "Administration", 1)
@(Input::new("name", i18n!(ctx.1, "Name")) .default(&form.name) diff --git a/templates/instance/admin_header.rs.html b/templates/instance/admin_header.rs.html new file mode 100644 index 00000000..c83b65a6 --- /dev/null +++ b/templates/instance/admin_header.rs.html @@ -0,0 +1,22 @@ +@use crate::template_utils::*; +@use crate::routes::*; + +@(ctx: BaseContext, title: &str, selected_tab: u8) + +

@i18n!(ctx.1, title)

+ +@if ctx.2.clone().map(|u| u.is_admin()).unwrap_or(false) { + @tabs(&[ + (&uri!(instance::admin).to_string(), i18n!(ctx.1, "Configuration"), selected_tab == 1), + (&uri!(instance::admin_instances: page = _).to_string(), i18n!(ctx.1, "Instances"), selected_tab == 2), + (&uri!(instance::admin_users: page = _).to_string(), i18n!(ctx.1, "Users"), selected_tab == 3), + (&uri!(instance::admin_email_blocklist: page=_).to_string(), i18n!(ctx.1, "Email blocklist"), selected_tab == 4) + ]) +} else { + @tabs(&[ + (&uri!(instance::admin_instances: page = _).to_string(), i18n!(ctx.1, "Instances"), selected_tab == 2), + (&uri!(instance::admin_users: page = _).to_string(), i18n!(ctx.1, "Users"), selected_tab == 3), + (&uri!(instance::admin_email_blocklist: page=_).to_string(), i18n!(ctx.1, "Email blocklist"), selected_tab == 4) + ]) +} + diff --git a/templates/instance/admin_mod.rs.html b/templates/instance/admin_mod.rs.html index e215d7e1..fc1cb0a9 100644 --- a/templates/instance/admin_mod.rs.html +++ b/templates/instance/admin_mod.rs.html @@ -1,15 +1,8 @@ -@use crate::templates::base; +@use crate::templates::{base, instance::admin_header}; @use crate::template_utils::*; -@use crate::routes::*; @(ctx: BaseContext) @:base(ctx, i18n!(ctx.1, "Moderation"), {}, {}, { -

@i18n!(ctx.1, "Moderation")

- - @tabs(&[ - (&uri!(instance::admin).to_string(), i18n!(ctx.1, "Home"), true), - (&uri!(instance::admin_instances: page = _).to_string(), i18n!(ctx.1, "Instances"), false), - (&uri!(instance::admin_users: page = _).to_string(), i18n!(ctx.1, "Users"), false), - ]) + @:admin_header(ctx, "Moderation", 0) }) diff --git a/templates/instance/emailblocklist.rs.html b/templates/instance/emailblocklist.rs.html index fe9729fc..7e82cea4 100644 --- a/templates/instance/emailblocklist.rs.html +++ b/templates/instance/emailblocklist.rs.html @@ -1,17 +1,11 @@ @use plume_models::blocklisted_emails::BlocklistedEmail; -@use crate::templates::base; +@use crate::templates::{base, instance::admin_header}; @use crate::template_utils::*; @use crate::routes::*; @(ctx:BaseContext, emails: Vec, page:i32, n_pages:i32) - @:base(ctx, i18n!(ctx.1, "Blocklisted Emails"), {}, {}, { -

@i18n!(ctx.1,"Blocklisted Emails")

- @tabs(&[ - (&uri!(instance::admin).to_string(), i18n!(ctx.1, "Configuration"), false), - (&uri!(instance::admin_instances: page = _).to_string(), i18n!(ctx.1, "Instances"), false), - (&uri!(instance::admin_users: page = _).to_string(), i18n!(ctx.1, "Users"), false), - (&uri!(instance::admin_email_blocklist:page=_).to_string(), i18n!(ctx.1, "Email blocklist"), true), - ]) +@:base(ctx, i18n!(ctx.1, "Blocklisted Emails"), {}, {}, { + @:admin_header(ctx, "Blocklisted Emails", 4) @(Input::new("email_address", i18n!(ctx.1, "Email address")) .details(i18n!(ctx.1, "The email address you wish to block. In order to block domains, you can use globbing syntax, for example '*@example.com' blocks all addresses from example.com")) diff --git a/templates/instance/list.rs.html b/templates/instance/list.rs.html index 619689f7..baeb11a0 100644 --- a/templates/instance/list.rs.html +++ b/templates/instance/list.rs.html @@ -1,19 +1,12 @@ @use plume_models::instance::Instance; -@use crate::templates::base; +@use crate::templates::{base, instance::admin_header}; @use crate::template_utils::*; @use crate::routes::*; @(ctx: BaseContext, instance: Instance, instances: Vec, page: i32, n_pages: i32) @:base(ctx, i18n!(ctx.1, "Administration of {0}"; instance.name), {}, {}, { -

@i18n!(ctx.1, "Instances")

- - @tabs(&[ - (&uri!(instance::admin).to_string(), i18n!(ctx.1, "Configuration"), false), - (&uri!(instance::admin_instances: page = _).to_string(), i18n!(ctx.1, "Instances"), true), - (&uri!(instance::admin_users: page = _).to_string(), i18n!(ctx.1, "Users"), false), - (&uri!(instance::admin_email_blocklist:page=_).to_string(), i18n!(ctx.1, "Email blocklist"), false), - ]) + @:admin_header(ctx, "Instances", 2))
@for instance in instances { diff --git a/templates/instance/users.rs.html b/templates/instance/users.rs.html index 1499fb52..38299f37 100644 --- a/templates/instance/users.rs.html +++ b/templates/instance/users.rs.html @@ -1,19 +1,12 @@ @use plume_models::users::User; -@use crate::templates::base; +@use crate::templates::{base, instance::admin_header}; @use crate::template_utils::*; @use crate::routes::*; @(ctx: BaseContext, users: Vec, user: Option<&str>, page: i32, n_pages: i32) @:base(ctx, i18n!(ctx.1, "Users"), {}, {}, { -

@i18n!(ctx.1, "Users")

- - @tabs(&[ - (&uri!(instance::admin).to_string(), i18n!(ctx.1, "Configuration"), false), - (&uri!(instance::admin_instances: page = _).to_string(), i18n!(ctx.1, "Instances"), false), - (&uri!(instance::admin_users: page = _).to_string(), i18n!(ctx.1, "Users"), true), - (&uri!(instance::admin_email_blocklist: page=_).to_string(), i18n!(ctx.1, "Email blocklist"), false) - ]) + @:admin_header(ctx, "Users", 3))
-- 2.45.2