Skip to content

Commit

Permalink
Merge pull request 'Fix moderation issues #1156' (#1157) from gro2bl/…
Browse files Browse the repository at this point in the history
…Plume:fix_moderation_issues into main

Reviewed-on: https://git.joinplu.me/Plume/Plume/pulls/1157
Reviewed-by: trinity-1686a <trinity-1686a@[email protected]>
  • Loading branch information
trinity-1686a committed Jul 8, 2024
2 parents 304fb74 + 33b65a1 commit d9464d1
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 47 deletions.
19 changes: 18 additions & 1 deletion plume-models/src/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<InclusiveAdmin, ()> {
let user = request.guard::<User>()?;
if user.is_admin() {
Outcome::Success(InclusiveAdmin(user))
} else {
Outcome::Forward(())
}
}
}

/// Same as `Admin` but for moderators.
pub struct Moderator(pub User);

Expand Down
2 changes: 1 addition & 1 deletion src/routes/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub fn index(conn: DbConn, rockets: PlumeRocket) -> Result<Ructe, ErrorPage> {
}

#[get("/admin")]
pub fn admin(_admin: Admin, conn: DbConn, rockets: PlumeRocket) -> Result<Ructe, ErrorPage> {
pub fn admin(_admin: InclusiveAdmin, conn: DbConn, rockets: PlumeRocket) -> Result<Ructe, ErrorPage> {
let local_inst = Instance::get_local()?;
Ok(render!(instance::admin(
&(&conn, &rockets).to_context(),
Expand Down
2 changes: 2 additions & 0 deletions templates/base.rs.html
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ <h3>@Instance::get_local().map(|i| i.name).unwrap_or_default()</h3>
<a href="@uri!(instance::privacy)">@i18n!(ctx.1, "Privacy policy")</a>
@if ctx.2.clone().map(|u| u.is_admin()).unwrap_or(false) {
<a href="@uri!(instance::admin)">@i18n!(ctx.1, "Administration")</a>
} else if ctx.2.clone().map(|u| u.is_moderator()).unwrap_or(false) {
<a href="@uri!(instance::admin_mod)">@i18n!(ctx.1, "Moderation")</a>
}
</div>
<div>
Expand Down
11 changes: 2 additions & 9 deletions templates/instance/admin.rs.html
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
@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::*;

@(ctx: BaseContext, instance: Instance, form: InstanceSettingsForm, errors: ValidationErrors)

@:base(ctx, i18n!(ctx.1, "Administration of {0}"; instance.name.clone()), {}, {}, {
<h1>@i18n!(ctx.1, "Administration")</h1>

@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)

<form method="post" action="@uri!(instance::update_settings)">
@(Input::new("name", i18n!(ctx.1, "Name"))
Expand Down
21 changes: 21 additions & 0 deletions templates/instance/admin_header.rs.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
@use crate::template_utils::*;
@use crate::routes::*;

@(ctx: BaseContext, title: &str, selected_tab: u8)

<h1>@i18n!(ctx.1, title)</h1>

@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)
])
}
11 changes: 2 additions & 9 deletions templates/instance/admin_mod.rs.html
Original file line number Diff line number Diff line change
@@ -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"), {}, {}, {
<h1>@i18n!(ctx.1, "Moderation")</h1>

@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)
})
12 changes: 3 additions & 9 deletions templates/instance/emailblocklist.rs.html
Original file line number Diff line number Diff line change
@@ -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<BlocklistedEmail>, page:i32, n_pages:i32)
@:base(ctx, i18n!(ctx.1, "Blocklisted Emails"), {}, {}, {
<h1>@i18n!(ctx.1,"Blocklisted Emails")</h1>
@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)
<form method="post" action="@uri!(instance::add_email_blocklist)">
@(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"))
Expand Down
11 changes: 2 additions & 9 deletions templates/instance/list.rs.html
Original file line number Diff line number Diff line change
@@ -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<Instance>, page: i32, n_pages: i32)

@:base(ctx, i18n!(ctx.1, "Administration of {0}"; instance.name), {}, {}, {
<h1>@i18n!(ctx.1, "Instances")</h1>

@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))

<div class="list">
@for instance in instances {
Expand Down
11 changes: 2 additions & 9 deletions templates/instance/users.rs.html
Original file line number Diff line number Diff line change
@@ -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>, user: Option<&str>, page: i32, n_pages: i32)

@:base(ctx, i18n!(ctx.1, "Users"), {}, {}, {
<h1>@i18n!(ctx.1, "Users")</h1>

@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))

<form method="get" action="@uri!(instance::admin_search_users: page = _, user = user.unwrap_or_default())">
<header>
Expand Down

0 comments on commit d9464d1

Please sign in to comment.