From 1f2bd105b613381ab5801fd0d60c6660f1e42682 Mon Sep 17 00:00:00 2001 From: Bat Date: Sun, 9 Sep 2018 11:25:55 +0100 Subject: [PATCH] Implement user ban --- plume-models/src/users.rs | 13 +++++++++++++ src/main.rs | 3 +++ src/routes/instance.rs | 24 ++++++++++++++++++++++++ templates/instance/admin.html.tera | 2 +- templates/instance/list.html.tera | 2 +- templates/instance/users.html.tera | 30 ++++++++++++++++++++++++++++++ 6 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 templates/instance/users.html.tera diff --git a/plume-models/src/users.rs b/plume-models/src/users.rs index 76b1127..4e096ef 100644 --- a/plume-models/src/users.rs +++ b/plume-models/src/users.rs @@ -100,6 +100,10 @@ impl User { find_by!(users, find_by_name, username as String, instance_id as i32); find_by!(users, find_by_ap_url, ap_url as String); + pub fn delete(&self, conn: &PgConnection) { + diesel::delete(self).execute(conn).expect("Couldn't remove user from DB"); + } + pub fn get_instance(&self, conn: &PgConnection) -> Instance { Instance::get(conn, self.instance_id).expect("Couldn't find instance") } @@ -297,6 +301,15 @@ impl User { } } + pub fn get_local_page(conn: &PgConnection, (min, max): (i32, i32)) -> Vec { + users::table.filter(users::instance_id.eq(1)) + .order(users::username.asc()) + .offset(min.into()) + .limit((max - min).into()) + .load::(conn) + .expect("Error getting local users page") + } + pub fn outbox(&self, conn: &PgConnection) -> ActivityStream { let acts = self.get_activities(conn); let n_acts = acts.len(); diff --git a/src/main.rs b/src/main.rs index f4fcd5e..01cfdf6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -66,6 +66,9 @@ fn main() { routes::instance::admin, routes::instance::admin_instances, routes::instance::admin_instances_paginated, + routes::instance::admin_users, + routes::instance::admin_users_paginated, + routes::instance::ban, routes::instance::toggle_block, routes::instance::update_settings, routes::instance::shared_inbox, diff --git a/src/routes/instance.rs b/src/routes/instance.rs index 0251ef9..a8d7965 100644 --- a/src/routes/instance.rs +++ b/src/routes/instance.rs @@ -163,6 +163,30 @@ fn toggle_block(_admin: Admin, conn: DbConn, id: i32) -> Redirect { Redirect::to(uri!(admin_instances)) } +#[get("/admin/users")] +fn admin_users(admin: Admin, conn: DbConn) -> Template { + admin_users_paginated(admin, conn, Page::first()) +} + +#[get("/admin/users?")] +fn admin_users_paginated(admin: Admin, conn: DbConn, page: Page) -> Template { + let users = User::get_local_page(&*conn, page.limits()).into_iter() + .map(|u| u.to_json(&*conn)).collect::>(); + + Template::render("instance/users", json!({ + "account": admin.0.to_json(&*conn), + "users": users, + "page": page.page, + "n_pages": Page::total(User::count_local(&*conn) as i32) + })) +} + +#[get("/admin/users//ban")] +fn ban(_admin: Admin, conn: DbConn, id: i32) -> Redirect { + User::get(&*conn, id).map(|u| u.delete(&*conn)); + Redirect::to(uri!(admin_users)) +} + #[post("/inbox", data = "")] fn shared_inbox(conn: DbConn, data: String) -> String { let act: serde_json::Value = serde_json::from_str(&data[..]).unwrap(); diff --git a/templates/instance/admin.html.tera b/templates/instance/admin.html.tera index 3832a95..865c8a9 100644 --- a/templates/instance/admin.html.tera +++ b/templates/instance/admin.html.tera @@ -8,7 +8,7 @@ Administration of {{ instance.name }} {% block content %}

{{ "Administration" | _ }}

- {{ macros::tabs(links=['/admin', '/admin/instances'], titles=['Configuration', 'Instances'], selected=1) }} + {{ macros::tabs(links=['/admin', '/admin/instances', '/admin/users'], titles=['Configuration', 'Instances', 'Users'], selected=1) }}
{{ macros::input(name="name", label="Name", errors=errors, form=form, props='minlenght="1"', default=instance) }} diff --git a/templates/instance/list.html.tera b/templates/instance/list.html.tera index cabddcd..6780e99 100644 --- a/templates/instance/list.html.tera +++ b/templates/instance/list.html.tera @@ -8,7 +8,7 @@ Administration of {{ instance.name }} {% block content %}

{{ "Instances" | _ }}

- {{ macros::tabs(links=['/admin', '/admin/instances'], titles=['Configuration', 'Instances'], selected=2) }} + {{ macros::tabs(links=['/admin', '/admin/instances', '/admin/users'], titles=['Configuration', 'Instances', 'Users'], selected=2) }}
{% for instance in instances %} diff --git a/templates/instance/users.html.tera b/templates/instance/users.html.tera new file mode 100644 index 0000000..475ad96 --- /dev/null +++ b/templates/instance/users.html.tera @@ -0,0 +1,30 @@ +{% extends "base" %} +{% import "macros" as macros %} + +{% block title %} +Users +{% endblock title %} + +{% block content %} +

{{ "Users" | _ }}

+ + {{ macros::tabs(links=['/admin', '/admin/instances', '/admin/users'], titles=['Configuration', 'Instances', 'Users'], selected=3) }} + +
+ {% for user in users %} +
+ {{ user.name }} +

+ {{ user.name }} + @{{ user.username }} +

+ {% if not user.is_admin %} + + Ban + + {% endif %} +
+ {% endfor %} +
+ {{ macros::paginate(page=page, total=n_pages) }} +{% endblock content %}